home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e065 / 2.ddi / BSBHEBB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  14.9 KB  |  459 lines

  1. /* 15:30  04-Jul-88  (bsbhebb.c)   Brain-State-in-a-Box Hebb I/O */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1988-1990 Neural-Ware Inc         *
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276, U.S.A  *
  6.  *                  *
  7.  * All rights reserved.  No part of this program may be reproduced, *
  8.  * stored in a retrieval system, or transmitted, in any form or by any  *
  9.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  10.  * without the prior written permission of the copyright owner,   *
  11.  * Neural-Ware, Inc.              *
  12.  ************************************************************************
  13.  */
  14.  
  15. /************************************************************************
  16.  *                  *
  17.  *  Hebbian Learning for BSB (built-in data)      *
  18.  *                  *
  19.  ************************************************************************
  20.  */
  21. #include "userutl.h"
  22.  
  23. #ifndef SUN
  24. #ifndef DLC
  25. #include <stdlib.h>
  26. #endif
  27. #endif
  28.  
  29. #ifdef MAC
  30. #include "macuio.redef"
  31. #endif
  32.  
  33. /* Input arrays */
  34. static  int  curr_index[3] = {0}; /* indices into character arrays */
  35. static  int  num_words[3] = { 7, 3, 3 };/* # words in each category */
  36.  
  37. static  char  name[2][7][17] = {
  38. {
  39.   {'S','o','c','r','a','t','e','s',' ',' ',' ',' ',' ',' ',' ',' ',0},
  40.   {'A','l','c','i','b','i','a','d','e','s',' ',' ',' ',' ',' ',' ',0},
  41.   {'P','l','a','t','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  42.   {'Z','e','u','s',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  43.   {'A','p','o','l','l','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  44.   {'D','i','a','n','a',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  45.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
  46. },
  47. {
  48.   {'+','+','+','+','+','+','+','+','-','-','-','-','-','-','-','-',0},
  49.   {'-','-','-','-','+','+','+','+','-','-','-','-','+','+','+','+',0},
  50.   {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
  51.   {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
  52.   {'+','+','+','+','-','-','-','-','-','-','-','-','+','+','+','+',0},
  53.   {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
  54.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  55. }};
  56. static  char  species[2][3][17] = {
  57. {
  58.   {'M','a','n',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  59.   {'G','o','d',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  60.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
  61. },
  62. {
  63.   {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
  64.   {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
  65.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  66. }};
  67. static  char  lifespan[2][3][17] = {
  68. {
  69.   {'M','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  70.   {'I','m','m','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',0},
  71.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  72. },
  73. {
  74.   {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
  75.   {'-','-','-','-','-','-','-','-','+','+','+','+','+','+','+','+',0},
  76.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  77. }};
  78. static  char  outcode[3][17] = {
  79.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  80.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  81.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  82. };
  83. #define BSB_NAME  0
  84. #define BSB_SPECIES 1
  85. #define BSB_LIFESPAN  2
  86. #define BSB_ENTER 3
  87. #define BSB_QUIT  4
  88. #define BSB_OUTPUT  5
  89. /* Local menu definitions */
  90.  
  91. #define STR1 "Socrates        "
  92. #define STR2 "++++++++--------"
  93. #define STR3 "Man             "
  94. #define STR4 "+-+-+-+-+-+-+-+-"
  95. #define STR5 "Mortal          "
  96. #define STR6 "+--++--++--++--+"
  97. #define STR7 "Enter"
  98. #define STR8 "Quit"
  99. #define STR9 "................ ................ ................"
  100. #define NULL_STR 0
  101. #ifdef MAC  /* Mac cannot assign pointers at compile time in code resources */
  102.  
  103. static GMENU_ITEM BsbList[] = {
  104.     { BSB_NAME,   NULL_STR, NULL_STR },
  105.     { BSB_SPECIES,  NULL_STR, NULL_STR },
  106.     { BSB_LIFESPAN, NULL_STR, NULL_STR },
  107.     { BSB_ENTER,  NULL_STR, NULL_STR  },
  108.     { BSB_QUIT,   NULL_STR, NULL_STR },
  109.     { BSB_OUTPUT, NULL_STR, NULL_STR, 0x0002 }
  110. };
  111.  
  112. #else /* MAC */
  113.  
  114. static GMENU_ITEM BsbList[] = {
  115.     { BSB_NAME,   "Socrates        ", "++++++++--------" },
  116.     { BSB_SPECIES,  "Man             ", "+-+-+-+-+-+-+-+-" },
  117.     { BSB_LIFESPAN, "Mortal          ", "+--++--++--++--+" },
  118.     { BSB_ENTER, "Enter", ""  },
  119.     { BSB_QUIT, "Quit" , "" },
  120.     { BSB_OUTPUT, "................ ................ ................",
  121.       "", 0x0002 }
  122. };
  123. #endif /* MAC */
  124.  
  125. static GMENU  InpMenu  = {  /* Input menu */
  126.   0,
  127.   3,        /* 3 items */
  128.   1,        /* key */
  129.   0x0001        /* auxillary line of text */
  130. };
  131.  
  132. static GMENU  UtMenu  = { /* Utility menu */
  133.   0,
  134.   2,        /* 2 items */
  135.   2,        /* key */
  136.   0x0000
  137. };
  138.  
  139. static GMENU  OutMenu  = {  /* Output menu */
  140.   0,
  141.   1,        /* 1 item */
  142.   3,        /* key */
  143.   0x0000
  144. };
  145.  
  146. static int  x_inp,y_inp,x_out,y_out,x_ut,y_ut; /* menu window positions */
  147.  
  148. /************************************************************************
  149.  *                  *
  150.  *  UsrIO - user I/O routine to handle requests from NWORKS   *
  151.  *                  *
  152.  ************************************************************************
  153.  */
  154.  
  155. static int InitFlag = 0;    /* initialize things flag */
  156.  
  157. int UsrIO()     /* handle NWORKS requests */
  158. {
  159.     GMENU_ITEM  *gmip;
  160.     char *sp;         /* string pointer */
  161.     int   wx, wy;       /* work indices */
  162.     int   key, xp, yp, button;      /* mouse interface */
  163.     int  *ci, nw;
  164.     int   xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
  165.  
  166.     if ( InitFlag == 0 ) {
  167.         /* open any files which may be required at this point in the
  168.      code. */
  169.   /* Get screen parameters */
  170.  
  171.     InpMenu.item = &BsbList[BSB_NAME];
  172.     UtMenu.item  = &BsbList[BSB_ENTER];
  173.     OutMenu.item  = &BsbList[BSB_OUTPUT];
  174. #ifdef MAC  
  175.     BsbList[0].text = STR1;
  176.     BsbList[0].text2 = STR2;
  177.     BsbList[1].text = STR3;
  178.     BsbList[1].text2 = STR4;
  179.     BsbList[2].text = STR5;
  180.     BsbList[2].text2 = STR6;
  181.     BsbList[3].text = STR7;
  182.     BsbList[4].text = STR8;
  183.     BsbList[5].text = STR9;
  184. #endif
  185.   ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
  186.   
  187.   if ( ncolor < 8 ) {
  188.     gm_intcolor = 1;
  189.     gm_txtcolor = 0;
  190.     gm_outcolor = 0;
  191.   } else {
  192.     gm_intcolor = 7;
  193.     gm_txtcolor = 4;
  194.     gm_outcolor = 0;
  195.   }
  196.  
  197.         /* Initialize menus and set window positions */
  198.   InitGMenu( &InpMenu, chrx, chry );
  199.   InitGMenu( &OutMenu, chrx, chry );
  200.   InitGMenu( &UtMenu, chrx, chry );
  201.   x_inp = (xsize - InpMenu.x1) / 2;
  202.   x_out = (xsize - OutMenu.x1) / 2;
  203.   x_ut = (xsize - UtMenu.x1) / 2;
  204.   y_ut = ysize / 3;
  205.   y_out = y_ut + 30;
  206.   y_inp = y_out + 30;
  207.  
  208.   
  209.   InpMenu.x0 = 0; /* Menu position relative to window */
  210.   InpMenu.y0 = 0;
  211.   OutMenu.x0 = 0; /* Menu position relative to window */
  212.   OutMenu.y0 = 0;
  213.   UtMenu.x0 = 0;  /* Menu position relative to window */
  214.   UtMenu.y0 = 0;
  215.  
  216.   InitFlag = 1;
  217.     }
  218.  
  219.     IORTNCDE = 0;       /* good return for data */
  220.     switch( IOREQCDE ) {
  221.     case RQ_ATTENTION:
  222.   /* This is invoked at the request of the user from the
  223.      execute menu.  It allows parameters to be changed or
  224.      altered.  Any graphics or other interactions are allowable
  225.      as usual for the other options.
  226.   */
  227.   break;
  228.  
  229.     case RQ_LSTART:       /* starting learn */
  230.   /* This tells the user that the program is about to start
  231.      learning.  It is called once for a LEARN ALL, LEARN ONE,
  232.      LEARN N, or LEARN START
  233.   */
  234.   break;
  235.  
  236.     case RQ_LEARNIN:        /* read training input */
  237.   /* IODATA points to an empty array of IOCOUNT elements.  The
  238.      values placed in this array by the user will become the
  239.      inputs to the network for training purposes.
  240.   */
  241.     case RQ_READ:       /* read test data */
  242.   /* IODATA points to an empty array of IOCOUNT values.  The
  243.      user must fill in these values.  The elements of the
  244.      array will become the "sum" of the inputs to the input
  245.      layer of processing elements.
  246.      */ 
  247.  
  248.   ug_window( InpMenu.key, gm_intcolor, x_inp, y_inp,
  249.        x_inp + InpMenu.x1 + 2, y_inp + InpMenu.y1 + 2);
  250.   ug_window( UtMenu.key, gm_intcolor, x_ut, y_ut,
  251.        x_ut + UtMenu.x1 + 2, y_ut + UtMenu.y1 + 2);
  252.  
  253.   DispGMenu( &InpMenu );
  254.   DispGMenu( &UtMenu );
  255.   for ( ; ; ) {
  256.     while ( (gmip=LookGMenu(&InpMenu,&button)) != (GMENU_ITEM *) 0 ) {
  257.       switch( gmip->code ) {
  258.       case BSB_NAME:
  259.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  260.     ci = &curr_index[BSB_NAME];
  261.     nw = num_words[BSB_NAME];
  262.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  263.     else *ci = (*ci + nw - 1) % nw;
  264.     gmip->text = name[0][*ci];
  265.     gmip->text2 = name[1][*ci];
  266.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  267.         }
  268.         break;
  269.       case BSB_SPECIES:
  270.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  271.     ci = &curr_index[BSB_SPECIES];
  272.     nw = num_words[BSB_SPECIES];
  273.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  274.     else *ci = (*ci + nw - 1) % nw;
  275.     gmip->text = species[0][*ci];
  276.     gmip->text2 = species[1][*ci];
  277.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  278.         }
  279.         break;
  280.       case BSB_LIFESPAN:
  281.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  282.     ci = &curr_index[BSB_LIFESPAN];
  283.     nw = num_words[BSB_LIFESPAN];
  284.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  285.     else *ci = (*ci + nw - 1) % nw;
  286.     gmip->text = lifespan[0][*ci];
  287.     gmip->text2 = lifespan[1][*ci];
  288.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  289.         }
  290.         break;
  291.       }
  292.       /* If button was pressed, wait for it to be released */
  293.       while ( button != 0 )
  294.         ug_mouse( &key, &xp, &yp, &button );
  295.     }
  296.     while ( (gmip=LookGMenu( &UtMenu, &button )) != (GMENU_ITEM *) 0 ) {
  297.       switch( gmip->code ) {
  298.       case BSB_ENTER:
  299.         /* Fill in IODATA with bit patterns for current input */
  300.         if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  301.     for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  302.       if (wy == 0 ) sp = name[1][curr_index[wy]];
  303.       else if (wy == 1) sp = species[1][curr_index[wy]];
  304.       else sp = lifespan[1][curr_index[wy]];
  305.       for( ; wx<(wy+1)*16; wx++, sp++ ) {
  306.         if ( *sp == '+' ) IODATA[wx] = 1.0;
  307.         else if ( *sp == '-' ) IODATA[wx] = -1.0;
  308.         else IODATA[wx] = 0.0;
  309.       }
  310.     }
  311.     goto end_read;
  312.         }
  313.         break;
  314.       case BSB_QUIT:
  315.         if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  316.     IORTNCDE = -1;
  317.     goto end_read;
  318.         }
  319.         break;
  320.       }
  321.       /* If button was pressed, wait for it to be released */
  322.       while ( button != 0 )
  323.         ug_mouse( &key, &xp, &yp, &button );
  324.     }
  325.   }
  326. end_read:
  327.   break;
  328.  
  329.     case RQ_LEARNOUT:       /* read desired output */
  330.   /* IODATA points to an empty array of IOCOUNT values.  The
  331.      elements of the array will become the desired outputs for
  332.      training purposes.  These desired outputs correspond to
  333.      the most recent "RQ_LEARNIN" request.
  334.   */
  335.  
  336.   break;
  337.  
  338.     case RQ_WRITE:        /* write out results */
  339.   /* IODATA points to an array of IOCOUNT "float" type values.
  340.      The values are the outputs of the top-most layer of the
  341.      network.
  342.   */
  343.   IORTNCDE = 1;
  344.   break;
  345.  
  346.     case RQ_LEARNRSLT:
  347.   /* IODATA points to an array of IOCOUNT values.  These are the
  348.      output of the network caused by the inputs from RQ_LEARNIN.
  349.   */
  350.   ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  351.        x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  352.  
  353.   sp = BsbList[BSB_OUTPUT].text;
  354.   for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  355.     if ( wy != 0 ) *sp++ = ' ';
  356.     for( ; wx<(wy+1)*16; wx++, sp++ ) {
  357.       if (IODATA[wx] > 0.0) *sp = '+';
  358.       else if (IODATA[wx] < 0.0) *sp = '-';
  359.       else *sp = '.';
  360.     }
  361.   }
  362.   DispGMenu( &OutMenu );
  363.  
  364.   /* Lock Items */
  365.   BsbList[BSB_QUIT].flag |= GM_LOCKED;
  366.   BsbList[BSB_NAME].flag |= GM_LOCKED;
  367.   BsbList[BSB_SPECIES].flag |= GM_LOCKED;
  368.   BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
  369.   PutStr( "Select ENTER to continue\n" );
  370.   for ( ; ; ) {
  371.     while ( (gmip=LookGMenu( &UtMenu, &button )) == (GMENU_ITEM *)0 ) ;
  372.     if ( button == MBUT_RIGHT || button == MBUT_LEFT ) break;
  373.   }
  374.   /* Unlock Items */
  375.   BsbList[BSB_QUIT].flag &= ~GM_LOCKED;
  376.   BsbList[BSB_NAME].flag &= ~GM_LOCKED;
  377.   BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
  378.   BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
  379.   IORTNCDE = 1;
  380.   break;
  381.  
  382.     case RQ_WRSTEP:       /* write interim results */
  383.   /* each recall cycle for the Hopfield and BAM control strategies,
  384.      the intermediate output is made available to userio to test
  385.      for convergence or other desired states.  This option is
  386.      not used for other control strategies.
  387.   */
  388.  
  389.   ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  390.        x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  391.  
  392.   sp = BsbList[BSB_OUTPUT].text;
  393.   for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  394.     if ( wy != 0 ) *sp++ = ' ';
  395.     for( ; wx<(wy+1)*16; wx++, sp++ ) {
  396.       if (IODATA[wx] > 0.0) *sp = '+';
  397.       else if (IODATA[wx] < 0.0) *sp = '-';
  398.       else *sp = '.';
  399.     }
  400.   }
  401.   DispGMenu( &OutMenu );
  402.  
  403.   /* Lock Items */
  404.   BsbList[BSB_NAME].flag |= GM_LOCKED;
  405.   BsbList[BSB_SPECIES].flag |= GM_LOCKED;
  406.   BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
  407.   PutStr( "Select ENTER to continue, QUIT to terminate recall\n" );
  408.   for ( ; ; ) {
  409.     while ( (gmip=LookGMenu(&UtMenu,&button )) == (GMENU_ITEM *)0 ) ;
  410.     switch( gmip->code ) {
  411.     case BSB_ENTER:
  412.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) goto end_wr;
  413.       break;
  414.     case BSB_QUIT:
  415.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  416.         IORTNCDE = -1;
  417.         goto end_wr;
  418.       }
  419.       break;
  420.     }
  421.   }
  422. end_wr:
  423.   /* Unlock Items */
  424.   BsbList[BSB_NAME].flag &= ~GM_LOCKED;
  425.   BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
  426.   BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
  427.  
  428.   break;
  429.   
  430.     case RQ_RSTART:       /* starting recall */
  431.   /* This tells the user that the program is about to start
  432.      a recall.  It is called once for a REACLL ALL, RECALL ONE,
  433.      RECALL N, or RECALL START.
  434.   */
  435.   break;
  436.  
  437.     case RQ_RCLTST:   /* read desired output during recall test */
  438.   /* IODATA points to an empty array of IOCOUNT values.  The
  439.      elements of the array will become the desired outputs for
  440.      recall purposes.  This request is only made during a
  441.      Execute Network/Recall Test.
  442.   */
  443.  
  444.   break;
  445.  
  446.     case RQ_TERM:       /* terminate interface */
  447.   /* close any files which may be open.  Deallocate any memory
  448.      which was allocated.  (This is VERY VERY important.  If
  449.      allocated memory is NOT released, dos memory will become
  450.      fragmented and it will become necessary to reboot.
  451.   */
  452.  
  453.   PutStr( "bye bye\n" );
  454.   break;
  455.     }
  456.  
  457.     return;
  458. }
  459.