?? anndlg.cpp
字號:
// 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("輸入層數(shù)目必須為大于等于1的正整數(shù)!"), _T("未完成初始設(shè)置"), MB_OK | MB_ICONINFORMATION);
return;
}
if(m_nHideLayerNumber <= 0)
{
::MessageBox (this->m_hWnd, _T("隱含層數(shù)目必須為大于等于1的正整數(shù)!"), _T("未完成初始設(shè)置"), MB_OK | MB_ICONINFORMATION);
return;
}
if(m_nOutputLayerNumber <= 0)
{
::MessageBox (this->m_hWnd, _T("輸出層數(shù)目必須為大于等于1的正整數(shù)!"), _T("未完成初始設(shè)置"), 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("測試樣本列數(shù)應(yīng)為輸入層與輸出層之和!!!"), _T("錯誤!"), MB_OK | MB_ICONERROR);
// 釋放全局變量所占的內(nèi)存
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("## 樣本的輸入層的數(shù)目: ##\n");
SaveConstantStringToFile(m_strNetWorkPath, strInputLayerNumber_T);
CString strInputLayerNumber;
strInputLayerNumber.Format ("%u", m_nInputLayerNumber);
SaveConstantStringToFile(m_strNetWorkPath, strInputLayerNumber);
CString strHideLayerNumber_T = _T("\n## 樣本的隱含層的數(shù)目: ##\n");
SaveConstantStringToFile(m_strNetWorkPath, strHideLayerNumber_T);
CString strHideLayerNumber;
strHideLayerNumber.Format ("%u", m_nHideLayerNumber);
SaveConstantStringToFile(m_strNetWorkPath, strHideLayerNumber);
CString strOutputLayerNumber_T = _T("\n## 樣本的輸出層的數(shù)目: ##\n");
SaveConstantStringToFile(m_strNetWorkPath, strOutputLayerNumber_T);
CString strOutputLayerNumber;
strOutputLayerNumber.Format ("%u", m_nOutputLayerNumber);
SaveConstantStringToFile(m_strNetWorkPath, strOutputLayerNumber);
CString strSeparator = _T("\n## 輸入層到隱含層的權(quán)值矩陣 ##\n");
SaveConstantStringToFile(m_strNetWorkPath, strSeparator);
// 存儲模擬過的輸入到隱含層的權(quán)值矩陣
m_matrixInputToHideWeight.SaveDataToFile (m_strNetWorkPath);
strSeparator = _T("\n## 隱含層的閾值矩陣 ##\n");
SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
// 存儲模擬過的隱含層的閾值矩陣
m_matrixHideLayerThreshold.SaveDataToFile (m_strNetWorkPath);
strSeparator = _T("\n## 隱含層到輸出層的權(quán)值矩陣 ##\n");
SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
// 存儲模擬過的隱含層到輸出層的權(quán)值矩陣
m_matrixHideToOutputWeight.SaveDataToFile (m_strNetWorkPath);
strSeparator = _T("\n## 輸出層的閾值矩陣 ##\n");
SaveConstantStringToFile(m_strNetWorkPath,strSeparator);
// 存儲模擬過的輸出層的閾值矩陣
m_matrixOutputLayerThreshold.SaveDataToFile (m_strNetWorkPath);
// 這部分全局變量如果沒有設(shè)置保存路徑就不釋放了
// 已設(shè)置路徑就是放掉
m_matrixInputToHideWeight.InitializeZero ();
m_matrixHideLayerThreshold.InitializeZero ();
m_matrixHideToOutputWeight.InitializeZero ();
m_matrixOutputLayerThreshold.InitializeZero ();
// ReInitialize the global value
m_nInputLayerNumber = 0;
m_nHideLayerNumber = 0;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -