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 >
Wrap
C/C++ Source or Header
|
1995-07-15
|
16KB
|
447 lines
/*************************************************************************
* Program: ant.c menu interface to train and process with the perceptron
* neural net
*
* Version Date Author Comments & modif.
* -------- --------- ---------------------------- -------------------
* V1.00 13-Jun-95 I. Labrador, R. Carrasco, LML Very first version
*/
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include "ant.h" /* neural net header */
#include "antlib.h" /* functions prototypes */
int ant_store_flag=0; /* store weights flag */
int ant_end_flag=0; /* end train flag */
int almid; /* alarm id */
/************************************************************************
* Name : ant_menu() *
* Description : Present the menu option for net processing
* inputs : None
* outputs : None
* return : Character of option selected *
************************************************************************/
char ant_menu()
{
unsigned int j;
char c;
while(True)
{
printf("\n\n"); /* display menu */
system("date");
printf("\n");
for(j = 1; j < 70; j++)
(j < 11) ? putchar(' ') : putchar('_');
printf("\n\n\n");
printf("\t\t [T] TRAIN NET \n\n");
printf("\t\t [I] IMPROVE NET \n\n");
printf("\t\t [P] PROCESS AN INPUT \n\n");
printf("\t\t [E] EXIT \n\n");
for(j = 1; j < 70; j++)
(j < 11) ? putchar(' ') : putchar('_');
printf("\n\n\n Select an option :");
fseek(stdin,0,2);
c=getchar(); /* get answer from user */
return c;
}
}
/************************************************************************
* Name : ant_train() *
* Description : Performs trainning of the net
* inputs : argc number of argument of the ant main
* ant_netD, struct with net data configuration
* ant_neuronD, array of neuron data structs
* ant_inout, struct with train examples data
* outputs : new values of weigths
* return : none
************************************************************************/
void ant_train(argc,ant_netD,ant_neuronD,ant_inout)
int argc;
ant_neuron **ant_neuronD;
ant_net *ant_netD;
ant_examp *ant_inout;
{
#undef DEBUG
#define DEBUG 0
int l,n,d;
int ant_ex;
int ant_train_flag;
/* start train of net */
do{
ant_train_flag=ANT_END; /* train end flag */
for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++) /* per each example */
{
while(True) /* while error bigger than limit */
{
ant_propag(ant_ex,ant_netD,ant_neuronD,ant_inout); /* propagate inputs */
if (ant_out_error(ant_ex,ant_netD,ant_neuronD,ant_inout) < ant_netD->errlim)
break;
ant_retro(ant_ex,ant_netD,ant_neuronD,ant_inout); /* recompute weights */
ant_train_flag=ANT_NO_END;
if(argc == 1) printf("]"); /* printout a char ] per net computation */
}
if(argc == 1) printf("e%d\n",ant_ex);
#if DEBUG
for(l = 1; l < ant_netD->l;l++) /* per each layer */
for(n = 1; n < ant_netD->n[l];n++) /* per each neuron */
for(d = 0; d < ant_netD->n[l - 1] ;d++) /* per each dendrite */
{
printf("%-12.4lf",ant_neuronD[l][n].w[d]); /* write weights */
}
printf("\n");
#endif
if(ant_store_flag == 1) /* if store alarm detected */
{
if(ant_wrt_w(ant_netD,ant_neuronD) < 0) /* write weights in init file*/
_errmsg(0,"\t\t Error writing weights \n");
ant_store_flag =0;
}
}
if(ant_end_flag == 1) /* if end signal detected */
{
if(ant_wrt_w(ant_netD,ant_neuronD) < 0) /* write weights in init file*/
_errmsg(0,"\t\t Error writing weights \n");
exit(_errmsg(0,"\t\t Bye, Good luck \n"));
}
}while( ant_train_flag != ANT_END );
if(ant_wrt_w(ant_netD,ant_neuronD) < 0) /* write weights in init file*/
printf("\t\t Error writing weights\n");
}
/************************************************************************
* Name : ant_sighand() *
* Description : signal handling routine
* inputs : sig, signal value
* outputs : end and file flags
* return :
************************************************************************/
int ant_sighand(sig)
int sig;
{
switch (sig)
{
case 3:
alm_delete(almid);
ant_end_flag=1;
break;
case 2:
alm_delete(almid);
exit(_errmsg(0,"Exit requested by user\n"));
break;
case ANT_STORE:
ant_store_flag=1;
break;
default:
alm_delete(almid);
exit(_errmsg(0,"Exit by signal unknown\n"));
}
}
/************************************************************************
* Name : ant_display_goto() *
* Description : cursor set to c column and l line
* inputs : c,l column and line
* outputs : none
* return : none
***********************************************************************/
void ant_display_goto(c,l)
int c,l;
{
int i;
char sdum[12];
char sdumc[3];
char sduml[3];
sprintf(sdumc,"%d",c);
sprintf(sduml,"%d",l);
sdum[0]=27; sdum[1]='['; sdum[2]='\0';
strcat(sdum,sduml);
strcat(sdum,";");
strcat(sdum,sdumc);
strcat(sdum,"f");
puts(sdum);
}
/************************************************************************
* Name : ant_display_erase() *
* Description : Erase screen display
* inputs : none
* outputs : none
* return : none
************************************************************************/
void ant_display_erase()
{
char sdum[5];
sdum[0]=27; sdum[1]='['; sdum[2]='2'; sdum[3]='J';sdum[4]='\0';
puts(sdum);
}
/************************************************************************
* Name : strextrac() *
* Description : copy from n character of a string pointed by str1
* in a string pointed by str2.
* inputs : n character number of str1 to be first character of
* str2
* : str1 pointer to source string
* : str2 pointer to destination string
* outputs : None
* return : None
************************************************************************/
void strextract(n,str1,str2)
int n;
char *str1,*str2;
{
int i;
for( i = n ; str1[i] != '\0'; i++)
str2[i-n]=str1[i];
str2[i-n]='\0';
}
/************************************************************************
* Name : ant_use() *
* Description : Display help when inadecuate use or missing parameters
* inputs : None
* outputs : None
* return : None
************************************************************************/
void ant_use()
{
printf("Syntax:\t ant [<options>] \n");
printf("Function:\t Run the Artificial Neural Tool (see readme file)\n");
printf("Options:\n");
printf("\tNone\t\t Run in menu mode\n");
printf(
"\t-examp=<path> get net examples file from path (for -i and -t options)\n");
printf("\t-i\t\t improve a trained net (examp file needed)\n");
printf("\t-in=<path> get net input file from path (needed with -p option)\n");
printf("\t-init=<path> get net init file from path (always needed)\n");
printf("\t-out=<path> get net output file from path (needed with -p option)\n");
printf("\t-p\t\t process an input (an input and output file needed)\n");
printf("\t-t\t\t train the net (example file needed)\n\n");
printf("Hint:\t Try first to run the menu mode just typing ant \n");
exit(0);
}
/************************************************************************
* Name : main() *
* Description : main routine of neural net utility
* inputs : if desired init file name as argument
* outputs : none
* return : none
************************************************************************/
main (argc, argv)
int argc;
char **argv;
{
int sig,ant_ex,ant_arg;
char c,ant_opt,ant_answ[4];
ant_neuron **ant_neuronD;
ant_net ant_netD;
ant_examp *ant_inout;
intercept(ant_sighand);
system("tmode pause");
if(argc == 1) /* if menu mode ask for init file name */
do{
ant_display_erase();
ant_display_goto(10,12);
printf("File of net configuration: ");
scanf("%s",ant_netD.init);
ant_neuronD = ant_init(&ant_netD);
}while(ant_neuronD == NULL);
while(argc == 1){ /* loop of menu mode */
ant_display_erase();
ant_display_goto(0,0);
ant_opt=ant_menu(); /* display menu options and return selection */
switch (ant_opt) /* switch on option */
{
case 'T': case 't': /* train */
ant_gen_w(&ant_netD,ant_neuronD); /* generate random weights */
printf("File name with trainning examples : "); /* ask for example file name */
scanf("%s",ant_netD.examp); /* get example file name */
ant_inout=ant_rd_examp(&ant_netD); /* read examples and return ant_inout */
if( ant_inout == NULL) /* on error exit ant */
{
printf(" Error read example\n");
fseek(stdin,0,2);
c=getchar();
break;
}
almid=alm_cycle(ANT_STORE,ANT_TIME); /* set alarm to store weights */
system("tmode nopause");
ant_display_erase();
ant_display_goto(0,0);
ant_train(argc,&ant_netD,ant_neuronD,ant_inout); /* perform train */
alm_delete(almid); /* delete alarm */
ant_display_erase();
ant_display_goto(10,12);
printf(" *** End of training (type any key to continue)\n");
fseek(stdin,0,2);
c=getchar();
free((ant_examp **)ant_inout); /* free ant_inout */
system("tmode pause");
ant_display_erase();
ant_display_goto(0,0);
break;
case 'I': case 'i': /* improve */
printf("File name with trainning examples : "); /* ask for example file name */
scanf("%s",ant_netD.examp); /* get example file name */
ant_inout=ant_rd_examp(&ant_netD); /* read examples */
if( ant_inout == NULL) /* on error exit ant */
{
printf(" Error read example\n");
fseek(stdin,0,2);
c=getchar();
break;
}
almid=alm_cycle(ANT_STORE,ANT_TIME); /* set alarm to store weights*/
system("tmode nopause");
ant_display_erase();
ant_display_goto(0,0);
ant_train(argc,&ant_netD,ant_neuronD,ant_inout); /* perform train */
alm_delete(almid); /* delete alarm */
ant_display_erase();
ant_display_goto(10,12);
printf(" *** End of improve (type any key to continue)\n");
fseek(stdin,0,2);
c=getchar();
free((ant_examp **)ant_inout); /* free ant_inout */
system("tmode pause");
ant_display_erase();
ant_display_goto(0,0);
break;
case 'P': case 'p': /* process inputs */
printf("File name of inputs : "); /* ask for input file name */
scanf("%s",ant_netD.in); /* get input file name */
ant_inout=ant_rd_input(&ant_netD); /* read input file */
if( ant_inout == NULL) /* on error exit ant */
{
printf(" Error read example\n");
fseek(stdin,0,2);
c=getchar();
break;
}
system("tmode pause");
ant_display_erase();
ant_display_goto(10,12);
printf(" Do you want to store outputs? (Yes/No) : "); /* ask if output in file */
scanf("%s",ant_answ);
if (findstr(1, ant_answ,"y") || findstr(1, ant_answ,"Y"))
{
printf("\n\n File name to store outputs : "); /* ask for output file name */
scanf("%s",ant_netD.out); /* get output file name */
}
ant_display_erase();
ant_display_goto(0,0);
for ( ant_ex = 0 ; ant_ex < ant_inout->nex ;ant_ex++) /* per each example */
{
ant_propag(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* net computation, propagation of the input */
if (findstr(1, ant_answ,"y") || findstr(1, ant_answ,"Y"))
ant_wrt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* write file with results */
ant_prt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* printout results */
}
strcpy(ant_netD.out,"\0");
free((ant_examp **)ant_inout);
break;
case 'E': case 'e': /* exit ant */
ant_display_erase();
ant_display_goto(10,12);
printf(" Good luck "); /* bye */
ant_display_goto(10,24);
exit(0);
break;
default: /* on erroneous option */
printf("*** Unknown option (type any key to continue)***\n");
fseek(stdin,0,2);
c=getchar();
} /* end switch menu options */
} /* end while */
/* */
/* more than one argument => command line utility use */
/* set null strings */
ant_opt='\0';
strcpy(ant_netD.init,"\0");
strcpy(ant_netD.examp,"\0");
strcpy(ant_netD.in,"\0");
strcpy(ant_netD.out,"\0");
/* get information of arguments */
for( ant_arg = 1 ; ant_arg < argc ; ant_arg++)
{
if (findstr(1, argv[ant_arg],"-init="))
strextract(6,argv[ant_arg],ant_netD.init);
else if (findstr(1, argv[ant_arg],"-examp="))
strextract(7,argv[ant_arg],ant_netD.examp);
else if (findstr(1, argv[ant_arg],"-out="))
strextract(5,argv[ant_arg],ant_netD.out);
else if (findstr(1, argv[ant_arg],"-in="))
strextract(4,argv[ant_arg],ant_netD.in);
else if (findstr(1, argv[ant_arg],"-t"))
ant_opt='t';
else if (findstr(1, argv[ant_arg],"-i"))
ant_opt='i';
else if (findstr(1, argv[ant_arg],"-p"))
ant_opt='p';
}
/* Check if correct information */
if(strlen(ant_netD.init) == 0)
ant_use();
switch (ant_opt)
{
case 't': case 'i' : /* if train or improve a example file needed*/
if(strlen(ant_netD.examp) == 0)
ant_use();
break;
case 'p': /* if process an input needed */
if((strlen(ant_netD.in) == 0) || (strlen(ant_netD.out) == 0 ))
ant_use();
break;
default:
ant_use();
}
/* train the net, improve weigths or process an input file with the net */
ant_neuronD = ant_init(&ant_netD); /* read the init file */
switch (ant_opt)
{
case 't': /* train */
ant_gen_w(&ant_netD,ant_neuronD); /* generate random weights */
ant_inout=ant_rd_examp(&ant_netD); /* read examples from file */
if( ant_inout == NULL) /* on error exit */
exit(_errmsg(0, "\t\t Error reading example file\n"));
almid=alm_cycle(ANT_STORE,ANT_TIME); /* set alarm for weight saving */
ant_train(argc,&ant_netD,ant_neuronD,ant_inout); /* perform trainning */
alm_delete(almid); /* delete alarm */
free((ant_examp **)ant_inout);
break;
case 'i': /* improve */
ant_inout=ant_rd_examp(&ant_netD); /* read examples file */
if( ant_inout == NULL) /* on error exit */
exit(_errmsg(0, "\t\t Error reading example file\n"));
almid=alm_cycle(ANT_STORE,ANT_TIME); /* set alarm for weights saving */
ant_train(argc,&ant_netD,ant_neuronD,ant_inout); /* perform the train */
alm_delete(almid); /* delete alarm */
free((ant_examp **)ant_inout);
break;
case 'p': /* process inputs */
ant_inout=ant_rd_input(&ant_netD); /* get input from file */
if( ant_inout == NULL) /* on error exit */
exit(_errmsg(0,"\t\t Error reading input file\n"));
for ( ant_ex = 0 ; ant_ex < ant_inout->nex ; ant_ex++) /* per each input vector */
{
ant_propag(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* perform computation */
ant_wrt_out(ant_ex,&ant_netD,ant_neuronD,ant_inout); /* write resuls in file */
}
free((ant_examp **)ant_inout);
break;
default:
ant_use();
} /* end switch utility options */
} /* end main */