home *** CD-ROM | disk | FTP | other *** search
- /*BrainFile.c Core routines for reading in a neural Network.
-
- This file is Copyright 1989 by Pierce T. Wetter III
-
- Permission granted for all non-commercial uses of this code. For commercial uses of this code
- contact me at
-
- Pierce T. Wetter III
- 45 Vista Lago Dr.
- Simi Valley, CA 93065
-
- or
-
- wetter@csvax.caltech.edu or pwetter@caltech.bitnet
-
- */
-
- #include <FileMgr.h>
- #include "NetLib.h"
- #include "Brain.h"
- #include "StdFilePkg.h"
-
-
- /* Brain.h can be eliminated if you use the following dec.
-
- typedef struct _patternlist{
- struct _patternlist *next,*before;
- double *inputs;
- double *outputs;
- } patlist,*patlistptr;
-
- */
-
- #define nil 0L
-
- extern mind *mymind;
- extern patlistptr patterns;
- static short ferror=0,OpenFile();
- extern double maxerr;
-
- double ReadFloat(),atof();
- short ReadInt();
- long atol();
-
- short GetNet()
- {
- short ni,nh,no;
- double moment,lrate;
- short fileref,i,j;
-
- fileref=OpenFile(true);
- if (ferror) return(false);
- ni=ReadInt(fileref);
- if (ferror) return(false);
- nh=ReadInt(fileref);
- if (ferror) return(false);
- no=ReadInt(fileref);
- if (ferror) return(false);
- moment=ReadFloat(fileref);
- if (ferror) return(false);
- lrate=ReadFloat(fileref);
- if (ferror) return(false);
- maxerr=ReadFloat(fileref);
- if (ferror) return(false);
- if (!mymind)
- {
- initmind(ni,nh,no,moment,lrate,&mymind);
- }
- else
- {
- mymind->momentum=moment;
- mymind->learnrate=lrate;
- if ((ni != mymind->nin) || (nh != mymind->nhidden) || (no != mymind->nout))
- {
- return(false);
- }
- }
- for (i=0;i<mymind->nin;i++)
- {
- mymind->inbias[i]=ReadFloat(fileref);
- if (ferror) return(false);
- for (j=0;j<mymind->nhidden;j++)
- {
- mymind->inweights[j][i]=ReadFloat(fileref);
- if (ferror) return(false);
- }
- }
- for(i=0;i<mymind->nhidden;i++)
- {
- mymind->hiddenbias[i]=ReadFloat(fileref);
- if (ferror) return(false);
- for (j=0;j<mymind->nout;j++)
- {
- mymind->hiddenweights[j][i]=ReadFloat(fileref);
- if (ferror) return(false);
- }
- }
- for(i=0;i<mymind->nout;i++)
- {
- mymind->outbias[i]=ReadFloat(fileref);
- if (ferror) return(false);
- }
- CloseFile(fileref);
-
- return (true);
- }
-
- short SaveNet()
- {
- char buf[256];
- short fileref,i,j;
-
- fileref=OpenFile(false);
- if (ferror) return(false);
- sprintf(buf,"%d\r",mymind->nin);
- WriteString(buf,fileref);
- if (ferror) return(false);
- sprintf(buf,"%d\r",mymind->nhidden);
- WriteString(buf,fileref);
- if (ferror) return(false);
- sprintf(buf,"%d\r",mymind->nout);
- WriteString(buf,fileref);
- if (ferror) return(false);
- sprintf(buf,"%g\r",mymind->momentum);
- WriteString(buf,fileref);
- if (ferror) return(false);
- sprintf(buf,"%g\r",mymind->learnrate);
- WriteString(buf,fileref);
- if (ferror) return(false);
- sprintf(buf,"%g\r",maxerr);
- WriteString(buf,fileref);
- if (ferror) return(false);
- for (i=0;i<mymind->nin;i++)
- {
- sprintf(buf,"%g\r",mymind->inbias[i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- for(j=0;j<mymind->nhidden;j++)
- {
- sprintf(buf,"%g\r",mymind->inweights[j][i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- }
- }
- for (i=0;i<mymind->nhidden;i++)
- {
- sprintf(buf,"%g\r",mymind->hiddenbias[i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- for(j=0;j<mymind->nout;j++)
- {
- sprintf(buf,"%g\r",mymind->hiddenweights[j][i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- }
- }
- for (i=0;i<mymind->nout;i++)
- {
- sprintf(buf,"%g\r",mymind->outbias[i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- }
- CloseFile(fileref);
-
- return (true);
- }
-
-
- short SavePat()
- {
- patlistptr cpat;
- short count,fileref,i;
- char buf[256];
-
- fileref=OpenFile(false);
- if (ferror) return(false);
- count=0;
- for(cpat=patterns;cpat;cpat=cpat->next)
- {
- count++;
- }
- sprintf(buf,"%d\r",count);
- WriteString(buf,fileref);
- if (ferror) return(false);
- for(cpat=patterns;cpat;cpat=cpat->next)
- {
- for(i=0;i<mymind->nin;i++)
- {
- sprintf(buf,"%g\r",cpat->inputs[i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- }
- for(i=0;i<mymind->nout;i++)
- {
- sprintf(buf,"%g\r",cpat->outputs[i]);
- WriteString(buf,fileref);
- if (ferror) return(false);
- }
- }
- CloseFile(fileref);
-
- return (true);
- }
-
- short GetPat()
- {
- short count,fileref,i,j;
- patlistptr opat,NewPattern();
-
- fileref=OpenFile(true);
- if (ferror) return(false);
- count=ReadInt(fileref);
- if (ferror) return(false);
- for (j=0;j<count;j++)
- {
- opat=patterns;
- patterns=NewPattern();
- for(i=0;i<mymind->nin;i++)
- {
- patterns->inputs[i]=ReadFloat(fileref);
- if (ferror) return(false);
- }
- for(i=0;i<mymind->nout;i++)
- {
- patterns->outputs[i]=ReadFloat(fileref);
- if (ferror) return(false);
- }
- patterns->next=opat;
- opat->before=patterns;
- }
- CloseFile(fileref);
- return (true);
- }
-
- short GenPat()
- {
-
- return (true);
- }
-
- double ReadFloat(fileref)
- short fileref;
- {
- char buf[256];
- short i;
- long count=1;
-
- if (!ferror)
- {
- for (i=0;i<256;i++)
- {
- ferror=FSRead(fileref,&count,&buf[i]);
- if (buf[i]=='\r') break;
- if (ferror) break;
- }
- buf[i]=0;
- }
- if (!ferror) return (atof(buf));
-
- }
-
- short ReadInt(fileref)
- short fileref;
- {
- char buf[256];
- short i;
- long count=1,result;
-
- if (!ferror)
- {
- for (i=0;i<256;i++)
- {
- ferror=FSRead(fileref,&count,&buf[i]);
- if (buf[i]=='\r') break;
- if (ferror) break;
- }
- result=atol(buf);
- }
- if (!ferror) return ((short) result);
-
- }
-
- WriteString(buf,fileref)
- char *buf;
- short fileref;
- {
- long count,ocount,ncount;
-
- if (!ferror)
- {
- ocount=count=strlen(buf);
- ferror=FSWrite(fileref,&count,buf);
- if (count!=ocount)
- {
- ocount -=count;
- ncount=ocount;
- ferror=Allocate(fileref,&ncount);
- ferror=FSWrite(fileref,&ocount,buf+count);
- }
- }
-
- }
-
- FileError()
- {
-
- switch (ferror)
- {
- case noErr: break;
- default: SysBeep(60);
- break;
- }
- }
-
- extern Point dlogWhere;
-
- short OpenFile(read)
- Boolean read;
- {
- short f;
- FInfo fndrInfo; /* finder info */
- SFReply tmpFile;
- Handle hText;
- long count;
- OSErr result;
- Boolean haveNewFile = false;
- OSType type = 'TEXT';
-
-
- ferror=0;
-
- /* GetFile/PutFile location */
-
- pStrCopy( "\pNeural.output",tmpFile.fName); /* initialize default name */
- if (read)
- {
- SFGetFile (dlogWhere, "\pChoose file:",nil,1,&type,
- 0L, &tmpFile);
- }
- else
- {
- SFPutFile (dlogWhere, "\pSave file as:", tmpFile.fName,
- 0L, &tmpFile);
- }
- if (!tmpFile.good)
- return (0);
- else
- {
- haveNewFile = true;
- if (GetFInfo (tmpFile.fName, tmpFile.vRefNum, &fndrInfo)
- == noErr) /* exists */
- {
- if (fndrInfo.fdType != 'TEXT')
- {
- ErrMesg ("\pNot a TEXT File");
- return (0);
- }
- }
- else /* doesn't exist. create it. */
- {
- if (Create (tmpFile.fName, tmpFile.vRefNum,
- 'MPS ', 'TEXT') != noErr)
- {
- ErrMesg ("\pCan't Create");
- return (0);
- }
- }
- }
-
- if (ferror=FSOpen (tmpFile.fName, tmpFile.vRefNum, &f) != noErr)
- ErrMesg ("\pCan't Open");
- else
- {
- (void) SetFPos (f, fsFromStart, 0L);
- }
- return (f);
- }
-
- CloseFile(f)
- short f;
- {
- long count;
- (void) GetFPos (f, &count);
- (void) SetEOF (f, count);
- (void) FSClose (f);
- }