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

📄 annview.cpp

📁 annbp_BP神经网络原代码,经过一些算法改动,希望大家多多支持.
💻 CPP
字号:
// ANNView.cpp : implementation of the CANNView class
//

#include "stdafx.h"
#include "ANN.h"

#include "ANNDoc.h"
#include "ANNView.h"
#include "math.h"

#include "ANNSetDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CANNView

IMPLEMENT_DYNCREATE(CANNView, CFormView)

BEGIN_MESSAGE_MAP(CANNView, CFormView)
	//{{AFX_MSG_MAP(CANNView)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_COMMAND(ID_MODEL_STUDY, OnModelStudy)
	ON_COMMAND(ID_MODEL_SET, OnModelSet)
	ON_COMMAND(ID_MODEL_TEST, OnModelTest)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CANNView construction/destruction

CANNView::CANNView()
	: CFormView(CANNView::IDD)
{
	//{{AFX_DATA_INIT(CANNView)
	m_StudyTimes = 0;
	m_StudyTol = 0.0;
	m_TestTol = 0.0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
}

CANNView::~CANNView()
{
}

void CANNView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CANNView)
	//}}AFX_DATA_MAP
}

BOOL CANNView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CANNView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	/////
	SetTimer(1,1000,0);
}

/////////////////////////////////////////////////////////////////////////////
// CANNView diagnostics

#ifdef _DEBUG
void CANNView::AssertValid() const
{
	CFormView::AssertValid();
}

