home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 177.lha / Neuron / Neuron.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  4KB  |  178 lines

  1. /*
  2.  
  3.    Original Author: Shawn P. Legrand      15-Sep-88
  4.  
  5.     This program simulates a simple neural network by  calculating the 
  6.     inner product of a vector and a matrix where the matrix T is symmetric
  7.     {T(i,j) = T(j,i)}, dilute (more 0's than 1's), and T(i,i)=0 (to prevent
  8.     oscillations and chaotism). Reports are sent to a report file showing the
  9.     input and output matrices and the energy level of the matrix after each
  10.     iteration.    
  11.  
  12.     This program is based on information contained in a book by Ed Rietman,
  13.     'Experiments in Artificial Neural Networks', published by TAB.
  14.  
  15.     Copyright 1988     Shawn P. Legrand, CCP         All Rights Reserved
  16.     
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. /*
  22.  * Random number generator -
  23.  * adapted from the FORTRAN version 
  24.  * in "Software Manual for the Elementary Functions"
  25.  * by W.J. Cody, Jr and William Waite.
  26. */
  27.  
  28. static long int iy = 100001;
  29.  
  30. sran(seed)
  31. long seed;
  32. {
  33.         iy = seed;
  34. }
  35.  
  36. double ran()
  37. {
  38.         iy *= 125;
  39.         iy -= (iy/2796203) * 2796203;
  40.         return (double) iy/ 2796203.0;
  41. }
  42.  
  43. /*
  44.  * Main program logic.
  45. */
  46.  
  47. main()
  48. {
  49.     int neurons, io, info, vector, matrix, i, j, iterate, sigma=0;
  50.     long seed;
  51.     float energy=0;
  52.     double r;
  53.     int t[100][100], u[100], v[100];
  54.     FILE *fp;
  55.  
  56. /* Open new CLI device */
  57.  
  58.     if ((fp = fopen("Neuron.output","w")) == NULL) {
  59.        printf("Error opening Neuron.output!!!\n");
  60.        exit(10);
  61.     }   
  62.  
  63. /* Retrive initial values */
  64.  
  65.     printf("Input the random seed: ");
  66.     scanf("%ld",&seed);
  67.     sran(seed);
  68.      
  69.     printf("Enter the number of neurons (100 maximum): ");
  70.     scanf("%d",&neurons);                                   
  71.     printf("Input the threshold value (0 to 2 are reasonable values): ");
  72.     scanf("%d",&io);
  73.     printf("Enter the value of the information (0 to 1 is a good value): ");
  74.     scanf("%d",&info);
  75.     printf("Do you want to enter the input vector yourself (1/Yes, 0/No)? ");
  76.     scanf("%d",&vector);
  77.     printf("Do you want to enter the T matrix (1/Yes, 0/No)? ");
  78.     scanf("%d",&matrix);
  79.  
  80. /* Initialize the weight matrix (matrix T) */    
  81.  
  82.     if (!matrix) {
  83.         for (i=0; i<neurons; i++) {
  84.         for (j=0; j<neurons; j++) {
  85.             if (i==j) {
  86.             t[i][j] = 0;
  87.         } else {
  88.                     r=ran();
  89.             if (r<0.8) {
  90.                 t[i][j] = 0;
  91.             } else {
  92.                 t[i][j] = 1;
  93.             }
  94.         }    
  95.         }
  96.     }
  97.     } else {
  98.         for (i=0; i<neurons; i++) {
  99.         for (j=0; j<neurons; j++) {
  100.             printf("T(%d,%d): ",i,j);
  101.         scanf("%d",&t[i][j]);
  102.         }
  103.         }
  104.     }
  105.  
  106. /* Output initial values */
  107.     
  108.     fprintf(fp,"Seed = %ld\n",seed); 
  109.     fprintf(fp,"Threshold = %d\n",io);
  110.     fprintf(fp,"Information = %d\n",info);
  111.     fprintf(fp,"T matrix:\n");
  112.     for (i=0; i<neurons; i++) {
  113.         for (j=0; j<neurons; j++) {
  114.         fprintf(fp,"%d",t[i][j]);
  115.     }
  116.     fprintf(fp,"\n");
  117.     }
  118.     fprintf(fp,"\n\n");    
  119.     
  120. /* Retrieve input vector (vector u) */
  121.  
  122.     if (!vector) {
  123.         for (i=0; i<neurons; i++) {
  124.         r=ran();
  125.         if (r<0.5) {
  126.             u[i]=0;
  127.         } else {
  128.             u[i]=1;
  129.         }
  130.     }
  131.     } else {
  132.         for (i=0; i<neurons; i++) {
  133.         fprintf(fp,"Input u(%d): ",i);
  134.         fscanf(fp,"%d",&u[i]);
  135.     }
  136.     }
  137.  
  138. /* Perform calculations, Report input and output vector, Energy level, 
  139.    Iterate using feedback from prior calculation */    
  140.  
  141.     for (iterate=0; iterate<8; iterate++) {
  142.         for (i=0; i<neurons; i++) {
  143.         for (j=0; j<neurons; j++) {
  144.             sigma+=t[i][j]*u[j];
  145.         }
  146.         sigma+=info;
  147.         if (sigma>io) {
  148.             sigma=1;
  149.         } else {
  150.             sigma=0;
  151.         }
  152.         v[i]=sigma;
  153.         sigma=0;
  154.     }
  155.     fprintf(fp,"Iteration %d:\n",iterate);
  156.     fprintf(fp,"Input vector U:\n");
  157.     for (i=0; i<neurons; i++) {
  158.         fprintf(fp,"%d",u[i]);
  159.     }
  160.     fprintf(fp,"\nOutput vector V:\n");
  161.     for (i=0; i<neurons; i++) {
  162.         fprintf(fp,"%d",v[i]);
  163.     }
  164.     fprintf(fp,"\n");
  165.     fprintf(fp,"            Energy: ");
  166.     for (i=0; i<neurons; i++) {
  167.         energy+=(float) (u[i]*v[i]);
  168.     }
  169.     energy*=-0.5;
  170.     fprintf(fp,"%f\n\n",energy);
  171.     for (i=0; i<neurons; i++) {
  172.         u[i]=v[i];
  173.     }
  174.     energy=0;
  175.     }
  176.     
  177. }
  178.