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

📄 testdoc.cpp

📁 用C++写的函数逼近的例子!可以设置步长
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// testDoc.cpp : implementation of the CTestDoc class
//

#include "stdafx.h"
#include "test.h"

#include "testDoc.h"
#include "DataInput.h"
#include "ValueInput.h"
#include "MyTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestDoc

IMPLEMENT_DYNCREATE(CTestDoc, CDocument)

BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
	//{{AFX_MSG_MAP(CTestDoc)
	ON_COMMAND(ID_MENU_LOAD, OnMenuLoad)
	ON_COMMAND(ID_MENU_LEARN, OnMenuLearn)
	ON_COMMAND(ID_MENU_NEWFILE, OnMenuNewfile)
	ON_COMMAND(ID_MENU_TEST, OnMenuTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDoc construction/destruction

CTestDoc::CTestDoc()
{
	// TODO: add one-time construction code here
	file_loaded = FALSE;
	data_learned = FALSE;
	can_learn = FALSE;
	progress = FALSE;

	max_error_tollerance = 0.0;
	learning_rate = 0.0;
	original_learning_rate = 0.0;
	last_original_rate = 0.0;
	alpha = 0.0;
	max_step = 0;
	dispmode = 0;
	ytemp = 0;
	ztemp = 0;
	total = 0;
	old_e = 0.0;
	total_e = 0.0;
	last_old_e = 0.0;
	delta_e = 0.0;
	min_e = 0.0;
	increase_length = 0.0;
	decrease_length = 0.0;

	input = NULL;
	hidden = NULL;
	output = NULL;
	gamma = 0;
	target = NULL;
	bias = NULL;
	old_bias = NULL;
	last_bias = NULL;
	last_old_bias = NULL;
	weight_i_h = NULL;
	weight_h_o = NULL;
	old_weight_ih = NULL;
	last_weight_ih = NULL;
	last_old_weight_ih = NULL;
	old_weight_ho = NULL;
	last_weight_ho = NULL;
	last_old_weight_ho = NULL;
	errorsignal_hidden = NULL;
	errorsignal_output = NULL;
	delta_error_output = NULL;
	last_delta_error_output = NULL;
	delta_error_hidden = NULL;
	last_delta_error_hidden = NULL;
	delta_weight_ho = NULL;
	last_delta_weight_ho = NULL;
	delta_weight_ih = NULL;
	last_delta_weight_ih = NULL;
}

CTestDoc::~CTestDoc()
{
	if (file_loaded)
	{
		clear_memory();
	}
}

BOOL CTestDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CTestDoc serialization

void CTestDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTestDoc diagnostics

#ifdef _DEBUG
void CTestDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CTestDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestDoc commands

void CTestDoc::OnMenuLoad() 
{
	// TODO: Add your command handler code here
    char * szFilter = "Text Files (.txt)|*.txt|All Files (.*)|*.*||";
 	CFileDialog dlg_file(TRUE,"txt",
						 NULL,
		                 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
						 szFilter);
	dlg_file.m_ofn.lpstrTitle = "读入数据";
	if (dlg_file.DoModal()==IDOK)
	{
		if (file_loaded)
		{
			clear_memory();
		}
		file_loaded = TRUE;
		
		ifstream in(dlg_file.GetPathName(),ios::nocreate|ios::in);
		if (in.fail()){::AfxMessageBox("No such file or path is wrong"); exit(1);}
		
		TRACE("\nfilename=%s\n",dlg_file.GetPathName());
		data_learned = FALSE;
		can_learn = TRUE;
	
		in >> input_array_size;
		in >> hidden_array_size;
		in >> output_array_size;
		in >> original_learning_rate;
		in >> number_of_input_patterns;
		TRACE("\n\nparameter=%d,%d,%d,%f,%d\n\n",input_array_size,
										   hidden_array_size,
										   output_array_size,
										   learning_rate,
										   number_of_input_patterns);
		bias_array_size = hidden_array_size + output_array_size; 

		initialize_net();//initialize the memory

		int x,y;
		for(x=0; x<bias_array_size; x++) 
		{
			in >> original_bias[x];
		}
		for(x=0; x<input_array_size; x++) 
		{ 
			for(y=0; y<hidden_array_size; y++)
			{
				in >> original_weight_ih[x][y];
			}
		}
		for(x=0; x<hidden_array_size; x++) 
		{ 
			for(y=0; y<output_array_size; y++)
			{
				in >> original_weight_ho[x][y];
			}
		}
		for(x=0; x<number_of_input_patterns; x++) 
		{
			for(y=0; y<input_array_size; y++) 
			{
				in >> input[x][y];
				TRACE("input[%d][%d]=%.0f\n",x,y,input[x][y]);
			}
		}
		for(x=0; x<number_of_input_patterns; x++)
		{
			for(y=0; y<output_array_size; y++)
			{
				in >> target[x][y];
				TRACE("target[%d][%d]=%.0f\n",x,y,target[x][y]);
			}
		}
		in.close();
		::AfxMessageBox("Data loaded");
	}
}

void CTestDoc::initialize_net()
{
	int x;
	input = new double* [number_of_input_patterns];
	if(!input) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<number_of_input_patterns; x++)
	{
		input[x] = new double [input_array_size];
		if(!input[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	//hidden = new double [hidden_array_size];
	hidden = new double* [number_of_input_patterns];
	if(!hidden) { ::AfxMessageBox("memory problem!"); exit(1); }
	for (x=0; x<number_of_input_patterns; x++)
	{
		hidden[x] = new double [hidden_array_size];
		if(!hidden[x]) {::AfxMessageBox("memory problem!"); exit(1); }
	}

	output = new double* [number_of_input_patterns];
	if(!output) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<number_of_input_patterns; x++)
	{
		output[x] = new double [output_array_size];
		if(!output[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	gamma = new double* [number_of_input_patterns];
	if(!gamma) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<number_of_input_patterns; x++)
	{
		gamma[x] = new double [output_array_size];
		if(!gamma[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	target = new double* [number_of_input_patterns];
	if(!target) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<number_of_input_patterns; x++)
	{
		target[x] = new double [output_array_size];
		if(!target[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	bias = new double [bias_array_size];
	if(!bias) { ::AfxMessageBox("memory problem!"); exit(1); }

	original_bias = new double [bias_array_size];
	if(!original_bias) { ::AfxMessageBox("memory problem!"); exit(1); }

	old_bias = new double [bias_array_size];
	if(!old_bias) { ::AfxMessageBox("memory problem!"); exit(1); }

	last_bias = new double [bias_array_size];
	if(!last_bias) { ::AfxMessageBox("memory problem!"); exit(1); }

	last_old_bias = new double [bias_array_size];
	if(!last_old_bias) { ::AfxMessageBox("memory problem!"); exit(1); }

	weight_i_h = new double* [input_array_size];
	if(!weight_i_h) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		weight_i_h[x] = new double [hidden_array_size];
		if(!weight_i_h[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	original_weight_ih = new double* [input_array_size];
	if(!original_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		original_weight_ih[x] = new double [hidden_array_size];
		if(!original_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	old_weight_ih = new double* [hidden_array_size];
	if(!old_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		old_weight_ih[x] = new double [hidden_array_size];
		if(!old_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	last_weight_ih = new double* [hidden_array_size];
	if(!last_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		last_weight_ih[x] = new double [hidden_array_size];
		if(!last_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	last_old_weight_ih = new double* [hidden_array_size];
	if(!last_old_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		last_old_weight_ih[x] = new double [hidden_array_size];
		if(!last_old_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	weight_h_o = new double* [hidden_array_size];
	if(!weight_h_o) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		weight_h_o[x] = new double [output_array_size];
		if(!weight_h_o[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	original_weight_ho = new double* [hidden_array_size];
	if(!original_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		original_weight_ho[x] = new double [output_array_size];
		if(!original_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}
	
	old_weight_ho = new double* [hidden_array_size];
	if(!old_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		old_weight_ho[x] = new double [output_array_size];
		if(!old_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}
	
	last_weight_ho = new double* [hidden_array_size];
	if(!last_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		last_weight_ho[x] = new double [output_array_size];
		if(!last_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	last_old_weight_ho = new double* [hidden_array_size];
	if(!last_old_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		last_old_weight_ho[x] = new double [output_array_size];
		if(!last_old_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	errorsignal_hidden = new double [hidden_array_size];
	if(!errorsignal_hidden) { ::AfxMessageBox("memory problem!"); exit(1); }

	errorsignal_output = new double [output_array_size];
	if(!errorsignal_output) { ::AfxMessageBox("memory problem!"); exit(1); }

	delta_error_output = new double [output_array_size];
	if (!delta_error_output){::AfxMessageBox("memory problem"); exit(1);}

	last_delta_error_output = new double [output_array_size];
	if (!last_delta_error_output){::AfxMessageBox("memory problem"); exit(1);}

	delta_error_hidden = new double [hidden_array_size];
	if (!delta_error_hidden){::AfxMessageBox("memory problem"); exit(1);}

	last_delta_error_hidden = new double [hidden_array_size];
	if (!last_delta_error_hidden){::AfxMessageBox("memory problem"); exit(1);}
	
	delta_weight_ih = new double* [input_array_size];
	if(!delta_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		delta_weight_ih[x] = new double [hidden_array_size];
		if(!delta_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	last_delta_weight_ih = new double* [input_array_size];
	if(!last_delta_weight_ih) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<input_array_size; x++)
	{
		last_delta_weight_ih[x] = new double [hidden_array_size];
		if(!last_delta_weight_ih[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}
	
	delta_weight_ho = new double* [hidden_array_size];
	if(!delta_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		delta_weight_ho[x] = new double [output_array_size];
		if(!delta_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	last_delta_weight_ho = new double* [hidden_array_size];
	if(!last_delta_weight_ho) { ::AfxMessageBox("memory problem!"); exit(1); }
	for(x=0; x<hidden_array_size; x++)
	{
		last_delta_weight_ho[x] = new double [output_array_size];
		if(!last_delta_weight_ho[x]) { ::AfxMessageBox("memory problem!"); exit(1); }
	}

	return;
}

void CTestDoc::clear_memory()
{	
	int x;
	for(x=0; x<number_of_input_patterns; x++)
	{
		delete [] input[x];
	}
	delete [] input;

	for (x=0; x<number_of_input_patterns; x++)
	{
		delete [] hidden[x];
	}
	delete [] hidden;

	for(x=0; x<number_of_input_patterns; x++)
	{
		delete [] output[x];
	}
	delete [] output;

	for(x=0; x<number_of_input_patterns; x++)
	{
		delete [] gamma[x];
	}
	delete [] gamma;

	for(x=0; x<number_of_input_patterns; x++)
	{
		delete [] target[x];
	}
	delete [] target;

	delete [] bias;
	 
	delete [] original_bias;

	delete [] old_bias;

	delete [] last_bias;
	
	delete [] last_old_bias;

	for(x=0; x<input_array_size; x++)
	{
		delete [] weight_i_h[x];

⌨️ 快捷键说明

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