📄 bpanndlg.cpp
字号:
// BpAnnDlg.cpp : implementation file
//
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//BpannsetDlg* pDialog;
CWnd *pWnd;
int Innodes=0,Hidelayer=0,Hidenodes=0,Outnodes=0;
double Learnratio=0.8; //学习类别
int m_Maxcount = 100; //最大迭代次数
double m_Precision = 0.001; //设置精度
double m_Moon = 1.1; //加快走出平坦区
double stepsize = 0.88; //步长通常取值在0.1-3之间
double inertia = 0.90; //惯性项系数 影响收敛速度
double Erro[300];
double Input[12] = {
0.1323 , 0.323 , -0.132 , -0.1342,
0.321 , 0.2434 , 0.456 , -0.5342,
-0.6546 , -0.3242 , 0.3255 , 0.2313
}; //训练集样本 即输入
double Output[12] = {
1.0 , 0.422 , -0.642 , -0.3425,
0.1 , 0.562 , 0.5675 , -0.3452,
-0.6464 , -0.756 , 0.11 , 0.2312
}; //预期输出值
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()
/////////////////////////////////////////////////////////////////////////////
// CBpAnnDlg dialog
CBpAnnDlg::CBpAnnDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBpAnnDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBpAnnDlg)
m_state = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pWnd = this;
}
void CBpAnnDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBpAnnDlg)
DDX_Text(pDX, IDC_STATE, m_state);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBpAnnDlg, CDialog)
//{{AFX_MSG_MAP(CBpAnnDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BPANNSET, OnBpannset)
ON_BN_CLICKED(IDC_BPANNSAVE, OnBpannsave)
ON_BN_CLICKED(IDC_BPANNINIT, OnBpanninit)
ON_BN_CLICKED(IDC_BPANNSAMPLE, OnBpannsample)
ON_BN_CLICKED(IDC_BPANNEXPECT, OnBpannexpect)
ON_BN_CLICKED(IDC_BPANNTRAIN, OnBpanntrain)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBpAnnDlg message handlers
BOOL CBpAnnDlg::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_BPANNSET)->EnableWindow(true);
GetDlgItem(IDC_BPANNINIT)->EnableWindow(false);
GetDlgItem(IDC_BPANNSAMPLE)->EnableWindow(false);
GetDlgItem(IDC_BPANNEXPECT)->EnableWindow(false);
GetDlgItem(IDC_BPANNTRAIN)->EnableWindow(false);
GetDlgItem(IDC_BPANNSAVE)->EnableWindow(false);
UpdateData(true);
return TRUE; // return TRUE unless you set the focus to a control
}
void CBpAnnDlg::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 CBpAnnDlg::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 CBpAnnDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBpAnnDlg::OnBpannset()
{
// TODO: Add your control notification handler code here
// 点击按钮显示对话框
// meathod 1
// pDialog = new BpannsetDlg();
// pDialog->Create(IDD_BPANNSET_DIALOG,this);
// pDialog->ShowWindow(SW_SHOW);
BpannsetDlg* newdlg=new BpannsetDlg;
newdlg->Create(IDD_BPANNSET_DIALOG,NULL);
newdlg->ShowWindow(SW_SHOW);
// meathod 2
// CDialog aboutDlg(IDD_BPANNSET_DIALOG);
// int nRet = -1;
// nRet = aboutDlg.DoModal();
//按钮允许
GetDlgItem(IDC_BPANNINIT)->EnableWindow(true);
//状态显示
m_state.Format("等待输入!\r\n");
UpdateData(false);
//按钮显示文字改变
// CString msg;
// msg.Format("Num");
// ((CBpAnnDlg *)pWnd)->SetDlgItemText(IDC_BPANNINIT, msg);
}
void CBpAnnDlg::OnBpanninit()
{
// TODO: Add your control notification handler code here
//按钮允许
GetDlgItem(IDC_BPANNSAMPLE)->EnableWindow(true);
//初始化
int tmp1=Ilnodes();
int tmp2=Hllayer();
int tmp3=Hlnodes();
int tmp4=Olnodes();
double tmp5=Lratio();
InitDataTrans(tmp1,tmp2,tmp3,tmp4,tmp5);
//状态显示
m_state.Format("BP神经网络初始设置完成!\r\n输入层节点数: %d\r\n隐层层数: %d\r\n隐层节点数: %d\r\n输出层节点: %d\r\n学习率: %.3f\r\n",tmp1,tmp2,tmp3,tmp4,tmp5);
UpdateData(false);
}
void CBpAnnDlg::OnBpannsample()
{
// TODO: Add your control notification handler code here
//按钮允许
GetDlgItem(IDC_BPANNEXPECT)->EnableWindow(true);
//状态显示
m_state.Format("选择待训练的样本文件.\r\n");
UpdateData(false);
}
void CBpAnnDlg::OnBpannexpect()
{
// TODO: Add your control notification handler code here
//按钮允许
GetDlgItem(IDC_BPANNTRAIN)->EnableWindow(true);
//状态显示
m_state.Format("选择期望的输出文件.\r\n");
UpdateData(false);
}
//=========================Train============================================
void CBpAnnDlg::OnBpanntrain()
{
// TODO: Add your control notification handler code here
//状态显示
m_state.Format("权值初始化开始!\r\n");
UpdateData(false);
//状态显示
m_state.Format("神经网络初始化中...请等待!\r\n");
UpdateData(false);
//===========================
//初始化
int Hidelayer_flag=1;
double *Wp = new double[Innodes*Hidenodes]; //输入层到隐层的权值
if (Wp==NULL)
{
m_state.Format("初始化权值失败!\r\n");
UpdateData(false);
return;
}
double *Vp=new double[Innodes*Hidenodes];//隐层到输出层的权值
if (Vp==NULL)
{
m_state.Format("初始化权值失败!\r\n");
UpdateData(false);
return;
}
double *Yp1=new double[Hidenodes*Hidenodes];
double *Yp2=new double[Hidenodes*Hidenodes];
double *Yp3=new double[Hidenodes*Hidenodes];
double *Yp4=new double[Hidenodes*Hidenodes];
switch(Hidelayer) //隐层到隐层的权值
{
case 1:
{
delete [] Yp1;
delete [] Yp2;
delete [] Yp3;
delete [] Yp4;
}
break;
case 2:
{
delete [] Yp2;
delete [] Yp3;
delete [] Yp4;
Hidelayer_flag=2;
}
break;
case 3:
{
delete [] Yp3;
delete [] Yp4;
Hidelayer_flag=3;
}
break;
case 4:
{
delete [] Yp4;
Hidelayer_flag=4;
}
break;
case 5:
{
Hidelayer_flag=5;
}
break;
default:
{
if (Hidelayer>5)
{
Hidelayer_flag=5;
}
else
{
m_state.Format("初始化隐层失败!\r\n");
UpdateData(false);
return;
}
}
break;
}
double *Y1=new double[Hidenodes];
double *Y2=new double[Hidenodes];
double *Y3=new double[Hidenodes];
double *Y4=new double[Hidenodes];
double *Y5=new double[Hidenodes];
switch(Hidelayer_flag) //隐层输出
{
case 1:
{
delete [] Y2;
delete [] Y3;
delete [] Y4;
delete [] Y5;
}
break;
case 2:
{
delete [] Y3;
delete [] Y4;
delete [] Y5;
}
break;
case 3:
{
delete [] Y4;
delete [] Y5;
}
break;
case 4:
{
delete [] Y5;
}
break;
default:
{
}
break;
}
double *Yout=new double[Outnodes];//输出层输出
double *Outpartial=new double[Outnodes];//输出层偏导
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -