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

📄 testview.cpp

📁 程序实现的是二层BP网络,通过从文件中读入数据来构建网络,同时读入对应的样本进行学习,测试. ε=0.09 变量为max_error_tollerance forward_pass[
💻 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)
	//}}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, bin;
		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("%.0f ",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("输出:%d  实际值:%f",bin,pDoc->output[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);
}

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

⌨️ 快捷键说明

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