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

📄 ann.txt

📁 神经网络拟合
💻 TXT
字号:
// ann.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ann.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinApp theApp;

using namespace std;

#define INPUTN 20
#define OUTPUTN 1
#define MIDN 30
#define POPUSIZE 1000
#define DATANUM 40
#define E 2.71828

double inputarray[80][1301];
double T[DATANUM][OUTPUTN];
void readfile(){
	int i=0,j=0;
	double data;
	char com;
	char fn[20];
	int mod=0;
	double sum=0.0;

	FILE *file;
	int ii=0;
	for(i=0;i<DATANUM+40;i++,ii++)
	{
		if(|i==1||i==2 ||i==3||i==4||i==5||i==7||i==9||i==10
		   ||i==11||i==13|i==15||i==16||i==18||i==19||i==22
			||i==23||i==26||i==27||i==28||i==29||i==31||i==35||i==38||i==39||i==41
			||i==42||i==44||i==45|i==52||i==58||i==65||i==67
			||i==68||i==72||i==73||i==75||i==76||i==77||i==78||i==80)
		{ii--;
			continue;
		}
		mod=0;
		sprintf(fn,"%i.asc",i+1);
		file=fopen(fn,"r");
		if(file==NULL) {
			printf("%i  ",i+1);
			continue;
		}
		int k=0;
		k=0;
		for(j=1;j<1301;j++){
			fscanf(file," %lg%c",&data,&com);
		
			fscanf(file," %lg",&data);
			if(j>950&& j<=970)
			{sum+=data;
			mod++;
			k++;
			if(mod==1){
				inputarray[ii][k] = data ;//sum;
				mod=0;
				sum=0;

			}
			}
		}
		
	    fclose(file);
	}
}
void readT(){

	int j=0;
	double data;
	
	char fn[20];
	

	FILE *file;
	sprintf(fn,"T.txt");
	file=fopen(fn,"r");
	for(j=0;j<DATANUM;j++){
		fscanf(file," %lg",&data);
		T[j][0] = data;
	}
	fclose(file);
	
}

void readfile(double ff,double * array){
	
	int j=0;
	int da;
	char fn[20];
	FILE *file;
	sprintf(fn,"bdata.txt");
	file=fopen(fn,"r");
	
	fscanf(file," %i",&da);
	fscanf(file," %i",&da);
	fscanf(file," %i",&da);
	
	fscanf(file," %lg",&ff);
	for(j=0;j<INPUTN * MIDN + MIDN * OUTPUTN + MIDN + OUTPUTN;j++){
		fscanf(file," %lg",&array[j]);
	}
	
	fclose(file);
}
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
class ANN;


class InputNode{
public:
	ANN *ann;
	double val;
    InputNode(){
		val=0.0;
	};
};
class MiddleNode{
public:
	ANN *ann;
    double val;
	double w0;
	double wights[INPUTN];
	
	double function();
	double compute();
	MiddleNode(){
		val=0.0;
	};
};
class OutputNode{
public:	
	ANN *ann;
	double val;
	double w0;
	double wights[MIDN];

	double compute();
};
class ANN{
public:
	InputNode input[INPUTN];
	MiddleNode middle[MIDN];
	OutputNode output[OUTPUTN];
	void compute();
	void initialize(double *in);
	void setwights(double *w);
	void getwights(double *w);
	void getval(double *val);
};

double MiddleNode::compute(){
	int i=0;
	double s=-w0;
	double y=0.0;
	for(i=0;i<INPUTN;i++)
	{
		s-=wights[i]*(ann->input[i].val);
	}
	s*=5.0;
	y=1.0+pow(E,s);
	val=1.0/y;
	return val;


}

double OutputNode::compute(){
	int i=0;
	double s=w0;
	
	for(i=0;i<MIDN;i++)
	{
		s+=wights[i]*(ann->middle[i].val);
	}
	val=s;
	return val;
}


void ANN::compute(){
	int i=0;
	for(i=0;i<MIDN;i++){
		middle[i].compute();
	}
	for(i=0;i<OUTPUTN;i++)
	{
		output[i].compute();
	}
}

void ANN::initialize(double *in){
	//read input val    
	
	
	int i=0;
	for(i=0;i<INPUTN;i++)
	{
		input[i].ann=this;
		input[i].val=in[i];
	}
	for(i=0;i<MIDN;i++)
	{
		middle[i].ann=this;
		
	}
	for(i=0;i<OUTPUTN;i++)
	{
		output[i].ann=this;
		
	}


}

void ANN::getwights(double *w){
	
	
	
}
void ANN::setwights(double *w){
	int i=0,j=0,c=0;

	for(i=0;i<MIDN;i++){
		middle[i].w0=w[c];
		c++;
		for(j=0;j<INPUTN;j++){
			middle[i].wights[j]=w[c];
			c++;
		}
		
	}
	for(i=0;i<OUTPUTN;i++){
		output[i].w0=w[c];
		
		c++;
		for(j=0;j<MIDN;j++){
			output[i].wights[j]=w[c];
			c++;
		}
	}

}
void ANN::getval(double *val){
	int i=0;
	for(i=0;i<OUTPUTN;i++)
	{
		val[i]=output[i].val;
	}
}

ANN a;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CString strHello;
		//strHello.LoadString(IDS_HELLO);
		cout << (LPCTSTR)strHello << endl;
	}
	readfile();
	readT();
int i=0;

double ff=0.0;
double array[INPUTN * MIDN + MIDN * OUTPUTN + MIDN + OUTPUTN];
readfile(ff,array);
a.setwights(array);
double avg=0.0;
for(i=0;i<DATANUM;i++){
		a.initialize(inputarray[i]);
		a.compute();
		double val[1];
		a.getval(val);
		cout<<"样本"<<i+1<<"  计算浓度:"<<val[0]<<"   误差:"<<(val[0]-T[i][0])/T[i][0]*100<<"%\n";
		avg+=abs((val[0]-T[i][0])/T[i][0]*100);
		//cout<<val[0]<<"\t";
}
cout<<"平均误差"<<avg/40.0<<"\n";

cout<<"数据中未选择的样本为:6 8 12 14 17 20 21 24 25 30 32 33 34 36 37 40 43 46 47 48 49 50 51 53 54 55 56 57 59 60 61 62 63 64 66 69 70 71 74 79\n";
	cin >> i;
	return nRetCode;
}


⌨️ 快捷键说明

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