📄 modular.c
字号:
//FMNMS.C Fuzzy Modular Network Modelling System for DOS
# include <dos.h>
# include <stdio.h>
# include <math.h>
# include <stdlib.h>
# include <conio.h>
# define M_nSample 100 //最多学习样本数目
# define M_xSample 6 //最多学习样本的维数
# define M_Rule_num 8 //最多规则条数
# define F_Grade 2 //模糊加权指数
# define Max_weight 1 //最大初始权值
void FCM(void); //隶属度学习函数
void EXPERT_NET(void); //专家系统学习函数
void GATING_NET(void); //隶属度生成函数
void PREDICT(void); //预测函数
int i,j,k,l;
int nSample=20; //实际学习样本数
int xSample=3; //实际学习样本维数
int Rule_num; //实际规则条数
long int IT=0; //迭代次数计数变量
long int Cycle_times; //最大迭代次数
float Study_rate; //学习率
float uERROR; //隶属度学习中止误差
float nERROR; //专家网络学习中止误差
float err,Q;
float U[M_nSample][M_Rule_num]; //规则对样本适合度数组
float Z[M_Rule_num][M_xSample]; //规则中心向量数组
float Sample[M_nSample][M_xSample+1]; //学习样本数组
float B[M_xSample][M_Rule_num]; //专家网络权值数组
int main(void)
{
int ch=1,FLAG=0;
FILE *f;
char F_name[20];
while(ch==1)
{
clrscr();
printf("\n--------------MODULAR NETWORK MODELLING SYSTEM--------------\n");
printf("\n");
printf("1. Training the network\n");
printf("2. Save the network\n");
printf("3. Load the network\n");
printf("4. Predict\n");
printf("Q. Exit\n");
ch=2;
while(ch==2)
{
ch=getch();
switch(ch)
{
case '1':
{
printf("%c\n",ch);
sound(1000);
delay(200);
nosound();
clrscr();
printf("\n--------------TRAINING THE NETWORK--------------\n");
printf("\n**Please input filename of learned samples:");
scanf("%12s",F_name);
sound(1000);
delay(200);
nosound();
if((f=fopen(F_name,"r"))==NULL)
{
printf("\n\n Cannot open the file!");
printf("\n\n Press any key to return...");
getch();
clrscr();
ch=1;
break;
}
else
{
//scanf(f,"%d%d",&nSample,&xSample);
for(i=0;i<nSample;i++)
for(j=0;j<xSample;j++)
fscanf(f,"%f",&Sample[i][j]);
fclose(f);
fflush(stdin);
printf("\n**Please input the num of rules:");
scanf("%d",&Rule_num);
sound(1000);
delay(200);
nosound();
fflush(stdin);
printf("\n**Please input the study rate:");
scanf("%f",&Study_rate);
sound(1000);
delay(200);
nosound();
fflush(stdin);
printf("\n**Please input the learning error:");
scanf("%f",&nERROR);
sound(1000);
delay(200);
nosound();
fflush(stdin);
printf("\n**Please input the cycle times:");
scanf("%ld",&Cycle_times);
sound(1000);
delay(200);
nosound();
fflush(stdin);
printf("\n**Please input the membership error:");
scanf("%f",&uERROR);
sound(1000);
delay(200);
nosound();
fflush(stdin);
printf("\n**Please wait......");
FCM();
EXPERT_NET();
FLAG=1;
ch=1;
break;
}
}
case '2':
{
printf("%c\n",ch);
sound(1000);delay(200);nosound();
//clrscr();
//if(FLAG==0)
// {
// ch=1;
// break;
// }
printf("\n--------------SAVE THE NETWORK--------------\n");
printf("\n**Please input the filename of the network:");
scanf("%12s",F_name);
sound(1000);
delay(200);
nosound();
if((f=fopen(F_name,"w"))==NULL)
{
printf("\n\nCannot open the file!");
printf("\n\nPress any key to return...");
getch();
clrscr();
ch=1;
break;
}
else
{
//printf(f,"%d%d\n",xSample,Rule_num);
for(i=0;i<Rule_num;i++);
{
for(j=0;j<xSample-1;j++)
fprintf(f,"%f",Z[i][j]);
fprintf(f,"\n");
}
for(i=0;i<Rule_num;i++)
{
for(j=0;j<xSample;j++)
fprintf(f,"%f",B[i][j]);
fprintf(f,"\n");
}
for(i=0;i<nSample;i++)
{
for(j=0;j<xSample;j++)
fprintf(f,"%f",Sample[i][j]);
fprintf(f,"\n");
}
fprintf(f,"Q=%f,ERROR=%f\n",Q,err);
fclose(f);
FLAG=2;
ch=1;
break;
}
}
case '3':
{
printf("%c\n",ch);
sound(1000);
delay(200);
nosound();
clrscr();
printf("\n--------------LOAD THE NETWORK--------------\n");
printf("\n**Please input the filename of the network:");
scanf("%12s",F_name);
sound(1000);
delay(200);
nosound();
if((f=fopen(F_name,"r"))==NULL)
{
printf("\n\nCannot open the file!");
printf("\n\nPress any key to return...");
getch();
clrscr();
ch=1;
break;
}
else
{
//printf(f,"%d%d",&xSample,&Rule_num);
for(i=0;i<Rule_num;i++);
{
for(j=0;j<xSample-1;j++)
fscanf(f,"%f",&Z[i][j]);
}
for(i=0;i<Rule_num;i++)
{
for(j=0;j<xSample;j++)
fscanf(f,"%f",&B[i][j]);
}
fclose(f);
FLAG=3;
ch=1;
break;
}
}
case '4':
{
printf("%c\n",ch);
sound(1000);
delay(200);
nosound();
clrscr();
printf("\n--------------LOAD THE NETWORK--------------\n");
printf("\n**Please input the filename of test samples:");
scanf("%12s",F_name);
sound(1000);
delay(200);
nosound();
if((f=fopen(F_name,"r"))==NULL)
{
printf("\n\nCannot open the file!");
printf("\n\nPress any key to return...");
getch();
clrscr();
ch=1;
break;
}
else
{
//fscanf(f,"%d%d",&nSample,&xSample);
for(i=0;i<nSample;i++);
for(j=0;j<xSample;j++)
fscanf(f,"%f",&Sample[i][j]);
fclose(f);
GATING_NET();
PREDICT();
FLAG=4;
printf("\n\n**Please input the filename of the result: ");
scanf("%12s",F_name);
sound(1000);delay(200);nosound();
if((f=fopen(F_name,"w"))!=NULL)
{
for(i=0;i<nSample;i++);
{
for(j=0;j<xSample;j++)
fprintf(f,"%f",Sample[i][j]);
fprintf(f,"\n");
}
fprintf(f,"Q=%f\n",Q);
fclose(f);
}
ch=1;
break;
}
}
case 'q':
case 'Q':
{
printf("%c\n",ch);
sound(1000);delay(200);nosound();
ch=0;
break;
}
default:
ch=2;
break;
}
}
}
return 0;
}
void PREDICT(void)
{
float sum;
for(i=0,Q=0.0,err=0.0;i<nSample;i++)
{
for(j=0,Sample[i][xSample]=0.0;j<Rule_num;j++)
{
sum=B[0][j];
for(k=0;k<xSample-1;k++)
{
sum=sum+Sample[i][k]*B[k+1][j];
}
Sample[i][xSample]=Sample[i][xSample]+sum*U[i][j];
}
Q=Q+pow(Sample[i][xSample]-Sample[i][xSample-1],2);
err=err+fabs(Sample[i][xSample]-Sample[i][xSample-1]);
}
err=err/nSample;
sound(1000);delay(200);nosound();
return;
}
void EXPERT_NET(void)
{
float o_err,result,sum,res;
for(i=0;i<xSample;i++)
for(j=0;j<Rule_num;j++)
B[i][j]=random(1000*Max_weight)/1000.0-Max_weight/2.0;
err=1000;IT=0;
while((IT++<Cycle_times)&&(err>nERROR))
{
for(i=0,err=0.0;i<nSample;i++)
{
for(j=0,result=0;j<Rule_num;j++)
{
sum=B[0][j];
for(k=0,res=1;k<xSample-1;k++)
{
sum=sum+Sample[i][k]*B[k+1][j];
res=res+pow(Sample[i][k],2);
}
result=result+sum*U[i][j];
}
o_err=result-Sample[i][xSample-1];
err=err+fabs(o_err);
for(j=0;j<Rule_num;j++)
{
B[0][j]=B[0][j]-Study_rate*o_err*U[i][j]/res;
for(k=1;k<xSample;k++)
B[k][j]=B[k][j]-Study_rate*o_err*U[i][j]*Sample[i][k-1]*Sample[i][k-1]/res;
}
}
}
Q=0.0;
for(i=0;i<nSample;i++)
{
for(j=0,result=0;j<Rule_num;j++)
{
sum=B[0][j];
for(k=0,res=1;k<xSample-1;k++)
{
sum=sum+Sample[i][k]*B[k+1][j];
res=res+pow(Sample[i][k],2);
}
result=result+sum*U[i][j];
}
Sample[i][xSample]=result;
Q=Q+pow((Sample[i][xSample-1]-result),2);
}
sound(1000);delay(200);nosound();
return;
}
void FCM(void)
{
int I[M_Rule_num];
float U_SUM,Z1,Dik,Djk,OLD_U[M_nSample][M_Rule_num];
for(i=0;i<nSample;i++)
{
for(j=0,U_SUM=0.0;j<Rule_num-1;j++)
{
U[i][j]=random(1000-1000*U_SUM)/1000.0;
OLD_U[i][j]=U[i][j];
U_SUM=U_SUM+U[i][j];
}
U[i][j]=1-U_SUM;
OLD_U[i][j]=U[i][j];
}
IT=0;err=100;
while((IT++<Cycle_times)&&(err>uERROR))
{
for(k=0;k<Rule_num;k++)
for(i=0;i<xSample-1;i++)
{
for(j=0,Z1=0.0,Z[k][i]=0.0;j<nSample;j++)
{
Z[k][i]=Z[k][i]+Sample[j][i]*pow(U[j][k],F_Grade);
Z1=Z1+pow(U[j][k],F_Grade);
}
Z[k][i]=Z[k][i]/Z1;
}
for(k=0;k<Rule_num;k++)
for(i=0,I[k]=-1;i<nSample;i++)
{
for(j=0,Dik=0.0;j<xSample-1;j++)
Dik=Dik+pow(Z[k][j]-Sample[i][j],2);
if(Dik<uERROR)
I[k]=i;
}
for(i=0;i<nSample;i++)
for(j=0;j<Rule_num;j++)
{
if(I[j]==-1)
{
for(k=0,U[i][j]=0.0;k<Rule_num;k++)
{
for(l=0,Djk=0.0,Dik=0.0;l<xSample-1;l++)
{
Djk=Djk+(Z[k][l]-Sample[i][l])*(Z[k][l]-Sample[i][l]);
Dik=Dik+(Z[j][l]-Sample[i][l])*(Z[j][l]-Sample[i][l]);
}
U[i][j]=U[i][j]+pow(Dik/Djk,1/(F_Grade-1));
}
U[i][j]=1/U[i][j];
}
else if(I[j]==i)
U[i][j]=1.0;
else
U[i][j]=0.0;
}
for(i=0,err=0.0;i<Rule_num;i++)
for(j=0;j<nSample;j++)
{
err=err+fabs(OLD_U[j][i]-U[j][i]);
OLD_U[j][i]=U[j][i];
}
}
sound(1000);delay(200);nosound();
return;
}
void GATING_NET(void)
{
float Z1, D[M_xSample][M_Rule_num],Djk,Dik;
for(i=0;i<nSample;i++)
{
for(j=0;j<Rule_num;j++)
{
for(k=0,U[i][j]=0.0;k<Rule_num;k++)
{
for(l=0,Djk=0.0,Dik=0.0;l<xSample-1;l++)
{
Djk=Djk+(Z[k][l]-Sample[i][l])*(Z[k][l]-Sample[i][l]);
Dik=Dik+(Z[j][l]-Sample[i][l])*(Z[j][l]-Sample[i][l]);
}
U[i][j]=U[i][j]+pow(Dik/Djk,1/(F_Grade-1));
}
U[i][j]=1/U[i][j];
}
}
sound(1000);delay(200);nosound();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -