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

📄 bptoxorsview.cpp

📁 一个关于bp网络解决异或问题的动态演示程序
💻 CPP
字号:
// BPtoXorSView.cpp : implementation of the CBPtoXorSView class
//

#include "stdafx.h"
#include "BPtoXorS.h"

#include "BPtoXorSDoc.h"
#include "BPtoXorSView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBPtoXorSView

IMPLEMENT_DYNCREATE(CBPtoXorSView, CView)

BEGIN_MESSAGE_MAP(CBPtoXorSView, CView)
	//{{AFX_MSG_MAP(CBPtoXorSView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBPtoXorSView construction/destruction

CBPtoXorSView::CBPtoXorSView()
{
	// TODO: add construction code here
	for(int j=0; i<300; i++)
	{
		point[j].x = 0;
		point[j].y = 0;
	}
	i = 0; //在不刷新的情况下i=1 否则会出现point[-1]的情况,奇怪也不报错
	origin_y = 0;
	k = 0;
}

CBPtoXorSView::~CBPtoXorSView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBPtoXorSView drawing

void CBPtoXorSView::OnDraw(CDC* pDC)
{
	CBPtoXorSDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//加载图像
	CDC memDC;		//内存设备场景
	CBitmap cb;		//位图类对象
	CRect rect;		//
	GetWindowRect(&rect);
	cb.LoadBitmap(IDB_BITMAP1);		//载入资源
	memDC.CreateCompatibleDC(pDC);	//创建内存设备上下文
	CBitmap *oldbitmap = NULL;
	oldbitmap = memDC.SelectObject(&cb);	//将位图选入当前设备场景
	pDC->BitBlt(350,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);		//将内存场景的图像拷贝到屏幕上
	memDC.SelectObject(oldbitmap);

	
	BPnet net;

	net.train();
	pDC->TextOut(0,0,"动态显示个BP网中的参数:");


	CString s;
	s.Format("%f",net.a);
	pDC->TextOut(0,50,"学习系数 a = "+s);
	s.Format("%f",net.b);
	pDC->TextOut(200,50," b = "+s);

	//输入值
	s.Format("%f",net.input[0]);
	pDC->TextOut(0,70,"输入值 x0 = "+s);
	s.Format("%f",net.input[1]);
	pDC->TextOut(200,70,"x1 = "+s);

	//输入层到中间层的权值
	pDC->TextOut(0,90,"输入层到中间层的权值:");
	s.Format("%f",net.w_middle[0]);
	pDC->TextOut(0,50 + 20 * 3,"w01 = "+s);
	s.Format("%f",net.w_middle[1]);
	pDC->TextOut(200,50 + 20 * 3,"w02 = "+s);
	s.Format("%f",net.w_middle[2]);
	pDC->TextOut(0, 50 + 20 * 4,"w10 = "+s);
	s.Format("%f",net.w_middle[3]);
	pDC->TextOut(200, 50 + 20 * 4,"w11 = "+s);

	//中间层到输出层的权值
	pDC->TextOut(0,50 + 20 * 6,"中间层到输出层的权值:");
	s.Format("%f",net.w_output[0]);
	pDC->TextOut(0,50 + 20 * 7,"t0 = "+s);
	s.Format("%f",net.w_output[1]);
	pDC->TextOut(200,50 + 20 * 7,"t1 = "+s);

	//中间层的阈值
	pDC->TextOut(0,50 + 20 * 9,"中间层的阈值:");
	s.Format("%f",net.threshold_m[0]);
	pDC->TextOut(0,50 + 20 * 10,"h0 = "+s);
	s.Format("%f",net.threshold_m[1]);
	pDC->TextOut(200,50 + 20 * 10,"h1 = "+s);

	//输出层的阈值
	pDC->TextOut(0,50 + 20 * 12,"输出层的阈值:");
	s.Format("%f",net.threshold_o);
	pDC->TextOut(0,50 + 20 * 13,"u0 = "+s);

	//误差
	pDC->TextOut(0,50 + 20 * 15,"输出误差:");
	s.Format("%f",net.error);
	pDC->TextOut(0,50 + 20 * 16,"error = "+s);

	//输出
	pDC->TextOut(0,50 + 20 * 18,"输出:");
	s.Format("%f",net.output_o);
	pDC->TextOut(0,50 + 20 * 19,"output_o = "+s);

	//
	pDC->TextOut(0,50 + 20 * 21,"****************************************");

	CClientDC dc(this);

	CPen mpen;
	mpen.CreatePen(PS_SOLID,1,RGB(255,0,0));
	dc.SelectObject(&mpen);
	
	//y坐标
	dc.MoveTo(350,300);
	dc.LineTo(350,450);
	
	//箭头
	dc.MoveTo(350,300);
	dc.LineTo(340,310);

	dc.MoveTo(350,300);
	dc.LineTo(360,310);

	

	//x坐标
	dc.MoveTo(350,420);
	dc.LineTo(700,420);
	
	//箭头
	dc.MoveTo(700,420);
	dc.LineTo(690,410);
	
	dc.MoveTo(700,420);
	dc.LineTo(690,430);

	
	//输出零点值
	pDC->TextOut(330,420,"0");

	//输出一点值
	pDC->TextOut(330,370,"1");

	//在1值处输出一条线
	dc.MoveTo(350,370);
	dc.LineTo(700,370);
	
	
	origin_y =  ((420.0 - (net.output_o * 50)));

	if(origin_y < 220)		//输出范围限定,在220 ——520之间
		origin_y = 220; 
	if(origin_y > 520)
		origin_y = 520;
	
	if(i<=300)				//算法 very good!!
	{
		point[i].x = (350 + i);
		point[i].y = (long)origin_y;
		
		for(k=0; k<(i-1); k++)
		{
			
			dc.MoveTo(point[k]);	
			dc.LineTo(point[k+1]);
		}
	i++;		//重复时使其在坐标原点输出
	}
	else
	{
		i = 0;
		for(k=0; k<300;k++)
		{
			point[k].x = 0;
			point[k].y = 0;
		}

	}
	



	
	//停0.25秒
	Sleep(250);
	if(net.error>0.1)
		dc.LineTo(point[i-1]);
		CWnd::Invalidate(TRUE);

	DeleteObject(mpen);		//删除所选画笔,好像没用??

	/*//用不刷新试一下 。————坐标擦除不了
	point[0].x = 350;
	point[0].y = 420;
	if(i<=300)
	{
		point[i].x = (350 + i);
		point[i].y = origin_y;
		dc.MoveTo(point[i-1]);	

		dc.LineTo(point[i]);
		i++;	//重复时使其在坐标原点输出

		
	}
	else
	{
		i = 1;
		for(k=0; k<300;k++)
		{
			point[k].x = 0;
			point[k].y = 0;
		}

	}
	


	DeleteObject(mpen);


	
	//停0.15秒
		Sleep(150);
	if(net.error>0.01)
		CWnd::Invalidate(FALSE);*/

	


	


	



	
}

/////////////////////////////////////////////////////////////////////////////
// CBPtoXorSView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBPtoXorSView message handlers

⌨️ 快捷键说明

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