anndemodlg.cpp

来自「用于抑郁症状检测的人工神经网络程序」· C++ 代码 · 共 425 行

CPP
425
字号
// ANNdemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ANNdemo.h"
#include "ANNdemoDlg.h"
#include "dynbp.h"
#include <math.h>
#include <fstream.h>
#include <assert.h>
#include "nNodeDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CANNdemoDlg dialog

CANNdemoDlg::CANNdemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CANNdemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CANNdemoDlg)


	m_forNorMax=1;
	m_forNorMin=0;
	m_fRate=0.5;
	m_Grandmax=0.5;
	m_Grandmin=-0.5;
	m_nLaynum=3;
	m_nNodenum1=3;
	m_nNodenumfirst=2;
	m_nNodenumlast=1;
	m_expect_err = 0.1f;
	m_maxcircle = 3000;
	m_bLayerChange=0;

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

	char str[30];    
	ifstream rdset("setting.txt");
	rdset>>str;
	rdset>>m_Grandmax;
	rdset>>str;
	rdset>>m_Grandmin;
	rdset>>str;
	rdset>>m_forNorMax;
	rdset>>str;
	rdset>>m_forNorMin;
	rdset>>str;
	rdset>>m_nLaynum;
	rdset>>str;
	rdset>>m_fRate;
//	rdset>>str;
//	rdset>>m_expect_err;
//	rdset>>str;
//	rdset>>m_maxcircle;
	rdset>>str;
	rdset>>m_nNodenum1;
	LayerNode=new int[m_nLaynum];
	LayerNode[0]=m_nNodenum1;
	for(int i=1;i<m_nLaynum-1;i++)
	{
		rdset>>str;
		rdset>>LayerNode[i];
	}
	rdset>>str;
	rdset>>m_nNodenumlast;
	LayerNode[m_nLaynum-1]=m_nNodenumlast;
	rdset.close();

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CANNdemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CANNdemoDlg)
	DDX_Control(pDX, IDC_Status_EDIT, m_status_edit);
	DDX_Text(pDX, IDC_forNorMax_EDIT, m_forNorMax);
	DDX_Text(pDX, IDC_forNorMin_EDIT, m_forNorMin);
	DDX_Text(pDX, IDC_fRate_EDIT, m_fRate);
	DDX_Text(pDX, IDC_Grandmax_EDIT, m_Grandmax);
	DDX_Text(pDX, IDC_Grandmin_EDIT, m_Grandmin);
	DDX_Text(pDX, IDC_nLaynum_EDIT, m_nLaynum);
//	DDX_Text(pDX, IDC_nNodenum1_EDIT, m_nNodenum1);
//	DDX_Text(pDX, IDC_nNodenumfirst_EDIT, m_nNodenumfirst);
//	DDX_Text(pDX, IDC_nNodenumlast_EDIT, m_nNodenumlast);
	DDX_Text(pDX, IDC_expect_err_EDIT, m_expect_err);
	DDX_Text(pDX, IDC_maxcircle_EDIT, m_maxcircle);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CANNdemoDlg, CDialog)
	//{{AFX_MSG_MAP(CANNdemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_TrainNewNet_Button, OnTrainNewNetButton)
	ON_BN_CLICKED(IDC_TrainOldNet_Button, OnTrainOldNetButton)
	ON_BN_CLICKED(IDC_RunNet_Button, OnRunNetButton)
	ON_BN_CLICKED(IDC_Initialparms_Botton, OnInitialparmsBotton)
	ON_EN_CHANGE(IDC_nLaynum_EDIT, OnChangenLaynumEDIT)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CANNdemoDlg message handlers

BOOL CANNdemoDlg::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 CANNdemoDlg::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 CANNdemoDlg::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 CANNdemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CANNdemoDlg::OnTrainNewNetButton() 
{
	// TODO: Add your control notification handler code here
    UpdateData(1);
    CString cs;
	cs.Format("Training A new Net Now!");
	m_status_edit.SetWindowText(cs);
	SaveData();
    
	CNet net;
    net.group_train(m_expect_err,m_maxcircle);
    cs+=0x0D;
	cs+=0x0A;
	cs+="Group training...";
    m_status_edit.SetWindowText(cs);

	net.save_weight();
    cs+=0x0D;
	cs+=0x0A;
	cs+="weight saved.";
    m_status_edit.SetWindowText(cs);

	net.save_threshold();
	cs+=0x0D;
	cs+=0x0A;
	cs+="Threshold saved.";
	cs+=0x0D;
	cs+=0x0A;
	cs+="A new net has been trained already.";
    m_status_edit.SetWindowText(cs);


	//Output msg to guide the client


}

