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

📄 knnbpdlg.cpp

📁 KNN和BP的算法实现。输入一系列的样本值
💻 CPP
字号:
// KNNBPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "KNNBP.h"
#include "KNNBPDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CKNNBPDlg dialog




CKNNBPDlg::CKNNBPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKNNBPDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKNNBPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CKNNBPDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_KNN, &CKNNBPDlg::OnBnClickedKnn)
	ON_BN_CLICKED(IDC_BP, &CKNNBPDlg::OnBnClickedBp)
END_MESSAGE_MAP()


// CKNNBPDlg message handlers

BOOL CKNNBPDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKNNBPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKNNBPDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
	int data[20][3] = {
		42,  208,  1,
		30, 69, 0,
		69, 12, 0,
		51,  232,  1,
		95,  104,  0,
		196, 75,  0,
		288, 47,   1,
		65,  35,   1,
		239, 115, 1,
		93, 118,  0,
		204, 217,  1,
		79,  45,   1,
		40,  259, 0,
		40,  85,   0,
		118, 216,  1,
		9,   31,   0,
		30, 109, 1,
		41,  50, 1,
		36,  255,  0,
		216, 108,  1
	};
    CButton* pKnn = (CButton*)GetDlgItem(IDC_KNN);
	CButton* pBp = (CButton*)GetDlgItem(IDC_BP);
	//initialization
	SolidBrush brush(Color(255, 255, 255, 255));//white
	Pen pen(Color(255, 0, 0, 0), 1);//black
	pen.SetEndCap(LineCapArrowAnchor);
	Pen redPen(Color(255, 156, 0, 156), 1);//red
	Pen greenPen(Color(255, 50, 156, 150), 1);//green
	SolidBrush redBrush(Color(255, 255, 0, 0));//redpoint
	SolidBrush greenBrush(Color(255, 0, 255, 0));//greenpoint
	CRect rect;
	CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
	CDC* pDC = pWnd->GetDC();
	//pWnd->Invalidate();
	//pWnd->UpdateWindow();
	pWnd->GetWindowRect(rect);
	Graphics graphics(pDC->m_hDC);
	graphics.FillRectangle(&brush, 1, 1, rect.Width()-2, rect.Height()-2);
	graphics.DrawLine(&pen, 10, rect.Height() - 10, 311, rect.Height() - 10);
	graphics.DrawLine(&pen, 10, rect.Height() - 10, 10, rect.Height() - 311);
	//graphics.d
	if(pKnn->GetCheck())
	{
		
		float distance[20];
		int status[20];
		int pstatus[300];
		float temp1;
		int temp2, sum = 0;
		int knn = 5;
		for(int i = 0; i <300; i++)
		{
			for (int j = 0; j < 300; j++)
			{
				for (int k = 0; k < 20; k++)
				{
					distance[k] = (float)sqrt(pow((data[k][0] - i), 2.0) + pow((data[k][1] - j), 2.0));
					status[k] = data[k][2];
				}
				//sort
				for (int m = 0; m < knn; m++)
				{
					for (int n = m + 1; n < 20; n++)
					{
						if(distance[m] > distance[n])
						{
							temp1 = distance[m];
							distance[m] = distance[n];
							distance[n] = temp1;
							temp2 = status[m];
							status[m] = status[n];
							status[n] = temp2;
						}
					}
				}
				sum = 0;
				for (int m = 0; m < knn; m++)
				{
					sum += status[m];
				}
				if(sum > (int)(knn/2))
					pstatus[j] = 1;
				else
					pstatus[j] = 0;
			}
			
			for (int j = 0; j < 300; j++)
			{
				if(pstatus[j] == 1)
				{
					
					graphics.DrawLine(&greenPen, 11+i, rect.Height() - (311-j), 11+i, rect.Height() - (311-(j+1)));
				}
				else
				{
					graphics.DrawLine(&redPen, 11+i, rect.Height() - (311-j), 11+i, rect.Height() - (311-(j+1)));
				}
			}
			
		}
	
		for(int i = 0; i < 20; i++)//20个样本点
		{
			for (int j = 0; j < 2; j++)
			{
				if(data[i][2] == 1)
					graphics.FillEllipse(&greenBrush, 11 + data[i][0], rect.Height() - (311-data[i][1]), 10, 10);
				else
					graphics.FillEllipse(&redBrush, 11 + data[i][0], rect.Height() - (311-data[i][1]), 10, 10);
			}
		}	
	}
	if(pBp->GetCheck())
	{
		//a - learning speed, w - weight, q - threshold, e - error
		double w13[2],w14[2],w23[2],w24[2],w35[2],w45[2],q3[2],q4[2],q5[2],e[20],a =0.1,y3,y4,y5,e3,e4,e5;
		w13[0]=0.5,w14[0]=0.9,w23[0]=0.4,w24[0]=1.0,w35[0]=-1.2,w45[0]=1.1,q3[0]=0.8,q4[0]=-0.1,q5[0]=0.3;
		do 
		{
			for(int i = 0; i < 20; i++)
		    {
				//compute
				y3 = sigmoid(data[i][0]*w13[0]+data[i][1]*w23[0]-q3[0]);
				y4 = sigmoid(data[i][0]*w14[0]+data[i][1]*w24[0]-q4[0]);
				y5 = sigmoid(y3*w35[0]+y4*w45[0]-q5[0]);
				e[i] = data[i][2] - y5;
				e5 = y5*(1-y5)*e[i];
				w35[1] = a*y3*e5;
				w45[1] = a*y4*e5;
				q5[1] = a*(-1)*e5;
				e3 = y3*(1-y3)*e5*w35[0];
				e4 = y4*(1-y4)*e5*w45[0];
				w13[1] = a*data[i][0]*e3;
				w23[1] = a*data[i][1]*e3;
				q3[1] = a*(-1)*e3;
				w14[1] = a*data[i][0]*e4;
				w24[1] = a*data[i][1]*e4;
				q4[1] = a*(-1)*e4;
				//updata
				w13[0] += w13[1];
				w14[0] += w14[1];
				w23[0] += w23[1];
				w24[0] += w24[1];
				w35[0] += w35[1];
				w45[0] += w45[1];
				q3[0] += q3[1];
				q4[0] += q4[1];
				q5[0] += q5[1];
			}
			double temp = error(e);
		} while(error(e) >= 5.0409);//error<0.001
		for(int i = 0; i < 300; i++)
		{
			for (int j = 0; j < 300; j++)
			{
				y3 = sigmoid(i*w13[0]+j*w23[0]-q3[0]);
				y4 = sigmoid(i*w14[0]+j*w24[0]-q4[0]);
				y5 = sigmoid(y3*w35[0]+y4*w45[0]-q5[0]);
				
				if(y5 != 0.55118738802672185)
				{

					graphics.DrawLine(&greenPen, 11+i, rect.Height() - (311-j), 11+i, rect.Height() - (311-(j+1)));
				}
				else
				{
					graphics.DrawLine(&redPen, 11+i, rect.Height() - (311-j), 11+i, rect.Height() - (311-(j+1)));
				}
				
			}
		}

		for(int i = 0; i < 20; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				if(data[i][2] == 1)
					graphics.FillEllipse(&greenBrush, 11 + data[i][0], rect.Height() - (311-data[i][1]), 10, 10);
				else
					graphics.FillEllipse(&redBrush, 11 + data[i][0], rect.Height() - (311-data[i][1]), 10, 10);
			}
		}	
	}
	pWnd->ReleaseDC(pDC);
}
// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKNNBPDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

double CKNNBPDlg::error(double e[])
{
	double result = 0.0;
	for(int i = 0; i < 20; i++)
	{
		result += pow(e[i], 2.0);
	}
	return result; 
}

double CKNNBPDlg::sigmoid(double x)
{
	double result = 0.0, e = 2.71;
	result = 1/(1 + pow(e, (0.0 - x)));
	return result; 
}
void CKNNBPDlg::OnBnClickedKnn()
{
	CRect rect;
	CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
	CDC* pDC = pWnd->GetDC();
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	pWnd->GetWindowRect(rect);
	InvalidateRect(&rect);
	//Invalidate();
	// TODO: Add your control notification handler code here
}

void CKNNBPDlg::OnBnClickedBp()
{
	CRect rect;
	CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
	CDC* pDC = pWnd->GetDC();
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	pWnd->GetWindowRect(rect);
	InvalidateRect(&rect);
	//Invalidate();
	// TODO: Add your control notification handler code here
}

⌨️ 快捷键说明

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