NNBPDlg.cpp
上传用户:guanx8y8
上传日期:2007-07-30
资源大小:326k
文件大小:44k
开发平台:

Visual C++

  1. // NNBPDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "NNBP.h"
  5. #include "NNBPDlg.h"
  6. //***************************************************************************
  7. // Add the user-defined header file
  8. #include "AllDef.h"
  9. //***************************************************************************
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. CButtonST m_btnHelpDocument;
  25. //}}AFX_DATA
  26. // ClassWizard generated virtual function overrides
  27. //{{AFX_VIRTUAL(CAboutDlg)
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  30. //}}AFX_VIRTUAL
  31. // Implementation
  32. protected:
  33. //{{AFX_MSG(CAboutDlg)
  34. virtual BOOL OnInitDialog();
  35. afx_msg void OnHelpdocument();
  36. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  37. //}}AFX_MSG
  38. DECLARE_MESSAGE_MAP()
  39. };
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42. //{{AFX_DATA_INIT(CAboutDlg)
  43. //}}AFX_DATA_INIT
  44. }
  45. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialog::DoDataExchange(pDX);
  48. //{{AFX_DATA_MAP(CAboutDlg)
  49. DDX_Control(pDX, IDC_HELPDOCUMENT, m_btnHelpDocument);
  50. //}}AFX_DATA_MAP
  51. }
  52. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  53. //{{AFX_MSG_MAP(CAboutDlg)
  54. ON_BN_CLICKED(IDC_HELPDOCUMENT, OnHelpdocument)
  55. ON_WM_LBUTTONDOWN()
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CNNBPDlg dialog
  60. CNNBPDlg::CNNBPDlg(CWnd* pParent /*=NULL*/)
  61. : CDialog(CNNBPDlg::IDD, pParent)
  62. {
  63. //{{AFX_DATA_INIT(CNNBPDlg)
  64. m_strDemoDataInput = _T("");
  65. m_strDemoSaveResult = _T("");
  66. m_nInputLayerNumber = 0;
  67. m_nHideLayerNumber = 0;
  68. m_nOutputLayerNumber = 0;
  69. m_nComboFunc = -1;
  70. m_nSystemError = 0.0;
  71. m_nTrainTimes = 0;
  72. m_nSystemErrorLevel = SYSTEM_ERROR_LEVEL;
  73. m_nMaxTrainTimes = LOOP_MAX_TIMES;
  74. m_strDemoSaveNetwork = _T("");
  75. m_strSimuNetworkFold = _T("");
  76. m_strSimuDataInput = _T("");
  77. m_strSimuResult = _T("");
  78. m_nComboArithmetic = -1;
  79. m_nStep = INIT_STEP;
  80. //}}AFX_DATA_INIT
  81. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  82. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  83. // Initialize the member variables
  84. m_bStartFlag = false;
  85. m_bSimulateDataFlag = false;
  86. m_SystemErrorNew = MAX_SYSTEM_ERROR;
  87. m_SystemErrorOld = MAX_SYSTEM_ERROR;
  88. m_hThread = NULL;
  89. }
  90. void CNNBPDlg::DoDataExchange(CDataExchange* pDX)
  91. {
  92. CDialog::DoDataExchange(pDX);
  93. //{{AFX_DATA_MAP(CNNBPDlg)
  94. DDX_Control(pDX, IDC_COMBO_ARITHMETIC, m_ctrlComboArithmetic);
  95. DDX_Control(pDX, IDC_COMBO_FUNC, m_ctrlComboFunc);
  96. DDX_Control(pDX, IDC_STATIC_TITLE, m_staticTextTitle);
  97. DDX_Control(pDX, IDC_STATE, m_btnState);
  98. DDX_Control(pDX, IDC_SIMU_RESULT_BROWSER, m_btnSimuResultBrowser);
  99. DDX_Control(pDX, IDC_SIMU_NETWORK_FOLD_BROWSER, m_btnSimuNetworkFoldBrowser);
  100. DDX_Control(pDX, IDC_SIMU_DATA_INPUT_BROWSER, m_btnSimuDataInputBrowser);
  101. DDX_Control(pDX, IDC_DEMO_SAVE_RESULT_BROWSER, m_btnDemoSaveResultBrowser);
  102. DDX_Control(pDX, IDC_DEMO_SAVE_NETWORK_BROWSER, m_btnDemoSaveNetworkBrowser);
  103. DDX_Control(pDX, IDC_DEMO_DATA_INPUT_BROWSER, m_btnDemoDataInputBrowser);
  104. DDX_Control(pDX, IDC_STOP_TRAIN, m_btnStopTrain);
  105. DDX_Control(pDX, IDC_EXIT, m_btnExit);
  106. DDX_Control(pDX, IDC_ABOUT_US, m_btnAboutUs);
  107. DDX_Control(pDX, IDC_BEGIN_TRAIN, m_btnBeginTrain);
  108. DDX_Text(pDX, IDC_DEMO_DATA_INPUT, m_strDemoDataInput);
  109. DDX_Text(pDX, IDC_DEMO_SAVE_RESULT, m_strDemoSaveResult);
  110. DDX_Text(pDX, IDC_INPUT_LAYER_NUMBER, m_nInputLayerNumber);
  111. DDX_Text(pDX, IDC_HIDE_LAYER_NUMBER, m_nHideLayerNumber);
  112. DDX_Text(pDX, IDC_OUTPUT_LAYER_NUMBER, m_nOutputLayerNumber);
  113. DDX_CBIndex(pDX, IDC_COMBO_FUNC, m_nComboFunc);
  114. DDX_Text(pDX, IDC_SYSTEM_ERROR, m_nSystemError);
  115. DDX_Text(pDX, IDC_TRAIN_TIMES, m_nTrainTimes);
  116. DDX_Text(pDX, IDC_SYSTEM_LEVEL, m_nSystemErrorLevel);
  117. DDX_Text(pDX, IDC_MAX_TRAIN_TIMES, m_nMaxTrainTimes);
  118. DDX_Text(pDX, IDC_DEMO_SAVE_NETWORK, m_strDemoSaveNetwork);
  119. DDX_Text(pDX, IDC_SIMU_NETWORK_FOLD, m_strSimuNetworkFold);
  120. DDX_Text(pDX, IDC_SIMU_DATA_INPUT, m_strSimuDataInput);
  121. DDX_Text(pDX, IDC_SIMU_RESULT, m_strSimuResult);
  122. DDX_CBIndex(pDX, IDC_COMBO_ARITHMETIC, m_nComboArithmetic);
  123. DDX_Text(pDX, IDC_STEP, m_nStep);
  124. //}}AFX_DATA_MAP
  125. }
  126. BEGIN_MESSAGE_MAP(CNNBPDlg, CDialog)
  127. //{{AFX_MSG_MAP(CNNBPDlg)
  128. ON_WM_SYSCOMMAND()
  129. ON_WM_PAINT()
  130. ON_WM_QUERYDRAGICON()
  131. ON_BN_CLICKED(IDC_EXIT, OnExit)
  132. ON_BN_CLICKED(IDC_ABOUTUS, OnAboutus)
  133. ON_WM_TIMER()
  134. ON_BN_CLICKED(IDC_DEMO_DATA_INPUT_BROWSER, OnDemoDataInputBrowser)
  135. ON_BN_CLICKED(IDC_DEMO_SAVE_NETWORK_BROWSER, OnDemoSaveNetworkBrowser)
  136. ON_BN_CLICKED(IDC_DEMO_SAVE_RESULT_BROWSER, OnDemoSaveResultBrowser)
  137. ON_BN_CLICKED(IDC_SIMU_NETWORK_FOLD_BROWSER, OnSimuNetworkFoldBrowser)
  138. ON_BN_CLICKED(IDC_SIMU_DATA_INPUT_BROWSER, OnSimuDataInputBrowser)
  139. ON_BN_CLICKED(IDC_SIMU_RESULT_BROWSER, OnSimuResultBrowser)
  140. ON_BN_CLICKED(IDC_BEGIN_TRAIN, OnBeginTrain)
  141. ON_BN_CLICKED(IDC_STATE, OnState)
  142. ON_BN_CLICKED(IDC_STOP_TRAIN, OnStopTrain)
  143. //}}AFX_MSG_MAP
  144. END_MESSAGE_MAP()
  145. /////////////////////////////////////////////////////////////////////////////
  146. // CNNBPDlg message handlers
  147. BOOL CNNBPDlg::OnInitDialog()
  148. {
  149. CDialog::OnInitDialog();
  150. // Add "About..." menu item to system menu.
  151. // IDM_ABOUTBOX must be in the system command range.
  152. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  153. ASSERT(IDM_ABOUTBOX < 0xF000);
  154. CMenu* pSysMenu = GetSystemMenu(FALSE);
  155. if (pSysMenu != NULL)
  156. {
  157. CString strAboutMenu;
  158. strAboutMenu.LoadString(IDS_ABOUTBOX);
  159. if (!strAboutMenu.IsEmpty())
  160. {
  161. pSysMenu->AppendMenu(MF_SEPARATOR);
  162. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  163. }
  164. }
  165. // Set the icon for this dialog.  The framework does this automatically
  166. //  when the application's main window is not a dialog
  167. SetIcon(m_hIcon, TRUE); // Set big icon
  168. SetIcon(m_hIcon, FALSE); // Set small icon
  169. // TODO: Add extra initialization here
  170. // Set the Graphic Title
  171. {
  172. m_staticTextTitle.Initialise( RGB(160,180,220) );
  173. m_staticTextTitle.SetAlignCentre();
  174. m_staticTextTitle.SetDrawShadow();
  175. CFont font;
  176. font.CreateFont(36,0,0,0,FW_BOLD,0,0,0,GB2312_CHARSET,0,0,0,0,TEXT("黑体"));
  177. LOGFONT logfont;
  178. font.GetLogFont(&logfont);
  179. m_staticTextTitle.SetMainTextLogFont(logfont);
  180. }
  181. // "浏览"按钮的资源的装入
  182. // For Demo Data Input Browser Button
  183. m_btnDemoDataInputBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  184. m_btnDemoDataInputBrowser.SetIcon ( IDI_FILEOPEN);
  185. m_btnDemoDataInputBrowser.SetActiveBgColor (RGB(160,180,220));
  186. m_btnDemoDataInputBrowser.SetInactiveBgColor (RGB(160,180,220));
  187. m_btnDemoDataInputBrowser.AddToolTip(TEXT("浏览"));
  188. // For Demo Save Network Browser Button
  189. m_btnDemoSaveNetworkBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  190. m_btnDemoSaveNetworkBrowser.SetIcon ( IDI_FILEOPEN);
  191. m_btnDemoSaveNetworkBrowser.SetActiveBgColor (RGB(160,180,220));
  192. m_btnDemoSaveNetworkBrowser.SetInactiveBgColor (RGB(160,180,220));
  193. m_btnDemoSaveNetworkBrowser.AddToolTip(TEXT("浏览"));
  194. // For Demo Save Result Browser Button
  195. m_btnDemoSaveResultBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  196. m_btnDemoSaveResultBrowser.SetIcon ( IDI_FILEOPEN);
  197. m_btnDemoSaveResultBrowser.SetActiveBgColor (RGB(160,180,220));
  198. m_btnDemoSaveResultBrowser.SetInactiveBgColor (RGB(160,180,220));
  199. m_btnDemoSaveResultBrowser.AddToolTip(TEXT("浏览"));
  200. // For Simu Data Input Browser Button
  201. m_btnSimuDataInputBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  202. m_btnSimuDataInputBrowser.SetIcon ( IDI_FILEOPEN);
  203. m_btnSimuDataInputBrowser.SetActiveBgColor (RGB(160,180,220));
  204. m_btnSimuDataInputBrowser.SetInactiveBgColor (RGB(160,180,220));
  205. m_btnSimuDataInputBrowser.AddToolTip(TEXT("浏览"));
  206. // For Simu Network Fold Browser Button
  207. m_btnSimuNetworkFoldBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  208. m_btnSimuNetworkFoldBrowser.SetIcon ( IDI_FILEOPEN);
  209. m_btnSimuNetworkFoldBrowser.SetActiveBgColor (RGB(160,180,220));
  210. m_btnSimuNetworkFoldBrowser.SetInactiveBgColor (RGB(160,180,220));
  211. m_btnSimuNetworkFoldBrowser.AddToolTip(TEXT("浏览"));
  212. // For Simu Result Browser Button
  213. m_btnSimuResultBrowser.SubclassDlgItem (IDI_FILEOPEN,this);
  214. m_btnSimuResultBrowser.SetIcon ( IDI_FILEOPEN);
  215. m_btnSimuResultBrowser.SetActiveBgColor (RGB(160,180,220));
  216. m_btnSimuResultBrowser.SetInactiveBgColor (RGB(160,180,220));
  217. m_btnSimuResultBrowser.AddToolTip(TEXT("浏览"));
  218. // "开始训练"按钮的资源的装入
  219. m_btnBeginTrain.SubclassDlgItem ( IDI_START,this);
  220. m_btnBeginTrain.SetIcon ( IDI_START);
  221. m_btnBeginTrain.SetActiveBgColor (RGB(160,180,220));
  222. m_btnBeginTrain.SetInactiveBgColor (RGB(160,180,220));
  223. m_btnBeginTrain.AddToolTip(TEXT("开始训练"));
  224. // "停止训练"按钮的资源的装入
  225. m_btnStopTrain.SubclassDlgItem ( IDI_STOP,this);
  226. m_btnStopTrain.SetIcon ( IDI_STOP);
  227. m_btnStopTrain.SetActiveBgColor (RGB(160,180,220));
  228. m_btnStopTrain.SetInactiveBgColor (RGB(160,180,220));
  229. m_btnStopTrain.AddToolTip(TEXT("停止训练"));
  230. // "显示程序状态"按钮的资源的装入
  231. m_btnState.SubclassDlgItem ( IDI_TRAINING,this);
  232. m_btnState.SubclassDlgItem ( IDI_SIMULATOR,this);
  233. m_btnState.SetIcon ( IDI_TRAINING);
  234. m_btnState.SetActiveBgColor (RGB(160,180,220));
  235. m_btnState.SetInactiveBgColor (RGB(160,180,220));
  236. m_btnState.AddToolTip (TEXT("样本训练状态"));
  237. // "关于我们"按钮的资源的装入
  238. m_btnAboutUs.SubclassDlgItem (IDI_ABOUT,this);
  239. m_btnAboutUs.SetIcon (IDI_ABOUT);
  240. m_btnAboutUs.SetActiveBgColor (RGB(160,180,220));
  241. m_btnAboutUs.SetInactiveBgColor (RGB(160,180,220));
  242. m_btnAboutUs.AddToolTip (TEXT("关于我们"));
  243. // "退出程序"按钮的资源的装入--->Yes
  244. m_btnExit.SubclassDlgItem (IDI_EXIT,this);
  245. m_btnExit.SetIcon (IDI_EXIT);
  246. m_btnExit.SetActiveBgColor (RGB(160,180,220));
  247. m_btnExit.SetInactiveBgColor (RGB(160,180,220));
  248. m_btnExit.AddToolTip (TEXT("退出程序"));
  249. /////////////////////////////////////////////////////////////////////////
  250. // 初始化对话框上各控件的状态
  251. //
  252. /////////////////////////////////////////////////////////////////////
  253. // Disabled the following controls
  254. /////////////////////////////////////////////////////////////////////
  255. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD)->EnableWindow(FALSE);
  256. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD_BROWSER)->EnableWindow(FALSE);
  257. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT)->EnableWindow(FALSE);
  258. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT_BROWSER)->EnableWindow(FALSE);
  259. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT)->EnableWindow(FALSE);
  260. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT_BROWSER)->EnableWindow(FALSE);
  261. /////////////////////////////////////////////////////////////////////
  262. // Enabled the following controls
  263. /////////////////////////////////////////////////////////////////////
  264. (CNNBPDlg *)GetDlgItem(IDC_SYSTEM_LEVEL)->EnableWindow(TRUE);
  265. (CNNBPDlg *)GetDlgItem(IDC_MAX_TRAIN_TIMES)->EnableWindow(TRUE);
  266. (CNNBPDlg *)GetDlgItem(IDC_STEP)->EnableWindow(TRUE);
  267. (CNNBPDlg *)GetDlgItem(IDC_INPUT_LAYER_NUMBER)->EnableWindow(TRUE);
  268. (CNNBPDlg *)GetDlgItem(IDC_HIDE_LAYER_NUMBER)->EnableWindow(TRUE);
  269. (CNNBPDlg *)GetDlgItem(IDC_OUTPUT_LAYER_NUMBER)->EnableWindow(TRUE);
  270. (CNNBPDlg *)GetDlgItem(IDC_COMBO_TRAIN)->EnableWindow(TRUE);
  271. (CNNBPDlg *)GetDlgItem(IDC_COMBO_FUNC)->EnableWindow(TRUE);
  272. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT)->EnableWindow(TRUE);
  273. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT_BROWSER)->EnableWindow(TRUE);
  274. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK)->EnableWindow(TRUE);
  275. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK_BROWSER)->EnableWindow(TRUE);
  276. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT)->EnableWindow(TRUE);
  277. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT_BROWSER)->EnableWindow(TRUE);
  278. //
  279. // End of initializing the state of the controls on the CNNBPDlg
  280. /////////////////////////////////////////////////////////////////////////
  281. return TRUE;  // return TRUE  unless you set the focus to a control
  282. }
  283. void CNNBPDlg::OnSysCommand(UINT nID, LPARAM lParam)
  284. {
  285. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  286. {
  287. CAboutDlg dlgAbout;
  288. dlgAbout.DoModal();
  289. }
  290. else
  291. {
  292. CDialog::OnSysCommand(nID, lParam);
  293. }
  294. }
  295. // If you add a minimize button to your dialog, you will need the code below
  296. //  to draw the icon.  For MFC applications using the document/view model,
  297. //  this is automatically done for you by the framework.
  298. void CNNBPDlg::OnPaint() 
  299. {
  300. // Draw the title label
  301. m_staticTextTitle.SetMainTextColour (RGB(0,128,192));
  302. m_staticTextTitle.Display (TEXT("神经网络仿真工具"),TEXT(""),0);
  303. if (IsIconic())
  304. {
  305. CPaintDC dc(this); // device context for painting
  306. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  307. // Center icon in client rectangle
  308. int cxIcon = GetSystemMetrics(SM_CXICON);
  309. int cyIcon = GetSystemMetrics(SM_CYICON);
  310. CRect rect;
  311. GetClientRect(&rect);
  312. int x = (rect.Width() - cxIcon + 1) / 2;
  313. int y = (rect.Height() - cyIcon + 1) / 2;
  314. // Draw the icon
  315. dc.DrawIcon(x, y, m_hIcon);
  316. }
  317. else
  318. {
  319. CDialog::OnPaint();
  320. }
  321. }
  322. // The system calls this to obtain the cursor to display while the user drags
  323. //  the minimized window.
  324. HCURSOR CNNBPDlg::OnQueryDragIcon()
  325. {
  326. return (HCURSOR) m_hIcon;
  327. }
  328. void CNNBPDlg::OnExit() 
  329. {
  330. // TODO: Add your control notification handler code here
  331. DWORD dwExitCode = 0;
  332. if(::GetExitCodeThread (m_hThread, &dwExitCode))
  333. {
  334. if(dwExitCode == STILL_ACTIVE)
  335. {
  336. ::TerminateThread (m_hThread, (DWORD)0);
  337. }
  338. CloseHandle (m_hThread);
  339. m_hThread = NULL;
  340. }
  341. CDialog::OnOK ();
  342. }
  343. void CNNBPDlg::OnAboutus() 
  344. {
  345. // TODO: Add your control notification handler code here
  346. CAboutDlg dlg;
  347. if(dlg.DoModal () == IDOK)
  348. return;
  349. }
  350. void CNNBPDlg::OnTimer(UINT nIDEvent) 
  351. {
  352. // TODO: Add your message handler code here and/or call default
  353. CDialog::OnTimer(nIDEvent);
  354. }
  355. void CNNBPDlg::OnDemoDataInputBrowser() 
  356. {
  357. // TODO: Add your control notification handler code here
  358. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  359. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  360. //Create the dialog to select the demo data file to load the data
  361. CFileDialog dlg(TRUE,
  362.             lpszDefExt,
  363. NULL,
  364. OFN_READONLY | 
  365. OFN_FILEMUSTEXIST   |
  366. OFN_PATHMUSTEXIST,
  367. szFilter,
  368. this);
  369. if(dlg.DoModal () == IDOK)
  370. {
  371. m_strDemoDataInput = dlg.GetPathName ();
  372. HWND hWnd = ::GetDlgItem (this->m_hWnd,IDC_DEMO_DATA_INPUT);
  373. ::SetWindowText (hWnd,m_strDemoDataInput);
  374. }
  375. else
  376. {
  377. return;
  378. }
  379. }
  380. void CNNBPDlg::OnDemoSaveNetworkBrowser() 
  381. {
  382. // TODO: Add your control notification handler code here
  383. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  384. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  385. //Create the dialog to select the demo data file to save the network
  386. CFileDialog dlg(FALSE,
  387.             lpszDefExt,
  388. NULL,
  389. OFN_HIDEREADONLY    | 
  390. OFN_CREATEPROMPT |
  391. OFN_OVERWRITEPROMPT,
  392. szFilter,
  393. this );
  394. if(dlg.DoModal ()==IDOK)
  395. {
  396. m_strDemoSaveNetwork = dlg.GetPathName ();
  397. HWND hWnd = ::GetDlgItem (this->m_hWnd,IDC_DEMO_SAVE_NETWORK);
  398. ::SetWindowText (hWnd,m_strDemoSaveNetwork);
  399. }
  400. else
  401. {
  402. return;
  403. }
  404. }
  405. void CNNBPDlg::OnDemoSaveResultBrowser() 
  406. {
  407. // TODO: Add your control notification handler code here
  408. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  409. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  410. //Create the dialog to select the demo data file to save the result
  411. CFileDialog dlg(FALSE,
  412.             lpszDefExt,
  413. NULL,
  414. OFN_HIDEREADONLY    | 
  415. OFN_CREATEPROMPT |
  416. OFN_OVERWRITEPROMPT,
  417. szFilter,
  418. this);
  419. if(dlg.DoModal ()==IDOK)
  420. {
  421. m_strDemoSaveResult = dlg.GetPathName ();
  422. HWND hWnd = ::GetDlgItem (this->m_hWnd , IDC_DEMO_SAVE_RESULT);
  423. ::SetWindowText (hWnd, m_strDemoSaveResult);
  424. }
  425. else
  426. {
  427. return;
  428. }
  429. }
  430. void CNNBPDlg::OnSimuNetworkFoldBrowser() 
  431. {
  432. // TODO: Add your control notification handler code here
  433. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  434. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  435. //Create the dialog to select the network data file
  436. CFileDialog dlg(TRUE,
  437.             lpszDefExt,
  438. NULL,
  439. OFN_READONLY | 
  440. OFN_FILEMUSTEXIST   |
  441. OFN_PATHMUSTEXIST,
  442. szFilter,
  443. this);
  444. if(dlg.DoModal ()==IDOK)
  445. {
  446. m_strSimuNetworkFold = dlg.GetPathName ();
  447. HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_NETWORK_FOLD);
  448. ::SetWindowText (hWnd, m_strSimuNetworkFold);
  449. }
  450. else
  451. {
  452. return;
  453. }
  454. }
  455. void CNNBPDlg::OnSimuDataInputBrowser() 
  456. {
  457. // TODO: Add your control notification handler code here
  458. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  459. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  460. //Create the dialog to select the simulate data file
  461. CFileDialog dlg(TRUE,
  462.             lpszDefExt,
  463. NULL,
  464. OFN_READONLY | 
  465. OFN_FILEMUSTEXIST   |
  466. OFN_PATHMUSTEXIST,
  467. szFilter,
  468. this);
  469. if(dlg.DoModal ()==IDOK)
  470. {
  471. m_strSimuDataInput = dlg.GetPathName ();
  472. HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_DATA_INPUT);
  473. ::SetWindowText (hWnd,m_strSimuDataInput);
  474. }
  475. else
  476. {
  477. return;
  478. }
  479. }
  480. void CNNBPDlg::OnSimuResultBrowser() 
  481. {
  482. // TODO: Add your control notification handler code here
  483. static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
  484. static char BASED_CODE lpszDefExt[] = TEXT("txt");
  485. //Create the dialog to select the demo data file
  486. CFileDialog dlg(FALSE,
  487.             lpszDefExt,
  488. NULL,
  489. OFN_HIDEREADONLY    | 
  490. OFN_CREATEPROMPT |
  491. OFN_OVERWRITEPROMPT,
  492. szFilter,
  493. this);
  494. if(dlg.DoModal ()==IDOK)
  495. {
  496. m_strSimuResult = dlg.GetPathName ();
  497. HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_RESULT);
  498. ::SetWindowText (hWnd,m_strSimuResult);
  499. }
  500. else
  501. {
  502. return;
  503. }
  504. }
  505. void CNNBPDlg::OnBeginTrain() 
  506. {
  507. // TODO: Add your control notification handler code here
  508. UpdateData(TRUE);
  509. STHREADDATA* pData = new STHREADDATA;
  510. pData->pDlg = this;
  511. DWORD dwExitCode = 0;
  512. ::GetExitCodeThread (m_hThread, &dwExitCode);
  513. if(dwExitCode == STILL_ACTIVE)
  514. {
  515. ::MessageBox (this->m_hWnd, _T("程序中的训练线程仍然在运行中!"), _T("警告:"), MB_ICONWARNING | MB_OK);
  516. return;
  517. }
  518. ULONG ulThreadID;
  519. m_hThread = ::CreateThread (NULL,
  520. 0,
  521. WorkThreadProc,
  522. pData,
  523. CREATE_SUSPENDED,
  524. &ulThreadID
  525. );
  526. ResumeThread (m_hThread);
  527. }
  528. BOOL CAboutDlg::OnInitDialog() 
  529. {
  530. CDialog::OnInitDialog();
  531. // TODO: Add extra initialization here
  532. // "使用说明"按钮的资源的装入
  533. m_btnHelpDocument.SubclassDlgItem (IDI_HELP,this);
  534. m_btnHelpDocument.SetIcon ( IDI_HELP);
  535. m_btnHelpDocument.SetActiveBgColor (RGB(160,180,220));
  536. m_btnHelpDocument.SetInactiveBgColor (RGB(160,180,220));
  537. m_btnHelpDocument.AddToolTip (TEXT("使用说明"));
  538. return TRUE;  // return TRUE unless you set the focus to a control
  539.               // EXCEPTION: OCX Property Pages should return FALSE
  540. }
  541. void CNNBPDlg::OnState() 
  542. {
  543. // TODO: Add your control notification handler code here
  544. /////////////////////////////////////////////////////////////////////////
  545. // Change the button flag
  546. m_bSimulateDataFlag = !m_bSimulateDataFlag;
  547. if(m_bSimulateDataFlag)
  548. {
  549. // 程序处在数据仿真状态
  550. m_btnState.SetIcon (IDI_SIMULATOR);
  551. m_btnState.AddToolTip (TEXT("数据仿真状态"));
  552. // the BeginTrain Button State
  553. m_btnBeginTrain.AddToolTip(TEXT("开始仿真"));
  554. /////////////////////////////////////////////////////////////////////
  555. // Disabled the following controls
  556. /////////////////////////////////////////////////////////////////////
  557. (CNNBPDlg *)GetDlgItem(IDC_SYSTEM_LEVEL)->EnableWindow(FALSE);
  558. (CNNBPDlg *)GetDlgItem(IDC_MAX_TRAIN_TIMES)->EnableWindow(FALSE);
  559. (CNNBPDlg *)GetDlgItem(IDC_STEP)->EnableWindow(FALSE);
  560. (CNNBPDlg *)GetDlgItem(IDC_INPUT_LAYER_NUMBER)->EnableWindow(FALSE);
  561. (CNNBPDlg *)GetDlgItem(IDC_HIDE_LAYER_NUMBER)->EnableWindow(FALSE);
  562. (CNNBPDlg *)GetDlgItem(IDC_OUTPUT_LAYER_NUMBER)->EnableWindow(FALSE);
  563. (CNNBPDlg *)GetDlgItem(IDC_COMBO_TRAIN)->EnableWindow(FALSE);
  564. (CNNBPDlg *)GetDlgItem(IDC_COMBO_FUNC)->EnableWindow(FALSE);
  565. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT)->EnableWindow(FALSE);
  566. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT_BROWSER)->EnableWindow(FALSE);
  567. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK)->EnableWindow(FALSE);
  568. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK_BROWSER)->EnableWindow(FALSE);
  569. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT)->EnableWindow(FALSE);
  570. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT_BROWSER)->EnableWindow(FALSE);
  571. (CNNBPDlg *)GetDlgItem(IDC_STOP_TRAIN)->EnableWindow(FALSE);
  572. /////////////////////////////////////////////////////////////////////
  573. // Enabled the following controls
  574. /////////////////////////////////////////////////////////////////////
  575. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD)->EnableWindow(TRUE);
  576. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD_BROWSER)->EnableWindow(TRUE);
  577. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT)->EnableWindow(TRUE);
  578. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT_BROWSER)->EnableWindow(TRUE);
  579. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT)->EnableWindow(TRUE);
  580. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT_BROWSER)->EnableWindow(TRUE);
  581. }
  582. else
  583. {
  584. // 程序处在样本训练状态
  585. m_btnState.SetIcon (IDI_TRAINING);
  586. m_btnState.AddToolTip (TEXT("样本训练状态"));
  587. // the BeginTrain Button State 
  588. m_btnBeginTrain.AddToolTip(TEXT("开始训练"));
  589. /////////////////////////////////////////////////////////////////////
  590. // Disabled the following controls
  591. /////////////////////////////////////////////////////////////////////
  592. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD)->EnableWindow(FALSE);
  593. (CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD_BROWSER)->EnableWindow(FALSE);
  594. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT)->EnableWindow(FALSE);
  595. (CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT_BROWSER)->EnableWindow(FALSE);
  596. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT)->EnableWindow(FALSE);
  597. (CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT_BROWSER)->EnableWindow(FALSE);
  598. /////////////////////////////////////////////////////////////////////
  599. // Enabled the following controls
  600. /////////////////////////////////////////////////////////////////////
  601. (CNNBPDlg *)GetDlgItem(IDC_SYSTEM_LEVEL)->EnableWindow(TRUE);
  602. (CNNBPDlg *)GetDlgItem(IDC_MAX_TRAIN_TIMES)->EnableWindow(TRUE);
  603. (CNNBPDlg *)GetDlgItem(IDC_STEP)->EnableWindow(TRUE);
  604. (CNNBPDlg *)GetDlgItem(IDC_INPUT_LAYER_NUMBER)->EnableWindow(TRUE);
  605. (CNNBPDlg *)GetDlgItem(IDC_HIDE_LAYER_NUMBER)->EnableWindow(TRUE);
  606. (CNNBPDlg *)GetDlgItem(IDC_OUTPUT_LAYER_NUMBER)->EnableWindow(TRUE);
  607. (CNNBPDlg *)GetDlgItem(IDC_COMBO_TRAIN)->EnableWindow(TRUE);
  608. (CNNBPDlg *)GetDlgItem(IDC_COMBO_FUNC)->EnableWindow(TRUE);
  609. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT)->EnableWindow(TRUE);
  610. (CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT_BROWSER)->EnableWindow(TRUE);
  611. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK)->EnableWindow(TRUE);
  612. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK_BROWSER)->EnableWindow(TRUE);
  613. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT)->EnableWindow(TRUE);
  614. (CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT_BROWSER)->EnableWindow(TRUE);
  615. (CNNBPDlg *)GetDlgItem(IDC_STOP_TRAIN)->EnableWindow(TRUE);
  616. }
  617. }
  618. bool CNNBPDlg::TrainingDemo()
  619. {
  620. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(FALSE);
  621. if(m_bSimulateDataFlag)
  622. {
  623. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  624. return false;
  625. }
  626. if(m_nInputLayerNumber <= 0)
  627. {
  628. ::MessageBox (this->m_hWnd, _T("输入层数目必须为大于等于1的正整数!"), _T(":("), MB_OK);
  629. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  630. return false;
  631. }
  632. if(m_nHideLayerNumber <= 0)
  633. {
  634. ::MessageBox (this->m_hWnd, _T("隐含层数目必须为大于等于1的正整数!"), _T(":("), MB_OK);
  635. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  636. return false;
  637. }
  638. if(m_nOutputLayerNumber <= 0)
  639. {
  640. ::MessageBox (this->m_hWnd, _T("输出层数目必须为大于等于1的正整数!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  641. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  642. return false;
  643. }
  644. if(m_nComboArithmetic == -1)
  645. {
  646. ::MessageBox (this->m_hWnd, _T("你还没有选取用来训练的算法!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  647. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  648. return false;
  649. }
  650. if(m_nComboFunc == -1)
  651. {
  652. ::MessageBox (this->m_hWnd, _T("你还没有选取用来训练的激活函数!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  653. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  654. return false;
  655. }
  656. if(m_strDemoDataInput.IsEmpty ())
  657. {
  658. ::MessageBox (this->m_hWnd, _T("你还没选取用来训练的样本数据文件的装载路径!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  659. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  660. return false;
  661. }
  662. if(m_strDemoSaveNetwork.IsEmpty ())
  663. {
  664. ::MessageBox (this->m_hWnd, _T("你还没选取最终模拟输出网络参数的文件的存放路径!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  665. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  666. return false;
  667. }
  668. if(m_strDemoSaveResult.IsEmpty ())
  669. {
  670. ::MessageBox (this->m_hWnd, _T("你还没选取最终模拟输出结果的文件的存放路径!"), _T(":("), MB_OK | MB_ICONINFORMATION);
  671. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  672. return false;
  673. }
  674. // 装载资源
  675. m_matrixDemoDataInput.LoadDataFromFile (m_strDemoDataInput);
  676. if((m_nInputLayerNumber + m_nOutputLayerNumber) != m_matrixDemoDataInput.GetMatrixColNumber () )
  677. {
  678. ::MessageBox (this->m_hWnd, _T("所输入的输入层和输出层数目之和与被读取文件中的数目不相符!"), _T("错误!"), MB_OK | MB_ICONERROR);
  679. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  680. return FALSE;
  681. }
  682. // 第一次前向计算初始化
  683. ForwardCalculateInit();
  684. // 反复反馈运算
  685. bool bSuccess = DemoDataTrainRepeat();
  686. if(bSuccess) // Successful!
  687. {
  688. //////////////////////////////////////////////////////////////////////////////
  689. // 定义存储矩阵格式 //
  690. //////////////////////////////////////////////////////////////////////////////
  691. if(!(m_strDemoSaveNetwork.IsEmpty ()))
  692. {
  693. CString strDemoInputLayerNumber = _T("## 样本的输入层的数目: ##n");
  694. SaveConstantStringToFile(m_strDemoSaveNetwork, strDemoInputLayerNumber);
  695. CString strInputLayerNumber;
  696. strInputLayerNumber.Format ("%u", m_nInputLayerNumber);
  697. SaveConstantStringToFile(m_strDemoSaveNetwork, strInputLayerNumber);
  698. CString strDemoHideLayerNumber = _T("n## 样本的隐含层的数目: ##n");
  699. SaveConstantStringToFile(m_strDemoSaveNetwork, strDemoHideLayerNumber);
  700. CString strHideLayerNumber;
  701. strHideLayerNumber.Format ("%u", m_nHideLayerNumber);
  702. SaveConstantStringToFile(m_strDemoSaveNetwork, strHideLayerNumber);
  703. CString strDemoOutputLayerNumber = _T("n## 样本的输出层的数目: ##n");
  704. SaveConstantStringToFile(m_strDemoSaveNetwork, strDemoOutputLayerNumber);
  705. CString strOutputLayerNumber;
  706. strOutputLayerNumber.Format ("%u", m_nOutputLayerNumber);
  707. SaveConstantStringToFile(m_strDemoSaveNetwork, strOutputLayerNumber);
  708. CString strSelectAlgorithm = _T("n## 训练网络所采用的神经网络算法: ##n");
  709. SaveConstantStringToFile(m_strDemoSaveNetwork, strSelectAlgorithm);
  710. CString strComboArithmetic;
  711. strComboArithmetic.Format ("%d", m_nComboArithmetic);
  712. SaveConstantStringToFile(m_strDemoSaveNetwork, strComboArithmetic);
  713. CString strSelectFunction = _T("n## 网络中使用的函数的索引值: ##n");
  714. SaveConstantStringToFile(m_strDemoSaveNetwork, strSelectFunction);
  715. CString strComboFunc;
  716. strComboFunc.Format ("%d", m_nComboFunc);
  717. SaveConstantStringToFile(m_strDemoSaveNetwork, strComboFunc);
  718. CString strSeparator = _T("n## 输入层到隐含层的权值矩阵 ##n");
  719. SaveConstantStringToFile(m_strDemoSaveNetwork, strSeparator);
  720. // 存储模拟过的输入到隐含层的权值矩阵
  721. m_matrixInputToHideWeightValue.SaveDataToFile (m_strDemoSaveNetwork);
  722. strSeparator = _T("n## 隐含层的阀值矩阵 ##n");
  723. SaveConstantStringToFile(m_strDemoSaveNetwork,strSeparator);
  724. // 存储模拟过的隐含层的阀值矩阵
  725. m_matrixHideLayerValveValue.SaveDataToFile (m_strDemoSaveNetwork);
  726. strSeparator = _T("n## 隐含层到输出层的权值矩阵 ##n");
  727. SaveConstantStringToFile(m_strDemoSaveNetwork,strSeparator);
  728. // 存储模拟过的隐含层到输出层的权值矩阵
  729. m_matrixHideToOutputWeightValue.SaveDataToFile (m_strDemoSaveNetwork);
  730. strSeparator = _T("n## 输出层的阀值矩阵 ##n");
  731. SaveConstantStringToFile(m_strDemoSaveNetwork,strSeparator);
  732. // 存储模拟过的输出层的阀值矩阵
  733. m_matrixOutputLayerValveValue.SaveDataToFile (m_strDemoSaveNetwork);
  734. }
  735. // 存储模拟过输出层的输出值矩阵
  736. if(!(m_strDemoSaveResult.IsEmpty ()))
  737. {
  738. (m_matrixOutputLayerOutput.Transpose()).SaveDataToFile (m_strDemoSaveResult);
  739. }
  740. // 释放全局变量所占的内存
  741. m_matrixDemoDataInput.InitializeZero ();
  742. m_matrixInputLayerValue.InitializeZero ();
  743. m_matrixInputToHideWeightValue.InitializeZero ();
  744. m_matrixHideLayerValveValue.InitializeZero ();
  745. m_matrixHideLayerOutput.InitializeZero ();
  746. m_matrixHideToOutputWeightValue.InitializeZero ();
  747. m_matrixOutputLayerValveValue.InitializeZero ();
  748. m_matrixOutputLayerOutput.InitializeZero ();
  749. // ReInitialize the global value
  750. m_nInputLayerNumber = 0;
  751. m_nHideLayerNumber = 0;
  752. m_nOutputLayerNumber = 0;
  753. m_nComboFunc = -1;
  754. m_nComboArithmetic = -1;
  755. m_nSystemError = 0;
  756. m_nTrainTimes = 0;
  757. m_nSystemErrorLevel = SYSTEM_ERROR_LEVEL;
  758. m_nMaxTrainTimes = LOOP_MAX_TIMES;
  759. m_SystemErrorNew = MAX_SYSTEM_ERROR;
  760. m_SystemErrorOld = MAX_SYSTEM_ERROR;
  761. m_nStep = INIT_STEP;
  762. m_strDemoDataInput = TEXT("");
  763. m_strDemoSaveNetwork = TEXT("");
  764. m_strDemoSaveResult = TEXT("");
  765. ::MessageBox (this->m_hWnd, _T("任务完成了!"), _T("恭喜!"), MB_OK);
  766. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  767. //*******************************************************************
  768. // handle the UI
  769. //
  770. CString strSystemErrorLevel;
  771. strSystemErrorLevel.Format ("%lf", m_nSystemErrorLevel);
  772. HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_LEVEL);
  773. ::SetWindowText (hWnd, (LPCTSTR)strSystemErrorLevel);
  774. CString strMaxTrainTimes;
  775. strMaxTrainTimes.Format ("%u", m_nMaxTrainTimes);
  776. hWnd = ::GetDlgItem (this->m_hWnd, IDC_MAX_TRAIN_TIMES);
  777. ::SetWindowText (hWnd, (LPCTSTR)strMaxTrainTimes);
  778. hWnd = ::GetDlgItem (this->m_hWnd, IDC_INPUT_LAYER_NUMBER);
  779. ::SetWindowText (hWnd, _T("0"));
  780. hWnd = ::GetDlgItem (this->m_hWnd, IDC_HIDE_LAYER_NUMBER);
  781. ::SetWindowText (hWnd, _T("0"));
  782. hWnd = ::GetDlgItem (this->m_hWnd, IDC_OUTPUT_LAYER_NUMBER);
  783. ::SetWindowText (hWnd, _T("0"));
  784. m_ctrlComboArithmetic.SetCurSel (-1);
  785. m_ctrlComboFunc.SetCurSel (-1);
  786. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_DATA_INPUT);
  787. ::SetWindowText (hWnd, _T(""));
  788. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_SAVE_NETWORK);
  789. ::SetWindowText (hWnd, _T(""));
  790. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_SAVE_RESULT);
  791. ::SetWindowText (hWnd, _T(""));
  792. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_ERROR);
  793. ::SetWindowText (hWnd, _T("0"));
  794. hWnd = ::GetDlgItem (this->m_hWnd, IDC_TRAIN_TIMES);
  795. ::SetWindowText (hWnd, _T("0"));
  796. }
  797. else // Fail to accomplish the task
  798. {
  799. // 释放全局变量所占的内存
  800. m_matrixDemoDataInput.InitializeZero ();
  801. m_matrixInputLayerValue.InitializeZero ();
  802. m_matrixInputToHideWeightValue.InitializeZero ();
  803. m_matrixHideLayerValveValue.InitializeZero ();
  804. m_matrixHideLayerOutput.InitializeZero ();
  805. m_matrixHideToOutputWeightValue.InitializeZero ();
  806. m_matrixOutputLayerValveValue.InitializeZero ();
  807. m_matrixOutputLayerOutput.InitializeZero ();
  808. // ReInitialize the global value
  809. m_nInputLayerNumber = 0;
  810. m_nHideLayerNumber = 0;
  811. m_nOutputLayerNumber = 0;
  812. m_nComboFunc = -1;
  813. m_nComboArithmetic = -1;
  814. m_nSystemError = 0;
  815. m_nTrainTimes = 0;
  816. m_nSystemErrorLevel = SYSTEM_ERROR_LEVEL;
  817. m_nMaxTrainTimes = LOOP_MAX_TIMES;
  818. m_SystemErrorNew = MAX_SYSTEM_ERROR;
  819. m_SystemErrorOld = MAX_SYSTEM_ERROR;
  820. m_nStep = INIT_STEP;
  821. m_strDemoDataInput = TEXT("");
  822. m_strDemoSaveNetwork = TEXT("");
  823. m_strDemoSaveResult = TEXT("");
  824. ::MessageBox (this->m_hWnd, _T("很抱歉,我未能完成您交给我的任务."), _T(":("),MB_OK | MB_ICONERROR);
  825. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  826. //*******************************************************************
  827. // handle the UI
  828. //
  829. CString strSystemErrorLevel;
  830. strSystemErrorLevel.Format ("%lf", m_nSystemErrorLevel);
  831. HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_LEVEL);
  832. ::SetWindowText (hWnd, (LPCTSTR)strSystemErrorLevel);
  833. CString strMaxTrainTimes;
  834. strMaxTrainTimes.Format ("%u", m_nMaxTrainTimes);
  835. hWnd = ::GetDlgItem (this->m_hWnd, IDC_MAX_TRAIN_TIMES);
  836. ::SetWindowText (hWnd, (LPCTSTR)strMaxTrainTimes);
  837. hWnd = ::GetDlgItem (this->m_hWnd, IDC_INPUT_LAYER_NUMBER);
  838. ::SetWindowText (hWnd, _T("0"));
  839. hWnd = ::GetDlgItem (this->m_hWnd, IDC_HIDE_LAYER_NUMBER);
  840. ::SetWindowText (hWnd, _T("0"));
  841. hWnd = ::GetDlgItem (this->m_hWnd, IDC_OUTPUT_LAYER_NUMBER);
  842. ::SetWindowText (hWnd, _T("0"));
  843. m_ctrlComboArithmetic.SetCurSel (-1);
  844. m_ctrlComboFunc.SetCurSel (-1);
  845. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_DATA_INPUT);
  846. ::SetWindowText (hWnd, _T(""));
  847. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_SAVE_NETWORK);
  848. ::SetWindowText (hWnd, _T(""));
  849. hWnd = ::GetDlgItem (this->m_hWnd, IDC_DEMO_SAVE_RESULT);
  850. ::SetWindowText (hWnd, _T(""));
  851. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_ERROR);
  852. ::SetWindowText (hWnd, _T("0"));
  853. hWnd = ::GetDlgItem (this->m_hWnd, IDC_TRAIN_TIMES);
  854. ::SetWindowText (hWnd, _T("0"));
  855. return false;
  856. }
  857. return true;
  858. }
  859. bool CNNBPDlg::SimulateData()
  860. {
  861. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(FALSE);
  862. if(!m_bSimulateDataFlag)
  863. {
  864. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  865. return false;
  866. }
  867. if(m_strSimuNetworkFold.IsEmpty ())
  868. {
  869. ::MessageBox (this->m_hWnd, _T("你还没选取提供网络参数的文件的装载路径!"), _T(":("),MB_OK | MB_ICONWARNING);
  870. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  871. return false;
  872. }
  873. if(m_strSimuDataInput.IsEmpty ())
  874. {
  875. ::MessageBox (this->m_hWnd, _T("你还没选取用来仿真的数据文件的装载路径!"), _T(":("),MB_OK | MB_ICONWARNING);
  876. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  877. return false;
  878. }
  879. if(m_strSimuResult.IsEmpty ())
  880. {
  881. ::MessageBox (this->m_hWnd, _T("你还没选取最终仿真结果的数据文件的存放路径!"), _T(":("),MB_OK | MB_ICONWARNING);
  882. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  883. return false;
  884. }
  885. // 装载网络参数文件
  886. m_matrixSimuNetwork.LoadDataFromFileSpecial(m_strSimuNetworkFold,
  887. m_matrixInputToHideWeightValue,
  888. m_matrixHideLayerValveValue,
  889. m_matrixHideToOutputWeightValue,
  890. m_matrixOutputLayerValveValue,
  891. m_nInputLayerNumber,
  892. m_nHideLayerNumber,
  893. m_nOutputLayerNumber,
  894. m_nComboArithmetic,
  895. m_nComboFunc);
  896. // 装载仿真数据文件
  897. m_matrixDemoDataInput.LoadDataFromFile (m_strSimuDataInput);
  898. if( m_matrixDemoDataInput.GetMatrixColNumber () != m_nInputLayerNumber )
  899. {
  900. ::MessageBox (this->m_hWnd, _T("待仿真数据文件的输入层数目与网络中的输入层数目不相等!!!"), _T("错误!"), MB_OK | MB_ICONERROR);
  901. // 释放全局变量所占的内存
  902. m_matrixSimuNetwork.InitializeZero ();
  903. m_matrixInputToHideWeightValue.InitializeZero ();
  904. m_matrixHideLayerValveValue.InitializeZero ();
  905. m_matrixHideToOutputWeightValue.InitializeZero ();
  906. m_matrixOutputLayerValveValue.InitializeZero ();
  907. m_matrixDemoDataInput.InitializeZero ();
  908. m_strSimuNetworkFold = TEXT("");
  909. m_strSimuDataInput = TEXT("");
  910. m_strSimuResult = TEXT("");
  911. m_nInputLayerNumber = 0;
  912. m_nHideLayerNumber = 0;
  913. m_nOutputLayerNumber = 0;
  914. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  915. //*******************************************************************
  916. // Update the UI
  917. //
  918. HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_NETWORK_FOLD);
  919. ::SetWindowText (hWnd, _T(""));
  920. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_DATA_INPUT);
  921. ::SetWindowText (hWnd, _T(""));
  922. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_RESULT);
  923. ::SetWindowText (hWnd, _T(""));
  924. return false;
  925. }
  926. else // Successful!!!
  927. {
  928. // 前向计算
  929. switch(m_nComboArithmetic)
  930. {
  931. case 0: // LM Arithmetic Selected
  932. LMForwardCalculate( m_nInputLayerNumber,
  933. m_nHideLayerNumber,
  934. m_nOutputLayerNumber,
  935. m_bSimulateDataFlag,
  936. m_nComboFunc,
  937. m_matrixDemoDataInput,
  938. m_matrixInputLayerValue,
  939. m_matrixInputToHideWeightValue,
  940. m_matrixHideLayerValveValue,
  941. m_matrixHideLayerOutput,
  942. m_matrixHideToOutputWeightValue,
  943. m_matrixOutputLayerOutput,
  944. m_matrixOutputLayerValveValue
  945.  );
  946. break;
  947. case 1: // BP Arithmetic Selected
  948. BPForwardCalculate2(m_nInputLayerNumber,
  949. m_nHideLayerNumber,
  950. m_nOutputLayerNumber,
  951. m_bSimulateDataFlag,
  952. m_nComboFunc,
  953. m_matrixDemoDataInput,
  954. m_matrixInputLayerValue,
  955. m_matrixInputToHideWeightValue,
  956. m_matrixHideLayerValveValue,
  957. m_matrixHideLayerOutput,
  958. m_matrixHideToOutputWeightValue,
  959. m_matrixOutputLayerOutput,
  960. m_matrixOutputLayerValveValue
  961.  );
  962. break;
  963. default: // No Arithmetic Selected
  964. return false;
  965. }
  966. // 存储模拟结果
  967. (m_matrixOutputLayerOutput.Transpose()).SaveDataToFile(m_strSimuResult);
  968. // 释放全局变量所占的内存
  969. m_matrixSimuNetwork.InitializeZero ();
  970. m_matrixInputToHideWeightValue.InitializeZero ();
  971. m_matrixHideLayerValveValue.InitializeZero ();
  972. m_matrixHideToOutputWeightValue.InitializeZero ();
  973. m_matrixOutputLayerValveValue.InitializeZero ();
  974. m_matrixDemoDataInput.InitializeZero ();
  975. m_matrixHideLayerOutput.InitializeZero ();
  976. m_matrixOutputLayerOutput.InitializeZero ();
  977. m_strSimuNetworkFold = _T("");
  978. m_strSimuDataInput = _T("");
  979. m_strSimuResult = _T("");
  980. m_nComboFunc = -1;
  981. m_nInputLayerNumber = 0;
  982. m_nHideLayerNumber = 0;
  983. m_nOutputLayerNumber = 0;
  984. ::MessageBox (this->m_hWnd, _T("数据仿真结果保存成功!"), _T("恭喜!"), MB_OK);
  985. (CNNBPDlg *)GetDlgItem(IDC_STATE)->EnableWindow(TRUE);
  986. //*******************************************************************
  987. // Update the UI
  988. //
  989. HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_NETWORK_FOLD);
  990. ::SetWindowText (hWnd, _T(""));
  991. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_DATA_INPUT);
  992. ::SetWindowText (hWnd, _T(""));
  993. hWnd = ::GetDlgItem (this->m_hWnd, IDC_SIMU_RESULT);
  994. ::SetWindowText (hWnd, _T(""));
  995. }
  996. return true;
  997. }
  998. void CNNBPDlg::ForwardCalculateInit()
  999. {
  1000. LMForwardCalculateInit( m_nInputLayerNumber,
  1001. m_nHideLayerNumber,
  1002. m_nOutputLayerNumber,
  1003. m_matrixDemoDataInput,
  1004. m_matrixInputLayerValue,
  1005. m_matrixInputToHideWeightValue,
  1006. m_matrixHideLayerValveValue,
  1007. m_matrixHideToOutputWeightValue,
  1008. m_matrixOutputLayerValveValue
  1009. );
  1010. }
  1011. bool CNNBPDlg::DemoDataTrainRepeat()
  1012. {
  1013. bool bReturn = false;
  1014. switch( m_nComboArithmetic)
  1015. {
  1016. case 0: // LM Arithmetic Selected
  1017. bReturn = LMDemoDataTrainRepeat ( m_nInputLayerNumber,
  1018. m_nHideLayerNumber,
  1019. m_nOutputLayerNumber,
  1020. m_bSimulateDataFlag,
  1021. m_nComboFunc,
  1022. m_SystemErrorOld,
  1023. m_SystemErrorNew,
  1024. m_nSystemErrorLevel,
  1025. m_nSystemError,
  1026. m_nStep,
  1027. m_nMaxTrainTimes,
  1028. m_nTrainTimes,
  1029. IDC_SYSTEM_ERROR,
  1030. IDC_TRAIN_TIMES,
  1031. this->m_hWnd,
  1032. m_matrixDemoDataInput,
  1033. m_matrixInputLayerValue,
  1034. m_matrixInputToHideWeightValue,
  1035. m_matrixHideLayerValveValue,
  1036. m_matrixHideLayerOutput,
  1037. m_matrixHideToOutputWeightValue,
  1038. m_matrixOutputLayerOutput,
  1039. m_matrixOutputLayerValveValue
  1040. );
  1041. break;
  1042. case 1: // BP Arithmetic Selected
  1043. bReturn = BPDemoDataTrainRepeat ( m_nInputLayerNumber,
  1044. m_nHideLayerNumber,
  1045. m_nOutputLayerNumber,
  1046. m_bSimulateDataFlag,
  1047. m_nComboFunc,
  1048. m_SystemErrorOld,
  1049. m_SystemErrorNew,
  1050. m_nSystemErrorLevel,
  1051. m_nSystemError,
  1052. m_nStep,
  1053. m_nMaxTrainTimes,
  1054. m_nTrainTimes,
  1055. IDC_SYSTEM_ERROR,
  1056. IDC_TRAIN_TIMES,
  1057. this->m_hWnd,
  1058. m_matrixDemoDataInput,
  1059. m_matrixInputLayerValue,
  1060. m_matrixInputToHideWeightValue,
  1061. m_matrixHideLayerValveValue,
  1062. m_matrixHideLayerOutput,
  1063. m_matrixHideToOutputWeightValue,
  1064. m_matrixOutputLayerOutput,
  1065. m_matrixOutputLayerValveValue
  1066.  );
  1067. break;
  1068. default: // No Arithmetic Selected
  1069. return bReturn;
  1070. }
  1071. return bReturn;
  1072. }
  1073. bool CNNBPDlg::SaveConstantStringToFile(CString& strFileName,CString& strConstantData)
  1074. {
  1075. // Convert CString to LPCTSTR 
  1076. LPCTSTR lpszFileName = "";
  1077. strFileName.TrimLeft ();
  1078. strFileName.TrimRight ();
  1079. lpszFileName = (LPCTSTR)strFileName;
  1080. CStdioFile dataFile;
  1081. if(!dataFile.Open (lpszFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText))
  1082. {
  1083. ::MessageBox(this->m_hWnd, _T("不能创建存储网络参数的文件!"), _T("错误!"), MB_OK | MB_ICONERROR);
  1084. dataFile.Close ();
  1085. return FALSE;
  1086. }
  1087. dataFile.SeekToEnd ();
  1088. // Write data into the file
  1089. char *pBuffer = new char[strConstantData.GetLength ()];
  1090. memcpy(pBuffer,strConstantData,strConstantData.GetLength ());
  1091. dataFile.Write (pBuffer,strConstantData.GetLength ());
  1092. dataFile.Close ();
  1093. return TRUE;
  1094. }
  1095. /////////////////////////////////////////////////////////////////////////////
  1096. // 工作线程过程
  1097. //
  1098. /////////////////////////////////////////////////////////////////////////////
  1099. ULONG __stdcall WorkThreadProc(LPVOID lParam)
  1100. {
  1101. if(lParam == NULL)
  1102. return -1;
  1103. STHREADDATA *pData = (STHREADDATA *)lParam;
  1104. // Set the flag
  1105. pData->pDlg->m_bStartFlag = true;
  1106. if(pData->pDlg->m_bSimulateDataFlag)
  1107. {
  1108. pData->pDlg->SimulateData();
  1109. }
  1110. else
  1111. {
  1112. pData->pDlg->TrainingDemo();
  1113. }
  1114. pData->pDlg->m_bStartFlag = false;
  1115. delete pData;
  1116. pData = 0;
  1117. return 0;
  1118. }
  1119. void CAboutDlg::OnHelpdocument() 
  1120. {
  1121. // TODO: Add your control notification handler code here
  1122. DWORD nBufferLength=256;
  1123. TCHAR lpBuffer[256];
  1124. CString m_strPath;
  1125. GetCurrentDirectory(nBufferLength,lpBuffer);
  1126. m_strPath = lpBuffer;
  1127. m_strPath = m_strPath + '\' + TEXT("Help.chm");
  1128. HINSTANCE hHelpFile = ShellExecute (NULL,"open", m_strPath, NULL, NULL, SW_SHOWNORMAL);
  1129. if((long)hHelpFile == ERROR_FILE_NOT_FOUND)
  1130. {
  1131. ::MessageBox(this->m_hWnd, _T("帮助文件没有找到,你是不是把它给删了?"), _T("没找到"), MB_ICONEXCLAMATION | MB_OK );
  1132. return ;
  1133. }
  1134. }
  1135. void CAboutDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  1136. {
  1137. // TODO: Add your message handler code here and/or call default
  1138. CDialog::OnOK ();
  1139. //CDialog::OnLButtonDown(nFlags, point);
  1140. }
  1141. void CNNBPDlg::OnStopTrain() 
  1142. {
  1143. // TODO: Add your control notification handler code here
  1144. DWORD dwExitCode = 0;
  1145. ::GetExitCodeThread (m_hThread, &dwExitCode);
  1146. if(dwExitCode == STILL_ACTIVE)
  1147. {
  1148. ::TerminateThread (m_hThread, (DWORD)0);
  1149. CloseHandle (m_hThread);
  1150. m_hThread = NULL;
  1151. HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_ERROR);
  1152. ::SetWindowText (hWnd, _T("0"));
  1153. hWnd = ::GetDlgItem (this->m_hWnd, IDC_TRAIN_TIMES);
  1154. ::SetWindowText (hWnd, _T("0"));
  1155. }
  1156. }