⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnbp.cpp

📁 BP神经网络,可以进行任意发酵过程建模,包括初始化,训练与测试,
💻 CPP
字号:
#include"stdafx.h"
#include <iostream.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "nnbp.h"
#define true 1
#define false 0
#define OK 1
#define ERROR 0
//........................................
void Init()
{
	for(int s=0;s<BPNN_SN;s++)
		for(int i=0;i<BPNN_IN;i++)
		{	W_S_I[s][i]=(rand()*0.02-0.01)/10000;
		Yita_S_I[s][i]=learnxl;
        TD0_I[s][i]=1.0;
		Thita_I[i]=(rand()*0.02-0.01)/10000;
		}
		for(int i=0;i<BPNN_IN;i++)
			for(int j=0;j<BPNN_HN;j++)
			{
				W_I_H[i][j]=(rand()*0.02-0.01)/10000;
				Yita_I_H[i][j]=learnxl;
				TD0_H[i][j]=1.0;
				Thita_H[j]=(rand()*0.02-0.01)/10000;
			}
			for(int j=0;j<BPNN_HN;j++)
				for(int k=0;k<BPNN_ON;k++)
				{
					W_H_O[j][k]=(rand()*0.02-0.01)/10000;
					Yita_H_O[j][k]=learnxl;
					TD0_O[j][k]=1.0;
					Thita_O[k]=(rand()*0.02-0.01)/10000;
				}
}
//....................................
void Read()
{
    int temp10;
	if(mxxz)
      temp10=BPNN_SN;
	else
		temp10=BPNN_SN;
    FILE *gpInputFile;
    recordnumber=0;
	float f;
	gpInputFile=fopen(strInputFileName,"rb");
	while(!feof(gpInputFile))
	{
		for(int i=0;i<temp10;i++)
		{
			fscanf(gpInputFile,"%f,",&f);
			getData[recordnumber][i]=f;
		}
		recordnumber++;
	}
	fclose(gpInputFile);
}
//..............................
void Sample()
{
	int temp=0;
	for(int j=0;j<BPNN_SN;j++)
	{
		lym_max[j]=0.0;
		lym_min[j]=100.0;
		for(int i=0;i<recordnumber;i++)
		{
			if(getData[i][j]>lym_max[j])
				lym_max[j]=getData[i][j];
			if(getData[i][j]<lym_min[j])
				lym_min[j]=getData[i][j];
		}
	}
	for(int i=0;i<recordnumber;i++)
		for(int j=0;j<BPNN_SN;j++)
		{			
			s[i][j]=(getData[i][j]-lym_min[j])/(lym_max[j]-lym_min[j]);
		}
		temp=recordnumber-1;
		recordnumber=0;
		for(Count=0;Count<temp;Count++)
			if(s[Count+1][0]!=s[0][0])   
			{
			
				for(int j=0;j<(BPNN_SN+BPNN_ON);j++)
				{
					if(j<BPNN_SN)
						in_ygval[j][recordnumber]=s[Count][j];
					else
						out_ygval[j-BPNN_SN][recordnumber]=s[Count+1][YGXH];
				}
				recordnumber++;
			}
			
}
//.................................
void CeLiangSample()
{
	int temp=0;
	for(int j=0;j<BPNN_SN+6;j++)
	{
		lym_max[j]=0.0;
		lym_min[j]=100.0;
		for(int i=0;i<recordnumber;i++)
		{
			if(getData[i][j]>lym_max[j])
				lym_max[j]=getData[i][j];
			if(getData[i][j]<lym_min[j])
				lym_min[j]=getData[i][j];
		}
	}
	for(int i=0;i<recordnumber;i++)
		for(int j=0;j<BPNN_SN+6;j++)
		{			
			s[i][j]=(getData[i][j]-lym_min[j]-0.1)/(lym_max[j]-lym_min[j]+0.1);
		}
		temp=recordnumber;
		recordnumber=0;
		for(Count=0;Count<temp;Count++)
   //	if(s[Count+1][0]!=s[0][0])   
			{
			
				for(int j=0;j<(BPNN_SN+6);j++)
				{
					if(j<CLQD)
						in_clval[j][recordnumber]=s[Count][j];
					if(j==YGXH)
						out_clval[0][recordnumber]=s[Count][j];
				}
				recordnumber++;
			}
///	模型输入:u=[t;pH;DO;pH_1;DO_1;DO_2]T,全部为在线可测参数。t,pH,DO,VV分别为K时刻的发酵时间/小时、pH、DO/%、搅拌速率的平方/rpm2;pH_1,DO_1分别表示K-1时刻的pH,DO;DO_2表示K-2时刻的DO。
//模型输出:残糖模型输出为K时刻的残糖估计值,菌体模型输出为K时刻的菌体估计值,产物模型输出为K时刻的产物估计值。
		
}
//..................................
void data()
{
	learnnum=0;
	testnum=0;
	for(Count=0;Count<recordnumber;Count++)
		if(int(Count/(BPNN_SUM-1))!=1)
		{	
			for(int s=0;s<BPNN_SN;s++)
				learnin[learnnum][s]=in_ygval[s][Count];
			for(int k=0;k<BPNN_ON;k++)
				learnout[learnnum][k]=out_ygval[k][Count];
			learnnum++;
		}
		else 
		{
			
			for(int s=0;s<BPNN_SN;s++)
				testin[testnum][s]=in_ygval[s][Count];
			for(int k=0;k<BPNN_ON;k++)
				testout[testnum][k]=out_ygval[k][Count];
			testnum++;
		}
}
//软测量数据处理
void CeLiangdata()
{

    learnnum=0;
	testnum=0;
	for(Count=0;Count<recordnumber;Count++)
		if(int(Count/BPNN_SUM)!=0)
		{	
			for(int s=0;s<BPNN_SN;s++)
				learnin[learnnum][s]=in_clval[s][Count];
			for(int k=0;k<BPNN_ON;k++)
				learnout[learnnum][k]=out_clval[k][Count];
			learnnum++;
		}
		else 
		{
			for(int s=0;s<BPNN_SN;s++)
				testin[testnum][s]=in_clval[s][Count];
			for(int k=0;k<BPNN_ON;k++)
				testout[testnum][k]=out_clval[k][Count];
			testnum++;
		}
}

//...................................................
void Output()
{
	FILE *lymOutputFile;
	lymOutputFile=fopen(strOutputFileName,"wb");
	//..................................................
	fprintf(lymOutputFile,"%s","W_S_I");
    fprintf(lymOutputFile,"%c",13);
    fprintf(lymOutputFile,"%c",10);
	for(int s=0;s<BPNN_SN;s++)
	{
		for(int i=0;i<BPNN_IN;i++)
			fprintf(lymOutputFile,"%f,",W_S_I[s][i]);
		fprintf(lymOutputFile,"%c",13);
		fprintf(lymOutputFile,"%c",10);
	}
	
	//..................................................
	//.......................................................
	fprintf(lymOutputFile,"%s","W_I_H");
    fprintf(lymOutputFile,"%c",13);
    fprintf(lymOutputFile,"%c",10);
	for(int i=0;i<BPNN_IN;i++)
	{
		for(int j=0;j<BPNN_HN;j++)
			fprintf(lymOutputFile,"%f,",W_I_H[i][j]);
		fprintf(lymOutputFile,"%c",13);
		fprintf(lymOutputFile,"%c",10);
	}
	//.................................................
	fprintf(lymOutputFile,"%s","W_H_O");
	fprintf(lymOutputFile,"%c",13);
    fprintf(lymOutputFile,"%c",10);
	for(int j=0;j<BPNN_HN;j++)
	{
		for(int k=0;k<BPNN_ON;k++)
			fprintf(lymOutputFile,"%f,",W_H_O[j][k]);
		fprintf(lymOutputFile,"%c",13);
		fprintf(lymOutputFile,"%c",10);			
	}
	//..................................................
	fprintf(lymOutputFile,"%s","Thita_I");
    fprintf(lymOutputFile,"%c",13);
    fprintf(lymOutputFile,"%c",10);
	for(i=0;i<BPNN_IN;i++)
		fprintf(lymOutputFile,"%f,",Thita_I[i]);
	fprintf(lymOutputFile,"%c",13);
	fprintf(lymOutputFile,"%c",10);
	
	//........................................................
    fprintf(lymOutputFile,"%s","Thita_H");
    fprintf(lymOutputFile,"%c",13);
    fprintf(lymOutputFile,"%c",10);
	for(j=0;j<BPNN_HN;j++)
		fprintf(lymOutputFile,"%f,",Thita_H[j]);
	fprintf(lymOutputFile,"%c",13);
	fprintf(lymOutputFile,"%c",10);
	//................................................
	fprintf(lymOutputFile,"%s","Thita_O");
	fprintf(lymOutputFile,"%c",13);
	fprintf(lymOutputFile,"%c",10);
	for(int K=0;K<BPNN_ON;K++)
		fprintf(lymOutputFile,"%f,",Thita_O[K]);
	fprintf(lymOutputFile,"%c",13);
	fprintf(lymOutputFile,"%c",10);
	fclose(lymOutputFile);
}
//.......................................................
void ForwardSI()
{
	double sum;
	for (int i=0;i<BPNN_IN;i++)
	{
		sum=0.0;
		for(int s=0;s<BPNN_SN;s++)
		{
			sum+=learnin[Count][s]*W_S_I[s][i];		
		}
		sum=sum-Thita_I[i];
		I[i][Count]=1/(1+exp(-sum));
	}		
}
////////////////////////////////////////////
void ForwardIH()
{
	double sum;
	for (int j=0;j<BPNN_HN;j++)
	{
		sum=0.0;
		for(int i=0;i<BPNN_IN;i++)
		{
			sum+=I[i][Count]*W_I_H[i][j];		
		}
		sum=sum-Thita_H[j];
		H[j][Count]=1/(1+exp(-sum));
	}	
}

