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

📄 testview.cpp

📁 C++编写的BP神经网络源程序,标准程序
💻 CPP
字号:
// testView.cpp : implementation of the CTestView class
//

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

#include "testDoc.h"
#include "testView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
	//{{AFX_MSG_MAP(CTestView)
	ON_COMMAND(ID_MENU_DISP, OnMenuDisp)
	ON_WM_KEYDOWN()
	ON_UPDATE_COMMAND_UI(ID_MENU_LEARN, OnUpdateMenuLearn)
	ON_UPDATE_COMMAND_UI(ID_MENU_DISP, OnUpdateMenuDisp)
	ON_UPDATE_COMMAND_UI(ID_MENU_TEST, OnUpdateMenuTest)
	ON_COMMAND(ID_MENU_SHOW, OnMenuShow)
	ON_UPDATE_COMMAND_UI(ID_MENU_SHOW, OnUpdateMenuShow)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
	// TODO: add construction code here
}

CTestView::~CTestView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CRect rect;
	GetClientRect(&rect);

	int dm = pDoc->dispmode;
	if (dm ==1)
	{
		TEXTMETRIC tm;
		pDC->GetTextMetrics(&tm);
		double error = pDoc->max_error_tollerance;
		int ptn = pDoc->selpattern;
		TRACE("\nPTN=%d\n",ptn);
		int outnum = pDoc->output_array_size;
		int innum = pDoc->input_array_size;
		TRACE("\ninnum=%d\n",innum);
		int nLineHeight = tm.tmHeight + tm.tmExternalLeading;
		CString str1,str2;
		CPoint pText(0,0);

		str1.Format("输入:");
		for (int y=0; y<innum; y++)
		{
			str2.Format("%f ",pDoc->input[ptn][y]);
			TRACE("pDoc->input[%d][%d]=%.0f\n",ptn,y,pDoc->input[ptn][y]);
			str1 += str2;
		}
		pDC->TextOut(pText.x,pText.y,str1);
		pText.y += nLineHeight;

		for (int i=0; i<outnum; i++)
		{
		/*	if ((1-pDoc->output[ptn][i])<error)
			{
				bin = 1;
			}
			else
			{
				if (pDoc->output[ptn][i]<error)
				{
					bin = 0;
				}
				else
				{
					bin = -1;
				}
		}*/
			str2.Format("输出:%f  实际值:%f",pDoc->output[ptn][i],pDoc->target[ptn][i]);
			pDC->TextOut(pText.x, pText.y+i*nLineHeight,str2);
		}
	}
	if (dm == 2)
	{
		pDC->DrawText("Learning... (Press a key will stop it!)",-1,&rect,
						DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	if (dm == 0)
	{
		pDC->DrawText("",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

void CTestView::OnMenuDisp() 
{
	// TODO: Add your command handler code here
	//CTestDoc* pDoc = GetDocument();
	//ASSERT_VALID(pDoc);

    double min = 0,
		   max = 0, 
		   delta = 0;
	int num = 0;

	ifstream in(BIASNUM_FILE);
	if (!in)
	{
		::AfxMessageBox("Fail to load file");
		return;
	}

	in >> num;
	in.close();

	ifstream in_e(BIAS_FILE);
	if (!in_e)
	{
		::AfxMessageBox("Fail to load file");
		return;
	}
	double *e_value = new double [num];
	for (int i=0;i<num;i++)
	{
		in_e >> e_value[i];
		if (e_value[i] > max) max = e_value[i];
	}
	min = e_value[num-1];
	delta = max-min;
	in_e.close();
	
	for (i=0;i<num;i++)
	{
		e_value[i] = (e_value[i]-min)/delta;
	}
	CClientDC dc(this);
	CRect rect;
	GetClientRect(&rect);
	int nWidth = rect.Width();
	int nHeight = rect.Height();
	int space_h = 100, space_w = 0;
	int div = nWidth / nHeight;
	space_w = space_h * div;
    rect.DeflateRect(space_w,space_h);
	nWidth = rect.Width();
	nHeight = rect.Height();

	int intvl_h = rect.Height()/5;
	int intvl_w = rect.Width()/50;
	
	CPen pen1(PS_DOT,1,RGB(128,128,128)),pen2(PS_SOLID,1,RGB(255,0,0));
	CPen *pOldPen = dc.SelectObject(&pen1);

	dc.Rectangle(rect.left,rect.top,rect.right,rect.bottom);

	for (i=0; i<50; i++)
	{
		dc.MoveTo(rect.left+i*intvl_w, rect.top);
		dc.LineTo(rect.left+i*intvl_w, rect.bottom);
	}
	for (i=0; i<5; i++)
	{
		dc.MoveTo(rect.left, rect.top+i*intvl_h);
		dc.LineTo(rect.right, rect.top+i*intvl_h);
	}
	
	dc.SelectObject(&pen2);
	
	CPoint *aPoint = new CPoint [num];
	if(!aPoint)::AfxMessageBox("Memory error");
	for (i=0;i<num;i++)
	{
		aPoint[i].x = (i*nWidth)/num;
		aPoint[i].y = (int)(-e_value[i]*nHeight);
	}
	
	dc.SetViewportOrg(rect.left, rect.bottom);
	dc.Polyline(aPoint, num);
	dc.SelectObject(pOldPen);

	delete [] e_value;
	delete [] aPoint;

}

void CTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CTestDoc *pDoc = GetDocument();
	pDoc->notkeyhit = FALSE;
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CTestView::OnUpdateMenuLearn(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->can_learn);
}

void CTestView::OnUpdateMenuDisp(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->data_learned);
	//pCmdUI->Enable(true);
}

void CTestView::OnUpdateMenuTest(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->data_learned);
}

void CTestView::OnMenuShow() 
{
	// TODO: Add your command handler code here
	double min = 0,
		   max = 0, 
		   delta = 0;
	int num = 1000;

//----------------forword_pass() begin---------------------------
    CTestDoc * pDoc = GetDocument();
	double  inputTemp,
		   
		    outputTemp;
	
	/*inputTemp = new double  [num];
	
	hiddenTemp = new double  [pDoc->hidden_array_size];
	

	outputTemp = new double  [num];*/
	
	


	CClientDC dc(this);
	CRect rect;
	GetClientRect(&rect);
	int nWidth = rect.Width();
	int nHeight = rect.Height();
	int space_h = 100, space_w = 0;
	int div = nWidth / nHeight;
	space_w = space_h * div;
    rect.DeflateRect(space_w,space_h);
	nWidth = rect.Width();
	nHeight = rect.Height();

	int intvl_h = rect.Height()/5;
	int intvl_w = rect.Width()/50;
	
	CPen pen1(PS_DOT,1,RGB(128,128,128)),pen2(PS_SOLID,1,RGB(255,0,0));
	CPen *pOldPen = dc.SelectObject(&pen1);

	dc.Rectangle(rect.left,rect.top,rect.right,rect.bottom);

	for (int i=0; i<50; i++)
	{
		dc.MoveTo(rect.left+i*intvl_w, rect.top);
		dc.LineTo(rect.left+i*intvl_w, rect.bottom);
	}
	for (i=0; i<5; i++)
	{
		dc.MoveTo(rect.left, rect.top+i*intvl_h);
		dc.LineTo(rect.right, rect.top+i*intvl_h);
	}
	
	dc.SelectObject(&pen2);
	CPoint *aPoint = new CPoint [num];
	if(!aPoint)::AfxMessageBox("Memory error");
	double pi=3.1415926535897932;
	for (i=0;i<num;i++)
	{
		aPoint[i].x = (i*nWidth)/num;
		aPoint[i].y = (int)(-(nWidth/pi)*sin((i*pi)/num));
	}
	
	dc.SetViewportOrg(rect.left, rect.bottom);
	dc.Polyline(aPoint, num);
    
	CPen pen3(PS_SOLID,1,RGB(0,0,255));
	dc.SelectObject(&pen3);
	
	for (i=0;i<num;i++)
	{
		inputTemp=(i*pi)/num;
    	outputTemp=usingnet(inputTemp);
		aPoint[i].x = (i*nWidth)/num;
		aPoint[i].y = (int)(-(nWidth/pi)*outputTemp);
	}
	dc.SetViewportOrg(rect.left, rect.bottom);
	dc.Polyline(aPoint, num);

	dc.SelectObject(pOldPen);

	//delete [] e_value;
	delete [] aPoint;
//-------------clear memery----------------
   /* TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	int nLineHeight = tm.tmHeight + tm.tmExternalLeading;
	CString str1,str2;
	CPoint pText(0,500);
	str1.Format("红线是实际sin函数");
	str2.Format("蓝线是拟合的sin函数");
    pDC->TextOut(pText.x,pText.y,str1);
	pDC->TextOut(pText.x,pText.y+nLineHeight,str2);
	*/
	

	
	
}

void CTestView::OnUpdateMenuShow(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->data_learned);
	//pCmdUI->Enable(true);
}

