#include #include #include #include /* Graphics stuff */ #include #include #define SIGNIF 0.1 #define BORDER 1 #define NULL_PTR ((Pixrect *) 0) #define LEFT_BUTTON -125 #define MIDDLE_BUTTON -123 #define FONT2 "/usr/lib/fonts/fixedwidthfonts/cour.r.16" void set_col() ; void wcb(int but) ; int mb ; /* Mouse descriptor */ Pixrect *screen ; Pixfont *font_small ; /* End of basic graphic stuff*/ /********************Network Parameters**************************/ #define EXP_ERR 0.01 #define TOL 6 #define MAX_N 200 #define PROB_SIZE 4000 /*Check if Segment errors occur*/ #define COMMAND "entnet.com" /*****************DONT FORGET TO ALTER FILE WIDTH****************/ #define W_ACC 12 /*Save,Load weights accuracy*/ #define RMAX 999999.0 #define R2MAX 2147500000.0 #define R2MIN 3862.0 #define Xo 150 #define Yo 500 #define MAXW 15 #define SQUARE(x) ((x)*(x)) /*************************Training and Test files**************/ char input_file[20],test_file[20],weights_file[20],test2_file[20],res_file[20]; int MAXTRY,MINTRY,NSUCC,NFAIL,N_INPUTS,CALC_B,FILE_WIDTH,FILE_SIZE,B_CON,RAN; int miscount=0; float POS; float VCONT,VEXP,VARIANCE,SIG_OUT,ENT_MAX[5],OFF,H_LEARN,IW,CTHRESH; int STEP_COUNT[100],ND,round(float),T_STEP=0,DRAW,MAX_LEV; int P_FULL,MULTI, TimeElapse; struct neuron { double activ, out, close_p, close_n,steep; float class, ent; double (*squash_fn)(double,double); struct con_list *fan_out, *fan_in; }; struct connect { double weight,best_w,DW,dc; struct neuron *f,*b; }; struct net { struct neuron *input_layer[MAX_N]; struct neuron *layers[20][MAX_N]; }; struct lnode { struct lnode *next; int s_part,e_part; int nr_p,nr_n; struct con_list *cons; }; struct con_list { struct connect *con; struct con_list *next; }; struct timeval *tp ; float *inputs[PROB_SIZE]; struct timezone *tpz ; struct con_list *C_LIST; struct lnode *root, *add_link(int,int,int,int,struct con_list *); int file_in(char n[20], float **i),leaves=0,seed; int st_flag ; double sig(double,double),gauss(double,double), random(double); struct net FFNN; struct neuron *add_neuron(); struct connect *add_connect(); float get_sign(float); /*************************Schedule ********************************/ main() { FILE *fp; char w_f[20]; void test(char[20]), set_up_input_layer(),weight_file(); void prune(); int build(),l,pt,jump; extern void *malloc(); tp = (struct timeval *)malloc(sizeof(struct timeval)) ; tpz = (struct timezone *)malloc(sizeof(struct timezone)) ; tpz->tz_minuteswest = 1 ; tpz->tz_dsttime = DST_GB ; printf("\n Enter seed 1 - 10 :"); scanf("%d",&seed); fp=fopen(COMMAND,"r"); fscanf(fp,"%s",input_file); printf("\n Input file is %s ",input_file); fscanf(fp,"%s",test_file); printf("\n Test file is %s ",test_file); fscanf(fp,"%s",w_f); sprintf(weights_file,"%s%d",w_f,seed); printf("\n Weights file is %s ",weights_file); fscanf(fp,"%d ",&jump); fscanf(fp,"%s",w_f); sprintf(res_file,"%s%d",w_f,seed); printf("\n Results file is : %s",res_file); fscanf(fp,"%d %d %d %f ",&N_INPUTS,&FILE_WIDTH, &FILE_SIZE,&POS); fscanf(fp,"%d %d %f",&NSUCC,&NFAIL,&H_LEARN); fscanf(fp,"%d %d %d %d %d %d %f",&CALC_B,&B_CON,&RAN,&DRAW,&P_FULL,&MULTI,&OFF); printf("\nInp: %d, Succ: %d ,Fail: %d ,{C_Bias: %d, F_Con: %d, RSEQ: %d, F_Rep %d Multi %d(1=on)}", N_INPUTS,NSUCC,NFAIL,CALC_B,B_CON,RAN,P_FULL,MULTI); if (DRAW==1) printf("\n Pattern space will be plotted. \n"); fscanf(fp,"%d %d %f %f %f %f %f %f",&MAXTRY,&MINTRY,&VEXP,&VCONT, &VARIANCE,&SIG_OUT,&IW,&CTHRESH); fscanf(fp,"%d",&MAX_LEV); for(l=0;l<=MAX_LEV;l++){ fscanf(fp," %f",&ENT_MAX[l]); printf(" Ent_stop[%d]: %f ",l,ENT_MAX[l]); } printf("\n Maxtry: %d, Mintry: %d, Vexp: %1.1f, Vcont: %1.1f, Var: %1.1f \nSig_out: %1.4f ,In-W %1.3f ,Leaf-T %1.3f",MAXTRY,MINTRY,VEXP,VCONT,VARIANCE,SIG_OUT,IW,CTHRESH); printf("\n maximium levels %d\n",MAX_LEV); fclose(fp); seed += jump; while(seed>0){ rand(); seed--; } set_up_input_layer(); gettimeofday(tp,tpz); TimeElapse=tp->tv_sec; pt=build(); gettimeofday(tp,tpz); TimeElapse=tp->tv_sec-TimeElapse; test(test_file); prune(); test(test_file); weight_file(); } /********************SET INPUT NEURONS*************************/ void set_up_input_layer() { struct neuron *n_ptr,**n; int i; for(i=0,n=FFNN.input_layer;i<=N_INPUTS;i++,n++) *n = add_neuron(); FFNN.input_layer[N_INPUTS]->out = -1.0; /****SET BIAS UNIT****/ } /***********************Random Number Generator*******************/ /*******************Returns a number between x and 0**************/ /*****************************************************************/ double random(double x) { int dec,l,n,i; char *sp,string[8]; if(RAN==0){ gettimeofday(tp,tpz) ; l = tp->tv_usec ; sprintf(string,"%d ",l) ; n=0; for(sp=string,dec=1;*sp!=' ';sp++,dec*=10) n+=((int)*sp-48) * dec; return (double)n / (RMAX/x); } else return (((rand()-R2MIN)/(R2MAX-R2MIN))*x); }; float get_sign(float x) { if (x<0.0) return -1.0; return 1.0; } /*****************Create neuron and connection*******************/ struct connect *add_connect() { struct connect *ptr; extern void *malloc(); ptr = (struct connect *) malloc (sizeof(struct connect)); ptr->weight = ptr->best_w = 1.0 - random(2.0); ptr->DW=ptr->dc=0.0; return ptr; } struct neuron *add_neuron() { struct neuron *ptr; extern void *malloc(); ptr=(struct neuron *) malloc (sizeof(struct neuron)); ptr->activ=ptr->out=0; ptr->close_p=999;ptr->close_n=-999; ptr->steep = 1.0; ptr->squash_fn=sig; /* set squash fn's */ ptr->fan_out=ptr->fan_in=NULL; return ptr; } void add_Gnode(struct con_list *cl,int s,int e,int level,float class) { int i,err; void feedforwards(),change_st(struct neuron *); struct neuron *ptr,**n; struct connect *c; struct con_list *link,*add_con_l(struct con_list *,struct connect *); float sum=0; float p=3.0,**in_p,*pat; printf(" hull: %d,",e-s); FFNN.layers[level][leaves++]=ptr=add_neuron(); ptr->fan_in=NULL; ptr->class=class; while(cl!=NULL){ c=add_connect(); c->f=ptr; c->b=cl->con->b; c->b->fan_out=add_con_l(c->b->fan_out,c); ptr->fan_in=add_con_l(ptr->fan_in,c); cl=cl->next; } pat=&p;i=s; while(*pat!=class){ for(pat=inputs[i],n=FFNN.input_layer;pat<(inputs[i]+N_INPUTS);pat++,n++) (*n)->out = *pat; i++; } feedforwards(); if (*pat==class) for(link=ptr->fan_in;link!=NULL;link=link->next){ c=link->con; if (c->b->out>0.5) {c->weight=IW;p=1.0;} else{ c->weight = -IW;p=0.0;} sum+=c->weight*p; } sum=sum-(c->weight*p)+(c->weight*0.5); c=add_connect(); c->f=ptr; c->b=FFNN.input_layer[N_INPUTS]; c->weight=sum; ptr->fan_in=add_con_l(ptr->fan_in,c); c->b->fan_out=add_con_l(c->b->fan_out,c); if (SIG_OUT>0.0) st_flag=1; err = 9 ; while(err!=0){ err=0; for(in_p=inputs+s;in_pout = *pat; feedforwards(); } if(ptr->out<0.7 && class==*pat){ err++; printf("!"); c->weight-=(c->weight*0.01); /*reduce bias weight*/ } if (SIG_OUT>0.0){ ptr->close_n = -ptr->close_p; change_st(ptr); st_flag=0; } } printf("\n"); } /**************Create Level Node*****************************/ struct lnode * add_link(int p,int n,int s,int e,struct con_list *cl) { struct lnode *ptr; extern void *malloc(); struct con_list *add_con_l(struct con_list *,struct connect *); struct connect *c; ptr=(struct lnode *) malloc (sizeof(struct lnode)); ptr->next=NULL; ptr->cons=NULL; while(cl!=NULL){ c=add_connect(); c->b=cl->con->b; ptr->cons=add_con_l(ptr->cons,c); cl=cl->next; } ptr->nr_p= p; ptr->nr_n= n; ptr->s_part=s; ptr->e_part=e; return ptr; } /****************** Add Connection link list *******************/ struct con_list *add_con_l(struct con_list *head,struct connect *connect) { struct con_list *ptr; extern void *malloc(); ptr=(struct con_list *) malloc (sizeof(struct con_list)); ptr->con = connect; ptr->next = head; return ptr; } /***********************Round Number******************************/ int round(float x) { int whole; whole = (int) x; if ((x - whole)>=0.5) return ++whole; else return whole; } /*******************Gaussian Function**************************/ double gauss(double x,double w_mag) { double gx; gx = 0.5 - fabs((w_mag - x)/w_mag); return sig(gx,10.0); } /*******************Squashing Function**************************/ double sig(double x,double steep) { return 1.0/(1 + exp(-steep*x)); } /******************Log 2 **************************************/ double log2(double x) { if (x==0.0) return -9999.999; else return log10(x)/log10(2.0); } /*******************File Routine**********************************/ /*Reads space seperated values from file and converts into double*/ /*****************************************************************/ int file_in(char name[],float **in_arr) { FILE *fp; int c,x; int i,pos; double rconvert(char [W_ACC],int,int); char line[FILE_WIDTH],*p,rstr[10],path[20]; float *inp; c = 0; sprintf(path,"../DATA/%s",name); if ((fp = fopen(path,"r")) == NULL) printf("Create input file !!!!"); else { inp=*in_arr; while (fgets(line,FILE_WIDTH,fp) != NULL){ p = line;x = 0; while(*p!='\n'){ pos=0; for(i=0;(*p!=' ')&&(*p!='\n')&&(*p!='|');p++){ if (((*p>='0')&&(*p<='9'))||(*p=='-')){ rstr[i] = *p; i++;} else if (*p = '.') pos = i; else printf("\nError in data file !!!\n"); } if (i>0){ *inp++ = rconvert(rstr,i,pos); x++; } if ((*p==' ') || (*p=='|')) p++; } /*while*/ if (x>0){ in_arr++;c++; inp = *in_arr; } /*if*/ } /*while fgets*/ fclose(fp); } return c; } double rconvert(char string[],int l,int pos) { int i,pl; float sum,dec,sign; if (string[0]=='-') sign=-1.0; else sign=1.0; if (pos!=0) for(dec=1.0,pl=(l-pos);pl>0;dec=dec/10.0,pl--); else dec=1.0; sum=0.0; for(i=(l-1);i>=0;i--) if (string[i]!='-'){ sum+= ((float)string[i]-48.0)*dec; dec=dec*10.0;} return (double)sum*sign; } int label_layer(struct neuron **n,int layer) { int i=0; while(*n!=NULL){ (*n)->close_p= (float)i; (*n)->close_n= (float) layer; i++;n++; } return i; } /***********************Output file of weights****************/ void weight_file() { void get_weights(struct neuron**,FILE *); int label_layer(struct neuron **,int); int cons(struct neuron *); FILE *fp; struct neuron **n; int i; if ((fp = fopen(weights_file,"w")) == NULL) printf("Can not open output file !!!!!"); else{ i=0; while(*(n=FFNN.layers[i])!=NULL){ fprintf(fp," %d",label_layer(FFNN.layers[i],i)); i++; } fprintf(fp,"\n%d", label_layer(FFNN.input_layer,-99)-1); get_weights(FFNN.input_layer,fp); i=0; while(*(n=FFNN.layers[i])!=NULL){ fprintf(fp,"\n%d", label_layer(n,i)); get_weights(FFNN.layers[i],fp); i++; } } fclose(fp); printf("\n \nWeights File created %s\n",weights_file); } /***************************************************************/ /****Writes fan_in connections to each node on 1 line ********/ /***************************************************************/ void get_weights(struct neuron **n,FILE *fp) { struct con_list *c; char string[10]; int cons(struct neuron *); for(;((*n)!=NULL&&(*n)!=FFNN.input_layer[N_INPUTS]);n++){ for(c=(*n)->fan_in;c!=NULL;c=c->next) if (c->con->b==FFNN.input_layer[N_INPUTS]) /*bias*/ fprintf(fp,"\n%f",c->con->weight); fprintf(fp,"\n%d \n",cons(*n)); for(c=(*n)->fan_out;c!=NULL;c=c->next){ fprintf(fp," %d %d %d ",(int)(*n)->close_p, (int)c->con->f->close_n,(int)c->con->f->close_p); sprintf(string,"%lf ",c->con->weight); fputs(string,fp); } putc('\n',fp); } } int cons(struct neuron *n) { int i; struct con_list *c; for(c=n->fan_out,i=0;c!=NULL;c=c->next,i++); return i; } /*****************Clear List*****************************/ struct lnode *clear_list(struct lnode *head) { struct con_list *clear_con(struct con_list *); if (head==NULL) return NULL; head->cons=clear_con(head->cons); free(head); return clear_list(head->next); } /*******************Clear con_list*************************/ struct con_list *clear_con(struct con_list *head) { struct con_list *p; if (head==NULL) return NULL; free(head->con); head->con=NULL; free(head); return clear_con(head->next); } /*******************Calculate Bias Term*********************/ void calc_bias(struct neuron * n,int level,float max[],float min[]) { struct connect *c; struct con_list *cl; double sum=0.0; int i; if(level==-99) for(cl=n->fan_in->next,i=0;cl!=NULL;cl=cl->next,i++){ c=cl->con; sum+=(random(max[i]-min[i])+min[i])*c->best_w; } else for(cl=n->fan_in->next;cl!=NULL;cl=cl->next){ c=cl->con; sum+=random(2.0)*c->best_w; } c=n->fan_in->con; if (CALC_B==1) c->best_w=c->weight=sum; } /******************set neuron connection************/ void setcon(struct neuron *to,struct neuron *from,double wgt) { struct connect *c_ptr; struct con_list *add_con_l(struct con_list *,struct connect *); c_ptr = add_connect(); if (wgt!=0.0) c_ptr->weight=wgt; c_ptr->f=to; c_ptr->b=from; to->fan_in=add_con_l(to->fan_in,c_ptr); from->fan_out=add_con_l(from->fan_out,c_ptr); } /**********************Build Network************************/ int build() { struct neuron *n_ptr,*n,**in_n; struct connect *c_ptr; float *pat,**in_p; float max[N_INPUTS],min[N_INPUTS]; void create_layer(struct neuron *,int,int),build_leaf(struct lnode *,int); double Entropy,N_Ent,sum=0.0; double train_weights(struct neuron *,int, int, int, double); int i,j,cc,patterns,l,nlnodes,tlnodes,np,nn; void calc_bias(struct neuron*,int,float[FILE_WIDTH],float[FILE_WIDTH]); struct con_list *link,*add_con_l(struct con_list *,struct connect *); int Count_nodes(struct lnode*),stop,start,xc,yc,x,y; void pat_space2D(float[N_INPUTS],float[N_INPUTS]); void outtrain(struct neuron*,int),reset(struct neuron*); void setcon(struct neuron *,struct neuron*,double); nn=np=0; for(i=0;i max[i]) max[i] = *pat; if (*pat < min[i]) min[i] = *pat; } if (*pat>=POS-0.1){ np++; *pat=1.0;} else{ nn++; *pat=0.0;} } printf("\n pos examples %d, neg examples %d \n",np,nn); tlnodes=0; nlnodes=999;l=0;ND=-1; while(l<=MAX_LEV){ nlnodes=0;Entropy=9.99; stop=0; root=add_link(np,nn,0,patterns,NULL); while((root!=NULL)&&(stop<3)&&(Entropy>ENT_MAX[l])){ nlnodes ++;tlnodes++; n_ptr = add_neuron(); STEP_COUNT[++ND]=0; T_STEP++; FFNN.layers[l][nlnodes-1] = n_ptr; if (B_CON==1) start=0; else start=l-1; if (start>=0){ for(i=start;if=n_ptr; c_ptr->b=n; n_ptr->fan_in=add_con_l(n_ptr->fan_in,c_ptr); n->fan_out=add_con_l(n->fan_out,c_ptr); } } } for(i=0;i<=N_INPUTS;i++){ /*Last neuron in input layer is bias*/ if((i==N_INPUTS)||(B_CON==1)||(l==0)){ c_ptr = add_connect(); /*First link in linked list at end */ c_ptr->f = n_ptr; c_ptr->b = FFNN.input_layer[i]; n_ptr->fan_in=add_con_l(n_ptr->fan_in,c_ptr); FFNN.input_layer[i]->fan_out= add_con_l(FFNN.input_layer[i]->fan_out,c_ptr); } } /***CALCULATE BIAS CONNECTION********/ calc_bias(n_ptr,l,max,min); N_Ent=train_weights(n_ptr,nlnodes,patterns,l,Entropy); stop=0; while ((N_Ent == Entropy)&&(stop<3)){ reset(n_ptr); stop++; N_Ent=train_weights(n_ptr,nlnodes,patterns,l,Entropy); } create_layer(n_ptr,patterns,l); Entropy=N_Ent; if (DRAW==0) printf("\nl %d, ln %d, Ent %f, tn %d, trn %d",l,nlnodes, Entropy,tlnodes,Count_nodes(root)); } l++; } if (root!=NULL) build_leaf(root,l); printf("\n Partitioning Complete"); FFNN.layers[l+1][0]=n_ptr=add_neuron(); for(in_n=FFNN.layers[l];*in_n!=NULL;in_n++) setcon(n_ptr,*in_n,0.0); outtrain(n_ptr,patterns); if((N_INPUTS==2 || MULTI==1) && DRAW==1) pat_space2D(max,min); return patterns; } /******************Train Output Neuron****************************/ void outtrain(struct neuron *trnode,int num_in) { int errs=1,ones=0; struct neuron **n; struct connect *c; struct con_list *link; float **in_p, *pat; void feedforwards(); double diff(double,double); while(errs!=0 && ones<2){ errs=0; for(in_p=inputs;in_pout = *pat; feedforwards(); ones=0; for(link=trnode->fan_in;link!=NULL;link=link->next){ c=link->con; if (c->b->out>0.5 && c->b->class==*pat){ ones++; if (((*pat==1.0)&&(trnode->outb->out-0.05))|| ((*pat==0.0)&&(trnode->out>(1.05-c->b->out)))){ errs++; c->weight+=H_LEARN*diff(trnode->out,trnode->steep)* (*pat-trnode->out); } } } if (ones>1) printf("\nErr ones %d ",ones); } } } /******************Random_step*************************************/ double random_step(double var) { return random(var) - var*0.5; } /*********************Reset neuron***************************/ void reset(struct neuron *node) { struct con_list *cl; struct connect *c; node->activ=node->out=0.0; for(cl=node->fan_in;cl!=NULL;cl=cl->next){ c=cl->con; c->weight = c->best_w = 1.0 - random(2.0); c->DW=c->dc=0.0; } } /******************Calculate weight updates***********************/ double train_weights(struct neuron *trnode,int num_nodes,int nt,int level,double Best_Err) { double Err,v,ve,vc,Last_Ent,decay; struct connect *link; struct con_list *cl; int ns,nf,nconf,tries,i,count=0; double entropy(struct neuron*, int); void update_weights(struct neuron *); double random_step(double); ns=nf=tries=0; Last_Ent=Best_Err; trnode->ent=Err=entropy(trnode,nt); /*if (DRAW==0) printf("\nInitial Ent %f",Err);*/ while(((Best_Err>=Last_Ent)||(triesEXP_ERR)&&(triesfan_in;cl!=NULL;cl=cl->next){ link=cl->con; link->DW = random_step(v); link->weight=link->best_w + link->DW + link->dc; } Err=entropy(trnode,nt); if (Err>=Best_Err){ /*Reversal step*/ for(cl=trnode->fan_in;cl!=NULL;cl=cl->next){ link=cl->con; link->DW = -link->DW; link->weight=link->best_w + link->DW + link->dc; } Err=entropy(trnode,nt); } if (Errfan_in;cl!=NULL;cl=cl->next){ link=cl->con; link->best_w=link->weight; link->dc=0.2*link->dc + 0.4*link->DW; } Best_Err=Err=entropy(trnode,nt); } else{ ns=0; nf++; nconf++; for(cl=trnode->fan_in;cl!=NULL;cl=cl->next){ link=cl->con; link->dc=0.5*link->dc; } } } } /*set found weights */ for(cl=trnode->fan_in;cl!=NULL;cl=cl->next){ link=cl->con; link->weight = link->best_w; } trnode->ent=trnode->ent-Best_Err; return Best_Err; } /***************Calculate Entropy*************************/ double entropy(struct neuron *noder,int num) { double n_pos1,n_neg1,lg1,lg2; int nr_pos,nr_neg,nr; double ent1,ent0,Ent; float *pat,**in_p; struct neuron **n; void feedforwards(); struct lnode *lp; Ent =0.0; for(lp=root;lp!=NULL;lp=lp->next){ n_pos1=n_neg1=0; nr_pos=lp->nr_p; nr_neg=lp->nr_n; nr=nr_pos+nr_neg; for(in_p=inputs+lp->s_part;in_pe_part;in_p++){ for(pat=*in_p,n=FFNN.input_layer;pat<(*in_p+N_INPUTS);pat++,n++) (*n)->out = *pat; feedforwards(); n_pos1 += noder->out * *pat; n_neg1 += (1 - *pat) * noder->out; } if ((n_pos1+n_neg1!=0.0)&&(n_pos1+n_neg1next)+1; } /***************Terminal Leaf***************************/ void build_leaf(struct lnode *root,int level){ struct lnode *lp; void add_Gnode(struct con_list *,int,int,int,float); for(lp=root;lp!=NULL;lp=lp->next){ if (lp->nr_p>lp->nr_n) add_Gnode(lp->cons,lp->s_part,lp->e_part,level,1.0); else add_Gnode(lp->cons,lp->s_part,lp->e_part,level,0.0); } } /*******************Create Layer****************************/ void create_layer(struct neuron *noder,int num,int level) { struct lnode *lp,**nlp,*newl; int nr_pos,nr_neg,n_pos1,n_neg1,n_pos0,n_neg0,c,pc,nc,s,e; float *pat,**in_p,*pos[num],*neg[num],np1_per,nn1_per,np0_per,nn0_per; struct neuron **n; struct connect *c_ptr; void feedforwards(),add_Gnode(struct con_list *,int,int,int,float); struct lnode *clear_list(struct lnode *); void change_st(struct neuron *); struct con_list *add_con_l(struct con_list*,struct connect*); st_flag=1; for(in_p=inputs;in_pout = *pat; feedforwards(); } if (SIG_OUT>0.0){ miscount=0; change_st(noder); if(level==0) printf("\n Miscount = %d #### \n",miscount); } st_flag=0; newl=NULL;nlp=&newl; for(lp=root;lp!=NULL;lp=lp->next){ n_pos1=n_neg1=pc=nc=0; nr_pos=lp->nr_p; nr_neg=lp->nr_n; for(in_p=inputs+lp->s_part;in_pe_part;in_p++){ for(pat=*in_p,n=FFNN.input_layer;pat<(*in_p+N_INPUTS);pat++,n++) (*n)->out = *pat; feedforwards(); if (noder->out>0.5){ pos[pc++]=*in_p; if (*pat>=0.9) n_pos1++; else n_neg1++; } else neg[nc++]=*in_p; } for(c=0;cs_part+c]=pos[c]; for(c=0;cs_part+pc+c]=neg[c]; s=lp->s_part;e=lp->s_part+pc; np1_per=(float)(n_pos1-n_neg1)/(float)(n_pos1+n_neg1); nn1_per=(float)(n_neg1-n_pos1)/(float)(n_pos1+n_neg1); n_pos0 = nr_pos-n_pos1; n_neg0 = nr_neg-n_neg1; np0_per=(float)(n_pos0-n_neg0)/(float)(n_pos0+n_neg0); nn0_per=(float)(n_neg0-n_pos0)/(float)(n_pos0+n_neg0); if ((n_pos1==nr_pos && n_neg1==nr_neg)||(n_pos1==0 && n_neg1==0)); else{ c_ptr = add_connect(); c_ptr->b = noder; lp->cons = add_con_l(lp->cons,c_ptr); } if (n_pos1>0 && n_neg1>0 && np1_percons); nlp=&((*nlp)->next);} else if (MAX_LEV==level){ if (np1_per>=CTHRESH) add_Gnode(lp->cons,s,e,level+1,1.0); else if (nn1_per>=CTHRESH) add_Gnode(lp->cons,s,e,level+1,0.0); } s=e;e=lp->e_part; if (n_pos0>0 && n_neg0>0 && np0_pere_part,lp->cons); nlp=&((*nlp)->next);} else if (MAX_LEV==level){ if (np0_per>0) add_Gnode(lp->cons,s,e,level+1,1.0); else if (nn0_per>0) add_Gnode(lp->cons,s,e,level+1,0.0); } } root = clear_list(root); root = newl; } /********************Forward Propagate Inputs**********************/ void prop_for(struct neuron **ptr) { struct neuron *n; struct connect *link; struct con_list *lp; struct neuron *win; float best=0.0; while(*ptr!=NULL){ for(lp=(*ptr)->fan_out;lp!=NULL;lp=lp->next){ link=lp->con; link->f->activ += link->weight * ((*ptr)->out); } ptr++; } } /*******************Sum Activation***********************************/ void activate(struct neuron **ptr) { struct neuron *np; struct con_list *lp; float sum; while((np=*ptr) != NULL){ if (np->squash_fn==sig) np->out = np->squash_fn(np->activ,np->steep); else{ for(lp=np->fan_in;lp!=NULL;lp=lp->next) sum+=SQUARE(lp->con->weight); np->out = np->squash_fn(np->activ,sqrt(sum)); } if (st_flag==1){ if ((np->activ > 0)&&(np->activ < np->close_p)) np->close_p=np->activ; else if ((np->activ < 0)&&(np->activ > np->close_n)) np->close_n=np->activ; } np->activ = 0; ptr++; } } /********************Propagation Activation Schedule*****************/ void feedforwards() { int i; struct neuron **p; void prop_for(struct neuron **), activate(struct neuron **); prop_for(FFNN.input_layer); /*Adds inputs to all neurons activ*/ p=FFNN.layers[0];i=0; while(*p!=NULL){ activate(p); prop_for(p); i++; p=FFNN.layers[i]; } } /*******************Scaling Function******************************/ double diff(double x,double steep) { return (steep*x*(1-x)) + 0.1; } /*****************Test Trained Network***************************/ void test(char file[]) { float test[FILE_SIZE][FILE_WIDTH],*inputs[FILE_SIZE],**in_p,*pat,error; FILE *fp; struct neuron **n,*n_out; int num_in,i,ns,tn=0,err=0; void feedforwards(),print_steep(FILE *); error = 0.0; if ((fp=fopen(res_file,"w"))==NULL) printf(" Can not open Results file !!!!"); fprintf(fp,"\n %s ****** OUTPUTS ******\n\n",file); fprintf(fp,"\nInp: %d, Succ: %d ,Fail: %d ,{C_Bias: %d, F_Con: %d, RSEQ: %d, F_Rep %d(1=on)}", N_INPUTS,NSUCC,NFAIL,CALC_B,B_CON,RAN,P_FULL); fprintf(fp,"\n seed %d \n",seed); fprintf(fp,"\n Maxtry: %d, Mintry: %d, Vexp: %1.1f, Vcont: %1.1f, Var: %1.1f \nSig_out: %1.4f ,In-W %1.3f ,Leaf-T %1.3f",MAXTRY,MINTRY,VEXP,VCONT,VARIANCE,SIG_OUT,IW,CTHRESH); fprintf(fp,"\n maximium levels %d\n",MAX_LEV); printf("\n %s ****** OUTPUTS ******\n\n",file); for(i=0;iout = *pat; feedforwards(); /* if (P_FULL==1){ printf(" d %f act %f \n",*pat,n_out->out); fprintf(fp," d %f act %f \n",*pat,n_out->out);} */ if (*pat>=POS-0.1) *pat=1.0; else *pat=0.0; if (fabs(*pat-n_out->out) > 0.4){ err++; error += fabs(*pat-n_out->out); } } printf(" Oerr %1.3f, ave-err %1.3f T-err %d, patterns %d,\n Class-rate %3.3f",error, error/num_in,err,num_in,100.0-100.0*(float)err/(float)num_in); fprintf(fp," Oerr %1.3f, ave-err %1.3f T-err %d, patterns %d,\n Class-rate %3.3f",error, error/num_in,err,num_in,100.0-100.0*(float)err/(float)num_in); printf("\nTotal Training Steps: %d Average : %f ",T_STEP,(float)T_STEP/(float)tn); fprintf(fp,"\nTotal Training Steps: %d Average : %f ",T_STEP,(float)T_STEP/(float)tn); fprintf(fp,"Timed at %d secs \n",TimeElapse); printf("\n\n FINISHED \n"); fclose(fp); } /**************TRAIN STEEP*****************************************/ void change_st(struct neuron *np) { double av_act; double sum; struct con_list *lp; for(lp=np->fan_in->next;lp!=NULL;lp=lp->next) sum+=SQUARE(lp->con->weight); sum= sqrt(sum);; if (fabs((np->close_p + np->close_n)/sum) > SIGNIF) miscount++; av_act = (np->close_p+fabs(np->close_n))/2.0; if (np->close_p>(-np->close_n)) av_act=-np->close_n; else av_act=np->close_p; np->steep = -(1.0/(av_act)) * log((1/SIG_OUT)-1); } void prune() { int i; struct neuron **n,**p; void pr_st(struct neuron **); p=FFNN.layers[0];i=0;ND=0; pr_st(p); /* while(*p!=NULL){ pr_st(p); i++; p=FFNN.layers[i]; } */ } void pr_st(struct neuron **ptr) { struct neuron *np, *n_b; float best; struct con_list * lp; if((*ptr)->ent != 0.0){ best = 9999.99; while((np=*ptr) != NULL){ if (np->ent < best){ best = np->ent; n_b = np; } ptr++; } for(lp=n_b->fan_in;lp!=NULL;lp=lp->next) lp->con->weight=0.0; for(lp=n_b->fan_out;lp!=NULL;lp=lp->next) lp->con->weight=0.0; } } /*****************Plot Pattern Space*************************/ void pat_space2D(float max[],float min[]) { float *inputs[FILE_SIZE],**in_p,*pat,error; FILE *fp; int num_in; struct neuron **n,*n_out; float xstep,ystep,x,y, in; int i,ns,colour,xp,yp,sx,sy; void feedforwards(); font_small = pf_open(FONT2) ; mb = open("/dev/mouse",O_RDONLY) ; screen = pr_open("/dev/fb") ; set_col() ; i=0;n=FFNN.layers[0]; pr_rop(screen,0,0,1150,900,PIX_CLR|PIX_DONTCLIP,screen,0,0) ; while(FFNN.layers[i][0]!=NULL) i++; /*find out-unit */ n_out = FFNN.layers[i-1][0]; if (MULTI==0){ xstep=(max[0]-min[0])/100.0; ystep=(max[1]-min[1])/100.0; for(x=min[0]-OFF,xp=0;x<=max[0]+OFF;x+=xstep,xp+=2) for(y=max[1]+OFF,yp=0;y>=min[1]-OFF;y-=ystep,yp+=2){ n=FFNN.input_layer; (*n)->out = x; n++; (*n)->out = y; feedforwards(); colour=4+round(n_out->out *10.0); for(sx=0;sx<2;sx++) for(sy=0;sy<2;sy++) pr_put(screen,xp+sx,yp+sy,colour); } }else{ if((fp=fopen("../BP/Draw_in","r"))!=NULL){ for(xp=0;xp<=200;xp+=2) for(yp=0;yp<=200;yp+=2){ for(n=FFNN.input_layer;nout = in;} feedforwards(); colour=4+round(n_out->out *10.0); for(sx=0;sx<2;sx++) for(sy=0;sy<2;sy++) pr_put(screen,xp+sx,yp+sy,colour); } fclose(fp); } else pr_text(screen,350,400,PIX_SRC,font_small,"CREATE DRAW FILE IN BP DIR !!!!!!!!!") ; } pr_text(screen,500,850,PIX_SRC,font_small,"LEFT, MID button"); wcb(LEFT_BUTTON) ; wcb(MIDDLE_BUTTON); pr_rop(screen,0,0,1150,900,PIX_CLR|PIX_DONTCLIP,screen,0,0) ; close(mb) ;/* Get rid of graphics */ pf_close(font_small) ; pr_close(screen) ; } /**** Graphics routines - set up colourmap and wait for mouse button ****/ void wcb(int but) { char buf[4] ; int butty ; do { read(mb,buf,3) ; butty = buf[0] ; }while (butty!=but) ; } /* Get colourmap and produce grey-scale */ void set_col() { unsigned char red[15],green[15],blue[15] ; register int index ; pr_getcolormap(screen,0,15,red,green,blue) ; for (index=4;index<15;index++) { red[index] = (index-4)*25 ; green[index] = (index-4)*25 ; blue[index] = (index-4)*25 ; } red[18]=254;blue[18]=0;green[18]=0; red[19]=0;blue[19]=254;green[19]=0; red[17]=0;blue[17]=0;green[17]=250; pr_putcolormap(screen,0,15,red,green,blue) ; } /* ----------------------------------------------------- ------------- */