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

📄 ganzhidlg.cpp

📁 神经网络感知器做的分类器的源码
💻 CPP
字号:
// ganzhiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ganzhi.h"
#include "ganzhiDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGanzhiDlg dialog

CGanzhiDlg::CGanzhiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGanzhiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGanzhiDlg)
	m_epochs = 200;
	m_eta = 0.75;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CGanzhiDlg::~CGanzhiDlg()
{
	if (m_pSample2 != NULL) 
	{
		for(int i=0; i<m_pointNum; i++)
		{
			delete[] m_pSample2[i];
		}
	}

	if (m_pType2 != NULL) 
	{
		delete m_pType2;
	}
}

void CGanzhiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGanzhiDlg)
	DDX_Text(pDX, IDC_EDIT_EPOCHS, m_epochs);
	DDX_Text(pDX, IDC_EDIT_ETA, m_eta);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGanzhiDlg, CDialog)
	//{{AFX_MSG_MAP(CGanzhiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SELECT, OnBtnSelect)
	ON_BN_CLICKED(IDC_BTN_TEST, OnBtnTest)
	ON_BN_CLICKED(IDC_BTN_TRAIN, OnBtnTrain)
	ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
	ON_BN_CLICKED(IDC_RADIO_RED, OnRadioRed)
	ON_BN_CLICKED(IDC_RADIO_BLUE, OnRadioBlue)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGanzhiDlg message handlers

BOOL CGanzhiDlg::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
	m_bDrawPoint = FALSE;
	m_bTest = FALSE;
	m_bTrain = FALSE;
	m_colour = -1;
	memset(m_pointArray, 0, sizeof(double)*200);
	memset(m_pointType, 0, sizeof(double)*100);
	m_pointNum = 0;

	CWnd *pWnd;
	CRect rect;
    pWnd = GetDlgItem(IDC_DRAWMAP);   
	pWnd->GetWindowRect(&rect);
	m_point.x = rect.left;
	m_point.y = rect.top - 20;
	m_point2.x = rect.right;
	m_point2.y = rect.bottom - 20;
	m_line_start.x = 0;
	m_line_start.y = 0;
	m_line_end.x = 0;
	m_line_end.y = 0;
	m_pType2 = NULL;
	m_pSample2 = NULL;

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