double CTestView::usingnet(double input)//由神经网络返回sin()函数的值
{
	CTestDoc * pDoc = GetDocument();
    _control87 (MCW_EM, MCW_EM);
	register double temp=0;
    register int x,y;
	double  hiddenTemp[15],
		   outputTemp;
//	hiddenTemp=new double [pDoc->hidden_array_size];
// INPUT -> HIDDEN
	for(y=0; y<pDoc->hidden_array_size; y++) {
		for(x=0; x<pDoc->input_array_size; x++) {
			temp += (input * pDoc->weight_i_h[x][y]); //∑Wi*Xi
		}
		hiddenTemp[y] = (1.0 / (1.0 + exp(-1.0 * (temp + pDoc->bias[y]))));//1/(1+e^(-uj)); uj=∑Wi*Xi - θj
		temp = 0;                                                //各隐层结点的输出
	}
// HIDDEN -> OUTPUT
	for(y=0; y<pDoc->output_array_size; y++) {	//有output_array_size个输出结点
		for(x=0; x<pDoc->hidden_array_size; x++) {
			temp += (hiddenTemp[x] * pDoc->weight_h_o[x][y]);
		}
		outputTemp = (1.0 / (1.0 + exp(-1.0 * (temp + pDoc->bias[y + pDoc->hidden_array_size]))));
		temp = 0;
	}
	
//	delete [] hiddenTemp;
	return outputTemp;
}

⌨️ 快捷键说明

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