home *** CD-ROM | disk | FTP | other *** search
- /* 14:00 22-Jun-88 (sundayio.c) Sunday Afternoon Advisor I/O package */
-
- /************************************************************************
- * Copyright(C) 1988-1990 NeuralWare Inc *
- * Penn Center West, IV-227, Pittsburgh, PA 15276 *
- * *
- * All rights reserved. No part of this program may be reproduced, *
- * stored in a retrieval system, or transmitted, in any form or by any *
- * means, electronic, mechanical, photocopying, recording or otherwise *
- * without the prior written permission of the copyright owner, *
- * NeuralWare, Inc. *
- ************************************************************************
- */
-
- #include <math.h>
-
- #define UIO_SERVER 1
- #define SERVER_EMULATOR_FILE 1
- #define SKIP_COMPAT_DATA 1
- #include "userutl.h"
- #include "uio_strc.h"
- #include "uio_tran.h"
- #include "uio_reqs.pro"
- #include "uio_cmds.pro"
-
- #ifdef MAC
- #include "macuio.redef"
- #endif
-
- #define arraysize(array) (sizeof(array) / sizeof(array[0]))
-
- /************************************************************************
- * *
- * SundayIO - Sunday Afternoon Advisor I/O *
- * *
- ************************************************************************
-
- This I/O module is designed to provide a convenient interface to
- the Sunday Afternoon Advisor.
-
- LEARNING: During learning, it uses a file (sunday.dat) which trains
- the network on specific input patterns (the amount of work at the
- office and my feelings toward my wife) and output class (0..4).
-
- The format of the input file is:
-
- Output_Class Amount_of_work Feelings_toward_Wife
-
- Look at "sunday.dat" to see how this is done. The data for this
- example is explained in the text.
-
- RECALL: In recall mode, you will be prompted for two inputs: Work and
- Feelings. The code will translate the first character of each
- input to a numeric value, or you can enter a numeric value. The
- translations performed are:
-
- Work: N = None (0.0)
- S = Some (0.5)
- L = Lots (1.0)
-
- Feelings: D = Dog_house (0.0)
- G = Good (0.5)
- R = Romantic (1.0)
-
- So, to enter Some Work, Feeling Romantic respond with:
-
- Some Romantic
-
- or: 0.5 1.0
-
- */
-
- /* */
- /************************************************************************
- * *
- * ReadSample() - get sample data *
- * *
- ************************************************************************
- */
-
- int _stack = 8192; /* leave lots of stack space */
-
- struct SAMPLE
- {
- long DesiredOut; /* output class */
- float Input[4]; /* input value */
- };
-
- #define MAXSAM 128 /* maximum number of samples */
-
- long PassPerUpdate = 0; /* # of passes between updates */
- int NSamples = 0; /* # of samples read in */
- struct SAMPLE TestData[MAXSAM] = {0}; /* test data read from user */
- char SampX[MAXSAM] = {0}; /* sample index buffer */
- int SX = MAXSAM; /* index into sample array */
-
-
- /* */
- NINT ReadSample(buf)
- char *buf;
- {
- char *sp; /* input string */
- int wx; /* work index */
- FILE *fp; /* input file pointer */
-
- TryAgain:
- NSamples = 0; /* kill sample array */
- for(;;)
- {
- PutStr("What is the sample input file: ");
- sp = GetStr();
- while(*sp && *sp <= ' ')
- sp++;
- if (*sp == '\0') /* no input file */
- return(UIO_ERROR);
-
- if ((fp = fopen( sp, "r")) == (FILE *)0)
- {
- PutStr( sp );
- PutStr( " -- will not open\n" );
- }
- else
- break;
- }
-
- /* read in the sample data */
- for(wx = 0; wx < MAXSAM;)
- {
- /* get a new line */
- if (fgets( buf, 100, fp ) == 0)
- break;
-
- /* kill trailing comments */
- for( sp = &buf[0]; *sp; sp++ )
- {
- if (*sp == '!' || *sp == '\n' || *sp == '\r')
- {
- *sp = '\0';
- break;
- }
- }
-
- /* skip leading white space */
- for(sp = &buf[0]; *sp != 0; sp++ )
- if (*sp > ' ')
- break;
- if (*sp == 0 )
- continue; /* ignore blank lines */
-
- /* convert the data on the line to float and save them */
- sscanf( sp, "%ld %f %f %f %f",
- &TestData[wx].DesiredOut,
- &TestData[wx].Input[0],
- &TestData[wx].Input[1],
- &TestData[wx].Input[2],
- &TestData[wx].Input[3]);
-
- wx++;
- }
- fclose(fp);
-
- NSamples = wx;
-
- /* */
- if (NSamples == 0)
- {
- PutStr("Nothing in the INPUT file, re-enter\n");
- goto TryAgain;
- }
-
- sprintf(buf, "Samples=%ld, How many passes per update: ", (long)NSamples);
- PutStr(buf);
-
- sp = GetStr();
- PassPerUpdate = 10l;
- sscanf(sp, "%ld", &PassPerUpdate);
-
- return(UIO_OK);
- }
-
- /* */
- float MSE = 0; /* mean square error */
- long Pass = 0; /* pass number */
- int Item = 0; /* item in the current pass */
- struct SAMPLE *SamP = 0; /* pointer to last sample */
- char buf[102]; /* work buffer */
-
- /*****************************************************************************/
- /* */
- /* Functions neccessary for handling the User I/O package itself. */
- /* */
- /*****************************************************************************/
-
- /* FUNCTION */
- NINT UIO_Init(file)
- TEXT *file;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Term(process)
- NINT process;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
- /* close any files which may be open. Deallocate any memory
- which was allocated. (This is VERY VERY important. If
- allocated memory is NOT released, dos memory will become
- fragmented and it will become necessary to reboot.
- */
-
- PutStr("bye bye\n");
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Attention()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- SX = MAXSAM;
- Item = 0;
- Pass = 0;
- PassPerUpdate = 10;
- NSamples = 0;
-
- return(ret_val);
- }
- /* */
-
-
- /*****************************************************************************/
- /* */
- /* Functions necessary for handling a learning session. */
- /* */
- /*****************************************************************************/
-
- /* FUNCTION */
- NINT UIO_Learn_Start()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- if (NSamples == 0)
- ret_val = ReadSample(&buf[0]); /* read samples */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Learn_Input(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- int wx; /* work index */
- int r; /* work random number */
-
- /* Datap points to an empty array of nPEs elements. The
- values placed in this array by the user will become the
- inputs to the network for training purposes.
- */
-
- if (NSamples <= 0)
- {
- return(UIO_ERROR);
- }
-
- /* */
- if (SX >= NSamples)
- {
- for(SX = 0; SX < NSamples; SX++)
- {
- RandAgain:
- r = rand() % NSamples; /* try a random number*/
- for( wx = 0; wx < SX; wx++ )
- {
- if (SampX[wx] == r) goto RandAgain;
- }
- SampX[SX] = r;
- }
- SX = 0;
- }
- r = SampX[SX++];
-
- SamP = &TestData[r];
- for(wx = 0; wx < nPEs; wx++)
- Datap[wx] = (wx < 4)? SamP->Input[wx]: 0.0;
-
- Item++; /* count items in a pass */
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Learn_Output(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- int wx; /* work index */
-
- /* Datap points to an empty array of nPEs values. The
- elements of the array will become the desired outputs for
- training purposes. These desired outputs correspond to
- the most recent "RQ_LEARNIN" request.
- */
-
- if (NSamples <= 0)
- {
- return(UIO_ERROR);
- }
-
- for(wx = 0; wx < nPEs; wx++)
- Datap[wx] = (wx==SamP->DesiredOut? 1.0:0.0);
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Learn_Result(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- int wx; /* work index */
- float v; /* real value */
-
- /* USER TO PLACE CODE HERE */
- for(wx = 0; wx < nPEs; wx++)
- {
- v = Datap[wx] - ((wx==SamP->DesiredOut) ? 1.0 : 0.0);
- MSE += v*v;
- }
-
- if (Item >= NSamples)
- {
- Item = 0;
- Pass++;
- if (PassPerUpdate == 0)
- PassPerUpdate = 1;
-
- if ((Pass % PassPerUpdate) != 0)
- return(ret_val);
-
- MSE = sqrt(MSE) / (NSamples * PassPerUpdate);
- sprintf(buf, "Pass = %4ld, MSE = %.4f\n", Pass, MSE);
- PutStr(buf);
-
- ret_val = (MSE < .00001) ?
- UIO_ERROR : UIO_UPDATE; /* end / update display */
- MSE = 0.0;
- }
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Learn_End()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
- /**/
-
-
- /*****************************************************************************/
- /* */
- /* Functions necessary for handling a recall or testing session. */
- /* */
- /*****************************************************************************/
-
- /* FUNCTION */
- NINT UIO_Recall_Start()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Read(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
- int wx; /* work index */
- float WorkIn[4]; /* work input */
- char *sp; /* string pointer */
- int r; /* work random number */
- float v; /* real value */
-
- /* USER TO PLACE CODE HERE */
- /* Datap points to an empty array of nPEs values. The
- user must fill in these values. The elements of the
- array will become the "sum" of the inputs to the input
- layer of processing elements.
- */
-
- for( wx = 0; wx < 4; wx++ )
- WorkIn[wx] = 0.0;
-
- PutStr("Enter 'Work Feelings' (E=End): "); /* prompt user */
- sp = GetStr(); /* read input */
- while(*sp == ' ') /* skip spaces */
- sp++;
-
- if (*sp == 'e' || *sp == 'E')
- {
- return(UIO_ERROR); /* end of input */
- }
-
-
- r = *sp;
- v = 0.0;
- if ( r == 'n' || r == 'N' ) v = 0.0; /* none */
- else if ( r == 's' || r == 'S' ) v = 0.5; /* some */
- else if ( r == 'l' || r == 'L' ) v = 1.0; /* lots */
- else {
- /* assume it is a number an convert it */
- sscanf( sp, "%f", &v );
- }
- Datap[0] = v;
-
- while( (r = *sp) != '\0' ) { /* skip number or text */
- if ( r == ' ' ) break;
- sp++;
- }
- while( (r = *sp) != '\0' ) { /* skip following space */
- if ( r != ' ' ) break;
- sp++;
- }
-
- v = 0.0;
- if ( r == 'd' || r == 'D' ) v = 0.0; /* dog_house */
- else if ( r == 'g' || r == 'G' ) v = 0.5; /* good */
- else if ( r == 'r' || r == 'R' ) v = 1.0; /* romantic */
- else {
- /* assume it is a number an convert it */
- sscanf( sp, "%f", &v );
- }
- if ( nPEs > 1 ) Datap[1] = v;
-
- return(ret_val);
- }
- /* */
-
- /* FUNCTION */
- NINT UIO_Write(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
- /* Datap points to an array of nPEs "float" type values.
- The values are the outputs of the top-most layer of the
- network.
- */
- #ifdef WRITEIT
- PutStr( "Result =" );
- for( wx = 0; wx < nPEs; wx++ ) {
- sprintf( buf, " %.4f", Datap[wx] );
- PutStr( buf );
- }
- PutStr( "\n" );
- #endif
-
- ret_val = UIO_UPDATE; /* update display */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Write_Step(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
- /* */
-
- /* FUNCTION */
- NINT UIO_Recall_Test(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
- /* Datap points to an empty array of nPEs values. The
- elements of the array will become the desired outputs for
- recall purposes. This request is only made during a
- Execute Network/Recall Test.
- */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Recall_End()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
-
- /**/
- /*****************************************************************************/
- /* */
- /* Other miscelaneous functions. */
- /* */
- /*****************************************************************************/
-
- /* FUNCTION */
- NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
- NINT Instrument_id;
- NINT nDataElems;
- SREAL *DataElemp;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
- /* FUNCTION */
- NINT UIO_Rewind()
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
-
- /* FUNCTION */
- NINT UIO_Explain(LayN, nPEs, Datap)
- NINT LayN;
- NINT nPEs;
- SREAL *Datap;
- {
- NINT ret_val = UIO_OK;
-
- /* USER TO PLACE CODE HERE */
-
- return(ret_val);
- }
-
-