📄 bpnview.cpp
字号:
// BPNView.cpp : implementation of the CBPNView class
//
#include "stdafx.h"
#include "BPN.h"
#include "BPNDoc.h"
#include "BPNView.h"
#include "NetWork.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TopLeftX 30
#define TopLeftY 30
#define BottomRightX 630
#define BottomRightY 430
#define Dist 10
#define MaxY 2.0
#define MinimumError 0.0015
/////////////////////////////////////////////////////////////////////////////
// CBPNView
IMPLEMENT_DYNCREATE(CBPNView, CFormView)
BEGIN_MESSAGE_MAP(CBPNView, CFormView)
//{{AFX_MSG_MAP(CBPNView)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_PAINT()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBPNView construction/destruction
CBPNView::CBPNView()
: CFormView(CBPNView::IDD)
{
//{{AFX_DATA_INIT(CBPNView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CBPNView::~CBPNView()
{
}
void CBPNView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBPNView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CBPNView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CBPNView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CBPNView printing
BOOL CBPNView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CBPNView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CBPNView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CBPNView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CBPNView diagnostics
#ifdef _DEBUG
void CBPNView::AssertValid() const
{
CFormView::AssertValid();
}
void CBPNView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CBPNDoc* CBPNView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBPNDoc)));
return (CBPNDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBPNView message handlers
void CBPNView::OnButton1()
{
// TODO: Add your control notification handler code here
DisplayNeuralWork();
}
void CBPNView::DisplayNeuralWork()
{
NET Net;
BOOL Stop;
double MinTestError;
NetWork Neural;
CDC *dc = GetDC();
dc->MoveTo(TopLeftX, BottomRightY);
int i = 0;
CPen NewPen, *OldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
OldPen = (CPen *)dc->SelectObject(&NewPen);
Neural.InitializeRandoms(); //初始化随机种子
Neural.GenerateNetwork(&Net); //初始化网络,分配空间
Neural.RandomWeights(&Net); //赋初始权值为-0.5到0.5之间的随机数
Neural.InitializeApplication(&Net); //初始化样本
Neural.ReadRecord();
Stop = FALSE;
MinTestError = MAX_REAL;
do {
Neural.TrainNet(&Net);
Neural.TestNet(&Net); //观察误差调整情况
if (Neural.TestError < MinTestError) {
fprintf(Neural.f, " - saving Weights ...");
MinTestError = Neural.TestError;
Neural.SaveWeights(&Net); //保存权值
int Error = BottomRightY - (int)(Neural.TestError*(BottomRightY-TopLeftY)/MaxY);
dc->LineTo(TopLeftX+i*Dist, Error);
i += 1;
if (Neural.TestError < MinimumError) {
fprintf(Neural.f, " - stopping Training and restoring Weights ...");
Stop = TRUE;
Neural.RestoreWeights(&Net);
}
}
else if (Neural.TestError > 1.2 * MinTestError) { //振荡反弹1.2倍为停止条件
fprintf(Neural.f, " - stopping Training and restoring Weights ...");
Stop = TRUE;
Neural.RestoreWeights(&Net);
}
} while (!Stop);
dc->SelectObject(OldPen);
ReleaseDC(dc);
Neural.TestNet(&Net); //观察误差调整情况
Neural.EvaluateNet(&Net); //输出预测结果
Neural.FinalizeApplication(&Net); //关闭文件
}
void CBPNView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
//DisplayNeuralWork();
CPen NewPen, *OldPen;
NewPen.CreatePen(PS_SOLID, 2, RGB(162, 162, 162));
OldPen = (CPen *)dc.SelectObject(&NewPen);
dc.TextOut(100, 20, "神经网络程序——误差曲线");
dc.MoveTo(TopLeftX, TopLeftY);
dc.LineTo(TopLeftX, BottomRightY);
dc.LineTo(BottomRightX, BottomRightY);
dc.SetBkMode(TRANSPARENT);
for(int i=0; i<=10; i++)
{
int TextX = (int)((BottomRightX-TopLeftX)*i/Dist) + TopLeftX;
int TextY = BottomRightY;
CString str;
str.Format("%d", (int)((TextX-TopLeftX)/10));
dc.TextOut(TextX, TextY, str);
}
for(i=0; i<=10; i++)
{
int TextX = TopLeftX - 20;
int TextY = (int)((BottomRightY-TopLeftY)*i/Dist) + TopLeftY - 10;
CString str;
str.Format("%.1f", MaxY*(10-i)/10);
dc.TextOut(TextX, TextY, str);
}
dc.SelectObject(OldPen);
ReleaseDC(&dc);
// Do not call CFormView::OnPaint() for painting messages
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -