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

📄 anndlg.cpp

📁 一个bp的VC源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ANNDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ANN.h"
#include "ANNDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CANNDlg dialog

CANNDlg::CANNDlg(CWnd* pParent /*=NULL*/)
: CDialog(CANNDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CANNDlg)
	m_nHideLayerNumber = 0;
	m_nInputLayerNumber = 0;
	m_nMaxTrainTimes = 2000;
	m_nOutputLayerNumber = 0;
	m_nSystemError = 0.0;
	m_nSystemErrorLevel = 0.000001;
	m_nTrainTimes = 0;
	m_nStep = 0.001;
	m_Display = _T("");
	m_strNetWorkPath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_SystemErrorNew	= 1000;
	m_SystemErrorOld	= 1000;
}

void CANNDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CANNDlg)
	DDX_Text(pDX, IDC_HIDE_LAYER_NUMBER, m_nHideLayerNumber);
	DDX_Text(pDX, IDC_INPUT_LAYER_NUMBER, m_nInputLayerNumber);
	DDX_Text(pDX, IDC_MAX_TRAIN_TIMES, m_nMaxTrainTimes);
	DDX_Text(pDX, IDC_OUTPUT_LAYER_NUMBER, m_nOutputLayerNumber);
	DDX_Text(pDX, IDC_SYSTEM_ERROR, m_nSystemError);
	DDX_Text(pDX, IDC_SYSTEM_LEVEL, m_nSystemErrorLevel);
	DDX_Text(pDX, IDC_TRAIN_TIMES, m_nTrainTimes);
	DDX_Text(pDX, IDC_STEP, m_nStep);
	DDX_Text(pDX, IDC_EDIT_DISPLAY, m_Display);
	DDX_Text(pDX, IDC_SAVE_NETWORK, m_strNetWorkPath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CANNDlg, CDialog)
//{{AFX_MSG_MAP(CANNDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_TRAIN, OnButtonTrain)
ON_BN_CLICKED(IDC_BUTTON_SIM, OnButtonSim)
	ON_BN_CLICKED(IDC_SET_SAVEPATH, OnSetSavepath)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CANNDlg message handlers

BOOL CANNDlg::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
	
	GetDlgItem(IDC_BUTTON_SIM)->EnableWindow(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CANNDlg::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 CANNDlg::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();
	}
}

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



