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

📄 classpage2.cpp

📁 我以前写的一个的神经网络学习函数逼近和分类的例子
💻 CPP
字号:
// ClassPage2.cpp : implementation file
//

#include "stdafx.h"
#include "neuronetsample.h"
#include "ClassPage2.h"
#include "NeuroNetwork.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClassPage2 property page

IMPLEMENT_DYNCREATE(CClassPage2, CPropertyPage)

CClassPage2::CClassPage2() : CPropertyPage(CClassPage2::IDD)
{
	//{{AFX_DATA_INIT(CClassPage2)
	m_dLearnFactor = 0.05;
	m_iTimes = 5;
	m_iSamples = 50;
	m_iOverlap = 0;
	//}}AFX_DATA_INIT

	m_pClass1 =	NULL;
	m_pClass2 = NULL;
	m_pOut1 = NULL;
	m_pOut2 = NULL;


	//m_iHidLayNodNum = 5;
//	m_pLayNodNum = new int [3];
//	m_pLayNodNum[0] = 2;
//	m_pLayNodNum[1] = m_iHidLayNodNum;
//	m_pLayNodNum[2] = 2;
	m_pNet = new CNeuroNetIT(2, 2, m_dLearnFactor);

	m_bReset = FALSE;
	m_bCleared = TRUE;

}

CClassPage2::~CClassPage2()
{
	int i;
	
	if (m_pClass1 != NULL)
	{
		for (i = 0; i < m_iSamples; i++)
		{
			delete [] m_pClass1[i];
		}
		delete [] m_pClass1;
	}

	if (m_pClass2 != NULL)
	{
		for (i = 0; i < m_iSamples; i++)
		{
			delete [] m_pClass2[i];
		}
		delete [] m_pClass2;
	}

	delete [] m_pOut1;
	delete [] m_pOut2;
	//delete [] m_pLayNodNum;
	delete m_pNet;
}

void CClassPage2::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassPage2)
	DDX_Control(pDX, IDC_SPIN_OVERLAP, m_spinOverlap);
	DDX_Control(pDX, IDC_SPIN_SAMPLE, m_spinSamples);
	DDX_Control(pDX, IDC_SPIN_TIMES, m_spinTimes);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDC_BUTTON_CLEAR, m_btnClear);
	DDX_Control(pDX, IDC_STATIC_PIC, m_pic);
	DDX_Text(pDX, IDC_EDIT_LEARNFACTOR, m_dLearnFactor);
	DDV_MinMaxDouble(pDX, m_dLearnFactor, 1.e-003, 1.);
	DDX_Text(pDX, IDC_EDIT_TIMES, m_iTimes);
	DDV_MinMaxInt(pDX, m_iTimes, 0, 10000);
	DDX_Text(pDX, IDC_EDIT_SAMPLE, m_iSamples);
	DDV_MinMaxInt(pDX, m_iSamples, 2, 200);
	DDX_Text(pDX, IDC_EDIT_OVERLAP, m_iOverlap);
	DDV_MinMaxInt(pDX, m_iOverlap, 0, 9);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClassPage2, CPropertyPage)
	//{{AFX_MSG_MAP(CClassPage2)
	ON_BN_CLICKED(IDC_BUTTON_DATA, OnButtonData)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
	ON_BN_CLICKED(IDC_BUTTON_TRAIN, OnButtonTrain)
	ON_WM_CREATE()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassPage2 message handlers

void CClassPage2::OnButtonData() 
{
	// TODO: Add your control notification handler code here
	//ClientToScreen(&rect);
	
	int i;

	if (m_pClass1 != NULL)
	{
		for (i = 0; i < m_iSamples; i++)
		{
			delete [] m_pClass1[i];
		}
		delete [] m_pClass1;
	}

	if (m_pClass2 != NULL)
	{
		for (i = 0; i < m_iSamples; i++)
		{
			delete [] m_pClass2[i];
		}
		delete [] m_pClass2;
	}

	if (m_pOut1 != NULL)
	{
		delete [] m_pOut1;
	}

	if (m_pOut2 != NULL)
	{
		delete [] m_pOut2;
	}

	UpdateData(TRUE);

	m_pClass1 = new double* [m_iSamples];
	for (i = 0; i < m_iSamples; i++)
		m_pClass1[i] = new double [2];

	m_pClass2 = new double* [m_iSamples];
	for (i = 0; i < m_iSamples; i++)
		m_pClass2[i] = new double [2];


	srand((unsigned)time( NULL ));

	double drand;
	for (i = 0; i < m_iSamples; i++)
	{
	//	m_pClass1[i] = new int [2];
		drand = rand();
		m_pClass1[i][0] = drand/RAND_MAX*1.0/2.0 * (m_iOverlap+9.0)/9.0;
		drand = rand();
		m_pClass1[i][1] = drand/RAND_MAX*1.0/2.0 * (m_iOverlap+9.0)/9.0;
	}
	
	//m_pClass2 = new int* [m_iSamples];
	for (i = 0; i < m_iSamples; i++)
	{
	//	m_pClass2[i] = new int [2];
		drand = rand();
		m_pClass2[i][0] = 1 - drand/RAND_MAX*1.0/2.0 * (m_iOverlap+9.0)/9.0;
		drand = rand();
		m_pClass2[i][1] = 1 - drand/RAND_MAX*1.0/2.0 * (m_iOverlap+9.0)/9.0;
	}

	m_pOut1 = new int [m_iSamples];
	m_pOut2 = new int [m_iSamples];

	//m_btnClear.SendMessage(BM_CLICK, 0, 0);
	//PaintData();

	m_bReset = TRUE;
	m_bCleared = FALSE;

	//UpdateWindow();
	ClearPaint();
	PaintData();

}



