/*******************************************************************/ /*************** NEURAL FUZZY NETWORK IDENTIFIER *****************/ /*** GOAL:(1) on-line initial parameter choosing ****/ /*** (2) parameter learning with BP algorithm ****/ /*** (3) add new rule ,if the current rules are lack ****/ /*** FILE: FNNIP2.C (the dynamic system separate to an unknown ****/ /** unlinear part and a known linear part) ****/ /*** This is example 2 -- a SISO system ****/ /******************************************** C. T. Chao 1993/3/8 */ #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define maxseed 32767.0 #define pi 3.14159 FILE *fp1,*fp2,*fp3,*fp4,*fp5; char initial[12],final[12],ferror[12],fout[12],foutp[12]; /********************************************************************** final: SAVE (1). STRUCTRUE DEFINITION (2). MEAN & var (3). w34 FMFILE : SAVE MEANERRR ***********************************************************************/ /********** MEMBERSHIP FUNCTIONS **************/ /* gaussaian : membership gaussian function */ /**********************************************/ float gaussian(float,float,float); main() { float **mean,**var,*w34; float *inv,desireout; float output,uk; float **o2,*o3; float **dmean,**dvar,*dw34; float **delta2,*delta3,delta4; unsigned int sizeofin=2,rulenum; /****** sizeofin is the state number of non-linear function ********/ float *meanerror; float errnow=0.0; long float den,index; /******************************************************* maxpass : MAX ITERATION NUMBER pass : PRESENT ITERATION NUMBER alphaw : LEARNING RATE OF w34 gammaw : MOMENTUM CONSTANCE OF w34 alpham : LEARNING RATE OF MEAN AND var gammam : MOMENTUM CONSTANCE OF MEAN AND var ********************************************************/ int maxpass,pass=0,ptspan,trainning=TRUE; float alphaw,gammaw,alpham,gammam,et; /* temporary parameters */ int i,j,k,L,l; float tempf,maxtempf,mintempf; float meanst,meannd,hold,overlapfactor; int maxrule; clrscr(); printf("\nINPUT THE OUTPUT FILE NAME :(.m)" ); scanf("%s",&final); strcpy(initial,final); strcpy(ferror,final); strcpy(fout,final); strcpy(foutp,final); /*** add the extension for each file ***/ strcat(final,"f.m"); strcat(initial,"i.m"); strcat(ferror,"e.m"); strcat(fout,"o.m"); strcat(foutp,"p.m"); printf("\n ################ THE STRUCTURE DEFINITION #################"); /* printf("\nHow many inputs are be fed into FUZZY IDENTIFIER? "); scanf("%d",&sizeofin); */ printf("\nHow many rules do you want for FUZZY IDENTIFIER? "); scanf("%d",&rulenum); printf("\nWhat is the overlap factor do you want? "); scanf("%f",&overlapfactor); printf("\n ############### THE LEARNING ENIROMENT ###################"); printf("\nHow many iterations are needed in parameter learning: "); scanf("%d",&maxpass); printf("\nWhat is the error tolence? "); scanf("%f",&et); printf("\nWhat is the learning rate alphaw (0 < alpha < 1) : "); scanf("%f",&alphaw); printf("\nWhat is the momentum rate gammaw (0 < gamma < 1) : "); scanf("%f",&gammaw); printf("\nWhat is the learning rate alpham (0 < alpha < 1) : "); scanf("%f",&alpham); printf("\nWhat is the momentum rate gammam (0 < gamma < 1) : "); scanf("%f",&gammam); printf("\nDisplay data every ? iterations (say 50) : "); scanf("%d",&ptspan); maxrule=40; /*********************************************/ /* MEMORY ALLOCATION DECLARATION */ /*********************************************/ /*********************************************/ /***** LAYER 1 *****/ inv = (float *)calloc( sizeofin ,sizeof(float) ); /***** LAYER 2 *****/ o2 = (float **)calloc( maxrule , sizeof(float *) ); for(i=0; i1.99) uk=2.0; if(uk<-1.99) uk=-2.0; tempf=1.0 + pow(inv[0],2)+pow(inv[1],2) ; desireout=inv[0]*inv[1]*( inv[0] + 2.5 )/tempf +uk; /********* find the mean and variance for this data point **********/ w34[L]=desireout; for(i=0;i mean[i][k])/*** find the second nearest meannd ***/ { meannd=10000; for(j=0;jmean[i][k]) { hold=fabs(mean[i][k]-mean[j][k]); if(hold1.99) uk=2.0; if(uk<-1.99) uk=-2.0; /*** THE OUTPUT OF DANAMIC SYSTEM **/ tempf=1.0 + pow(inv[0],2)+pow(inv[1],2) ; desireout=inv[0]*inv[1]*( inv[0] + 2.5 )/tempf +uk; /******** FIND THE BOUND FOR MEMBERSHIP FUNCTION *********/ for(k=0;kmaxtempf) maxtempf=mean[i][k]; if(mean[i][k]maxtempf) inv[k]=maxtempf; } /**************************************************************/ /*** PROPAGATE FOWARD THROUGH SUCCEDING LAYERS *****/ /*** AND CALCULATE OUTPUTS OF EACH LAYER ****************/ /**************************************************************/ /* LAYER 2 */ for(i=0; i mean[rulenum-1][k])/*** find the second nearest meannd ***/ { meannd=10000; for(j=0;jmean[rulenum-1][k]) { hold=fabs(mean[rulenum-1][k]-mean[j][k]); if(hold= 0)) trainning = FALSE; printf("\nIteration = %d\tError = %f\n",pass+1,meanerror[pass]); pass++; L=0; errnow = 0.0; } L++; /**** INPUT VECTOR SHIFP ****/ inv[1]=inv[0]; inv[0]=desireout; } /* end while ( LEARNING PHASE )*/ /******************************************************************/ /***** WRITE RESULTS INTO DATA FILE ************/ if((fp2= fopen(final,"wt")) == NULL) { printf("\aERROR : Cannot open file %s",final); exit(0); } fprintf(fp2,"%d\n",sizeofin); fprintf(fp2,"%d\n",rulenum); for(i=0;imaxtempf) maxtempf=mean[i][k]; if(mean[i][k]maxtempf) inv[k]=maxtempf; } /**************************************************************/ /*** PROPAGATE FOWARD THROUGH SUCCEDING LAYERS ***/ /*** AND CALCULATE OUTPUTS OF EACH LAYER ***/ /**************************************************************/ /* LAYER 2 */ for(i=0; imaxtempf) maxtempf=mean[i][k]; if(mean[i][k]maxtempf) inv[k]=maxtempf; } /**************************************************************/ /*** PROPAGATE FOWARD THROUGH SUCCEDING LAYERS ***/ /*** AND CALCULATE OUTPUTS OF EACH LAYER ***/ /**************************************************************/ /* LAYER 2 */ for(i=0; i