void CGanzhiDlg::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 CGanzhiDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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();
	}

	if (m_pointNum > 0) 
	{
		CDC *pDC = GetDC();
		CPen RedPen(PS_SOLID, 1, RGB(255, 0, 0));
		CPen BlackPen(PS_SOLID, 1, RGB(0, 0, 0));	

		for(int i=0; i<m_pointNum; i++)
		{
			if (m_pointType[i] == -1) 
			{
				pDC->SelectObject(BlackPen);
			}
			else
			{
				pDC->SelectObject(RedPen);	
			}

			pDC->Ellipse((int)(m_pointArray[i][0] + m_point.x - 5), (int)(m_pointArray[i][1] + m_point.y - 5), (int)(m_pointArray[i][0] + m_point.x + 5), (int)(m_pointArray[i][1] + m_point.y + 5));
		}
		ReleaseDC(pDC);
	}

	if(m_bTrain)
	{
		CDC *pDC = GetDC();
		CPen GreenPen(PS_SOLID, 1, RGB(0, 255, 0));
		pDC->SelectObject(GreenPen);
		pDC->MoveTo(m_line_start);
		pDC->LineTo(m_line_end);
		ReleaseDC(pDC);
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGanzhiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGanzhiDlg::OnBtnSelect() 
{
	if (m_colour == -1) 
	{
		MessageBox("Please Choose Color");
		return;
	}

	if (m_bDrawPoint == FALSE) 
	{
		((CButton*)GetDlgItem(IDC_BTN_SELECT))->SetWindowText("Stop Input");
		m_bDrawPoint = TRUE;
	}
	else
	{
		((CButton*)GetDlgItem(IDC_BTN_SELECT))->SetWindowText("Start Input");
		m_bDrawPoint = FALSE;
	}
}

void CGanzhiDlg::OnBtnTest() 
{
	if (m_bTest == FALSE) 
	{
		((CButton*)GetDlgItem(IDC_BTN_TEST))->SetWindowText("Stop Test");
		m_bTest = TRUE;
	}
	else
	{
		((CButton*)GetDlgItem(IDC_BTN_TEST))->SetWindowText("Start Test");
		m_bTest = FALSE;
	}
}

void CGanzhiDlg::OnBtnTrain() 
{
	UpdateData(TRUE);
	
	if (m_pointNum == 0) 
	{
		MessageBox("Please Input First");
		return;
	}

	if(!m_bDrawPoint)
	{
		((CButton*)GetDlgItem(IDC_BTN_TEST))->SetWindowText("Stop Test");
		m_bTest = TRUE;
	}

	if(!m_bTrain)
	{
		((CButton*)GetDlgItem(IDC_BTN_TRAIN)->EnableWindow(FALSE));
		m_bTrain = TRUE;
	}

	m_pSample2 = new double*[m_pointNum];
	m_pType2 = new double[m_pointNum];

	for (int i = 0; i < m_pointNum; i++)
	{
		m_pSample2[i] = new double[2];
	}

	for(int iTemp=0; iTemp<m_pointNum; iTemp++)
	{
		m_pSample2[iTemp][0] = m_pointArray[iTemp][0];
		m_pSample2[iTemp][1] = m_pointArray[iTemp][1];
	}
	for(int m=0; m<m_pointNum; m++)
	{
		m_pType2[m] = m_pointType[m];
	}
	
	pPerception = new CPerception(m_pointNum, 2, m_pSample2, m_pType2, m_epochs, m_eta);
	pPerception->Train();
	
	CString strTemp;
	strTemp.Format("%fX + %fY + %f", (pPerception->GetW()[0]), (pPerception->GetW()[1]), (pPerception->Getb()));
	MessageBox(strTemp.GetBuffer(strTemp.GetLength()));

	CDC *pDC = GetDC();
	CPen pen(PS_SOLID, 1, RGB(0, 255, 0));
	pDC->SelectObject(pen);

	m_line_start.x = m_point.x;
	m_line_start.y = (long)(m_point.y + -pPerception->Getb() / pPerception->GetW()[1]);
	m_line_end.x = m_point2.x;
	m_line_end.y = (long)(-(pPerception->Getb() + (m_point2.x - m_point.x) * pPerception->GetW()[0]) / pPerception->GetW()[1]);
	pDC->MoveTo(m_line_start);
	pDC->LineTo(m_line_end);

}

void CGanzhiDlg::OnBtnClear() 
{
	if (m_pSample2 != NULL) 
	{
		for(int i=0; i<m_pointNum; i++)
		{
			delete[] m_pSample2[i];
		}

		m_pSample2 = NULL;
	}
	
	if (m_pType2 != NULL) 
	{
		delete m_pType2;
		m_pType2 = NULL;
	}

	((CButton*)GetDlgItem(IDC_BTN_TEST))->SetWindowText("Start Test");
	m_bTest = FALSE;
	((CButton*)GetDlgItem(IDC_BTN_SELECT))->SetWindowText("Start Input");
	m_bDrawPoint = FALSE;
	((CButton*)GetDlgItem(IDC_BTN_TRAIN)->EnableWindow(TRUE));
	m_bTrain = FALSE;
	m_pointNum = 0;
	Invalidate();
}

void CGanzhiDlg::OnRadioRed() 
{
	m_colour = 1;
}

void CGanzhiDlg::OnRadioBlue() 
{
	m_colour = 2;
}

void CGanzhiDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (m_bDrawPoint == TRUE && m_bTest == FALSE && m_bTrain == FALSE) 
	{
		if(point.x < m_point.x || point.x > m_point2.x || point.y < m_point.y || point.y > m_point2.y)
			return;

		CPen pen;
		if (m_colour == 1) 
		{
			pen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
			m_pointType[m_pointNum] = 1;
		}
		else if(m_colour == 2)
		{
			pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
			m_pointType[m_pointNum] = -1;
		}
		else
		{
			MessageBox("Please Choose Color!!");
			return;
		}
		
		CDC *pDC = GetDC();
		pDC->SelectObject(pen);
		pDC->Ellipse(point.x - 5, point.y - 5, point.x + 5, point.y + 5);

		m_pointArray[m_pointNum][0] = point.x - m_point.x;
		m_pointArray[m_pointNum][1] = point.y - m_point.y;
		m_pointNum++;

		ReleaseDC(pDC);
	}

	if (m_bTest)
	{
		double temp = 0;
		CString strTemp;
		temp = (pPerception->m_pBias + (point.x - m_point.x) * pPerception->m_pWeight[0] + (point.y - m_point.y) * pPerception->m_pWeight[1]);
		if (temp >= 0)
		{
			strTemp.Format("This sample is in A class--Red");
			MessageBox(strTemp.GetBuffer(strTemp.GetLength()));
		}	
		else
		{
			strTemp.Format("This sample is in B class--Black");
			MessageBox(strTemp.GetBuffer(strTemp.GetLength()));
		}
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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