home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / util / neural.sit / BrainFile.c.bin / BrainFile.c
Encoding:
C/C++ Source or Header  |  1989-04-19  |  6.8 KB  |  386 lines  |  [TEXT/MACA]

  1. /*BrainFile.c    Core routines for reading in a neural Network.
  2.  
  3.      This file is Copyright 1989 by Pierce T. Wetter III
  4.      
  5.      Permission granted for all non-commercial uses of this code. For commercial uses of this code
  6.      contact me at 
  7.      
  8.          Pierce T. Wetter III
  9.          45 Vista Lago Dr.
  10.          Simi Valley, CA 93065
  11.          
  12.          or
  13.          
  14.          wetter@csvax.caltech.edu        or pwetter@caltech.bitnet
  15.          
  16. */
  17.  
  18. #include <FileMgr.h>
  19. #include "NetLib.h"
  20. #include "Brain.h"
  21. #include "StdFilePkg.h"
  22.  
  23.  
  24. /*   Brain.h can be eliminated if you use the following dec.
  25.  
  26. typedef struct _patternlist{
  27.                 struct _patternlist *next,*before;
  28.                 double *inputs;
  29.                 double *outputs;
  30.                 } patlist,*patlistptr;
  31.                 
  32. */
  33.  
  34. #define nil 0L
  35.  
  36. extern mind *mymind;
  37. extern patlistptr patterns;
  38. static short ferror=0,OpenFile();
  39. extern double maxerr;
  40.  
  41. double ReadFloat(),atof();
  42. short ReadInt();
  43. long atol();
  44.  
  45. short GetNet()
  46. {
  47. short ni,nh,no;
  48. double moment,lrate;
  49. short fileref,i,j;
  50.  
  51.     fileref=OpenFile(true);
  52.     if (ferror) return(false);
  53.     ni=ReadInt(fileref);
  54.     if (ferror) return(false);
  55.     nh=ReadInt(fileref);
  56.     if (ferror) return(false);
  57.     no=ReadInt(fileref);
  58.     if (ferror) return(false);
  59.     moment=ReadFloat(fileref);
  60.     if (ferror) return(false);
  61.     lrate=ReadFloat(fileref);
  62.     if (ferror) return(false);
  63.     maxerr=ReadFloat(fileref);
  64.     if (ferror) return(false);
  65.     if (!mymind) 
  66.     {
  67.         initmind(ni,nh,no,moment,lrate,&mymind);
  68.     }
  69.     else
  70.     {
  71.         mymind->momentum=moment;
  72.         mymind->learnrate=lrate;
  73.         if ((ni != mymind->nin) || (nh != mymind->nhidden) || (no != mymind->nout))
  74.         {
  75.             return(false);
  76.         }
  77.     }
  78.     for (i=0;i<mymind->nin;i++)
  79.     {
  80.         mymind->inbias[i]=ReadFloat(fileref);
  81.     if (ferror) return(false);
  82.         for (j=0;j<mymind->nhidden;j++)
  83.         {
  84.             mymind->inweights[j][i]=ReadFloat(fileref);
  85.     if (ferror) return(false);
  86.         }
  87.     }
  88.     for(i=0;i<mymind->nhidden;i++)
  89.     {
  90.         mymind->hiddenbias[i]=ReadFloat(fileref);
  91.     if (ferror) return(false);
  92.         for (j=0;j<mymind->nout;j++)
  93.         {
  94.             mymind->hiddenweights[j][i]=ReadFloat(fileref);
  95.     if (ferror) return(false);
  96.         }
  97.     }
  98.     for(i=0;i<mymind->nout;i++)
  99.     {
  100.         mymind->outbias[i]=ReadFloat(fileref);
  101.     if (ferror) return(false);
  102.     }
  103.     CloseFile(fileref);
  104.     
  105. return (true);
  106. }
  107.  
  108. short SaveNet()
  109. {
  110. char buf[256];
  111. short fileref,i,j;
  112.  
  113.     fileref=OpenFile(false);
  114.     if (ferror) return(false);
  115.     sprintf(buf,"%d\r",mymind->nin);
  116.     WriteString(buf,fileref);
  117.     if (ferror) return(false);
  118.     sprintf(buf,"%d\r",mymind->nhidden);
  119.     WriteString(buf,fileref);
  120.     if (ferror) return(false);
  121.     sprintf(buf,"%d\r",mymind->nout);
  122.     WriteString(buf,fileref);
  123.     if (ferror) return(false);
  124.     sprintf(buf,"%g\r",mymind->momentum);
  125.     WriteString(buf,fileref);
  126.     if (ferror) return(false);
  127.     sprintf(buf,"%g\r",mymind->learnrate);
  128.     WriteString(buf,fileref);
  129.     if (ferror) return(false);
  130.     sprintf(buf,"%g\r",maxerr);
  131.     WriteString(buf,fileref);
  132.     if (ferror) return(false);
  133.     for (i=0;i<mymind->nin;i++)
  134.     {
  135.         sprintf(buf,"%g\r",mymind->inbias[i]);
  136.         WriteString(buf,fileref);
  137.     if (ferror) return(false);
  138.         for(j=0;j<mymind->nhidden;j++)
  139.         {
  140.             sprintf(buf,"%g\r",mymind->inweights[j][i]);
  141.             WriteString(buf,fileref);
  142.     if (ferror) return(false);
  143.         }
  144.     }
  145.     for (i=0;i<mymind->nhidden;i++)
  146.     {
  147.         sprintf(buf,"%g\r",mymind->hiddenbias[i]);
  148.         WriteString(buf,fileref);
  149.     if (ferror) return(false);
  150.         for(j=0;j<mymind->nout;j++)
  151.         {
  152.             sprintf(buf,"%g\r",mymind->hiddenweights[j][i]);
  153.             WriteString(buf,fileref);
  154.     if (ferror) return(false);
  155.         }
  156.     }
  157.     for (i=0;i<mymind->nout;i++)
  158.     {
  159.         sprintf(buf,"%g\r",mymind->outbias[i]);
  160.         WriteString(buf,fileref);
  161.     if (ferror) return(false);
  162.     }
  163.     CloseFile(fileref);
  164.  
  165. return (true);
  166. }
  167.  
  168.  
  169. short SavePat()
  170. {
  171. patlistptr cpat;
  172. short count,fileref,i;
  173. char buf[256];
  174.     
  175.     fileref=OpenFile(false);
  176.     if (ferror) return(false);
  177.     count=0;
  178.     for(cpat=patterns;cpat;cpat=cpat->next)
  179.     {
  180.         count++;
  181.     }
  182.     sprintf(buf,"%d\r",count);
  183.     WriteString(buf,fileref);
  184.     if (ferror) return(false);
  185.     for(cpat=patterns;cpat;cpat=cpat->next)
  186.     {
  187.         for(i=0;i<mymind->nin;i++)
  188.         {
  189.             sprintf(buf,"%g\r",cpat->inputs[i]);
  190.             WriteString(buf,fileref);
  191.     if (ferror) return(false);
  192.         }
  193.         for(i=0;i<mymind->nout;i++)
  194.         {
  195.             sprintf(buf,"%g\r",cpat->outputs[i]);
  196.             WriteString(buf,fileref);
  197.     if (ferror) return(false);
  198.         }
  199.     }    
  200.     CloseFile(fileref);
  201.  
  202. return (true);
  203. }
  204.  
  205. short GetPat()
  206. {
  207. short count,fileref,i,j;
  208. patlistptr opat,NewPattern();
  209.     
  210.     fileref=OpenFile(true);
  211.     if (ferror) return(false);
  212.     count=ReadInt(fileref);
  213.     if (ferror) return(false);
  214.     for (j=0;j<count;j++)
  215.     {
  216.         opat=patterns;
  217.         patterns=NewPattern();
  218.         for(i=0;i<mymind->nin;i++)
  219.         {
  220.             patterns->inputs[i]=ReadFloat(fileref);
  221.     if (ferror) return(false);
  222.         }
  223.         for(i=0;i<mymind->nout;i++)
  224.         {
  225.             patterns->outputs[i]=ReadFloat(fileref);
  226.     if (ferror) return(false);
  227.         }
  228.         patterns->next=opat;
  229.         opat->before=patterns;
  230.     }
  231.     CloseFile(fileref);
  232. return (true);
  233. }
  234.  
  235. short GenPat()
  236. {
  237.  
  238. return (true);
  239. }
  240.  
  241. double ReadFloat(fileref)
  242. short fileref; 
  243. {
  244. char buf[256];
  245. short i;
  246. long count=1;
  247.     
  248.     if (!ferror) 
  249.     {
  250.         for (i=0;i<256;i++)
  251.         {
  252.             ferror=FSRead(fileref,&count,&buf[i]);
  253.             if (buf[i]=='\r') break;
  254.             if (ferror) break;
  255.         }
  256.         buf[i]=0;
  257.     }
  258.     if (!ferror) return (atof(buf));
  259.  
  260. }
  261.  
  262. short ReadInt(fileref)
  263. short fileref; 
  264. {
  265. char buf[256];
  266. short i;
  267. long count=1,result;
  268.     
  269.     if (!ferror)
  270.     {
  271.         for (i=0;i<256;i++)
  272.         {
  273.             ferror=FSRead(fileref,&count,&buf[i]);
  274.             if (buf[i]=='\r') break;
  275.             if (ferror) break;
  276.         }
  277.         result=atol(buf);
  278.     }
  279.     if (!ferror) return ((short) result);
  280.  
  281. }
  282.  
  283. WriteString(buf,fileref)
  284. char *buf;
  285. short fileref; 
  286. {
  287. long count,ocount,ncount;
  288.  
  289.     if (!ferror)
  290.     {
  291.         ocount=count=strlen(buf);
  292.         ferror=FSWrite(fileref,&count,buf);
  293.         if (count!=ocount)
  294.         {
  295.             ocount -=count;
  296.             ncount=ocount;
  297.             ferror=Allocate(fileref,&ncount);
  298.             ferror=FSWrite(fileref,&ocount,buf+count);
  299.         }
  300.     }
  301.  
  302. }
  303.  
  304. FileError()
  305. {
  306.  
  307.     switch (ferror)
  308.     {
  309.         case noErr: break;
  310.         default: SysBeep(60);
  311.                  break;
  312.     }
  313. }
  314.  
  315. extern Point    dlogWhere;
  316.  
  317. short OpenFile(read)
  318. Boolean read;
  319. {
  320. short f;
  321. FInfo    fndrInfo;    /* finder info */
  322. SFReply    tmpFile;
  323. Handle    hText;
  324. long    count;
  325. OSErr    result;
  326. Boolean    haveNewFile = false;
  327. OSType        type = 'TEXT';
  328.  
  329.  
  330.     ferror=0;
  331.     
  332.     /* GetFile/PutFile location */
  333.  
  334.     pStrCopy( "\pNeural.output",tmpFile.fName);                    /* initialize default name */
  335.     if (read)
  336.     {
  337.         SFGetFile (dlogWhere, "\pChoose file:",nil,1,&type,
  338.                     0L, &tmpFile);
  339.     }
  340.     else 
  341.     {
  342.         SFPutFile (dlogWhere, "\pSave file as:", tmpFile.fName,
  343.                     0L, &tmpFile);
  344.     }    
  345.     if (!tmpFile.good)
  346.         return (0);
  347.     else
  348.     {
  349.         haveNewFile = true;
  350.         if (GetFInfo (tmpFile.fName, tmpFile.vRefNum, &fndrInfo)
  351.                 == noErr) /* exists */
  352.         {
  353.             if (fndrInfo.fdType != 'TEXT')
  354.             {
  355.                 ErrMesg ("\pNot a TEXT File");
  356.                 return (0);
  357.             }
  358.         }
  359.         else    /* doesn't exist.  create it. */
  360.         {
  361.             if (Create (tmpFile.fName, tmpFile.vRefNum,
  362.                         'MPS ', 'TEXT') != noErr)
  363.             {
  364.                 ErrMesg ("\pCan't Create");
  365.                 return (0);
  366.             }
  367.         }
  368.     }
  369.     
  370.     if (ferror=FSOpen (tmpFile.fName, tmpFile.vRefNum, &f) != noErr)
  371.         ErrMesg ("\pCan't Open");
  372.     else
  373.     {
  374.         (void) SetFPos (f, fsFromStart, 0L);
  375.     }
  376.     return (f);
  377. }
  378.  
  379. CloseFile(f)
  380. short f;
  381. {
  382. long count;
  383.         (void) GetFPos (f, &count);
  384.         (void) SetEOF (f, count);
  385.         (void) FSClose (f);
  386. }