home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / neuron.zip / NETWORK.C < prev    next >
C/C++ Source or Header  |  1992-07-06  |  21KB  |  736 lines

  1. /* Using NEURON.C for a simple neural network                   */
  2. /* TC 2.0               E.FARHI                                 */
  3. /* versions:    1.0     10/91 Multi-layer net
  4.                 2.0     11/91 First multi-model net
  5.                 2.1     01/92 learn opt
  6.                 2.2     03/92 simulated tempering
  7.                 2.3     05/92 verify learning,
  8.                               solving memory problems,
  9.                               error handling    */
  10.  
  11. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  12. /*                      VERSION 2.3                             */
  13. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  14.  
  15.  
  16. /* modules import -----------------------------------   */
  17.  
  18. #include <stdio.h>      /* for printf */
  19. #include <process.h>    /* for system */
  20. #include "neuron.c"     /* computation module , how nice        */
  21. #include "savenet.c"    /* Disk network utilities               */
  22.  
  23.  
  24. /* used procedures and functions  --------------------------    */
  25.  
  26. /*                                      coming from NEURON.C    */
  27. extern float sqr(float);
  28. extern int   *list_norm(int);
  29. extern int   *list_alea(int,int);
  30. extern float network_state(NETWORKS  *,float[]);
  31. extern void  init_alea_network(NETWORKS  *);
  32. extern void  init_0_network(NETWORKS  *);
  33. extern void  init_flag_network(NETWORKS  *,int,int,int,int);
  34. extern void  init_var_network(NETWORKS  *,float,float,float,float,float,float,float,float,float,float,float,float,float,float);
  35. extern int   learn_opt(NETWORKS  *,int[],int,float[],float[]);
  36. extern int   learn_norm(NETWORKS  *,int[],int,float[],float[]);
  37. extern float learn_list(NETWORKS  *,int[],int,float[],float[]);
  38. extern int   learn_test(NETWORKS  *,int,long,long);
  39. extern float sqr_sum(NETWORKS  *);
  40. extern float compare_output(NETWORKS  *,float[]);
  41. extern void  print_network(NETWORKS  *);
  42. extern void  info_struct_network(NETWORKS  *);
  43. extern void  info_vars_network(NETWORKS  *);
  44. extern float learn_verify(NETWORKS  *,int[],int,float[],float[]);
  45.  
  46. /*                                      comming from SAVENET.C  */
  47. extern NETWORKS  *load_network(char);
  48. extern int save_network(NETWORKS  *,char);
  49.  
  50.  
  51. /* ------------------------------------------------------------ */
  52.  
  53. void main(void);
  54. void print_output(NETWORKS  *);
  55. void vector_modify(float[],int,float,float);            /* changes 0 and -1 */
  56. void sort(float[],int[],int);
  57. void print_result(NETWORKS  *);         /* print results !! */
  58. void print_input(int,int,float[]);
  59. void init_std_network(NETWORKS  *);
  60. int  learn(NETWORKS  *,int[],int,float[],float[],long,long);
  61. int  verif(NETWORKS  *,int[],int,float[],float[]);
  62.  
  63. /* ----------------------------------------------------------- */
  64.  
  65. void print_output(NETWORKS  *network)
  66. {
  67.   int neuron,layer;
  68.  
  69.   printf("\n-- Network output --\n");
  70.   layer=(*network).layer_order[(*network).nb_levels];   /* output */
  71.   for (neuron=0; neuron<=(*network).nb_n_layer[layer]; neuron++)
  72.     printf("Neuron No : %3i Output : %4.3f\n",neuron,(*network).neuron[neuron][layer].state);
  73.   printf("-----------------------\n\n");
  74. }
  75.  
  76. void vector_modify(float vector[],int size,float high,float low)
  77. {
  78. int i;
  79. float x;
  80. for (i=1; i<=size; i++)
  81.   {
  82.     x=vector[i-1];
  83.     if (x==0) x=low;
  84.     else
  85.      if (x==1) x=high;
  86.     vector[i-1]=x;
  87.   }
  88. }
  89.  
  90.  
  91. void sort(float v[],int order[],int n)  /* tri d'un tableau reel */
  92. {                               /* sends back a sorted array and its order */
  93.   int gap,i,j;
  94.   float temp;
  95.  
  96.   for (gap=n/2; gap>0; gap/=2)
  97.     for (i=gap; i<=n; i++)
  98.       for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
  99.       {
  100.         temp=v[j];              /* ascending sort */
  101.         v[j]=v[j+gap];
  102.         v[j+gap]=temp;
  103.         temp=order[j];
  104.         order[j]=order[j+gap];
  105.         order[j+gap]=temp;
  106.       }
  107. }
  108.  
  109. void print_input(int X,int Y,float input[])
  110. {
  111.   int i,j;
  112.   
  113.   printf("\nInput print :");
  114.   for (i=0; i<=Y; i++)
  115.   {
  116.     printf("\n");
  117.     for (j=0; j<=X; j++)
  118.       printf("%5.1f",input[i*X+j]);
  119.   }
  120.   printf("\n");
  121.   getchar();
  122. }   
  123.     
  124.  
  125. void print_result(NETWORKS  *network)
  126. {       /* gives the global analysed output */
  127.   float v[NB_N_LAYER_MAXI];
  128.   int order[NB_N_LAYER_MAXI];
  129.   int layer,neuron,n_out,i;
  130.   float percent,percent_m=0,state;
  131.   char letter;
  132.  
  133.   printf("\n------------- Network Output --------------\n");
  134.  
  135.   layer=(*network).layer_order[(*network).nb_levels];   /* output */
  136.   n_out=(*network).nb_n_layer[layer];
  137.   for (neuron=0; neuron<=n_out; neuron++)
  138.     v[neuron]=fabs((*network).neuron[neuron][layer].state-(*network).high_state*(*network).coef_out);
  139.   for (i=0; i<=n_out; i++)
  140.     order[i]=i;
  141.   sort(v,order,n_out);
  142.  
  143.   printf("\nAnalyze of results...\n");
  144.   for (i=n_out; i>=0; i--)
  145.   {
  146.     percent=(1-v[i]/((*network).high_state-(*network).low_state))*100;
  147.     letter='A'+order[i];
  148.     percent_m+=percent;
  149.     printf("\nLetter %c recognized at %3.1f %%. (state %4.2f)",letter,percent,(*network).neuron[order[i]][layer].state);
  150.   }
  151.  
  152.   percent_m/=n_out+1;
  153.  
  154.   percent=(1-v[0]/((*network).high_state-(*network).low_state))*100;
  155.   if (percent_m/percent<0.8)
  156.   {
  157.   letter='A'+order[0];
  158.   state=(*network).neuron[order[0]][layer].state;
  159.   printf("\n\nI recognize a %c at %3.1f %%. (state : %2.2f)",letter,percent,state);
  160.   }
  161.   else
  162.     printf("\n\nHard ! I can hardly identify your symbol !");
  163.   if (state<0)
  164.     printf("\n but, you are aware that it's not very clear...");
  165.   if (n_out>0)
  166.   {
  167.     percent_m=(1-v[1]/((*network).high_state-(*network).low_state))*100;
  168.     if (percent>2*percent_m)
  169.       printf("\n And I'm very proud of it...");
  170.   }
  171.   printf("\n\n <RETURN>");
  172.  
  173.   getchar();
  174. }
  175.  
  176. void init_std_network(NETWORKS  *network)
  177. {               /* initialize a standard network */
  178.   int i;
  179.  
  180.   int activity=1;                       /* all active */
  181.   int interconnection=0;                /* multi-perceptron */
  182.   int bias=0;                           /* no bias sur potentiel */
  183.  
  184.   char tempering=TEMPERING_ON;          /* simulated tempering */
  185.   float t_tempering_max=30;             /* tempering temperatures */
  186.   float t_tempering_min=0;
  187.   float cte_tempering=10;               /* exp decreasing cte */
  188.                         /* -------- vars -------- */
  189.   float err_threshold=0.04;             /* % for learning end test */
  190.   float beta=1;                         /* probabilistic neuron if beta<0 */
  191.   float coefficient=0.3;                /* not too important to avoid oscillations */
  192.   float coef_out=0.9;                   /* coef for continuum neuron */
  193.  
  194.   float high_threshold=0;               /* potential boundaries */
  195.   float low_threshold;
  196.   float weight_threshold=20;            /* weight boundaries */
  197.   float threshold=0;
  198.   float noise=0.1;                      /* % for noise */
  199.  
  200.   float high_state=1;
  201.   float low_state=-1;
  202.  
  203.   for (i=0; i<=(*network).nb_layers; i++)
  204.     if (high_threshold<(*network).nb_n_layer[i]+1)
  205.       high_threshold=(*network).nb_n_layer[i]+1;
  206.   high_threshold*=4;
  207.   low_threshold=-high_threshold;
  208.  
  209.   init_var_network(network,err_threshold,beta,coefficient,coef_out,high_threshold,low_threshold,weight_threshold,threshold,noise,high_state,low_state,t_tempering_max,t_tempering_min,cte_tempering);
  210.   init_flag_network(network,activity,interconnection,bias,tempering);
  211.   init_alea_network(network);
  212.  
  213. }
  214.  
  215. int learn(NETWORKS  *network,int *list_prototypes,int long_list,
  216.                 float inputs[],float outputs[],long sizein,long sizeout)
  217. {
  218.   int n=0,i;
  219.   float err=0;
  220.   printf("\nMaximal selected error : %3.1f (%%)",
  221.         (*network).err_threshold/(*network).nb_n_layer[(*network).layer_order[(*network).nb_levels]]*100);
  222.   do
  223.   {
  224.     printf("\nType in the maximal error you want (%%) :");
  225.     scanf("%4f",&err);
  226.   }
  227.   while ((err<0) || (err>100));
  228.   err/=100;
  229.  
  230.   init_var_network(network,err,(*network).beta,(*network).coefficient,(*network).coef_out,
  231.   (*network).high_threshold,(*network).low_threshold,(*network).weight_threshold,(*network).threshold,(*network).noise,
  232.   (*network).high_state,(*network).low_state,(*network).t_tempering_max,(*network).t_tempering_min,(*network).cte_tempering);
  233.   info_struct_network(network);
  234.   info_vars_network(network);
  235.  
  236.   printf("\nI begin to learn ...\n\n");
  237.  
  238.   if (learn_test(network,long_list,sizein,sizeout))
  239.     do
  240.     {
  241.       i=learn_opt(network,list_prototypes,long_list,inputs,outputs);
  242.       if (i<0)
  243.       {
  244.         printf("\n Sorry, I can't memorize everything !");
  245.         printf("\n You can make it again if you wish...");
  246.         i*=-1;
  247.         n+=i;
  248.         continue;
  249.       }
  250.       n+=i;
  251.     }
  252.     while(verif(network,list_prototypes,long_list,inputs,outputs));
  253.   else
  254.   {
  255.     printf("I can't learn those prototypes");
  256.     return(0);
  257.   }
  258.  
  259.   printf("\nThere were %3i learning turns.\n\a",n);
  260.   printf("\n <RETURN>");getchar();
  261.  
  262.   return(n);
  263. }
  264.  
  265. int verif(NETWORKS  *network,int *list_prototypes,int long_list,float inputs[],float outputs[])
  266. {
  267.   float err;
  268.  
  269.   printf("\n\nLearning verification");
  270.   err=learn_verify(network,list_prototypes,long_list,inputs,outputs);
  271.   printf("\n\nMaximal error: %4.2f",err);
  272.   return(err>(*network).err_threshold);
  273. }
  274.  
  275. NETWORKS  *network;
  276.  
  277. /* ------------------------------------------------------------ */
  278. /* network definition                                           */
  279. /* ------------------------------------------------------------ */
  280.  
  281.  
  282.  
  283. /* definition of the structure                  -------- */
  284.  
  285. int nb_layers=2;                        /* size */
  286. int nb_levels=2;
  287. int nb_n_layer[]={34,19,25};
  288. int layer_order[]={0,1,2};
  289.  
  290.  
  291. /* The prototypes ----------------------------------- */
  292.  
  293. /* NOTE: For the data input, we used:
  294.         state 1 active  (high_state)
  295.         state 0 inactive(low_state)
  296.         It enables an easier reading of protytpes.
  297.       The function 'vector_modify' sets the prototypes for 
  298.       the module 'neuron.c' ...                         */
  299.  
  300. int nb_prototypes_tot=29;       /* number of prototypes to learn */
  301.  
  302.  
  303. float inputs[]={                /* All prototype inputs         */
  304.  
  305.         0,1,1,1,0,              /* Inputs are   */
  306.         1,0,0,0,1,              /*  5*7 digitalized caracters */
  307.         1,0,0,0,1,              
  308.         1,1,1,1,1,
  309.         1,0,0,0,1,              /* This is the alphabet of the*/
  310.         1,0,0,0,1,              /* HP 28S                       */
  311.         1,0,0,0,1,
  312.  
  313.         1,1,1,1,0,
  314.         1,0,0,0,1,
  315.         1,0,0,0,1,
  316.         1,1,1,1,0,
  317.         1,0,0,0,1,
  318.         1,0,0,0,1,
  319.         1,1,1,1,0,
  320.  
  321.         0,1,1,1,0,
  322.         1,0,0,0,1,
  323.         1,0,0,0,0,
  324.         1,0,0,0,0,
  325.         1,0,0,0,0,
  326.         1,0,0,0,1,
  327.         0,1,1,1,0,
  328.  
  329.         1,1,1,0,0,
  330.         1,0,0,1,0,
  331.         1,0,0,0,1,
  332.         1,0,0,0,1,
  333.         1,0,0,0,1,
  334.         1,0,0,1,0,
  335.         1,1,1,0,0,
  336.         
  337.         1,1,1,1,1,
  338.         1,0,0,0,0,
  339.         1,0,0,0,0,
  340.         1,1,1,1,0,
  341.         1,0,0,0,0,
  342.         1,0,0,0,0,
  343.         1,1,1,1,1,
  344.         
  345.         1,1,1,1,1,
  346.         1,0,0,0,0,
  347.         1,0,0,0,0,
  348.         1,1,1,1,0,
  349.         1,0,0,0,0,
  350.         1,0,0,0,0,
  351.         1,0,0,0,0,
  352.  
  353.         0,1,1,1,0,
  354.         1,0,0,0,1,
  355.         1,0,0,0,0,
  356.         1,0,0,0,0,
  357.         1,0,0,1,1,
  358.         1,0,0,0,1,
  359.         0,1,1,1,0,
  360.  
  361.         1,0,0,0,1,
  362.         1,0,0,0,1,
  363.         1,0,0,0,1,
  364.         1,1,1,1,1,
  365.         1,0,0,0,1,
  366.         1,0,0,0,1,
  367.         1,0,0,0,1,
  368.  
  369.         1,1,1,1,1,
  370.         0,1,1,1,0,
  371.         0,1,1,1,0,
  372.         0,1,1,1,0,
  373.         0,1,1,1,0,
  374.         0,1,1,1,0,
  375.         1,1,1,1,1,
  376.  
  377.         0,0,0,0,1,
  378.         0,0,0,0,1,
  379.         0,0,0,0,1,
  380.         0,0,0,0,1,
  381.         0,0,0,0,1,
  382.         1,0,0,0,1,
  383.         0,1,1,1,0,
  384.  
  385.         1,0,0,0,1,
  386.         1,0,0,0,1,
  387.         1,0,0,1,0,
  388.         1,1,1,0,0,
  389.         1,0,0,1,0,
  390.         1,0,0,0,1,
  391.         1,0,0,0,1,
  392.  
  393.         1,0,0,0,0,
  394.         1,0,0,0,0,
  395.         1,0,0,0,0,
  396.         1,0,0,0,0,
  397.         1,0,0,0,0,
  398.         1,0,0,0,0,
  399.         1,1,1,1,1,
  400.  
  401.         1,0,0,0,1,
  402.         1,1,0,1,1,
  403.         1,0,1,0,1,
  404.         1,0,0,0,1,
  405.         1,0,0,0,1,
  406.         1,0,0,0,1,
  407.         1,0,0,0,1,
  408.  
  409.  
  410.         1,0,0,0,1,
  411.         1,0,0,0,1,
  412.         1,1,0,0,1,
  413.         1,0,1,0,1,
  414.         1,0,0,1,1,
  415.         1,0,0,0,1,
  416.         1,0,0,0,1,
  417.  
  418.         0,1,1,1,0,
  419.         1,0,0,0,1,
  420.         1,0,0,0,1,
  421.         1,0,0,0,1,
  422.         1,0,0,0,1,
  423.         1,0,0,0,1,
  424.         0,1,1,1,0,
  425.  
  426.         1,1,1,1,0,
  427.         1,0,0,0,1,
  428.         1,0,0,0,1,
  429.         1,1,1,1,0,
  430.         1,0,0,0,0,
  431.         1,0,0,0,0,
  432.         1,0,0,0,0,
  433.  
  434.         0,1,1,1,0,
  435.         1,0,0,0,1,
  436.         1,0,0,0,1,
  437.         1,0,0,0,1,
  438.         1,0,1,0,1,
  439.         1,0,0,1,0,
  440.         0,1,1,0,1,
  441.  
  442.         1,1,1,1,0,
  443.         1,0,0,0,1,
  444.         1,0,0,0,1,
  445.         1,1,1,1,0,
  446.         1,0,1,0,0,
  447.         1,0,0,1,0,
  448.         1,0,0,0,1,
  449.  
  450.         0,1,1,1,0,
  451.         1,0,0,0,1,
  452.         1,0,0,0,0,
  453.         0,1,1,1,0,
  454.         0,0,0,0,1,
  455.         1,0,0,0,1,
  456.         0,1,1,1,0,
  457.  
  458.         1,1,1,1,1,
  459.         0,0,1,0,0,
  460.         0,0,1,0,0,
  461.         0,0,1,0,0,
  462.         0,0,1,0,0,
  463.         0,0,1,0,0,
  464.         0,0,1,0,0,
  465.  
  466.         1,0,0,0,1,
  467.         1,0,0,0,1,
  468.         1,0,0,0,1,
  469.         1,0,0,0,1,
  470.         1,0,0,0,1,
  471.         1,0,0,0,1,
  472.         0,1,1,1,0,
  473.  
  474.         1,0,0,0,1,
  475.         1,0,0,0,1,
  476.         1,0,0,0,1,
  477.         1,0,0,0,1,
  478.         1,0,0,0,1,
  479.         0,1,0,1,0,
  480.         0,0,1,0,0,
  481.  
  482.         1,0,0,0,1,
  483.         1,0,0,0,1,
  484.         1,0,0,0,1,
  485.         1,0,1,0,1,
  486.         1,0,1,0,1,
  487.         1,1,0,1,1,
  488.         1,0,0,0,1,
  489.  
  490.         1,0,0,0,1,
  491.         1,0,0,0,1,
  492.         0,1,0,1,0,
  493.         0,0,1,0,0,
  494.         0,1,0,1,0,
  495.         1,0,0,0,1,
  496.         1,0,0,0,1,
  497.  
  498.         1,0,0,0,1,
  499.         1,0,0,0,1,
  500.         0,1,0,1,0,
  501.         0,0,1,0,0,
  502.         0,0,1,0,0,
  503.         0,0,1,0,0,
  504.         0,0,1,0,0,
  505.  
  506.         1,1,1,1,1,
  507.         0,0,0,0,1,
  508.         0,0,0,1,0,
  509.         0,0,1,0,0,
  510.         0,1,0,0,0,
  511.         1,0,0,0,0,
  512.         1,1,1,1,1,
  513.  
  514.         0,0,1,0,0,
  515.         0,1,1,1,0,
  516.         0,1,0,1,0,
  517.         1,1,0,1,1,
  518.         1,1,1,1,1,
  519.         1,1,0,1,1,
  520.         1,0,0,0,1,
  521.  
  522.         1,0,0,0,1,
  523.         0,1,0,0,1,
  524.         0,0,1,1,0,
  525.         0,0,1,1,0,
  526.         0,0,1,0,0,
  527.         0,1,0,0,0,
  528.         1,0,0,0,0,
  529.  
  530.         1,0,0,0,1,
  531.         1,0,0,0,1,
  532.         1,0,1,0,1,
  533.         1,1,1,1,1,
  534.         1,1,1,1,1,
  535.         0,1,0,1,0,
  536.         0,1,0,1,0,
  537.         
  538.         1,0,0,0,1,
  539.         1,0,0,0,1,
  540.         1,0,0,0,1,
  541.         1,1,0,1,1,
  542.         0,1,0,1,0,
  543.         0,1,1,1,0,
  544.         0,0,1,0,0
  545.         
  546.  
  547.  
  548.  
  549.         };
  550.  
  551. float outputs[]={       /* each caracter is associated to a neuron */
  552.         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  553.         0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  554.         0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  555.         0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  556.         0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  557.         0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  558.         0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  559.         0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  560.         0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  561.         0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  562.         0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  563.         0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  564.         0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
  565.         0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
  566.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
  567.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
  568.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
  569.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
  570.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  571.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
  572.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
  573.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,
  574.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
  575.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
  576.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
  577.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  578.         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  579.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
  580.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
  581.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0
  582.  
  583.         };
  584.         
  585. float input1[]= {       /* a strange 'V': twilight zone... */
  586.         1,0,0,0,1,
  587.         1,0,0,0,1,
  588.         1,0,0,0,1,
  589.         1,0,0,0,1,
  590.         1,0,0,0,1,
  591.         0,1,0,1,0,
  592.         0,0,1,0,0 };
  593.  
  594.  
  595. /* ************************************************************ */
  596.  
  597. void main(void)
  598. {
  599.  
  600.  
  601. /* used by 'main()'                                     */
  602.  
  603. int long_list;
  604. int n,*list_prototypes;
  605. char flag='3',choice;
  606. float h,l,coef;
  607.  
  608.  
  609. /* -------------------------------------------------------------- */
  610.  
  611.  
  612.  
  613. network=(NETWORKS  *)malloc(sizeof(NETWORKS));  /* allocating memory */
  614.  
  615. if (network == NULL)    /* is there any room here ? */
  616. {
  617.   printf("Not enough memory. Sorry NetWork too large");
  618.   exit (-1);
  619. }
  620.  
  621.  
  622.  
  623. /* transfering structure data                   */
  624.  
  625. (*network).nb_layers=nb_layers;
  626. (*network).nb_levels=nb_levels;
  627. for (n=0; n<=nb_layers; n++)
  628.  (*network).nb_n_layer[n]=nb_n_layer[n];
  629. for (n=0; n<=nb_levels; n++)
  630.  (*network).layer_order[n]=layer_order[n];
  631.  
  632. /* initialization of network -----------------------------------        */
  633.  
  634. printf("\nWe initialize the network ...\n");
  635. init_std_network(network);
  636.  
  637.  
  638.  
  639.  
  640. /* preparing learning ---------------------------------------   */
  641.  
  642. printf("\n\nPreparation of prototypes for learning\n");
  643.                                                 /* prepare prototypes */
  644. h=(*network).high_state;
  645. l=(*network).low_state;
  646. coef=(*network).coef_out;
  647. vector_modify(inputs,(int)(sizeof(inputs)/sizeof(float)),h,l);
  648. vector_modify(outputs,(int)(sizeof(outputs)/sizeof(float)),h*coef,l*coef);
  649.  
  650. vector_modify(input1,(int)(sizeof(input1)/sizeof(float)),h,l);
  651.  
  652. /* WARNING: for the outputs with continuum neurons continus you mustn't
  653.  impose the state boundaries in prototypes (state high and low accessible only if infinite potentiel !)
  654.  but some inferior states.
  655.  ex: for the states 1 et -1 -> 0.9 and -0.9 */
  656.  
  657.                                                 /* preparation of lists                 */
  658. long_list=nb_prototypes_tot;                    /* we learn all prototypes */
  659. list_prototypes=list_norm(long_list);           /* normal list for learn_opt */
  660.  
  661. /* ------------------------ menu ------------------------------ */
  662.  
  663. do              /* without end */
  664. {
  665.  
  666.   do            /* menu */
  667.   {
  668.  
  669.     printf("\n***************** MENU PRINCIPAL ******************");
  670.     printf("\n\n -> Enter your choice : ");
  671.     printf("\n\n0- Quit");
  672.     printf("\n\n1- Learning (default prototypes)");
  673.     printf("\n\n2- Load a network from disk");
  674.     printf("\n\n3- Listing of networks on disk");
  675.  
  676.     if (flag == '5')
  677.     {
  678.       printf("\n\n4- Save the active network to disk");
  679.       printf("\n\n5- Caracter recognition(example...)");
  680.     }
  681.  
  682.  
  683.     printf("\n\nYour choice (+RETURN): ");
  684.     choice=getchar();
  685.     printf("%c",choice);
  686.   }
  687.   while((choice < '0') || (choice > flag));
  688.  
  689.   switch(choice)
  690.   {
  691.     case '0':printf("\n\nHello world ! Bye!");getchar();
  692.              exit(0);
  693.     case '1':if (learn(network,list_prototypes,long_list,inputs,outputs,sizeof(inputs),sizeof(outputs)))
  694.              flag='5';
  695.              break;
  696.     case '2':printf("\nLoading network");
  697.              free(network);
  698.              network=load_network(NORMLOAD);
  699.              if (network == NULL)
  700.               {
  701.                 printf("\n\aNetwork handling error");
  702.                 flag='3';
  703.               }
  704.              else
  705.               {
  706.                 flag='5';
  707.               info_struct_network(network);
  708.               info_vars_network(network);
  709.               if (verif(network,list_prototypes,long_list,inputs,outputs))
  710.                 printf("\n You should proceed another learning...");
  711.               printf("\n <RETURN>");getchar();
  712.               }
  713.              break;
  714.     case '3':printf("\n");system("ls *.net");printf("\n\n <RETURN>");getchar();break;
  715.     case '4':printf("\nSaving network to disk");
  716.              save_network(network,NORMSAVE);
  717.              break;
  718.     case '5':printf("\n\aYou should do it!!!");
  719.              printf("\nHere is an example for a strange 'V'");
  720.              print_input(4,6,input1);
  721.              network_state(network,input1);
  722.                 /* input1 contains of caracter, has been modified with
  723.                    vector_modify */
  724.              print_result(network);        
  725.              break;
  726.     default:printf("* Bug *");
  727.   }     /* switch */
  728.  
  729. }       /* do */
  730. while(1);
  731.  
  732. }       /* main */
  733.  
  734. /*                      --ooOO  THE END  OOoo--                 */
  735.  
  736.