void CClassPage2::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	ClearPaint();
	m_bCleared = TRUE;
}

void CClassPage2::OnButtonReset() 
{
	// TODO: Add your control notification handler code here
	m_bReset = TRUE;
	ClearPaint();
	//UpdateWindow();		//send WM_PAINT message for redrawing the data 
	PaintData();
}

void CClassPage2::OnButtonTrain() 
{
	// TODO: Add your control notification handler code here
	if (m_pClass1 == NULL)
		return;

	if (!UpdateData(TRUE))
		return;

	//preparing data
	if (m_bReset)
	{
		delete m_pNet;
		m_pNet = new CNeuroNetIT(2, 2, m_dLearnFactor);
	}
	m_pNet->SetLearnFactor(m_dLearnFactor);

	//training	
	int iTimes = m_iTimes;
	int t, i;	
	m_Progress.SetRange32(1, m_iSamples * iTimes);
	m_Progress.SetStep(1);

	//double pExpOut1[2] = {1,0};
	//double pExpOut2[2] = {0,1};
	double pIV[2];
	for (t = 0; t < iTimes; t++)
	{
		for (i = 0; i < m_iSamples; i++)
		{
			pIV[0] = m_pClass1[i][0];
			pIV[1] = m_pClass1[i][1];
			//m_pNet->NormalizeVector(pIV, 2);

			m_pNet->SetInput(pIV);//m_pClass1[i]);
			//m_pNet->SetExpOutput(pExpOut1);
			if (!m_pNet->Train(1))
				return;

			pIV[0] = m_pClass2[i][0];
			pIV[1] = m_pClass2[i][1];
			//m_pNet->NormalizeVector(pIV, 2);
			m_pNet->SetInput(pIV);//m_pClass2[i]);
			//m_pNet->SetExpOutput(pExpOut2);
			if (!m_pNet->Train(1))
				return;
			m_Progress.StepIt();
		}
	}
	m_Progress.SetPos(0);

	for (i = 0; i < m_iSamples; i++)
	{
		m_pNet->SetInput(m_pClass1[i]);
		m_pNet->ForwardCalc();
		m_pOut1[i] = m_pNet->GetOutput();
	}
	for (i = 0; i < m_iSamples; i++)
	{
		m_pNet->SetInput(m_pClass2[i]);
		m_pNet->ForwardCalc();
		m_pOut2[i] = m_pNet->GetOutput();
	}

	//PaintClass();

	m_bReset = FALSE;
	m_bCleared = FALSE;

	//UpdateWindow();
	ClearPaint();
	PaintData();
	PaintClass();
}

void CClassPage2::PaintData() 
{

	RECT rect;
	m_pic.GetClientRect(&rect);
	int iHeight = rect.bottom;
	int iWidth = rect.right;

	CDC* pDC = m_pic.GetDC();
	CPen cPen;
	LOGPEN stLogPen;
	stLogPen.lopnStyle = PS_SOLID;
	stLogPen.lopnWidth = CPoint(0,0);
	stLogPen.lopnColor = 0x00000000;
	cPen.CreatePenIndirect(&stLogPen);
	CPen* pOldPen = pDC->SelectObject(&cPen);

	int i;
	
	for (i = 0; i < m_iSamples; i++)
	{
		rect.left = m_pClass1[i][0] * iWidth ;
		rect.top = m_pClass1[i][1] * iHeight ;
		rect.right = rect.left + 9;
		rect.bottom = rect.top + 9;
		pDC->Ellipse(&rect);
	}

	for (i = 0; i < m_iSamples; i++)
	{
		rect.left = m_pClass2[i][0] * iWidth;
		rect.top = m_pClass2[i][1] * iHeight;
		rect.right = rect.left - 9;
		rect.bottom = rect.top - 9;
		pDC->Rectangle(&rect);
	}
}