void CANNdemoDlg::OnTrainOldNetButton() 
{
	// TODO: Add your control notification handler code here

	UpdateData(1);
	SaveData();

	CNet net;
	net.get_weight();
    net.get_threshold();
    net.group_train(m_expect_err,m_maxcircle);
    net.save_weight();
    net.save_threshold();

	//Output msg to guide the client
}

void CANNdemoDlg::OnRunNetButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	SaveData();
    CNet net;
	net.run();
	char output[10000];
    for (int i=0;i<10000;i++)
		output[i]=0;

    CFile file;
	file.Open("outcome.txt",CFile::modeRead,NULL);
	file.Read(output,10000);
	file.Close();
	m_status_edit.SetWindowText(output);

}

void CANNdemoDlg::OnInitialparmsBotton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);

	

	CnNodeDlg nDlg;
	nDlg.nLay_num=m_nLaynum;
	nDlg.nNodes= new int[nDlg.nLay_num];
    for(int i=0;i<nDlg.nLay_num;i++)  nDlg.nNodes[i]=LayerNode[i];
	

	if(nDlg.DoModal()==IDOK)
	{
    for(int i=0;i<nDlg.nLay_num;i++)  LayerNode[i]=nDlg.nNodes[i];
	}
}

void CANNdemoDlg::OnOK() 
{
	// TODO: Add extra validation here
	SaveData();
/*	UpdateData(true);
    ofstream sw("setting.txt");
    assert(sw);
	CString str;
    str="[随机函数最大值]= ";
    sw<<str<<m_Grandmax<<endl;
    str="[随机函数最小值]= ";
	sw<<str<<m_Grandmin<<endl;
	str="[归一化最大值]= ";
	sw<<str<<m_forNorMax<<endl;
	str="[归一化最小值]= ";
	sw<<str<<m_forNorMin<<endl;
	sw<<endl;
	str="[层数]= ";
	sw<<str<<m_nLaynum<<endl;
	str="[学习率]= ";
	sw<<str<<m_fRate<<endl;

	str="[输入层节点数]= ";
	sw<<str<<LayerNode[0]<<endl;
	for(int i=1;i<m_nLaynum-1;i++)
	{
		  str.Format("[第%d隐层节点数]= ",i);
		  sw<<str<<LayerNode[i]<<endl;

	}
	str="[输出层节点数]= ";
	sw<<str<<LayerNode[m_nLaynum-1]<<endl;
	sw.close();

*/

	
//	CDialog::OnOK();
}

void CANNdemoDlg::OnChangenLaynumEDIT() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(true);
	delete []LayerNode;
	LayerNode=new int[m_nLaynum];

//	LayerNode=new float[m_nLaynum];
	for(int i=0;i<m_nLaynum;i++) LayerNode[i]=0;	
}

void CANNdemoDlg::SaveData()
{
	UpdateData(true);
    ofstream sw("setting.txt");
    assert(sw);
	CString str;
    str="[随机函数最大值]= ";
    sw<<str<<m_Grandmax<<endl;
    str="[随机函数最小值]= ";
	sw<<str<<m_Grandmin<<endl;
	str="[归一化最大值]= ";
	sw<<str<<m_forNorMax<<endl;
	str="[归一化最小值]= ";
	sw<<str<<m_forNorMin<<endl;
	sw<<endl;
	str="[层数]= ";
	sw<<str<<m_nLaynum<<endl;
	str="[学习率]= ";
	sw<<str<<m_fRate<<endl;

	str="[输入层节点数]= ";
	sw<<str<<LayerNode[0]<<endl;
	for(int i=1;i<m_nLaynum-1;i++)
	{
		  str.Format("[第%d隐层节点数]= ",i);
		  sw<<str<<LayerNode[i]<<endl;

	}
	str="[输出层节点数]= ";
	sw<<str<<LayerNode[m_nLaynum-1]<<endl;
	sw.close();
}

⌨️ 快捷键说明

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