void ForwardHO()
{
	double sum;
	for (int k=0;k<BPNN_ON;k++)
	{
		sum=0.0;
		for(int j=0;j<BPNN_HN;j++)
		{
			sum+=H[j][Count]*W_H_O[j][k];		
		}
		sum=sum-Thita_O[k];
		O[k][Count]=1/(1+exp(-sum));
	}	
}
////////////////////////////////////////////////////
void Counterror()
{  double temp;
error[Count]=0.0; 
for (int k=0;k<BPNN_ON;k++)
{
	temp=(O[k][Count]-learnout[Count][k])*(O[k][Count]-learnout[Count][k]);
	error[Count]+=temp;
}
error[Count]=error[Count]*0.5;
}
void totalerror()
{
	learnerror=0.0;
	for(int Count=0;Count<learnnum;Count++)
		learnerror+=error[Count];
}
bool  Compare()
{
	if(learnerror<E||number>BPNN_GDZ) 
	       return true;
	else
	       return false;
}
//...................
void BackDlta_O()
{
	for(int k=0;k<BPNN_ON;k++)
	{
		Dlta_O[k][Count]=O[k][Count]*(1.0-O[k][Count])*(learnout[Count][k]-O[k][Count]);
	}
}
void BackDlta_H()
{
	double sum;
	for (int j=0;j<BPNN_HN;j++)
	{ sum=0.0;
	for(int k=0;k<BPNN_ON;k++)
	{
		sum+=W_H_O[j][k]*Dlta_O[k][Count];
	}
	Dlta_H[j][Count]=H[j][Count]*(1.0-H[j][Count])*sum;
	}
}
void BackDlta_I()
{
	double sum;
	for (int i=0;i<BPNN_IN;i++)
	{
		sum=0.0;
		for(int j=0;j<BPNN_HN;j++)
		{
			sum+=W_I_H[i][j]*Dlta_H[j][Count];
		}
		Dlta_I[i][Count]=I[i][Count]*(1.0-I[i][Count])*sum;
	}
}
////////////////////////////////////////////////////////
//计算梯度值
//;
void BackTD_O_H()
{
	double  temp001;
	for(int j=0;j<BPNN_HN;j++)
		for(int k=0;k<BPNN_ON;k++)
		{  
			TD1_O[j][k]=0.0;
			for(int p=0;p<learnnum;p++)
			{
				temp001=Dlta_O[k][p]*H[j][p];
				TD1_O[j][k]+=temp001;
			}
		}
}
//..............................................
void BackTD_H_I()
{
	double  temp002;
	for(int i=0;i<BPNN_IN;i++)
		for(int j=0;j<BPNN_HN;j++)
		{  
			TD1_H[i][j]=0.0;
			for(int p=0;p<learnnum;p++)
			{
				temp002=Dlta_H[j][p]*I[i][p];
				TD1_H[i][j]+=temp002;
			}
		}
}
void BackTD_I_S()
{
  	 double  temp003;
	 for(int s=0;s<BPNN_SN;s++)
		 for(int i=0;i<BPNN_IN;i++)
		 {  
			 TD1_I[s][i]=0.0;
			 for(int p=0;p<learnnum;p++)
			 {
				 temp003=Dlta_I[i][p]*learnin[p][s];
				 TD1_I[s][i]+=temp003;
			 }
		 }
}	
/////////////////////////////////////////////////////////////
//计算学习效率
void BackYita_O_H()
{
	double lamdaoh=0.0;
	for(int j=0;j<BPNN_HN;j++)
		for(int k=0;k<BPNN_ON;k++)
		{
			lamdaoh=TD0_O[j][k]*TD1_O[j][k];
			if(lamdaoh>=0)  Yita_H_O[j][k]=(1.0+0.5/number)*Yita_H_O[j][k];
			else Yita_H_O[j][k]=(1.0-0.1/number)*Yita_H_O[j][k];
		//if(errorb<errora)Yita_H_O[j][k]=1.0*Yita_H_O[j][k];
			//if(errorb>1.04*errora)Yita_H_O[j][k]=0.95*Yita_H_O[j][k];
		}
}