int CClassPage2::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CPropertyPage::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	
	return 0;
}

BOOL CClassPage2::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	RECT rect;

	
	m_spinSamples.GetWindowRect(&rect);
	ScreenToClient(&rect);
	m_spinSamples.DestroyWindow();
	m_spinSamples.Create(WS_VISIBLE|UDS_WRAP|UDS_ARROWKEYS|UDS_SETBUDDYINT, rect, this, IDC_SPIN_SAMPLE);
	m_spinSamples.SetBuddy(GetDlgItem(IDC_EDIT_SAMPLE));
	m_spinSamples.SetRange(1, 1000);

	m_spinOverlap.GetWindowRect(&rect);
	ScreenToClient(&rect);
	m_spinOverlap.DestroyWindow();
	m_spinOverlap.Create(WS_VISIBLE|UDS_WRAP|UDS_ARROWKEYS|UDS_SETBUDDYINT, rect, this, IDC_SPIN_OVERLAP);
	m_spinOverlap.SetBuddy(GetDlgItem(IDC_EDIT_OVERLAP));
	m_spinOverlap.SetRange(0, 9);

	m_spinTimes.GetWindowRect(&rect);
	ScreenToClient(&rect);
	m_spinTimes.DestroyWindow();
	m_spinTimes.Create(WS_VISIBLE|UDS_WRAP|UDS_ARROWKEYS|UDS_SETBUDDYINT, rect, this, IDC_SPIN_TIMES);
	m_spinTimes.SetBuddy(GetDlgItem(IDC_EDIT_TIMES));
	m_spinTimes.SetRange(1, 100);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CClassPage2::PaintClass() 
{
	RECT rect;
	m_pic.GetClientRect(&rect);
	int iHeight = rect.bottom;
	int iWidth = rect.right;

	CDC* pDC = m_pic.GetDC();
	CRgn cRgn;
	CBrush pBrush[2];
	pBrush[0].CreateSolidBrush(0x000000FF);
	pBrush[1].CreateSolidBrush(0x00FF0000);

	int i;
	for (i = 0; i < m_iSamples; i++)
	{
		rect.left = m_pClass1[i][0] * iWidth + 1;
		rect.top = m_pClass1[i][1] * iHeight + 1;
		rect.right = rect.left + 7;
		rect.bottom = rect.top + 7;
		cRgn.DeleteObject();
		cRgn.CreateEllipticRgn(rect.left, rect.top, rect.right, rect.bottom);
		pDC->FillRgn(&cRgn, &pBrush[m_pOut1[i]]);
	}

	for (i = 0; i < m_iSamples; i++)
	{
		rect.left = m_pClass2[i][0] * iWidth - 1;
		rect.top = m_pClass2[i][1] * iHeight - 1;
		rect.right = rect.left - 7;
		rect.bottom = rect.top - 7;
		cRgn.DeleteObject();
		cRgn.CreateEllipticRgn(rect.left, rect.top, rect.right, rect.bottom);
		pDC->FillRgn(&cRgn, &pBrush[m_pOut2[i]]);
	}
}

void CClassPage2::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	//dc.SetViewportOrg(10,10);
		
	RECT rect;
	PAINTSTRUCT PS;

	m_pic.GetWindowRect(&rect);
	PS.hdc = (m_pic.GetDC())->m_hDC;
	PS.fErase = TRUE;
	PS.rcPaint = rect;
	m_pic.BeginPaint(&PS);
	// TODO: Add your message handler code here
	ClearPaint();
	if (!m_bCleared)
	{
		if (m_pClass1 != NULL)
		{
			PaintData();
			if (!m_bReset)
			{
				PaintClass();
			}
		}
	}
	m_pic.EndPaint(&PS);

	// Do not call CPropertyPage::OnPaint() for painting messages
}

void CClassPage2::ClearPaint() 
{
	CDC* pdc = m_pic.GetDC();

	RECT rect;
	m_pic.GetClientRect(&rect);
//	rect.bottom = rect.bottom - rect.top - 5;
//	rect.right = rect.right - rect.left - 5;
//	rect.top = 5;
//	rect.left = 5;

//	int iWidth = rect.right - rect.left;
//	int iHeight = rect.bottom - rect.top;

	pdc->FillSolidRect(&rect, 0x00FFFFFF);
}		

⌨️ 快捷键说明

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