void CANNView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CANNDoc* CANNView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CANNDoc)));
	return (CANNDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CANNView message handlers

void CANNView::OnButton1() 
{
	FILE *f,*f1;
	srand(GetTickCount());

	f = fopen("训练集.txt","w+");
	for (int i=0;i<20;i++)
	{
		double x[9];
		for(int j=0;j<2;j++)
		{
			x[j] = rand()*3.14159*0.5/RAND_MAX;
			//if (x[j]<1) 	x[j] += 1;
			fprintf(f,"%lf ",x[j]);
		}
		double z = sin(x[0])+log(x[1]+1);
		//double z = 0.4*x[0]+0.6*(1-x[1]);
		//double z = 0.2*exp(x[0])+0.8*exp(x[1]);
		//double z = 0.2*exp(-x[0])+0.8*exp(-x[1]);
		//double z = 0.2*log10(x[0])+0.8*log10(x[1]);
		//double z = 0.2*sin(x[0])+0.8*sin(x[1]);
		//double z = 0.2*x[0]+0.8*x[1];
		//double z = 0.2*x[0]+0.1*x[1]+0.4*x[2]+0.3*x[3];
		//double z = 0.2*x[0]+0.2*x[1]+0.25*x[2]+0.1*x[3]+0.1*x[4]+0.04*x[5]+0.03*x[6]+0.03*x[7]+0.05*x[8];
		fprintf(f," %lf\n",z);
		//double x=rand()*1.0/RAND_MAX;
		//double x=rand()*0.8/RAND_MAX;
		//if(x<1) x+=1;
		//double z=pow(1+0.062,x)*106.7/100;
		//double z=exp(-3*x);
		//double z=pow(1+0.062,x);
		//double z=pow(1+0.062,x*100)*106.7/13126;
		//double z=0.087*x*sin(x);
		//fprintf(f,"%lf %lf\n",x,z);
	}
	fclose(f);

	f1 = fopen("测试集.txt","w+");
	for (i=0;i<10;i++)
	{
		double x[9];
		for(int j=0;j<2;j++)
		{
			x[j] = rand()*3.14159*0.5/RAND_MAX;
			//if (x[j]<1) 	x[j] += 1; 
			fprintf(f1,"%lf ",x[j]);
		}
		double z = sin(x[0])+log(x[1]+1);
		//double z = 0.4*x[0]+0.6*(1-x[1]);
		//double z = 0.2*exp(x[0])+0.8*exp(x[1]);
		//double z = 0.2*exp(-x[0])+0.8*exp(-x[1]);
		//double z = 0.2*log10(x[0])+0.8*log10(x[1]);
		//double z = 0.2*sin(x[0])+0.8*sin(x[1]);
		//double z = 0.2*x[0]+0.8*x[1];
		//double z = 0.2*x[0]+0.1*x[1]+0.4*x[2]+0.3*x[3];
		//double z = 0.2*x[0]+0.2*x[1]+0.25*x[2]+0.1*x[3]+0.1*x[4]+0.04*x[5]+0.03*x[6]+0.03*x[7]+0.05*x[8];
		fprintf(f1," %lf\n",z);
		//double x=rand()*1.0/RAND_MAX;
		//double x=rand()*0.8/RAND_MAX;
		//if(x<1) x+=1;
		//double z=pow(1+0.062,x)*106.7/100;
		//double z=exp(-3*x);
		//double z=pow(1+0.062,x);
		//double z=pow(1+0.062,x*100)*106.7/13126;
		//fprintf(f1,"%lf %lf\n",x,z);
		//double z=0.087*x*sin(x);
		//fprintf(f1,"%lf %lf\n",x,z);
	}
	fclose(f1);
}

void CANNView::OnModelStudy() 
{
	GetDocument()->m_pAnn->Study();
	m_StudyTimes = GetDocument()->m_pAnn->StudyTimes;
	m_StudyTol = GetDocument()->m_pAnn->StudyTol;
}

void CANNView::OnModelSet() 
{
	CANNSetDlg m_SetDlg;
	if(m_SetDlg.DoModal()==IDOK)
	{
		GetDocument()->m_pAnn->TrainingSet = m_SetDlg.m_trainingset;
		GetDocument()->m_pAnn->TestSet = m_SetDlg.m_testset;
		GetDocument()->m_pAnn->TrainingSetNum = m_SetDlg.m_trainingSetNum;
		GetDocument()->m_pAnn->TestSetNum = m_SetDlg.m_testSetNum;

		GetDocument()->m_pAnn->InputNum = m_SetDlg.m_InputNum;
		GetDocument()->m_pAnn->OutputNum = m_SetDlg.m_OutputNum;
		GetDocument()->m_pAnn->HideCoverNum = m_SetDlg.m_HideCoverNum;
		GetDocument()->m_pAnn->HideNum[0] = m_SetDlg.m_InputNum;
		GetDocument()->m_pAnn->OutputFunctionType = 1;

		GetDocument()->m_pAnn->maxStudyTimes = m_SetDlg.m_maxStudyTimes;
		GetDocument()->m_pAnn->rateOfTolChg = m_SetDlg.m_rateOfTolChg;
		GetDocument()->m_pAnn->Tol = m_SetDlg.m_tol;

		GetDocument()->m_pAnn->HideStep = m_SetDlg.m_HideStep;
		GetDocument()->m_pAnn->OutputStep = m_SetDlg.m_OutputStep;
		GetDocument()->m_pAnn->a = m_SetDlg.m_a;
	}
	else
	{
		GetDocument()->m_pAnn->TrainingSet = "训练集.txt";
		GetDocument()->m_pAnn->TrainingSetNum=238;
		GetDocument()->m_pAnn->TestSet = "测试集.txt";
		GetDocument()->m_pAnn->TestOutFile = "测试结果.txt";
		GetDocument()->m_pAnn->TestSetNum=46;

		//GetDocument()->m_pAnn->OutputMax = 1;
		//GetDocument()->m_pAnn->OutputMin = 0;
		GetDocument()->m_pAnn->FuncSlope = 1;

		GetDocument()->m_pAnn->InputNum = 2;
		GetDocument()->m_pAnn->OutputNum = 1;

		GetDocument()->m_pAnn->HideCoverNum = 1;
		GetDocument()->m_pAnn->HideNum[0] = 4;
		GetDocument()->m_pAnn->HideNum[1] = 5;

		GetDocument()->m_pAnn->Tol = 0.0001;
		//0---sigmoid函数;1---线性阈值函数;2---线性多分段函数;3---线性01分段函数;
		GetDocument()->m_pAnn->OutputFunctionType = 0;
		GetDocument()->m_pAnn->HideStep = 0.2;//0.5;//0.12;//0.05;//
		GetDocument()->m_pAnn->OutputStep = 0.2;//0.4;//0.1;//0.02;//
		GetDocument()->m_pAnn->a = 0.8;
		GetDocument()->m_pAnn->maxStudyTimes = 1000;
		GetDocument()->m_pAnn->rateOfTolChg = 0.000001;//0001;
	}

//	Invalidate();
	GetDocument()->m_pAnn->DrawStru();
	GetDocument()->nnType = 0;
}

void CANNView::OnModelTest() 
{
	m_TestTol = GetDocument()->m_pAnn->Test();
	UpdateData(false);
}

void CANNView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	CFormView::OnTimer(nIDEvent);
}

BOOL CANNView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	KillTimer(1);
	
	return CFormView::DestroyWindow();
}


void CANNView::OnDraw(CDC* pDC) 
{
	if (GetDocument()->nnType==0)
		GetDocument()->m_pAnn->DrawStru();
}

⌨️ 快捷键说明

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