home *** CD-ROM | disk | FTP | other *** search
- /* 15:30 04-Jul-88 (bsbhebb.c) Brain-State-in-a-Box Hebb I/O */
-
- /************************************************************************
- * Copyright(C) 1988-1990 Neural-Ware Inc *
- * Penn Center West, IV-227, Pittsburgh, PA 15276, U.S.A *
- * *
- * 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, *
- * Neural-Ware, Inc. *
- ************************************************************************
- */
-
- /************************************************************************
- * *
- * Hebbian Learning for BSB (built-in data) *
- * *
- ************************************************************************
- */
- #include "userutl.h"
-
- #ifndef SUN
- #ifndef DLC
- #include <stdlib.h>
- #endif
- #endif
-
- #ifdef MAC
- #include "macuio.redef"
- #endif
-
- /* Input arrays */
- static int curr_index[3] = {0}; /* indices into character arrays */
- static int num_words[3] = { 7, 3, 3 };/* # words in each category */
-
- static char name[2][7][17] = {
- {
- {'S','o','c','r','a','t','e','s',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'A','l','c','i','b','i','a','d','e','s',' ',' ',' ',' ',' ',' ',0},
- {'P','l','a','t','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'Z','e','u','s',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'A','p','o','l','l','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'D','i','a','n','a',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
- },
- {
- {'+','+','+','+','+','+','+','+','-','-','-','-','-','-','-','-',0},
- {'-','-','-','-','+','+','+','+','-','-','-','-','+','+','+','+',0},
- {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
- {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
- {'+','+','+','+','-','-','-','-','-','-','-','-','+','+','+','+',0},
- {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
- }};
- static char species[2][3][17] = {
- {
- {'M','a','n',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'G','o','d',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
- },
- {
- {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
- {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
- }};
- static char lifespan[2][3][17] = {
- {
- {'M','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {'I','m','m','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',0},
- {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
- },
- {
- {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
- {'-','-','-','-','-','-','-','-','+','+','+','+','+','+','+','+',0},
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
- }};
- static char outcode[3][17] = {
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
- {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
- };
- #define BSB_NAME 0
- #define BSB_SPECIES 1
- #define BSB_LIFESPAN 2
- #define BSB_ENTER 3
- #define BSB_QUIT 4
- #define BSB_OUTPUT 5
- /* Local menu definitions */
-
- #define STR1 "Socrates "
- #define STR2 "++++++++--------"
- #define STR3 "Man "
- #define STR4 "+-+-+-+-+-+-+-+-"
- #define STR5 "Mortal "
- #define STR6 "+--++--++--++--+"
- #define STR7 "Enter"
- #define STR8 "Quit"
- #define STR9 "................ ................ ................"
- #define NULL_STR 0
- #ifdef MAC /* Mac cannot assign pointers at compile time in code resources */
-
- static GMENU_ITEM BsbList[] = {
- { BSB_NAME, NULL_STR, NULL_STR },
- { BSB_SPECIES, NULL_STR, NULL_STR },
- { BSB_LIFESPAN, NULL_STR, NULL_STR },
- { BSB_ENTER, NULL_STR, NULL_STR },
- { BSB_QUIT, NULL_STR, NULL_STR },
- { BSB_OUTPUT, NULL_STR, NULL_STR, 0x0002 }
- };
-
- #else /* MAC */
-
- static GMENU_ITEM BsbList[] = {
- { BSB_NAME, "Socrates ", "++++++++--------" },
- { BSB_SPECIES, "Man ", "+-+-+-+-+-+-+-+-" },
- { BSB_LIFESPAN, "Mortal ", "+--++--++--++--+" },
- { BSB_ENTER, "Enter", "" },
- { BSB_QUIT, "Quit" , "" },
- { BSB_OUTPUT, "................ ................ ................",
- "", 0x0002 }
- };
- #endif /* MAC */
-
- static GMENU InpMenu = { /* Input menu */
- 0,
- 3, /* 3 items */
- 1, /* key */
- 0x0001 /* auxillary line of text */
- };
-
- static GMENU UtMenu = { /* Utility menu */
- 0,
- 2, /* 2 items */
- 2, /* key */
- 0x0000
- };
-
- static GMENU OutMenu = { /* Output menu */
- 0,
- 1, /* 1 item */
- 3, /* key */
- 0x0000
- };
-
- static int x_inp,y_inp,x_out,y_out,x_ut,y_ut; /* menu window positions */
-
- /************************************************************************
- * *
- * UsrIO - user I/O routine to handle requests from NWORKS *
- * *
- ************************************************************************
- */
-
- static int InitFlag = 0; /* initialize things flag */
-
- int UsrIO() /* handle NWORKS requests */
- {
- GMENU_ITEM *gmip;
- char *sp; /* string pointer */
- int wx, wy; /* work indices */
- int key, xp, yp, button; /* mouse interface */
- int *ci, nw;
- int xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
-
- if ( InitFlag == 0 ) {
- /* open any files which may be required at this point in the
- code. */
- /* Get screen parameters */
-
- InpMenu.item = &BsbList[BSB_NAME];
- UtMenu.item = &BsbList[BSB_ENTER];
- OutMenu.item = &BsbList[BSB_OUTPUT];
- #ifdef MAC
- BsbList[0].text = STR1;
- BsbList[0].text2 = STR2;
- BsbList[1].text = STR3;
- BsbList[1].text2 = STR4;
- BsbList[2].text = STR5;
- BsbList[2].text2 = STR6;
- BsbList[3].text = STR7;
- BsbList[4].text = STR8;
- BsbList[5].text = STR9;
- #endif
- ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
-
- if ( ncolor < 8 ) {
- gm_intcolor = 1;
- gm_txtcolor = 0;
- gm_outcolor = 0;
- } else {
- gm_intcolor = 7;
- gm_txtcolor = 4;
- gm_outcolor = 0;
- }
-
- /* Initialize menus and set window positions */
- InitGMenu( &InpMenu, chrx, chry );
- InitGMenu( &OutMenu, chrx, chry );
- InitGMenu( &UtMenu, chrx, chry );
- x_inp = (xsize - InpMenu.x1) / 2;
- x_out = (xsize - OutMenu.x1) / 2;
- x_ut = (xsize - UtMenu.x1) / 2;
- y_ut = ysize / 3;
- y_out = y_ut + 30;
- y_inp = y_out + 30;
-
-
- InpMenu.x0 = 0; /* Menu position relative to window */
- InpMenu.y0 = 0;
- OutMenu.x0 = 0; /* Menu position relative to window */
- OutMenu.y0 = 0;
- UtMenu.x0 = 0; /* Menu position relative to window */
- UtMenu.y0 = 0;
-
- InitFlag = 1;
- }
-
- IORTNCDE = 0; /* good return for data */
- switch( IOREQCDE ) {
- case RQ_ATTENTION:
- /* This is invoked at the request of the user from the
- execute menu. It allows parameters to be changed or
- altered. Any graphics or other interactions are allowable
- as usual for the other options.
- */
- break;
-
- case RQ_LSTART: /* starting learn */
- /* This tells the user that the program is about to start
- learning. It is called once for a LEARN ALL, LEARN ONE,
- LEARN N, or LEARN START
- */
- break;
-
- case RQ_LEARNIN: /* read training input */
- /* IODATA points to an empty array of IOCOUNT elements. The
- values placed in this array by the user will become the
- inputs to the network for training purposes.
- */
- case RQ_READ: /* read test data */
- /* IODATA points to an empty array of IOCOUNT 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.
- */
-
- ug_window( InpMenu.key, gm_intcolor, x_inp, y_inp,
- x_inp + InpMenu.x1 + 2, y_inp + InpMenu.y1 + 2);
- ug_window( UtMenu.key, gm_intcolor, x_ut, y_ut,
- x_ut + UtMenu.x1 + 2, y_ut + UtMenu.y1 + 2);
-
- DispGMenu( &InpMenu );
- DispGMenu( &UtMenu );
- for ( ; ; ) {
- while ( (gmip=LookGMenu(&InpMenu,&button)) != (GMENU_ITEM *) 0 ) {
- switch( gmip->code ) {
- case BSB_NAME:
- if (button == MBUT_RIGHT || button == MBUT_LEFT) {
- ci = &curr_index[BSB_NAME];
- nw = num_words[BSB_NAME];
- if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
- else *ci = (*ci + nw - 1) % nw;
- gmip->text = name[0][*ci];
- gmip->text2 = name[1][*ci];
- DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
- }
- break;
- case BSB_SPECIES:
- if (button == MBUT_RIGHT || button == MBUT_LEFT) {
- ci = &curr_index[BSB_SPECIES];
- nw = num_words[BSB_SPECIES];
- if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
- else *ci = (*ci + nw - 1) % nw;
- gmip->text = species[0][*ci];
- gmip->text2 = species[1][*ci];
- DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
- }
- break;
- case BSB_LIFESPAN:
- if (button == MBUT_RIGHT || button == MBUT_LEFT) {
- ci = &curr_index[BSB_LIFESPAN];
- nw = num_words[BSB_LIFESPAN];
- if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
- else *ci = (*ci + nw - 1) % nw;
- gmip->text = lifespan[0][*ci];
- gmip->text2 = lifespan[1][*ci];
- DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
- }
- break;
- }
- /* If button was pressed, wait for it to be released */
- while ( button != 0 )
- ug_mouse( &key, &xp, &yp, &button );
- }
- while ( (gmip=LookGMenu( &UtMenu, &button )) != (GMENU_ITEM *) 0 ) {
- switch( gmip->code ) {
- case BSB_ENTER:
- /* Fill in IODATA with bit patterns for current input */
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
- for ( wy = 0, wx = 0; wy < 3; wy++ ) {
- if (wy == 0 ) sp = name[1][curr_index[wy]];
- else if (wy == 1) sp = species[1][curr_index[wy]];
- else sp = lifespan[1][curr_index[wy]];
- for( ; wx<(wy+1)*16; wx++, sp++ ) {
- if ( *sp == '+' ) IODATA[wx] = 1.0;
- else if ( *sp == '-' ) IODATA[wx] = -1.0;
- else IODATA[wx] = 0.0;
- }
- }
- goto end_read;
- }
- break;
- case BSB_QUIT:
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
- IORTNCDE = -1;
- goto end_read;
- }
- break;
- }
- /* If button was pressed, wait for it to be released */
- while ( button != 0 )
- ug_mouse( &key, &xp, &yp, &button );
- }
- }
- end_read:
- break;
-
- case RQ_LEARNOUT: /* read desired output */
- /* IODATA points to an empty array of IOCOUNT 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.
- */
-
- break;
-
- case RQ_WRITE: /* write out results */
- /* IODATA points to an array of IOCOUNT "float" type values.
- The values are the outputs of the top-most layer of the
- network.
- */
- IORTNCDE = 1;
- break;
-
- case RQ_LEARNRSLT:
- /* IODATA points to an array of IOCOUNT values. These are the
- output of the network caused by the inputs from RQ_LEARNIN.
- */
- ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
- x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
-
- sp = BsbList[BSB_OUTPUT].text;
- for ( wy = 0, wx = 0; wy < 3; wy++ ) {
- if ( wy != 0 ) *sp++ = ' ';
- for( ; wx<(wy+1)*16; wx++, sp++ ) {
- if (IODATA[wx] > 0.0) *sp = '+';
- else if (IODATA[wx] < 0.0) *sp = '-';
- else *sp = '.';
- }
- }
- DispGMenu( &OutMenu );
-
- /* Lock Items */
- BsbList[BSB_QUIT].flag |= GM_LOCKED;
- BsbList[BSB_NAME].flag |= GM_LOCKED;
- BsbList[BSB_SPECIES].flag |= GM_LOCKED;
- BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
- PutStr( "Select ENTER to continue\n" );
- for ( ; ; ) {
- while ( (gmip=LookGMenu( &UtMenu, &button )) == (GMENU_ITEM *)0 ) ;
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) break;
- }
- /* Unlock Items */
- BsbList[BSB_QUIT].flag &= ~GM_LOCKED;
- BsbList[BSB_NAME].flag &= ~GM_LOCKED;
- BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
- BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
- IORTNCDE = 1;
- break;
-
- case RQ_WRSTEP: /* write interim results */
- /* each recall cycle for the Hopfield and BAM control strategies,
- the intermediate output is made available to userio to test
- for convergence or other desired states. This option is
- not used for other control strategies.
- */
-
- ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
- x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
-
- sp = BsbList[BSB_OUTPUT].text;
- for ( wy = 0, wx = 0; wy < 3; wy++ ) {
- if ( wy != 0 ) *sp++ = ' ';
- for( ; wx<(wy+1)*16; wx++, sp++ ) {
- if (IODATA[wx] > 0.0) *sp = '+';
- else if (IODATA[wx] < 0.0) *sp = '-';
- else *sp = '.';
- }
- }
- DispGMenu( &OutMenu );
-
- /* Lock Items */
- BsbList[BSB_NAME].flag |= GM_LOCKED;
- BsbList[BSB_SPECIES].flag |= GM_LOCKED;
- BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
- PutStr( "Select ENTER to continue, QUIT to terminate recall\n" );
- for ( ; ; ) {
- while ( (gmip=LookGMenu(&UtMenu,&button )) == (GMENU_ITEM *)0 ) ;
- switch( gmip->code ) {
- case BSB_ENTER:
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) goto end_wr;
- break;
- case BSB_QUIT:
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
- IORTNCDE = -1;
- goto end_wr;
- }
- break;
- }
- }
- end_wr:
- /* Unlock Items */
- BsbList[BSB_NAME].flag &= ~GM_LOCKED;
- BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
- BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
-
- break;
-
- case RQ_RSTART: /* starting recall */
- /* This tells the user that the program is about to start
- a recall. It is called once for a REACLL ALL, RECALL ONE,
- RECALL N, or RECALL START.
- */
- break;
-
- case RQ_RCLTST: /* read desired output during recall test */
- /* IODATA points to an empty array of IOCOUNT 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.
- */
-
- break;
-
- case RQ_TERM: /* terminate interface */
- /* 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" );
- break;
- }
-
- return;
- }
-