home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / misc / b186_1 / Source / c / aa next >
Text File  |  1994-02-15  |  21KB  |  851 lines

  1. /*
  2.  
  3.        This file is part of the PDP software package.
  4.          
  5.        Copyright 1987 by James L. McClelland and David E. Rumelhart.
  6.        
  7.        Please refer to licensing information in the file license.txt,
  8.        which is in the same directory with this source file and is
  9.        included here by reference.
  10. */
  11.  
  12.  
  13. /* file: aa.c
  14.  
  15.     Do the actual work for the aa program.
  16.     
  17.     First version implemented by Elliot Jaffe
  18.     
  19.     Date of last revision:  8-12-87/JLM
  20.  
  21. */
  22.  
  23. #include "general.h"
  24. #include "aa.h"
  25. #include "variable.h"
  26. #include "patterns.h"
  27. #include "command.h"
  28. #include <math.h>
  29.  
  30. char   *Prompt = "aa: ";
  31. char   *Default_step_string = "pattern";
  32.  
  33. boolean System_Defined = FALSE;
  34.  
  35. /* parameters: */
  36.  
  37. float   estr = 0.15;        /* strength parameter */
  38. float    istr = 0.15;
  39. float   decay = 0.15;     /* proportion of old activation lost */
  40. float   lrate = 0.125;       /* proportion of increment aquired */
  41. float   pflip = 0.0;
  42. float    ecrit = 0.001;
  43.  
  44. /* mostly program control variables: */
  45.  
  46. int     epochno = 0;
  47. int    cycleno = 0;
  48. int    patno = 0;
  49. int     ncycles = 25;
  50. int    nepochs = 1;
  51. int     nunits = 0;
  52. int    ninputs;
  53. int    noutputs;
  54. /* int     npatterns = 0; */
  55. int    tallflag = 0;
  56.  
  57. boolean    lflag = TRUE;
  58.  
  59. /* modes: */
  60.  
  61. boolean hebb = 0;
  62. boolean linear = 0;
  63. boolean bsb  = 0;
  64. boolean self_connect = 0;
  65.  
  66.  
  67. /* arrays (or more correctly pointers to arrays) */
  68.  
  69. float  **weight;        /* nunits X nunits */
  70. float  *activation;        /* nunits */
  71. float  *prioract;        /* nunits */
  72. float  *netinput;        /* nunits */
  73. float  *intinput;        /* nunits */
  74. float  *error;            /* nunits */
  75. float  *extinput;        /* nunits */
  76.  
  77. float   pss = 0.0;        /* pattern sum of squares */
  78. float   tss,            /* total sum of squares */
  79.     ndp,            /* normalized dot product */
  80.     nvl,            /* normalized vector length */
  81.     vcor;            /* vector correlation */
  82.  
  83. float   *sumv1;
  84. float    *sumv2;
  85. float    *sumv3;
  86. int    sumlen;
  87.  
  88. float *
  89. readvec(pstr,len) char *pstr; int len; {
  90.     int j;
  91.     float *tvec;
  92.     char *str;
  93.     char tstr[60];
  94.     
  95.     if (pstr == NULL) {
  96.         tvec = (float *) emalloc((unsigned)(sizeof(float)*len));
  97.     for (j = 0; j < len; j++) {
  98.         tvec[j] = 0.0;
  99.     }
  100.     return(tvec);
  101.     }
  102.     sprintf(tstr,"give %selements:  ",pstr);
  103.     tvec = (float *) emalloc((unsigned)(sizeof(float)*len));
  104.     for (j = 0; j < len; j++) {
  105.     tvec[j] = 0.0;
  106.     }
  107.     for (j = 0; j < len+1; j++) {
  108.         str = get_command(tstr);
  109.     if (str == NULL || strcmp(str,"end") == 0) {
  110.         if (j) return(tvec); else return (NULL);
  111.     }
  112.     if (strcmp("+",str) == 0) tvec[j] = 1.0;
  113.     else if (strcmp("-",str) == 0) tvec[j] = -1.0;
  114.     else if (strcmp(".",str) == 0) tvec[j] = 0.0;
  115.     else sscanf(str,"%f",&tvec[j]);
  116.     }
  117.     return(tvec);
  118. }
  119.  
  120. /* first get the number of units, then malloc all the necessary
  121.    data structures.
  122. */
  123.  
  124. define_system() {
  125.     int     i,j;
  126.  
  127.     if(nunits <= 0) {
  128.     put_error("cannot initialize system without first defining nunits");
  129.     return(FALSE);
  130.     }
  131.     ninputs = nunits;
  132.  
  133.     weight = ((float **) emalloc((unsigned)(sizeof(float*) * nunits)));
  134.     (void) install_var("weight", PVfloat,(int *) weight, nunits, nunits, 
  135.                SETWTMENU);
  136.     for (i = 0 ; i < nunits; i++) {
  137.     weight[i] = ((float *) emalloc((unsigned)(sizeof(float) * nunits)));
  138.     for (j = 0; j < nunits; j++) {
  139.         weight[i][j] = 0.0;
  140.     }
  141.     }
  142.     activation = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  143.     (void) install_var("activation", Vfloat,(int *) activation, 
  144.                             nunits, 0, SETSVMENU);
  145.     for (i = 0; i < nunits; i++)
  146.     activation[i] = 0.0;
  147.  
  148.     prioract = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  149.     (void) install_var("prioract", Vfloat,(int *) prioract,
  150.                             nunits, 0, SETSVMENU);
  151.     for (i = 0; i < nunits; i++)
  152.     prioract[i] = 0.0;
  153.  
  154.     netinput = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  155.     for (i = 0; i < nunits; i++)
  156.     netinput[i] = 0.0;
  157.  
  158.     intinput = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  159.     (void) install_var("intinput",Vfloat,(int *)intinput,nunits,0,SETSVMENU);
  160.     for (i = 0; i < nunits; i++)
  161.     intinput[i] = 0.0;
  162.  
  163.     error = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  164.     (void) install_var("error",Vfloat,(int *)error,nunits,0,SETSVMENU);
  165.     for (i = 0; i < nunits; i++)
  166.     error[i] = 0.0;
  167.  
  168.     extinput = (float *) emalloc((unsigned)(sizeof(float) * nunits));
  169.     (void) install_var("extinput",Vfloat,(int *) extinput,nunits,0,SETSVMENU);
  170.     for (i = 0; i < nunits; i++)
  171.     extinput[i] = 0.0;
  172.  
  173.     sumv1 = extinput;
  174.     sumv2 = activation;
  175.     sumv3 = intinput;
  176.     sumlen = nunits;
  177.     System_Defined = TRUE;
  178.  
  179.     wreset();
  180.  
  181.     return(TRUE);
  182. }
  183.  
  184.  
  185. get_weights() {
  186.     register int    i,
  187.                     j;
  188.     char   *str;
  189.     FILE * iop;
  190.  
  191.     if(! System_Defined)
  192.       if(! define_system())
  193.        return(BREAK);
  194.  
  195.     str = get_command("fname: ");
  196.     if (str == NULL) return(CONTINUE);
  197.     if ((iop = fopen(str, "r")) == NULL) {
  198.         sprintf(err_string,"Cannot open %s.",str);
  199.     return(put_error(err_string));
  200.     }
  201.     for (i = 0; i < nunits; i++) {
  202.     for (j = 0; j < nunits; j++) {
  203.         (void) fscanf(iop, "%f", &weight[i][j]);
  204.     }
  205.     (void) fscanf(iop, "\n");
  206.     }
  207.     epochno = 0;
  208.     ndp = 0;
  209.     vcor = 0;
  210.     nvl = 0;
  211.     tss = 0;
  212.     areset();
  213.     fclose(iop);
  214.     return(CONTINUE);
  215. }
  216.  
  217.  
  218. save_weights() {
  219.     register int    i,
  220.                     j;
  221.     float  *fp;
  222.     FILE * iop;
  223.     char   *str;
  224.     char tstr[40];
  225.     char fname[BUFSIZ];
  226.     char *star_ptr;
  227.  
  228.     if(! System_Defined)
  229.       if(! define_system())
  230.        return(CONTINUE);
  231.  
  232. nameagain:
  233.     str = get_command("file name: ");
  234.     if (str == NULL) return(CONTINUE);
  235.     if ( (star_ptr = strchr(str,'*')) != NULL) {
  236.         strcpy(tstr,star_ptr+1);
  237.         sprintf(star_ptr,"%d",epochno);
  238.     strcat(str,tstr);
  239.     }
  240.     strcpy(fname,str);
  241.     if ((iop = fopen(fname, "r")) != NULL) {
  242.         fclose(iop);
  243.         get_command("file exists -- clobber? ");
  244.     if (str == NULL || str[0] != 'y') {
  245.        goto nameagain;
  246.     }
  247.     }
  248.     if ((iop = fopen(fname, "w")) == NULL) {
  249.     return(put_error("cannot open file for weights"));
  250.     }
  251.     for (i = 0; i < nunits; i++) {
  252.     for (j = 0; j < nunits; j++) {
  253.         fprintf(iop, "%6.3f", weight[i][j]);
  254.     }
  255.     fprintf(iop, "\n");
  256.     }
  257.     (void) fclose(iop);
  258.     return(CONTINUE);
  259. }
  260.  
  261.  
  262. getinput() {
  263.     register int    j;
  264.     char   *str;
  265.  
  266.     if(! System_Defined)
  267.       if(! define_system())
  268.        return(BREAK);
  269.  
  270.     str = get_command("pattern: ");
  271.     if (str == NULL) return(CONTINUE);
  272.     for (j = 0; j < nunits; j++) {
  273.     if (str[j] == '1' || str[j] == '+')
  274.         extinput[j] = 1.;
  275.     else
  276.         if (str[j] == '-')
  277.         extinput[j] = -1.;
  278.     else
  279.         extinput[j] = 0.;
  280.     }
  281.     return(CONTINUE);
  282. }
  283.  
  284. float
  285. dotprod(v1,v2,len) float *v1, *v2; int len; {
  286.     register int i;
  287.     double dp = 0;
  288.     double denom;
  289.     denom = (double) len;
  290.  
  291.     for (i = 0; i < len; i++) {
  292.         dp += (double) ((*v1++)*(*v2++));
  293.     }
  294.     dp /= denom;
  295.     return(dp);
  296. }
  297.  
  298. float
  299. sumsquares(v1,v2,len) float *v1, *v2; int len; {
  300.     register int i;
  301.     double ss = 0;
  302.  
  303.     for (i = 0; i < len; i++,v1++,v2++) {
  304.         ss += (double)((*v1 - *v2) * (*v1 - *v2));
  305.     }
  306.     return(ss);
  307. }
  308.  
  309. /* the following function computes the vector correlation, or the
  310.    cosine of the angle between v1 and v2 */
  311.  
  312. float
  313. veccor(v1,v2,len) float *v1, *v2; int len; {
  314.     register int i;
  315.     float denom,cv;
  316.     float dp = 0.0;
  317.     float l1 = 0.0;
  318.     float l2 = 0.0;
  319.  
  320.     for (i = 0; i < len; i++,v1++,v2++) {
  321.         dp += (*v1)*(*v2);
  322.         l1 += (*v1)*(*v1);
  323.         l2 += (*v2)*(*v2);
  324.     }
  325.     if (l1 == 0.0 || l2 == 0.0) return (0.0);
  326.     denom = (float) sqrt( (double) (l1*l2));
  327.     cv = dp/denom;
  328.     return(cv);
  329. }
  330.  
  331. float
  332. veclen(v,len) float *v; int len; {
  333.     int i;
  334.     float denom;
  335.     float vlen = 0;
  336.     denom = (float) len;
  337.     
  338.     for (i = 0; i < len; i++,v++) {
  339.         vlen += (*v)*(*v)/denom;
  340.     }
  341.     vlen = (float) sqrt((double) (vlen));
  342.     return(vlen);
  343. }
  344.  
  345. sumstats(level) int level; {
  346.     ndp = dotprod(sumv1,sumv2,sumlen);
  347.     nvl = veclen(sumv2,sumlen);
  348.     vcor = veccor(sumv1,sumv2,sumlen);
  349.     if (level) pss = sumsquares(sumv1,sumv3,sumlen);
  350. }
  351.  
  352. cycle() {
  353.     char *str;
  354.     int iter;
  355.     for (iter = 0; iter < ncycles; iter++) {
  356.     cycleno++;
  357.     getnet();
  358.     if (update() == BREAK) return (BREAK);
  359.     if (step_size == CYCLE) {
  360.         sumstats(0);
  361.         update_display();
  362.         if (single_flag) {
  363.             if (contin_test() == BREAK) return (BREAK);
  364.         }
  365.     }
  366.     if (Interrupt) {
  367.         Interrupt_flag = 0;
  368.         sumstats(0);
  369.         update_display();
  370.         if (contin_test() == BREAK) return (BREAK);
  371.     }
  372.     }
  373.     return(CONTINUE);
  374. }
  375.  
  376. trial() {
  377.     int br;
  378.     if (patno < 0) cpname[0] = '\0';
  379.     else strcpy(cpname,pname[patno]);
  380.     areset();
  381.     br = cycle();
  382.     compute_error();
  383.     sumstats(1);
  384.     tss += pss;
  385.     return(br);
  386. }
  387.  
  388. getnet() {
  389.     register int    i,j;
  390.  
  391.     for (i = 0; i < nunits; i++) { /*receiver*/
  392.         intinput[i] = 0.0;
  393.     for (j = 0; j < nunits; j++) { /*sender */
  394.         if ( (i == j) && !self_connect) continue;
  395.         intinput[i] += activation[j]*weight[i][j];
  396.     }
  397.     }
  398.     for (i = 0; i < nunits; i++) {
  399.     netinput[i] = istr*intinput[i] + estr*extinput[i];
  400.     }
  401. }
  402.  
  403. update() {
  404.     float omd;
  405.     float *np, *op, *pp;
  406.  
  407.     omd = (1 - decay);
  408.     if (!linear) {
  409.       for (op = activation, np = netinput, pp = prioract;
  410.                       op < activation + nunits; op++,np++,pp++) {
  411.     *pp = *op;
  412.     if (*np > 0)
  413.         *op = omd * (*op) + *np * (1.0 - *op);
  414.     else
  415.         *op = omd * (*op) + *np * (*op - (-1.0));
  416.     if (*op > 1.0)
  417.         *op = 1.0;
  418.     else if (*op < -1.0)
  419.         *op = -1.0;
  420.       }
  421.     }
  422.     else {
  423.       for (op = activation, np = netinput, pp = prioract;
  424.                             op < activation + nunits; op++,np++,pp++) {
  425.     *pp = *op;
  426.     *op = omd * (*op) + *np;
  427.         if (bsb) {
  428.       if (*op > 1.0)
  429.         *op = 1.0;
  430.       else if (*op < -1.0)
  431.         *op = -1.0;
  432.     }
  433.     else {
  434.       if (*op > 10.0 || *op < -10.0) {
  435.          get_command("Runaway activation!! Hit <cr> for command prompt: ");
  436.          return(BREAK);
  437.       }
  438.     }
  439.       }
  440.     }
  441.     return (CONTINUE);
  442. }
  443.  
  444. compute_error() {
  445.      int i;
  446.   
  447.      for (i = 0; i < nunits; i++) {
  448.        error[i] = extinput[i] - intinput[i];
  449.      }
  450. }
  451.  
  452. change_weights() {
  453.     register int i,j;
  454.  
  455.     /* The hebbian scheme is based on the notion that the
  456.        pattern learned is the outer product of the input
  457.        with itself */
  458.        
  459.     if (hebb) {
  460.       for (i = 0; i < nunits; i++) {
  461.     for (j = 0; j < nunits; j++) {
  462.       if ( (i == j) && !self_connect) continue;
  463.       weight[i][j] += lrate*extinput[i]*extinput[j];
  464.     }
  465.       }
  466.     }
  467.     else {
  468.       for (i = 0; i < nunits; i++) {
  469.     for (j = 0; j < nunits; j++) {
  470.       if ( (i == j) && !self_connect) continue;
  471.       weight[i][j] += lrate*error[i]*activation[j];
  472.     }
  473.       }
  474.     }
  475. }
  476.  
  477. areset() {
  478.     register int    i;
  479.  
  480.     if(! System_Defined)
  481.       if(! define_system())
  482.        return;
  483.     
  484.     pss = ndp = vcor = nvl = 0.0;
  485.     cycleno = 0;
  486.  
  487.     for (i = 0; i < nunits; i++) {
  488.     intinput[i] = netinput[i] = activation[i] = error[i] = prioract[i] = 0;
  489.     }
  490. }
  491.  
  492. newstart() {
  493.     random_seed = rand();
  494.     wreset();
  495. }
  496.  
  497. wreset() {
  498.     register int    i,
  499.                     j;
  500.     
  501.     if(! System_Defined)
  502.       if(! define_system())
  503.        return(BREAK);
  504.  
  505.     epochno = 0;
  506.     pss = 0;
  507.     tss = 0;
  508.     cpname[0] = '\0';
  509.     
  510.     srand(random_seed);
  511.  
  512.     if(weight != NULL) {
  513.     for(i = 0; i < nunits; i++)
  514.        for(j = 0; j < nunits; j++)
  515.          weight[i][j] = 0.0;
  516.     }
  517.     for (i = 0; i < nunits; i++) extinput[i] = 0.0;
  518.  
  519.     areset();
  520.     update_display();
  521.     return(CONTINUE);
  522. }
  523.  
  524. distort(vect,pattern,len,amount) 
  525. float *vect;
  526. float *pattern;
  527. int len;
  528. float   amount;
  529. {
  530.     int    i;
  531.     float   prop,val;
  532.  
  533.     for (i = 0; i < len; i++) {
  534.     prop = (float) rnd();
  535.     val = pattern[i];
  536.     if (prop > amount)
  537.         vect[i] = val;
  538.     else
  539.         vect[i] = 0.0 - val;
  540.     }
  541. }
  542.  
  543. strain() {
  544.     return(train('s'));
  545. }
  546.  
  547. ptrain() {
  548.     return(train('p'));
  549. }
  550.  
  551. train(c) char c; {
  552.     int     t,i,old,npair,br;
  553.     char    *str;
  554.  
  555.     if (!System_Defined)
  556.     if (! define_system())
  557.         return(CONTINUE);
  558.  
  559.     for (t = 0; t < nepochs; t++) {
  560.     if (!tallflag) epochno++;
  561.     for (i = 0; i < npatterns; i++)
  562.         used[i] = i;
  563.     if (c == 'p') {
  564.       for (i = 0; i < npatterns; i++) {
  565.         npair = rnd() * (npatterns - i) + i;
  566.         old = used[i];
  567.         used[i] = used[npair];
  568.         used[npair] = old;
  569.       }
  570.     }
  571.     tss = 0.0;
  572.     for (i = 0; i < npatterns; i++) {
  573.         if (Interrupt) {
  574.         Interrupt_flag = 0;
  575.         update_display();
  576.         if (contin_test() == BREAK) return(BREAK);
  577.         }
  578.         patno = used[i];
  579.         distort(extinput, ipattern[patno], nunits, pflip);
  580.         if ((br = trial()) == BREAK) return(BREAK);
  581.         if(lflag) change_weights();
  582.         if ((lflag && step_size < PATTERN) || (step_size == PATTERN)) {
  583.           update_display();
  584.           if (single_flag) {
  585.               if (contin_test() == BREAK) return(BREAK);
  586.           }
  587.         }
  588.     }
  589.     if (step_size == EPOCH) {
  590.       update_display();
  591.       if (single_flag) {
  592.                if (contin_test() == BREAK) return(BREAK);
  593.       }
  594.     }    
  595.     if (tss < ecrit) break;
  596.     }
  597.     if (step_size == NEPOCHS) {
  598.       update_display();
  599.     }
  600.     return(CONTINUE);
  601. }
  602.  
  603. comptest() {
  604.     int     cl_start = 0;
  605.     int        cl_end = 0;
  606.     int i;
  607.     char   *str;
  608.     int save_single;
  609.     int save_step;
  610.  
  611.     if(! System_Defined)
  612.       if(! define_system())
  613.        return(BREAK);
  614.  
  615.     tss = 0.0;
  616.  
  617.     str = get_command("which pattern? ");
  618.     if (str == NULL) return(CONTINUE);
  619.     if((patno = get_pattern_number(str)) < 0) {
  620.     return(put_error("Invalid pattern number."));
  621.     }
  622.     distort(extinput, ipattern[patno], nunits, 0.0);
  623.  
  624.     str = get_command("first element to clear? ");
  625.     if (str == NULL) return(put_error("Must specify first element."));
  626.     sscanf(str,"%d",&cl_start);
  627.     if(cl_start >= nunits) {
  628.       return(put_error("value must be from 0 to nunits - 1."));
  629.     }
  630.     str = get_command("last element? ");
  631.     if (str == NULL) return(put_error("Must specify last element."));
  632.     sscanf(str,"%d",&cl_end);
  633.     if(cl_end >= nunits) {
  634.       return(put_error("value must be from first to nunits - 1."));
  635.     }
  636.     for (i = cl_start; i <= cl_end; i++) {
  637.     extinput[i] = 0.0;
  638.     }
  639.     /* next lines over-writes normal values with values 
  640.        for the completed part of vector only */
  641.     sumv1 = ipattern[patno]+cl_start;
  642.     sumv2 = activation+cl_start;
  643.     sumv3 = intinput+cl_start;
  644.     sumlen = 1+cl_end - cl_start;
  645.     save_single = single_flag; single_flag = 1;
  646.     save_step = step_size;
  647.     if (step_size > NCYCLES) {
  648.         step_size = NCYCLES;
  649.     }
  650.     trial();
  651.     single_flag = save_single; step_size = save_step;
  652.     update_display();
  653.     /* resetting to normal values */
  654.     sumv1 = extinput;
  655.     sumv2 = activation;
  656.     sumv3 = intinput;
  657.     sumlen = nunits;
  658.     return(CONTINUE);
  659. }
  660.  
  661. test() {
  662.     float  old_pflip;
  663.     float *ivec;
  664.     char   *str;
  665.     int save_single;
  666.     int save_step;
  667.     
  668.     tss = 0.0;
  669.  
  670.     if(! System_Defined)
  671.       if(! define_system())
  672.        return(CONTINUE);
  673.  
  674.     str = get_command
  675.      ("test what (#N for pattern N, ?N to distort, L for last, E for enter)? ");
  676.     if (str == NULL) return(CONTINUE);
  677.     if(*str == '#') {
  678.     if((patno = get_pattern_number(++str)) < 0) {
  679.        return(put_error("Invalid pattern specification."));
  680.     }
  681.         distort(extinput, ipattern[patno], nunits, 0.0);
  682.     }
  683.     else if (*str == '?') {
  684.     if((patno = get_pattern_number(++str)) < 0) {
  685.        return(put_error("Invalid pattern specification."));
  686.     }
  687.         distort(extinput, ipattern[patno], nunits, pflip);
  688.     }
  689.     else if (*str == 'L') {
  690.         /* do nothing, leaving the last pattern in place */
  691.     }
  692.     else if (*str == 'E') {
  693.     patno = -1;
  694.     if ((ivec = readvec(" input ",nunits)) == (float *) NULL) 
  695.         return(CONTINUE);
  696.         distort(extinput, ivec, nunits, 0.0);
  697.     }
  698.     else {
  699.         return(put_error("Invalid input to the test commmand."));
  700.     }
  701.     save_single = single_flag; single_flag = 1;
  702.     save_step = step_size; 
  703.     if (step_size > NCYCLES) {
  704.         step_size = NCYCLES;
  705.     }
  706.     trial();
  707.     single_flag = save_single; step_size = save_step;
  708.     update_display();
  709.     return(CONTINUE);
  710. }
  711.  
  712. tall() {
  713.     int save_lflag;
  714.     int save_nepochs;
  715.     int save_single_flag;
  716.     int save_step_size;
  717.     save_step_size = step_size;
  718.     if (step_size > PATTERN) step_size = PATTERN;
  719.     save_lflag = lflag; lflag = 0;
  720.     save_nepochs = nepochs; nepochs = 1;
  721.     save_single_flag = single_flag; 
  722.     if (in_stream == stdin) single_flag = 1;
  723.     tallflag = 1;
  724.     train('s');
  725.     tallflag = 0;
  726.     single_flag = save_single_flag;
  727.     lflag = save_lflag;
  728.     nepochs = save_nepochs;
  729.     step_size = save_step_size;
  730.     return(CONTINUE);
  731. }
  732.  
  733. make_patterns() {
  734.     register int    i,
  735.                     j;
  736.     float   frac;
  737.     char   *str;
  738.     char    temp[20];
  739.  
  740.     if(! System_Defined)
  741.       if(! define_system())
  742.        return;
  743.  
  744.     str = get_command("How many patterns? ");
  745.     if (str == NULL) return(CONTINUE);
  746.     sscanf(str,"%d",&npatterns);
  747.     str = get_command("make input + with probability: ");
  748.     if (str == NULL) {
  749.         return (put_error("Must give probability."));
  750.     }
  751.     sscanf(str,"%f",&frac);
  752.     reset_patterns(!PAIRS);
  753.     for (i = 0; i < npatterns; i++) {
  754.         sprintf(temp,"%s%d","r",i);
  755.     if (i == maxpatterns) {
  756.         enlarge_patterns(!PAIRS);
  757.     }
  758.     pname[i] = (char *) emalloc((unsigned) (strlen(temp) +1));
  759.     strcpy(pname[i],temp);
  760.     ipattern[i] = (float *) emalloc((unsigned)nunits*sizeof(float));
  761.     for (j = 0; j < nunits; j++) {
  762.         if ((rnd()) < frac)
  763.         ipattern[i][j] = 1.0;
  764.         else
  765.         ipattern[i][j] = -1.0;
  766.     }
  767.     }
  768.     change_variable_length("ipattern",npatterns,nunits);
  769.     change_variable_length("pname",npatterns,0);
  770.     clear_display();
  771.     update_display();
  772.     return(CONTINUE);
  773. }
  774.  
  775. save_patterns() {
  776.     register int    i,j;
  777.     FILE * iop;
  778.     char   *str;
  779.  
  780.     if(! System_Defined)
  781.       if(! define_system())
  782.        return(BREAK);
  783.  
  784. fnameagain:
  785.     str = get_command("filename for patterns: ");
  786.     if (str == NULL) return(CONTINUE);
  787.     if ((iop = fopen(str, "r")) != NULL) {
  788.         fclose(iop);
  789.     get_command("File exists -- clobber? ");
  790.     if (str == NULL || str[0] != 'y') 
  791.         goto fnameagain;
  792.     }
  793.     if ((iop = fopen(str, "w")) == NULL) {
  794.     return(put_error("cannot open output file"));
  795.     }
  796.     for (i = 0; i < npatterns; i++) {
  797.        fprintf(iop,"%s ",pname[i]);
  798.        for (j = 0; j < nunits; j++) {
  799.         fprintf(iop,"%f ",ipattern[i][j]);
  800.        }
  801.        fprintf(iop,"\n");
  802.     }
  803.     (void) fclose(iop);
  804.     return(CONTINUE);
  805. }
  806.  
  807. init_system() {
  808.  
  809.      int get_unames();
  810.  
  811.     (void) install_command("strain", strain, BASEMENU,(int *) NULL);
  812.     (void) install_command("ptrain", ptrain, BASEMENU,(int *) NULL);
  813.     (void) install_command("tall", tall, BASEMENU,(int *) NULL);
  814.     (void) install_command("ctest", comptest, BASEMENU,(int *) NULL);
  815.     (void) install_command("test", test, BASEMENU,(int *) NULL);
  816.     (void) install_command("rpatterns", make_patterns, GETMENU,(int *) NULL);
  817.     (void) install_command("weights",get_weights, GETMENU, (int *) NULL);
  818.     (void) install_command("patterns",get_patterns, GETMENU,(int *) NULL);
  819.     (void) install_command("unames", get_unames, GETMENU,(int *) NULL);
  820.     (void) install_command("weights",save_weights, SAVEMENU, (int *) NULL);
  821.     (void) install_command("patterns", save_patterns, SAVEMENU,(int *) NULL);
  822.     (void) install_command("newstart", newstart, BASEMENU,(int *) NULL);
  823.     (void) install_command("reset", wreset, BASEMENU,(int *) NULL);
  824.  
  825.     (void) install_var("linear", Int, (int *) & linear, 0, 0,SETMODEMENU);
  826.     (void) install_var("bsb", Int, (int *) & bsb, 0, 0, SETMODEMENU);
  827.     (void) install_var("hebb", Int, (int *) & hebb, 0, 0, SETMODEMENU);
  828.     (void) install_var("selfconnect", Int, (int *) & self_connect, 0, 0, 
  829.                                     SETMODEMENU);
  830.     (void) install_var("nunits", Int,(int *) & nunits, 0, 0, SETCONFMENU);
  831.     (void) install_var("lflag", Int, (int *) & lflag, 0, 0, SETPCMENU);
  832.     (void) install_var("estr", Float,(int *) & estr, 0, 0, SETPARAMMENU);
  833.     (void) install_var("istr", Float,(int *) & istr, 0, 0,  SETPARAMMENU);
  834.     (void) install_var("decay", Float,(int *) & decay, 0, 0, SETPARAMMENU);
  835.     (void) install_var("lrate", Float,(int *) & lrate, 0, 0, SETPARAMMENU);
  836.     (void) install_var("pflip", Float,(int *) & pflip, 0, 0, SETPARAMMENU);
  837.     (void) install_var("nepochs", Int,(int *) & nepochs, 0, 0, SETPCMENU);
  838.     (void) install_var("ncycles", Int,(int *) & ncycles, 0, 0, SETPCMENU);
  839.     (void) install_var("ecrit", Float, (int *)& ecrit,0,0,SETPCMENU);
  840.     (void) install_var("epochno", Int, (int *) & epochno, 0,0, SETSVMENU);
  841.     (void) install_var("patno", Int, (int *) & patno, 0,0, SETSVMENU);
  842.     init_patterns();
  843.     (void) install_var("cycleno", Int, (int *) & cycleno, 0,0, SETSVMENU);
  844.     (void) install_var("tss", Float,(int *) & tss, 0, 0, SETSVMENU);
  845.     (void) install_var("pss", Float,(int *) & pss, 0, 0, SETSVMENU);
  846.     (void) install_var("ndp", Float, (int *) & ndp, 0, 0, SETSVMENU);
  847.     (void) install_var("nvl", Float, (int *) & nvl, 0, 0, SETSVMENU);
  848.     (void) install_var("vcor", Float, (int *) & vcor, 0, 0, SETSVMENU);
  849.  
  850. }
  851.