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 / ant.c < prev    next >
C/C++ Source or Header  |  1995-07-15  |  16KB  |  447 lines

  1.  
  2. /*************************************************************************
  3.  *   Program: ant.c  menu interface to train and process with the perceptron 
  4.  *                    neural net 
  5.  *
  6.  *  Version   Date       Author                         Comments & modif.
  7.  *  --------  ---------  ----------------------------   -------------------
  8.  *  V1.00     13-Jun-95  I. Labrador, R. Carrasco, LML  Very first version
  9.  */
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <errno.h>
  13. #include "ant.h"        /* neural net header */
  14. #include "antlib.h"      /* functions prototypes */
  15.  
  16.  
  17. int ant_store_flag=0;      /* store weights flag */
  18. int ant_end_flag=0;       /* end train flag */
  19. int almid;        /* alarm id */
  20.  
  21. /************************************************************************
  22.  *    Name         : ant_menu()                                            *
  23.  *    Description    : Present the menu option for net processing    
  24.  *      inputs          : None            
  25.  *      outputs         : None            
  26.  *      return          : Character of option selected                        *
  27.  ************************************************************************/    
  28. char ant_menu()
  29. {
  30. unsigned int j;
  31. char         c;
  32. while(True)
  33.   {
  34.   printf("\n\n");    /* display menu */
  35.   system("date");    
  36.   printf("\n");
  37.   for(j = 1; j < 70; j++)
  38.     (j < 11) ? putchar(' ') : putchar('_');
  39.   printf("\n\n\n");
  40.   printf("\t\t [T]  TRAIN NET        \n\n");
  41.   printf("\t\t [I]  IMPROVE NET      \n\n");
  42.   printf("\t\t [P]  PROCESS AN INPUT \n\n");
  43.   printf("\t\t [E]  EXIT             \n\n");    
  44.   for(j = 1; j < 70; j++)
  45.     (j < 11) ? putchar(' ') : putchar('_');
  46.   printf("\n\n\n Select an option :");
  47.   fseek(stdin,0,2);
  48.   c=getchar();      /* get answer from user */
  49.   return c;
  50.   }
  51. }
  52. /************************************************************************
  53.  *    Name         : ant_train()                                            *
  54.  *    Description    : Performs trainning of the net 
  55.  *      inputs          : argc number of argument of the ant main
  56.  *                        ant_netD, struct with net data configuration
  57.  *                        ant_neuronD, array of neuron data  structs
  58.  *                        ant_inout, struct with train examples data
  59.  *      outputs         : new values of weigths 
  60.  *      return          : none 
  61.  ************************************************************************/    
  62. void ant_train(argc,ant_netD,ant_neuronD,ant_inout)
  63. int argc;
  64. ant_neuron **ant_neuronD;
  65. ant_net *ant_netD;
  66. ant_examp *ant_inout;
  67. {
  68. #undef DEBUG
  69. #define DEBUG 0
  70. int l,n,d;
  71. int ant_ex;
  72. int ant_train_flag;
  73. /* start train of net */
  74. do{
  75.   ant_train_flag=ANT_END;        /* train end flag */
  76.    for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++)        /* per each example */
  77.      { 
  78.      while(True)        /* while error bigger than limit */
  79.        {
  80.        ant_propag(ant_ex,ant_netD,ant_neuronD,ant_inout);    /* propagate inputs */
  81.        if (ant_out_error(ant_ex,ant_netD,ant_neuronD,ant_inout) < ant_netD->errlim)
  82.          break;
  83.        ant_retro(ant_ex,ant_netD,ant_neuronD,ant_inout);    /* recompute weights */
  84.        ant_train_flag=ANT_NO_END;                
  85.        if(argc == 1) printf("]");             /* printout a char ] per net computation */
  86.        }
  87.      if(argc == 1) printf("e%d\n",ant_ex); 
  88. #if DEBUG
  89.   for(l = 1; l < ant_netD->l;l++)   /* per each layer */
  90.     for(n = 1; n < ant_netD->n[l];n++)   /* per each neuron */
  91.       for(d = 0; d < ant_netD->n[l - 1] ;d++)   /*  per each dendrite */
  92.       {
  93.         printf("%-12.4lf",ant_neuronD[l][n].w[d]);    /* write weights */
  94.       }
  95.    printf("\n"); 
  96. #endif 
  97.      if(ant_store_flag == 1)    /* if store alarm detected */
  98.        {
  99.        if(ant_wrt_w(ant_netD,ant_neuronD) < 0)         /* write weights in init file*/
  100.          _errmsg(0,"\t\t Error writing weights  \n");
  101.        ant_store_flag =0;
  102.        }
  103.      }
  104.    if(ant_end_flag == 1)        /* if end signal detected */
  105.      {
  106.      if(ant_wrt_w(ant_netD,ant_neuronD) < 0)    /* write weights in init file*/ 
  107.        _errmsg(0,"\t\t Error writing weights  \n");
  108.      exit(_errmsg(0,"\t\t   Bye, Good luck   \n"));
  109.      }
  110.   }while( ant_train_flag != ANT_END );     
  111. if(ant_wrt_w(ant_netD,ant_neuronD) < 0)     /* write weights in init file*/
  112.    printf("\t\t Error writing weights\n");
  113. }
  114.  
  115. /************************************************************************
  116.  *    Name         : ant_sighand()                                            *
  117.  *    Description    : signal handling routine
  118.  *      inputs          : sig, signal value
  119.  *      outputs         : end and file flags
  120.  *      return          : 
  121.  ************************************************************************/    
  122. int ant_sighand(sig)
  123. int sig;
  124. {
  125. switch (sig)
  126.  {
  127.  case 3:
  128.    alm_delete(almid);
  129.    ant_end_flag=1;
  130.    break;
  131.  case 2:
  132.    alm_delete(almid);
  133.    exit(_errmsg(0,"Exit requested by user\n"));
  134.    break;
  135.  case ANT_STORE:
  136.    ant_store_flag=1;
  137.    break;
  138.  default:
  139.    alm_delete(almid);
  140.    exit(_errmsg(0,"Exit by signal unknown\n"));
  141.  }
  142. }
  143.  
  144. /************************************************************************
  145.  *    Name         : ant_display_goto()                                            *
  146.  *    Description    : cursor set to c column and l line
  147.  *      inputs          : c,l column and line
  148.  *      outputs         : none
  149.  *      return          : none
  150.  ***********************************************************************/
  151. void ant_display_goto(c,l)
  152. int c,l;
  153. {
  154. int i;
  155. char sdum[12];
  156. char sdumc[3];
  157. char sduml[3];
  158. sprintf(sdumc,"%d",c);
  159. sprintf(sduml,"%d",l);
  160. sdum[0]=27; sdum[1]='['; sdum[2]='\0'; 
  161. strcat(sdum,sduml);    
  162. strcat(sdum,";");
  163. strcat(sdum,sdumc);    
  164. strcat(sdum,"f");    
  165. puts(sdum);
  166. }
  167.  
  168.  
  169. /************************************************************************
  170.  *    Name         : ant_display_erase()                                            *
  171.  *    Description    : Erase screen display 
  172.  *      inputs          : none
  173.  *      outputs         : none
  174.  *      return          : none
  175.  ************************************************************************/    
  176. void ant_display_erase()
  177. {
  178. char sdum[5];
  179. sdum[0]=27; sdum[1]='['; sdum[2]='2'; sdum[3]='J';sdum[4]='\0';
  180. puts(sdum);
  181. }
  182. /************************************************************************
  183.  *    Name         : strextrac()                                            *
  184.  *    Description    : copy from n character of a string pointed by str1
  185.  *                         in a string pointed by str2.
  186.  *      inputs          : n character number of str1 to be first character of 
  187.  *                           str2
  188.  *                      : str1 pointer to source string            
  189.  *                      : str2 pointer to destination string            
  190.  *      outputs         : None            
  191.  *      return          : None
  192.  ************************************************************************/    
  193. void strextract(n,str1,str2)
  194. int n;
  195. char *str1,*str2;
  196. {
  197. int i;
  198.   for( i = n ; str1[i] != '\0'; i++)
  199.     str2[i-n]=str1[i];
  200.   str2[i-n]='\0';
  201. }
  202.  
  203. /************************************************************************
  204.  *    Name         : ant_use()                                            *
  205.  *    Description    : Display help when inadecuate use or missing parameters 
  206.  *      inputs          : None            
  207.  *      outputs         : None            
  208.  *      return          : None
  209.  ************************************************************************/    
  210. void ant_use()
  211. {
  212. printf("Syntax:\t ant [<options>] \n");
  213. printf("Function:\t Run the Artificial Neural Tool (see readme file)\n");
  214. printf("Options:\n");
  215. printf("\tNone\t\t  Run in menu mode\n");
  216. printf(
  217. "\t-examp=<path> get net examples file from path (for -i and -t options)\n");
  218. printf("\t-i\t\t      improve a trained net (examp file needed)\n");
  219. printf("\t-in=<path>    get net input file from path (needed with -p option)\n");
  220. printf("\t-init=<path>  get net init file from path (always needed)\n");
  221. printf("\t-out=<path>   get net output file from path (needed with -p option)\n");
  222. printf("\t-p\t\t      process an input (an input and output file needed)\n");
  223. printf("\t-t\t\t      train the net (example file needed)\n\n");
  224. printf("Hint:\t Try first to run the menu mode just typing  ant \n");
  225. exit(0);
  226. }
  227.  
  228. /************************************************************************
  229.  *    Name         : main()                                            *
  230.  *    Description    : main routine of neural net utility
  231.  *      inputs          : if desired init file name as argument
  232.  *      outputs         : none
  233.  *      return          : none
  234.  ************************************************************************/    
  235. main (argc, argv)
  236. int argc;
  237. char **argv;
  238. {
  239. int    sig,ant_ex,ant_arg;
  240. char      c,ant_opt,ant_answ[4];
  241. ant_neuron  **ant_neuronD;
  242. ant_net     ant_netD;
  243. ant_examp   *ant_inout;
  244.  
  245. intercept(ant_sighand);                
  246. system("tmode pause");
  247.  
  248. if(argc == 1)          /* if menu mode ask for init file name */
  249.   do{
  250.     ant_display_erase();
  251.     ant_display_goto(10,12);
  252.     printf("File of net configuration: ");
  253.     scanf("%s",ant_netD.init);
  254.     ant_neuronD = ant_init(&ant_netD);
  255.     }while(ant_neuronD == NULL);
  256.  
  257. while(argc == 1){       /* loop of menu mode */              
  258.   ant_display_erase();
  259.   ant_display_goto(0,0);
  260.   ant_opt=ant_menu();    /* display menu options and return selection */
  261.  
  262.   switch (ant_opt)      /* switch on option */
  263.     {
  264.     case 'T': case 't':   /* train */
  265.       ant_gen_w(&ant_netD,ant_neuronD);    /* generate random weights */
  266.       printf("File name with trainning examples : ");    /* ask for example file name */
  267.       scanf("%s",ant_netD.examp);        /* get example file name */
  268.       ant_inout=ant_rd_examp(&ant_netD);      /* read examples and return ant_inout */
  269.       if( ant_inout == NULL)           /* on error exit ant */   
  270.         { 
  271.         printf(" Error read example\n");
  272.         fseek(stdin,0,2);
  273.         c=getchar();
  274.         break;
  275.         }
  276.       almid=alm_cycle(ANT_STORE,ANT_TIME);    /* set alarm to store weights */
  277.       system("tmode nopause");                
  278.       ant_display_erase();            
  279.       ant_display_goto(0,0);
  280.       ant_train(argc,&ant_netD,ant_neuronD,ant_inout);  /* perform train */
  281.       alm_delete(almid);                      /* delete alarm */
  282.       ant_display_erase();
  283.       ant_display_goto(10,12);
  284.       printf(" *** End of training (type any key to continue)\n");
  285.       fseek(stdin,0,2);
  286.       c=getchar();
  287.       free((ant_examp **)ant_inout);        /* free ant_inout */
  288.       system("tmode pause");
  289.       ant_display_erase();
  290.       ant_display_goto(0,0);
  291.       break; 
  292.     case 'I': case 'i':  /* improve */
  293.       printf("File name with trainning examples : ");  /* ask for example file name  */
  294.       scanf("%s",ant_netD.examp);                /* get example file name */     
  295.       ant_inout=ant_rd_examp(&ant_netD);         /* read examples */ 
  296.       if( ant_inout == NULL)                   /* on error exit ant */
  297.         { 
  298.         printf(" Error read example\n");
  299.         fseek(stdin,0,2);
  300.         c=getchar();
  301.         break;
  302.         }
  303.       almid=alm_cycle(ANT_STORE,ANT_TIME);     /* set alarm to store weights*/
  304.       system("tmode nopause");                  
  305.       ant_display_erase();
  306.       ant_display_goto(0,0);
  307.       ant_train(argc,&ant_netD,ant_neuronD,ant_inout);    /* perform train */
  308.       alm_delete(almid);             /* delete alarm */
  309.       ant_display_erase();
  310.       ant_display_goto(10,12);
  311.       printf(" *** End of improve (type any key to continue)\n");
  312.       fseek(stdin,0,2);
  313.       c=getchar();
  314.       free((ant_examp **)ant_inout);    /* free ant_inout */
  315.       system("tmode pause");
  316.       ant_display_erase();
  317.       ant_display_goto(0,0);
  318.       break;
  319.     case 'P': case 'p':  /* process inputs  */
  320.       printf("File name of inputs : ");      /* ask for input file name  */
  321.       scanf("%s",ant_netD.in);        /* get input file name  */
  322.       ant_inout=ant_rd_input(&ant_netD);     /* read input file */
  323.       if( ant_inout == NULL)        /* on error exit ant */
  324.         { 
  325.         printf(" Error read example\n");
  326.         fseek(stdin,0,2);
  327.         c=getchar();
  328.         break;
  329.         }
  330.       system("tmode pause");
  331.       ant_display_erase();
  332.       ant_display_goto(10,12);
  333.       printf("  Do you want to store outputs? (Yes/No) : "); /* ask if output in file */
  334.       scanf("%s",ant_answ);
  335.       if (findstr(1, ant_answ,"y") || findstr(1, ant_answ,"Y"))
  336.         { 
  337.         printf("\n\n  File name to store outputs : ");       /* ask for output file name  */
  338.         scanf("%s",ant_netD.out);        /* get output file name  */
  339.         }
  340.       ant_display_erase();
  341.       ant_display_goto(0,0);
  342.       for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++)    /* per each example */
  343.         { 
  344.         ant_propag(ant_ex,&ant_netD,ant_neuronD,ant_inout);    /* net computation, propagation of the input */
  345.         if (findstr(1, ant_answ,"y") || findstr(1, ant_answ,"Y"))
  346.             ant_wrt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout);  /* write file with results */
  347.         ant_prt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout);    /* printout results */
  348.         }
  349.       strcpy(ant_netD.out,"\0");
  350.       free((ant_examp **)ant_inout);
  351.       break;
  352.     case 'E': case 'e':    /* exit ant */
  353.       ant_display_erase();
  354.       ant_display_goto(10,12);
  355.       printf("   Good luck  ");   /* bye */
  356.       ant_display_goto(10,24);
  357.       exit(0); 
  358.       break; 
  359.     default:   /* on erroneous option */
  360.       printf("*** Unknown option  (type any key to continue)***\n");
  361.       fseek(stdin,0,2);
  362.       c=getchar();
  363.    }  /* end switch menu options */
  364.   }  /* end  while */
  365.  
  366. /*  */
  367. /* more than one argument => command line utility use */
  368. /* set null strings */
  369. ant_opt='\0';
  370. strcpy(ant_netD.init,"\0");
  371. strcpy(ant_netD.examp,"\0");
  372. strcpy(ant_netD.in,"\0");
  373. strcpy(ant_netD.out,"\0");
  374. /* get information of arguments */
  375. for( ant_arg = 1 ; ant_arg < argc ; ant_arg++)
  376.   {
  377.   if (findstr(1, argv[ant_arg],"-init="))
  378.     strextract(6,argv[ant_arg],ant_netD.init);
  379.   else if (findstr(1, argv[ant_arg],"-examp="))
  380.     strextract(7,argv[ant_arg],ant_netD.examp);
  381.   else if (findstr(1, argv[ant_arg],"-out="))
  382.     strextract(5,argv[ant_arg],ant_netD.out);
  383.   else if (findstr(1, argv[ant_arg],"-in="))
  384.     strextract(4,argv[ant_arg],ant_netD.in);
  385.   else if (findstr(1, argv[ant_arg],"-t"))
  386.    ant_opt='t'; 
  387.   else if (findstr(1, argv[ant_arg],"-i"))
  388.    ant_opt='i'; 
  389.   else if (findstr(1, argv[ant_arg],"-p"))
  390.    ant_opt='p'; 
  391.   }
  392. /* Check if correct information */
  393. if(strlen(ant_netD.init) == 0)
  394.    ant_use();
  395. switch (ant_opt)
  396.     {
  397.     case 't': case 'i' :    /* if train or improve a example file needed*/
  398.       if(strlen(ant_netD.examp) == 0)
  399.         ant_use();
  400.       break;
  401.     case 'p':             /* if process an input needed */
  402.       if((strlen(ant_netD.in) == 0) || (strlen(ant_netD.out) == 0 )) 
  403.         ant_use();
  404.       break;
  405.     default:
  406.         ant_use();
  407.     }
  408. /* train the net, improve weigths or process an input file with the net */
  409.   ant_neuronD = ant_init(&ant_netD);         /* read the init file */
  410.   switch (ant_opt)
  411.     {
  412.     case 't':   /* train */
  413.       ant_gen_w(&ant_netD,ant_neuronD);   /* generate random weights */
  414.       ant_inout=ant_rd_examp(&ant_netD);  /* read examples from file */
  415.       if( ant_inout == NULL)           /* on error exit */
  416.         exit(_errmsg(0, "\t\t Error reading example file\n"));
  417.       almid=alm_cycle(ANT_STORE,ANT_TIME);    /* set alarm for weight saving */
  418.       ant_train(argc,&ant_netD,ant_neuronD,ant_inout);     /* perform trainning */
  419.       alm_delete(almid);        /* delete alarm */
  420.       free((ant_examp **)ant_inout);   
  421.       break; 
  422.     case 'i':  /* improve */
  423.       ant_inout=ant_rd_examp(&ant_netD);    /* read examples file */
  424.       if( ant_inout == NULL)            /* on error exit */
  425.         exit(_errmsg(0, "\t\t Error reading example file\n"));
  426.       almid=alm_cycle(ANT_STORE,ANT_TIME);    /* set alarm for weights saving */
  427.       ant_train(argc,&ant_netD,ant_neuronD,ant_inout);  /* perform the train */
  428.       alm_delete(almid);             /* delete alarm */
  429.       free((ant_examp **)ant_inout);
  430.       break;
  431.     case 'p':  /* process inputs  */
  432.       ant_inout=ant_rd_input(&ant_netD);        /* get input from file */
  433.       if( ant_inout == NULL)            /* on error exit */
  434.         exit(_errmsg(0,"\t\t Error reading input file\n"));
  435.       for ( ant_ex = 0 ; ant_ex < ant_inout->nex ; ant_ex++)  /* per each input vector */
  436.         { 
  437.         ant_propag(ant_ex,&ant_netD,ant_neuronD,ant_inout);  /* perform computation */
  438.         ant_wrt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* write resuls in file */
  439.         }
  440.       free((ant_examp **)ant_inout);
  441.       break;
  442.     default:  
  443.       ant_use();
  444.    }  /* end switch utility options */
  445.  
  446. }  /* end main */
  447.