home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / ant.ytar / ant / ANT / LIB / antlib.c next >
Text File  |  1995-07-15  |  26KB  |  673 lines

  1. /*************************************************************************
  2.  *   Program: antlib.c  library of ant functions
  3.  *
  4.  *  Version   Date       Author                         Comments & modif.
  5.  *  --------  ---------  ----------------------------   -------------------
  6.  *  V1.00     13-Jun-95  I. Labrador, R. Carrasco, LML  Very first version
  7.  */
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <errno.h>
  11. #include "ant.h"     /* neural net header file */
  12.  
  13.  
  14. /************************************************************************
  15.  *    Name         : ant_init()                                            *
  16.  *    Description    : Read from file named netD->init the net configuration 
  17.  *                        values and weights, allocate memory of net variables
  18.  *      inputs          : ant_netD, struct with net data configuration
  19.  *      outputs         : Net configuration and ant_neural struct data
  20.  *      return          : pointer to array of ant_neuron structures (net neurons)
  21.  ************************************************************************/    
  22. ant_neuron **ant_init(ant_netD)
  23. ant_net *ant_netD;
  24. {
  25. #undef DEBUG 
  26. #define DEBUG 0
  27. int l,n,d;
  28. char c;
  29. FILE *init_fp;
  30. ant_neuron **ant_neuronD;
  31.  /* Generate seed for random numbers */
  32.     ant_netD->seed = ant_seed();
  33.   /* Get init parameters of neural net */
  34.   init_fp=fopen(ant_netD->init,"r");    /* open init file  */
  35.   if( init_fp == NULL)
  36.     return (ant_neuron **) NULL;     /* return -1 if error openning file */
  37.   while( fscanf(init_fp,"%*s %d",&(ant_netD->SIG)) == 0);    /* read sigmoid type */
  38.   while( fscanf(init_fp,"%*s %d",&(ant_netD->l)) == 0);    /* read number of layers */
  39.   ++(ant_netD->l);  /* include inputs as layer 0  */
  40. #if DEBUG
  41.   printf(" layers %d\n",ant_netD->l);
  42. #endif
  43.   ant_netD->n=( int *) calloc(ant_netD->l,sizeof(int));    /* allocate memory number of neurons */    
  44.   for(l = 0; l < ant_netD->l;l++)   /*  number of neurons per layer */
  45.     {
  46.     while( fscanf(init_fp,"%*s %d",&(ant_netD->n[l])) == 0);
  47. #if DEBUG
  48.     printf(" neurons %d in layer %d\n",ant_netD->n[l],l);
  49. #endif
  50.       ant_netD->n[l]++;    /*add 1 pol neuron to all layers*/
  51.     }
  52.   while( fscanf(init_fp,"%*s %lf",&(ant_netD->pol)) == 0);    /* read pol value  */
  53.   while( fscanf(init_fp,"%*s %lf",&(ant_netD->mu)) == 0);    /* read coef weights convergence */
  54.   while( fscanf(init_fp,"%*s %lf",&(ant_netD->noise)) == 0);    /* read coef weights noise */
  55.   while( fscanf(init_fp,"%*s %lf",&(ant_netD->errlim)) == 0);    /* read error limit in convergence */
  56. #if DEBUG
  57.   printf(" pol %12.4lf\n",ant_netD->pol);
  58.   printf(" mu %12.4lf\n",ant_netD->mu);
  59.   printf(" noise %12.4lf\n",ant_netD->noise);
  60.   printf(" err %12.4lf\n",ant_netD->errlim);
  61. #endif
  62.  
  63.   ant_netD->S=( double *) calloc(ant_netD->l-1,sizeof(int));    /* allocate memory number of neurons */    
  64.  
  65.   --ant_netD->S;  /*offset slope per layer array starts at index 1 (first real layer) */
  66.   for(l = 1 ; l < ant_netD->l ; l++)   /*   per each layer */
  67.     {
  68.       while( fscanf(init_fp,"%*s %lf",&(ant_netD->S[l])) == 0);    /* slope of each sigmoid */
  69. #if DEBUG
  70.       printf(" %12.4lf ",ant_netD->S[l]);
  71.       printf("\n");
  72. #endif
  73.     }
  74.  
  75.   ant_neuronD= ( ant_neuron **)
  76.             calloc((ant_netD->l - 1),sizeof(ant_neuron *));    /* allocate mem  array of neuron structure */
  77.   --ant_neuronD;  /*offset neurons array starts at index 1 (first real layer) */
  78.   for(l = 1; l < ant_netD->l;l++)   /*  per each layer */
  79.     ant_neuronD[l] = ( ant_neuron *)     
  80.                    calloc((ant_netD->n[l]),sizeof(ant_neuron));    /* allocate neurons mem */
  81.  
  82.   for(l = 1; l < ant_netD->l;l++)   /* per each layer */
  83.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  84.       {
  85.       ant_neuronD[l][n].w = 
  86.             ( double *)calloc((ant_netD->n[l-1]),sizeof(double));    /* Allocate weight memory  */
  87.       }
  88.  
  89.    while( (c=getc(init_fp)) != EOF)    /* read weights if any  */
  90.      { 
  91.      for(l = 1; l < ant_netD->l;l++)    /* per each layer */
  92.        for(n = 1; n < ant_netD->n[l];n++)    /*  per each neuron */
  93.          for(d = 0; d < ant_netD->n[l - 1] ;d++)        /* per each dendrite */
  94.            {
  95.            while(fscanf(init_fp,"%*s %lf",&(ant_neuronD[l][n].w[d])) == 0);
  96. #if DEBUG
  97.            printf("w(%d,%d,%d) %12.4lf\n",l,n,d, ant_neuronD[l][n].w[d]);
  98. #endif
  99.            }
  100.      }
  101.   fclose(init_fp); 
  102.   return  ant_neuronD;
  103. }
  104.  
  105. /************************************************************************
  106.  *    Name         : ant_random()                                            *
  107.  *    Description    : Generate a random double value from -32768 to 32767
  108.  *      outputs         : None
  109.  *      return          : a random double value
  110.  ************************************************************************/    
  111.  
  112. double ant_random(ant_netD)
  113. ant_net *ant_netD;
  114. {
  115.     ant_netD->seed = ((ant_netD->seed) * 25173 + 13849) % 65536;
  116.      return ((double) (ant_netD->seed) - 32768.);
  117. }
  118. /************************************************************************
  119.  *    Name         : ant_seed()                                            *
  120.  *    Description    : Generate a seed at init from system clock
  121.  *      inputs          : none
  122.  *      outputs         : none 
  123.  *      return          : value of the seed in a int
  124.  ************************************************************************/    
  125. int ant_seed()
  126. {
  127.     int time, date, tick;
  128.     short day;
  129.     
  130.     _sysdate(1,&time,&date,&day,&tick);
  131.     return time%65536;
  132. }
  133.  
  134. /************************************************************************
  135.  *    Name         : ant_gen_w()                                            *
  136.  *    Description    : Generate init random weights
  137.  *      inputs          : ant_netD, struct with net data configuration
  138.  *                        ant_neuronD, array of neuron data  structs
  139.  *      outputs         : weight values in ant_neuronD array of structures
  140.  *      return          : none 
  141.  ************************************************************************/    
  142.  
  143. void ant_gen_w(ant_netD,ant_neuronD)
  144. ant_neuron **ant_neuronD;
  145. ant_net *ant_netD;
  146. {
  147. #undef DEBUG 
  148. #define DEBUG 0
  149. int l,n,d;
  150. /* generate random weights */
  151.      for(l = 1; l < ant_netD->l;l++)   /*  per each layer */
  152.        for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  153.          for(d = 0; d < ant_netD->n[l-1] ;d++)   /* per each dendrite */
  154.         {
  155.         ant_neuronD[l][n].w[d] = ant_random(ant_netD)/327680.;
  156. #if DEBUG
  157.         printf("w(%d,%d,%d) %12.4lf \n",l,n,d, ant_neuronD[l][n].w[d]);
  158. #endif
  159.         }
  160. }
  161.  
  162. /************************************************************************
  163.  *    Name         : ant_wrt_w()                                            *
  164.  *    Description    : Write in file named netD->init the net configuration 
  165.  *                        values and weights
  166.  *      inputs          : ant_netD, struct with net data configuration
  167.  *                        ant_neuronD, array of neuron data  structs
  168.  *      outputs         : none
  169.  *      return          : error in open file to write
  170.  ************************************************************************/    
  171. int ant_wrt_w(ant_netD,ant_neuronD)
  172. ant_neuron **ant_neuronD;
  173. ant_net *ant_netD;
  174. {
  175.   int l,n,d;
  176.     /* Get init parameters of neural net */
  177.   FILE *init_fp;
  178.   init_fp=fopen(ant_netD->init,"w");
  179.         if( init_fp == NULL)     { return -1;}    
  180.   fprintf( init_fp,"*********** NET STRUCTURE *********\n"); 
  181.   fprintf( init_fp," Comments: \n"); 
  182.   fprintf( init_fp," Sigmoid type(DIG(1) or SYM(2))  : %d\n",ant_netD->SIG);
  183.   fprintf( init_fp," Number of Layers(hiddens+out)   : %d\n",(ant_netD->l)-1);
  184.   fprintf( init_fp," Number of inputs                : %d\n",ant_netD->n[0]-1);
  185.  
  186.   for(l = 1; l < ant_netD->l;l++)   /*  per each layer */
  187.     {
  188.     fprintf( init_fp," Neurons in next layer           : %d\n",ant_netD->n[l]-1);
  189.     }
  190.  
  191.   fprintf( init_fp," Polarization potential          : %-12.4lf\n",ant_netD->pol);
  192.   fprintf( init_fp," Weight pertur. coef.            : %-12.4lf\n",ant_netD->mu);
  193.   fprintf( init_fp," Weight noise (zero to one)      : %-12.4lf\n",ant_netD->noise);
  194.   fprintf( init_fp," Convergence limit               : %-12.4lf\n",ant_netD->errlim);
  195.   fprintf( init_fp,   "*********** Simoid Slope *********\n"); 
  196.  
  197.   for(l = 1; l < ant_netD->l;l++)    /*  per each layer */
  198.     {
  199.     fprintf(init_fp,"S(%d) : %-12.4lf\n",l,ant_netD->S[l]); 
  200.     } 
  201.  
  202.   fprintf( init_fp,
  203.          "******** weitghs including polarization nodes**************");
  204.   
  205.   for(l = 1; l < ant_netD->l;l++)   /* per each layer */
  206.     for(n = 1; n < ant_netD->n[l];n++)   /* per each neuron */
  207.       for(d = 0; d < ant_netD->n[l - 1] ;d++)   /*  per each dendrite */
  208.       {
  209.         fprintf(init_fp,"\nw(%d,%d,%d) : %-12.4lf"
  210.           ,l,n,d,ant_neuronD[l][n].w[d]);    /* write weights */
  211.       }
  212.   fclose(init_fp); 
  213.   return  0;
  214. }
  215.  
  216. /************************************************************************
  217.  *    Name         : ant_rd_examp()                                            *
  218.  *    Description    : Read input and output values of the training examples
  219.  *      inputs          : ant_netD, struct with net data configuration
  220.  *      outputs         : input and output data of all trainning examples in 
  221.  *                        structure ant_examp
  222.  *      return          : pointer to ant_examp structure
  223.  ************************************************************************/    
  224. ant_examp  *ant_rd_examp(ant_netD)
  225. ant_net *ant_netD;
  226. {
  227. #undef DEBUG 
  228. #define DEBUG 0
  229. int l,n,ant_ex;
  230. FILE *ex_fp;
  231. ant_examp  *ant_inout;
  232.  
  233.   ex_fp=fopen(ant_netD->examp,"r");    /* open example file */
  234.         if( ex_fp == NULL)     { return (ant_examp *) NULL; }    /* return -1 if error */
  235.  
  236.   ant_inout = ( ant_examp *)
  237.             calloc( 1,sizeof(ant_examp));    /* allocate memory of ant_examp struc */
  238.   fscanf(ex_fp," %d",&(ant_inout->nex));    /* get number of examples */
  239.   ant_inout->in = ( double **)
  240.             calloc( ant_inout->nex , sizeof(double *));    /* allocate pointers to input array  */
  241.   ant_inout->out = ( double **)
  242.             calloc( ant_inout->nex , sizeof(double *));    /* allocate pointers to output array  */
  243.   for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++)
  244.     {
  245.     /* allocate mem for new example */
  246.     ant_inout->in[ant_ex]= ( double *) calloc((ant_netD->n[0]),sizeof(double));
  247.     ant_inout->out[ant_ex]= ( double *) calloc(ant_netD->n[(ant_netD->l)-1],sizeof(double));
  248.     for(n = 1; n < ant_netD->n[0];n++)      /* per each input ( neurons of layer 0 */
  249.       {
  250.       fscanf(ex_fp," %lf",&(ant_inout->in[ant_ex][n]));
  251. #if DEBUG
  252.       printf("input %lf\n",ant_inout->in[ant_ex][n]);
  253. #endif
  254.       }
  255.     ant_inout->in[ant_ex][0]=ant_netD->pol;        /* polarization fake neuron */
  256.     for(n = 1; n < ant_netD->n[(ant_netD->l)-1] ;n++)    /* per each output, real neurons in output layer */      
  257.       {
  258.       fscanf(ex_fp," %lf",&(ant_inout->out[ant_ex][n]));
  259. #if DEBUG
  260.       printf("output %lf\n",ant_inout->out[ant_ex][n]);
  261. #endif 
  262.       }
  263. #if DEBUG
  264.     printf("--------------------\n");
  265. #endif 
  266.     }
  267. #if DEBUG
  268.      printf("   %d--------------------\n",ant_inout->nex);
  269. #endif 
  270.   fclose(ex_fp); 
  271.   return  (ant_examp *) ant_inout;    /* return pointer to ant_examp structure */
  272. }
  273.  
  274. /************************************************************************
  275.  *    Name         : ant_rd_input()                                            *
  276.  *    Description    : Read input values of the examples to be computed 
  277.  *                        by the net
  278.  *      inputs          : ant_netD, struct with net data configuration
  279.  *      outputs         : input data of all trainning examples in 
  280.  *                        structure ant_examp
  281.  *      return          : pointer to ant_examp structure
  282.  ************************************************************************/    
  283. ant_examp  *ant_rd_input(ant_netD)
  284. ant_net *ant_netD;
  285. {
  286. #undef DEBUG 
  287. #define DEBUG 0
  288. int l,n,ant_ex;
  289. FILE *in_fp;
  290. ant_examp  *ant_inout;
  291.  
  292.   in_fp=fopen(ant_netD->in,"r");    /* open example file */
  293.         if( in_fp == NULL)     { return (ant_examp *) NULL; }    /* return -1 if error */
  294.  
  295.   ant_inout = ( ant_examp *)
  296.             calloc( 1,sizeof(ant_examp));    /* allocate memory of ant_examp struc */
  297.   fscanf(in_fp," %d",&(ant_inout->nex));    /* get number of examples */
  298.   ant_inout->in = ( double **)
  299.             calloc( ant_inout->nex , sizeof(double *));    /* allocate pointers to input array  */
  300.   for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++)
  301.     {
  302.     /* allocate mem for new example */
  303.     ant_inout->in[ant_ex]= ( double *) calloc((ant_netD->n[0]),sizeof(double));
  304.     for(n = 1; n < ant_netD->n[0];n++)      /* per each input ( neurons of layer 0 */
  305.       {
  306.       fscanf(in_fp," %lf",&(ant_inout->in[ant_ex][n]));
  307. #if DEBUG
  308.       printf("input %lf\n",ant_inout->in[ant_ex][n]);
  309. #endif
  310.       }
  311.     ant_inout->in[ant_ex][0]=ant_netD->pol;        /* polarization fake neuron */
  312. #if DEBUG
  313.     printf("--------------------\n");
  314. #endif
  315.     }
  316. #if DEBUG
  317.     printf("   %d--------------------\n",ant_inout->nex);
  318. #endif
  319.   fclose(in_fp); 
  320.   return  (ant_examp *) ant_inout;    /* return pointer to ant_examp structure */
  321. }
  322.  
  323. /************************************************************************
  324.  *    Name         : ant_wrt_out()                                            *
  325.  *    Description    : Write output values of the inputs computed 
  326.  *                        by the net
  327.  *      inputs          : ant_ex , example index
  328.  *                        ant_netD, struct with net data configuration
  329.  *                        ant_neuronD, array of neuron data  structs
  330.  *                        ant_inout, struct with train examples data
  331.  *      outputs         : none
  332.  *      return          : -1 error openning file out, 0 otherwise 
  333.  ************************************************************************/    
  334. int ant_wrt_out(ant_ex,ant_netD,ant_neuronD,ant_inout)
  335. int ant_ex;
  336. ant_neuron **ant_neuronD;
  337. ant_net *ant_netD;
  338. ant_examp *ant_inout;
  339. {
  340. #undef DEBUG 
  341. #define DEBUG 0
  342. int l,n;
  343. FILE *out_fp;
  344.   out_fp=fopen(ant_netD->out,"a");        /* open file output */
  345.         if( out_fp == NULL)     { return -1; }       /* return -1 on error */
  346.   fprintf(out_fp,"IN: ");
  347.   for(n = 1; n < ant_netD->n[0];n++)      /* per each input, layer 0 */
  348.     {
  349.     fprintf(out_fp," %12.6lf",(ant_inout->in[ant_ex][n]));
  350. #if DEBUG
  351.     printf("input %lf\n",ant_inout->in[ant_ex][n]);
  352. #endif
  353.     }
  354.     fprintf(out_fp,"\nOUT: ");
  355.   l = ant_netD->l-1;
  356.   for(n = 1; n < ant_netD->n[l] ;n++)    /* per each neuron of output layer */
  357.     {
  358.     fprintf(out_fp," %12.6lf",(ant_neuronD[l][n].out));
  359. #if DEBUG
  360.     printf("output %lf\n",ant_neuronD[l][n].out);
  361. #endif 
  362.     }
  363.   fprintf(out_fp,"\n\n");
  364. #if DEBUG 
  365.   printf("--------------------\n");
  366. #endif
  367.   fclose(out_fp); 
  368.   return  0;
  369. }
  370. /************************************************************************
  371.  *    Name         : ant_exp()                                            *
  372.  *    Description    : Compute exp with maximun limit 
  373.  *      inputs          : ant_expD, exponential input value
  374.  *      outputs         : none
  375.  *      return          : value of the exponential in a double
  376.  ************************************************************************/    
  377. double ant_exp(ant_expD)
  378. double ant_expD;
  379. {
  380. if ( fabs(ant_expD) <  ANT_EXP_MAX )
  381.   return exp(ant_expD);
  382. else if( ant_expD < 0 ) 
  383.   return exp(-ANT_EXP_MAX);
  384. else 
  385.   return exp(ANT_EXP_MAX);
  386. }
  387. /************************************************************************
  388.  *    Name         : ant_sigmoid()                                            *
  389.  *    Description    : Compute sigmoid value 
  390.  *      inputs          : ant_netD, struct with net data configuration
  391.  *                        ant_x, input
  392.  *                        ant_G, sigmoid slope
  393.  *      outputs         : none 
  394.  *      return          : value of the sigmoid in a double
  395.  ************************************************************************/    
  396. double  ant_sigmoid(ant_netD, ant_G,ant_x)
  397. ant_net *ant_netD;
  398. double ant_G,ant_x; 
  399. {
  400. double ant_expD;
  401. switch (ant_netD->SIG)
  402.   {
  403.   case  ANT_DIG: 
  404.     {
  405.     ant_expD = ant_exp( -1.* ant_G * ant_x );
  406.     return  (1. / (1. + ant_expD));
  407.     break;
  408.     }
  409.   case  ANT_SYM:  default: 
  410.     {
  411.     ant_expD = ant_exp( 2. * ant_G * ant_x );
  412.     return  (ant_expD - 1.)/(ant_expD + 1.);
  413.     break;
  414.     }
  415.   }
  416. }
  417. /************************************************************************
  418.  *    Name         : ant_propag()                                            *
  419.  *    Description    : Compute net output
  420.  *      inputs          : ant_ex , example index
  421.  *                        ant_netD, struct with net data configuration
  422.  *                        ant_neuronD, array of neuron data  structs
  423.  *                        ant_inout, struct with train examples data
  424.  *      outputs         : values of outputs in last neuron layer
  425.  *      return          : none
  426.  ************************************************************************/    
  427. void ant_propag(ant_ex,ant_netD,ant_neuronD,ant_inout)
  428. int ant_ex;
  429. ant_neuron **ant_neuronD;
  430. ant_net *ant_netD;
  431. ant_examp *ant_inout;
  432. {
  433. int l,n,d;
  434.   for(l = 1; l < ant_netD->l;l++)  /* per each layer */
  435.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  436.        ant_neuronD[l][n].out = 0;        /* reset output */
  437.  
  438.     l=1;  /* first layer of neurons */
  439.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron in layer 1 hidden */
  440.       {
  441.       for(d = 1; d < ant_netD->n[l - 1] ;d++)   /*  dendrites per neuron connecting with inputs, previous layer */
  442.         {
  443.         ant_neuronD[l][n].out +=
  444.           ant_inout->in[ant_ex][d] * ant_neuronD[l][n].w[d];
  445.         }  
  446.         ant_neuronD[l][n].out +=
  447.           ant_netD->pol * ant_neuronD[l][n].w[0]; /* polarization neuron */
  448.  
  449.         ant_neuronD[l][n].out = ant_sigmoid(ant_netD,ant_netD->S[l],ant_neuronD[l][n].out);        /* compute neuron output */
  450.       }  
  451.  
  452.  
  453.   for(l = 2; l < ant_netD->l;l++)   /*  per each hidden and output layers */
  454.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron per layer */
  455.       {
  456.       for(d = 1; d < ant_netD->n[l - 1] ;d++)   /*  per each dendrite connecting a neuron with previous layer*/
  457.         {
  458.         ant_neuronD[l][n].out += 
  459.           ant_neuronD[l-1][d].out * ant_neuronD[l][n].w[d];
  460.         }  
  461.       ant_neuronD[l][n].out += ant_netD->pol * ant_neuronD[l][n].w[0];
  462.  
  463.       ant_neuronD[l][n].out= ant_sigmoid(ant_netD, ant_netD->S[l],ant_neuronD[l][n].out);
  464.       }  
  465.  
  466. }
  467. /************************************************************************
  468.  *    Name         : ant_dsigmoid()                                            *
  469.  *    Description    : Compute derived sigmoid
  470.  *      inputs          : ant_netD, struct with net data configuration
  471.  *                        ant_x , input
  472.  *                        ant_G,  sigmoid slope
  473.  *      outputs         : none
  474.  *      return          : derived sigmoid value in a double
  475.  ************************************************************************/    
  476. double  ant_dsigmoid(ant_netD,ant_G,ant_x)
  477. ant_net *ant_netD;
  478. double ant_G,ant_x;
  479. {
  480. double ant_expD;
  481. switch (ant_netD->SIG)
  482.   {
  483.   case  ANT_DIG: 
  484.     {
  485.     ant_expD = ant_exp(-1.* ant_G * ant_x );
  486.     return (ant_G * ant_expD) / (ANT_SQ(1. + ant_expD));    
  487.     break;
  488.     }
  489.   case  ANT_SYM:  default: 
  490.     {
  491.     ant_expD = ant_exp( 2. * ant_G * ant_x );
  492.     return  ( 4. * ant_G * ant_expD) / ANT_SQ(1. + ant_expD);
  493.     break;
  494.     }
  495.   }
  496. }
  497. /************************************************************************
  498.  *    Name         : ant_retro()                                            *
  499.  *    Description    : Retro propagate net outputs to compute weights
  500.  *      inputs          : ant_ex , example index
  501.  *                        ant_netD, struct with net data configuration
  502.  *                        ant_neuronD, array of neuron data  structs
  503.  *                        ant_inout, struct with train examples data
  504.  *      outputs         : new values of weiths 
  505.  *      return          : none 
  506.  ************************************************************************/    
  507. void ant_retro(ant_ex,ant_netD,ant_neuronD,ant_inout)
  508. int ant_ex;
  509. ant_neuron **ant_neuronD;
  510. ant_net *ant_netD;
  511. ant_examp *ant_inout;
  512. {
  513. int l,n,d;
  514.   for(l = 1; l < ant_netD->l;l++)   /*  per each layer */
  515.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  516.        ant_neuronD[l][n].er=0;          /* reset error */
  517.  
  518.   /* compute error */
  519.   l = (ant_netD->l) - 1;  /* last layer of neurons,output */
  520.   for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  521.     {
  522.     ant_neuronD[l][n].er = (ant_inout->out[ant_ex][n] - ant_neuronD[l][n].out) *
  523.         ant_dsigmoid(ant_netD,ant_netD->S[l],ant_neuronD[l][n].out); 
  524.     }  
  525.  
  526.   for(l = (ant_netD->l) - 2; l > 0 ; l--)        /* rest of the layers */
  527.     for(n = 1; n < ant_netD->n[l];n++)            /* per each neuron  */  
  528.       {
  529.         for(d = 1; d < ant_netD->n[l+1] ;d++)      /* per each dendrite */
  530.         {
  531.         ant_neuronD[l][n].er +=
  532.          (ant_neuronD[l+1][d].er * ant_neuronD[l+1][d].w[n]);
  533.         }  
  534.       ant_neuronD[l][n].er *= ant_dsigmoid(ant_netD,ant_netD->S[l],ant_neuronD[l][n].out);   /* compute error */
  535.       }  
  536.  
  537.     /* recompute weights */
  538.     l=1;  /* first hidden layer of neurons */
  539.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each layer of neurons in 1 hidden layer */
  540.       {
  541.       for(d = 1; d < ant_netD->n[l - 1] ;d++)   /* per each dendrite */
  542.         {
  543.         ant_neuronD[l][n].w[d] += 
  544.         ant_netD->mu * ant_neuronD[l][n].er * ant_inout->in[ant_ex][d];
  545.         /* noise factor */
  546.         if( ant_netD->noise != 0 )
  547.           ant_neuronD[l][n].w[d] += ant_random(ant_netD)*(ant_netD->noise)/32768.;       
  548.         }  
  549.       ant_neuronD[l][n].w[0] += 
  550.           ant_netD->mu * ant_netD->pol * ant_neuronD[l][n].er;
  551.       /* noise factor */
  552.       if( ant_netD->noise != 0 )
  553.         ant_neuronD[l][n].w[0] += ant_random(ant_netD)*(ant_netD->noise)/32768.;                 
  554.       }  
  555.  
  556.   for(l = 2; l < ant_netD->l;l++)   /*  rest of the layers */
  557.     for(n = 1; n < ant_netD->n[l];n++)   /*  per each neuron */
  558.       {
  559.       for(d = 1; d < ant_netD->n[l - 1] ;d++)   /*  per dendrite */
  560.         {
  561.         ant_neuronD[l][n].w[d] += 
  562.             ant_netD->mu * ant_neuronD[l][n].er * ant_neuronD[l-1][d].out;
  563.         /* noise factor */
  564.         if( ant_netD->noise != 0 )
  565.         ant_neuronD[l][n].w[d] += ant_random(ant_netD)*(ant_netD->noise)/32768.;                   
  566.         }  
  567.         ant_neuronD[l][n].w[0] += 
  568.             ant_netD->mu * ant_netD->pol * ant_neuronD[l][n].er;
  569.         /* noise factor */
  570.         if( ant_netD->noise != 0 )
  571.           ant_neuronD[l][n].w[0] += ant_random(ant_netD)*(ant_netD->noise)/32768.;                   
  572.       }  
  573. }
  574. /************************************************************************
  575.  *    Name         : ant_out_error()                                            *
  576.  *    Description    : Compute error of net output and desired output
  577.  *      inputs          : ant_ex , example index
  578.  *                        ant_netD, struct with net data configuration
  579.  *                        ant_neuronD, array of neuron data  structs
  580.  *                        ant_inout, struct with train examples data
  581.  *      outputs         : none
  582.  *      return          : error value in a double 
  583.  ************************************************************************/    
  584. double ant_out_error(ant_ex,ant_netD,ant_neuronD,ant_inout)
  585. int ant_ex;
  586. ant_neuron **ant_neuronD;
  587. ant_net *ant_netD;
  588. ant_examp *ant_inout;
  589. {
  590. #undef DEBUG 
  591. #define DEBUG  0
  592. double value=0;
  593. int l,n;
  594.   l= (ant_netD->l) - 1;       /* last layer */
  595.   for ( n = 1 ; n < ant_netD->n[l] ;n++)       /* per each neuron */
  596.     value += ANT_SQ( (ant_neuronD[l][n].out) - (ant_inout->out[ant_ex][n]));
  597. #if DEBUG
  598.   printf( " error = %lf\n",value*0.5);
  599. #endif
  600.   return value*0.5;
  601. }
  602. /************************************************************************
  603.  *    Name         : ant_ptr_out()                                            *
  604.  *    Description    : Print the output of the net 
  605.  *      inputs          : ant_ex , example index
  606.  *                        ant_netD, struct with net data configuration
  607.  *                        ant_neuronD, array of neuron data  structs
  608.  *                        ant_inout, struct with input and output data
  609.  *      outputs         : new values of weigths 
  610.  *      return          : none 
  611.  ************************************************************************/    
  612. int ant_prt_out(ant_ex,ant_netD,ant_neuronD,ant_inout)
  613. int ant_ex;
  614. ant_neuron **ant_neuronD;
  615. ant_net *ant_netD;
  616. ant_examp *ant_inout;
  617. {
  618. int l,n;
  619. char c;
  620.      printf("INPUTS: ");
  621.    for(n = 1; n < ant_netD->n[0];n++)  /* per each input  */
  622.      {
  623.      printf("%12.4lf ",ant_inout->in[ant_ex][n]);
  624.      }
  625.      printf("\n");
  626.      printf("OUTPUTS: ");
  627.     l=ant_netD->l - 1;          /* output layer */
  628.     for(n = 1; n < ant_netD->n[l];n++)   /* per each output */
  629.       {
  630.        printf("%12.4lf", ant_neuronD[l][n].out);
  631.       }  
  632.      printf("\n---- press any key for next example--------- ");
  633.      fseek(stdin,0,2);
  634.      c=getchar();
  635. }
  636. /************************************************************************
  637.  *    Name         : ant_process()                                            *
  638.  *    Description    : perform the computation of an input using the net
  639.  *      inputs          : ant_in , vector of inputs 
  640.  *                        ant_out , vector of outputs
  641.  *                        ant_netD, struct with net data configuration
  642.  *                        ant_neuronD, array of neuron data  structs
  643.  *      outputs         : values of net outputs in ant_out vector
  644.  *      return          : none 
  645.  ************************************************************************/    
  646. double  *ant_process(ant_in,ant_netD,ant_neuronD)
  647. double *ant_in;
  648. ant_neuron  **ant_neuronD;
  649. ant_net     *ant_netD;
  650. {    
  651. int l,n;
  652. ant_examp  *ant_inout;
  653.  
  654. ant_inout = ( ant_examp *) calloc( 1,sizeof(ant_examp));    /* allocate memory of ant_examp struc */
  655. ant_inout->nex=1;
  656. ant_inout->in = ( double **)
  657.             calloc( ant_inout->nex , sizeof(double *));    /* allocate pointers to input array  */
  658. ant_inout->out = ( double **)
  659.             calloc( ant_inout->nex , sizeof(double *));    /* allocate pointers to output array  */
  660. ant_inout->out[0]= ( double *) calloc(ant_netD->n[(ant_netD->l)-1],sizeof(double));
  661.  
  662. ant_inout->in[0]=ant_in;
  663. --ant_inout->in[0];
  664.  
  665. ant_propag(0,ant_netD,ant_neuronD,ant_inout);   /* process output */
  666.  
  667. l = ant_netD->l-1;
  668. for(n = 1; n < ant_netD->n[l] ;n++)    /* per each neuron of output layer */
  669.   ant_inout->out[0][n] = ant_neuronD[l][n].out;
  670. return ++ant_inout->out[0];
  671. }    
  672.     
  673.