void CANNDlg::OnButtonTrain() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_nInputLayerNumber <= 0)
	{
		::MessageBox (this->m_hWnd, _T("输入层数目必须为大于等于1的正整数!"), _T("未完成初始设置"), MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	if(m_nHideLayerNumber <= 0)
	{
		::MessageBox (this->m_hWnd, _T("隐含层数目必须为大于等于1的正整数!"), _T("未完成初始设置"), MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	if(m_nOutputLayerNumber <= 0)
	{
		::MessageBox (this->m_hWnd, _T("输出层数目必须为大于等于1的正整数!"), _T("未完成初始设置"), MB_OK | MB_ICONINFORMATION);
		return;
	}
	static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
	static char BASED_CODE lpszDefExt[] = TEXT("txt");
	CString strFileName;
	CStdioFile dataFile;
	LPCTSTR	lpszFileName = "";
	
	//Create the dialog to load the data
	CFileDialog dlg(TRUE,
		lpszDefExt,
		NULL,
		OFN_READONLY		| 
		OFN_FILEMUSTEXIST   |
		OFN_PATHMUSTEXIST,
		szFilter,
		this);
	
	if(dlg.DoModal () == IDOK)
	{
		strFileName = dlg.GetPathName ();
		strFileName.TrimLeft ();
		strFileName.TrimRight ();
		lpszFileName = (LPCTSTR)strFileName;
		m_matrixDataInput.LoadDataFromFile (strFileName);
		if( m_matrixDataInput.GetMatrixColNumber () != m_nInputLayerNumber+m_nOutputLayerNumber)
		{
			::MessageBox (this->m_hWnd, _T("测试样本列数应为输入层与输出层之和!!!"), _T("错误!"), MB_OK | MB_ICONERROR);
			
			// 释放全局变量所占的内存
			m_matrixSimuNetwork.InitializeZero ();
			m_matrixInputToHideWeight.InitializeZero ();
			m_matrixHideLayerThreshold.InitializeZero ();
			m_matrixHideToOutputWeight.InitializeZero ();
			m_matrixOutputLayerThreshold.InitializeZero ();
			m_matrixDataInput.InitializeZero ();
			m_nInputLayerNumber	= 0;
			m_nHideLayerNumber = 0;
			m_nOutputLayerNumber = 0;
			return;
			
	}
		NetworkInit(m_nInputLayerNumber,
			m_nHideLayerNumber,
			m_nOutputLayerNumber,
			m_matrixDataInput,
			m_matrixInputLayerValue,
			m_matrixInputToHideWeight,
			m_matrixHideLayerThreshold,
			m_matrixHideToOutputWeight,
			m_matrixOutputLayerThreshold
			);
		bool bSuccess = BPtrain(
			m_nInputLayerNumber,
			m_nHideLayerNumber,
			m_nOutputLayerNumber,
			m_SystemErrorOld,
			m_SystemErrorNew,
			m_nSystemErrorLevel,
			m_nSystemError,
			m_nStep,
			m_nMaxTrainTimes,
			m_nTrainTimes,
			IDC_SYSTEM_ERROR,
			IDC_TRAIN_TIMES,
			this->m_hWnd,
			m_matrixDataInput,
			m_matrixInputLayerValue,
			m_matrixInputToHideWeight,
			m_matrixHideLayerThreshold,
			m_matrixHideLayerOutput,
			m_matrixHideToOutputWeight,
			m_matrixOutputLayerOutput,
			m_matrixOutputLayerThreshold);
		if(bSuccess)
		{
			//////////////////////////////////////////////////////////////////////////////
			//						定义存储矩阵格式									//
			//////////////////////////////////////////////////////////////////////////////
			
			if(!(m_strNetWorkPath.IsEmpty ()))
			{
				CString strInputLayerNumber_T = _T("## 样本的输入层的数目: ##\n");
				SaveConstantStringToFile(m_strNetWorkPath, strInputLayerNumber_T);
				
				CString strInputLayerNumber;
				strInputLayerNumber.Format ("%u", m_nInputLayerNumber);
				SaveConstantStringToFile(m_strNetWorkPath, strInputLayerNumber);
				
				CString strHideLayerNumber_T = _T("\n## 样本的隐含层的数目: ##\n");
				SaveConstantStringToFile(m_strNetWorkPath, strHideLayerNumber_T);
				
				CString strHideLayerNumber;
				strHideLayerNumber.Format ("%u", m_nHideLayerNumber);
				SaveConstantStringToFile(m_strNetWorkPath, strHideLayerNumber);
				
				CString strOutputLayerNumber_T = _T("\n## 样本的输出层的数目: ##\n");
				SaveConstantStringToFile(m_strNetWorkPath, strOutputLayerNumber_T);
				
				CString strOutputLayerNumber;
				strOutputLayerNumber.Format ("%u", m_nOutputLayerNumber);
				SaveConstantStringToFile(m_strNetWorkPath, strOutputLayerNumber);
				
				CString strSeparator = _T("\n## 输入层到隐含层的权值矩阵 ##\n");
				SaveConstantStringToFile(m_strNetWorkPath, strSeparator);
				
				// 存储模拟过的输入到隐含层的权值矩阵
				m_matrixInputToHideWeight.SaveDataToFile (m_strNetWorkPath);
				
				strSeparator = _T("\n## 隐含层的阈值矩阵 ##\n");
				SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
				
				// 存储模拟过的隐含层的阈值矩阵
				m_matrixHideLayerThreshold.SaveDataToFile (m_strNetWorkPath);
				
				strSeparator = _T("\n## 隐含层到输出层的权值矩阵 ##\n");
				SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
				
				// 存储模拟过的隐含层到输出层的权值矩阵
				m_matrixHideToOutputWeight.SaveDataToFile (m_strNetWorkPath);
				
				strSeparator = _T("\n## 输出层的阈值矩阵 ##\n");
				SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
				
				// 存储模拟过的输出层的阈值矩阵
				m_matrixOutputLayerThreshold.SaveDataToFile (m_strNetWorkPath);
			
				// 这部分全局变量如果没有设置保存路径就不释放了
				// 已设置路径就是放掉
				m_matrixInputToHideWeight.InitializeZero ();
				m_matrixHideLayerThreshold.InitializeZero ();
				m_matrixHideToOutputWeight.InitializeZero ();
				m_matrixOutputLayerThreshold.InitializeZero ();	
				// ReInitialize the global value
				m_nInputLayerNumber = 0;
				m_nHideLayerNumber = 0;

⌨️ 快捷键说明

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