//.................................
void BackYita_H_I()
{
	double lamdaoh=0.0;
	for(int j=0;j<BPNN_IN;j++)
		for(int k=0;k<BPNN_HN;k++)
		{
			lamdaoh=TD0_H[j][k]*TD1_H[j][k];
		
			if(lamdaoh>=0)  Yita_I_H[j][k]=(1.0+0.5/number)*Yita_I_H[j][k];
			else Yita_I_H[j][k]=(1.0-0.1/number)*Yita_I_H[j][k];
		
			//if(errorb<errora)Yita_I_H[j][k]=1.0*Yita_I_H[j][k];
			//if(errorb>1.04*errora)Yita_I_H[j][k]=0.95*Yita_I_H[j][k];
			
		}
}
void BackYita_I_S()
{
	double lamdaoh;
	for(int s=0;s<BPNN_SN;s++)
		for(int i=0;i<BPNN_IN;i++)
		{
			lamdaoh=TD0_I[s][i]*TD1_I[s][i];
		
			if(lamdaoh>=0)  Yita_S_I[s][i]=(1.0+0.5/number)*Yita_I_H[s][i];
			else Yita_S_I[s][i]=(1.0-0.1/number)*Yita_S_I[s][i];
		
			//if(errorb<errora) Yita_S_I[s][i]=1.0*Yita_I_H[s][i];
            //if(errorb>1.04*errora) Yita_S_I[s][i]=0.95*Yita_I_H[s][i];
		}
}
//.....................修正的权与阀值
///////////////////////////////////////////////////////
void BackW_O_H()
{
	for(int j=0;j<BPNN_HN;j++)
		for(int k=0;k<BPNN_ON;k++)
		{
			W_H_O[j][k]+=Yita_H_O[j][k]*(1.0-Alpha)*TD1_O[j][k]+Alpha*TD0_O[j][k];
			TD0_O[j][k]=TD1_O[j][k];
		}
}
void BackThita_O_H()
{
	double  temp1;
	for(int k=0;k<BPNN_ON;k++)
	{
		temp1=0.0;
		for(int p=0;p<learnnum;p++)
		{temp1+=Dlta_O[k][p];}
		Thita_O[k]=Thita_O[k]+0.2*temp1;
	}
}
void BackW_H_I()
{  double temp2;
for(int j=0;j<BPNN_IN;j++)
for(int k=0;k<BPNN_HN;k++)
{
	temp2=Yita_I_H[j][k]*(1.0-Alpha)*TD1_H[j][k]+Alpha*TD0_H[j][k]; 
	W_I_H[j][k]+=temp2;
	TD0_H[j][k]=TD1_H[j][k];
}


}
void BackThita_H_I()
{
	double  tempb;
	for(int j=0;j<BPNN_HN;j++)
	{
		tempb=0.0;
		for(int p=0;p<learnnum;p++)
		{tempb+=Dlta_H[j][p];}
		Thita_H[j]=Thita_H[j]+0.2*tempb;
	}	
}

////////////////////////////////////////////////////
void BackW_I_S()
{  double temp2;
for(int s=0;s<BPNN_SN;s++)
for(int i=0;i<BPNN_IN;i++)
{
	temp2=Yita_I_H[s][i]*(1.0-Alpha)*TD1_I[s][i]+Alpha*TD0_I[s][i];
	W_S_I[s][i]+=temp2;
	TD0_I[s][i]=TD1_I[s][i];
}
}
void BackThita_I_S()
{
	double  tempc;
	for(int i=0;i<BPNN_IN;i++)
	{
		tempc=0.0;
		for(int p=0;p<learnnum;p++)
		{
			tempc+=Dlta_I[i][p];
		}
		Thita_I[i]=Thita_I[i]+0.2*tempc;
	}	
}
//测试函数/////////////////////////////// 
void test()
{
	double sum;
	double temp;
	for(Count=0;Count<testnum;Count++)
	{
		error[Count]=0.0;	
		for (int i=0;i<BPNN_IN;i++)
		{
			sum=0.0;
			for(int s=0;s<BPNN_SN;s++)
			{
				sum+=testin[Count][s]*W_S_I[s][i];		
			}
			sum=sum-Thita_I[i];
			I[i][Count]=1/(1+exp(-sum));
		}		
		void ForwardIH();
		void ForwardHO();
		for (int k=0;k<BPNN_ON;k++)
		{
			temp=(O[k][Count]-testout[Count][k])*(O[k][Count]-testout[Count][k]);
			error[Count]+=temp;
		}
		error[Count]=error[Count]*0.5;
		testerror+=error[Count];
	}//for循环结束
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -