📄 bpnetdlg.cpp
字号:
// BPNetDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BPNet.h"
#include "BPNetDlg.h"
#include "math.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()
/////////////////////////////////////////////////////////////////////////////
// CBPNetDlg dialog
CBPNetDlg::CBPNetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBPNetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBPNetDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bLearning = false;
}
void CBPNetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBPNetDlg)
DDX_Control(pDX, IDC_SHOWLIST2, m_ListCtrl2);
DDX_Control(pDX, IDC_SHOWLIST1, m_ListCtrl1);
DDX_Control(pDX, IDC_TREE_INFO2, m_TreeCtrl2);
DDX_Control(pDX, IDC_TREE_INFO1, m_TreeCtrl1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBPNetDlg, CDialog)
//{{AFX_MSG_MAP(CBPNetDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_LEARN, OnButtonLearn)
ON_BN_CLICKED(IDC_BUTTON_OUTPUT, OnButtonOutput)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBPNetDlg message handlers
BOOL CBPNetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
LV_COLUMN lvColumn;
lvColumn.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH;
lvColumn.fmt=LVCFMT_LEFT;
lvColumn.cx=100;
lvColumn.iSubItem=0;//listview1的设置
lvColumn.pszText=" sample";
m_ListCtrl1.InsertColumn(0,&lvColumn);
lvColumn.iSubItem=1;
lvColumn.pszText=" output";
m_ListCtrl1.InsertColumn(1,&lvColumn);
lvColumn.iSubItem=2;
lvColumn.pszText=" target";
m_ListCtrl1.InsertColumn(2,&lvColumn);
lvColumn.iSubItem=0;//listview2的设置
lvColumn.pszText=" sample";
m_ListCtrl2.InsertColumn(0,&lvColumn);
lvColumn.iSubItem=1;
lvColumn.pszText=" output";
m_ListCtrl2.InsertColumn(1,&lvColumn);
// 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 CBPNetDlg::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 CBPNetDlg::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 CBPNetDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBPNetDlg::OnButtonLearn()
{
// TODO: Add your control notification handler code here
m_bLearning = true;
learning();
}
void CBPNetDlg::OnButtonOutput()
{
// TODO: Add your control notification handler code here
if(!m_bLearning)
{
AfxMessageBox("请先学习,点击“learning”按钮");
return;
}
output_generation();
m_bLearning = false;
}
void CBPNetDlg::learning()
{
if((fl=fopen("learning.txt","w"))==NULL)
{
AfxMessageBox("不能打开learning.txt");
return;
}
int result;
user_session();//任务设置
set_up();//基本设置
init();//初始化
do
{
initwt();//初始化权
result=rumelhart(0,ninput);//B-P算法
}
while(result==RESTRT);
if(result==FEXIT)
{
AfxMessageBox("\nMax number of iterations reached");
AfxMessageBox("\n but failed to decrease system");
AfxMessageBox("\n error sufficiently");
}
dwrite(task_name);//学习结果存入
wtwrite(task_name);//权的写入
fclose(fl);
}
void CBPNetDlg::user_session()//任务设置
{
int i,j;
CString fnam,dtype;
FILE *fp;
task_name="par3";//任务名称
ninattr=3;//输入神经元个数
noutattr=1;//输出神经元个数
ninput=8;//输入样本个数
fnam="par3.dat";//任务来源文件
TV_ITEM tvItem;//控件风格
tvItem.mask=TVIF_TEXT|TVIF_PARAM;//pszText和cchTextMax成员有效
tvItem.pszText = "任务设置";
tvItem.cchTextMax=2;//字符大小
TV_INSERTSTRUCT tvInsert;//表示项目在树中的位置
tvInsert.hParent=TVI_ROOT;//项目插入到视图的根部
tvInsert.hInsertAfter=TVI_LAST;//在列表末插入项目
tvInsert.item=tvItem;//定义将加入到树视图控件的项目的TV_ITEM结构
HTREEITEM hRoot=m_TreeCtrl1.InsertItem(&tvInsert);//得到目录位置
tvItem.pszText="学习模式为par3";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="输入样本个数8";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="任务来源文件为par3.dat";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="输入神经元个数为3";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="输出神经元个数为1";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
if((fp=fopen(fnam,"r"))==NULL)
{
AfxMessageBox("cannot open\n");
return;
}
for(i=0;i<ninput;i++)
{
for(j=0;j<ninattr;j++)
{
fscanf(fp,"%f",&input[i][j]);
}
for(j=0;j<noutattr;j++)
{
fscanf(fp,"%f",&target[i][j]);
}
}
if ((i=fclose(fp))!=0)
{
AfxMessageBox("\n file cannot be closed ");
return;
}
}
void CBPNetDlg::set_up()//定义网络结构
{
int i;
alpha=(float)0.9;//学习率
eta=(float)0.7;//冲量系数
maxe=(float)0.000001;//学习精度
maxep=(float)0.0000001; //最大干扰值
cnt_num=20000;//循环次数
nhlayer=1;//隐层数
TV_ITEM tvItem;//控件风格
tvItem.mask=TVIF_TEXT|TVIF_PARAM;//pszText和cchTextMax成员有效
tvItem.pszText = "基本信息设置";
tvItem.cchTextMax=2;//字符大小
TV_INSERTSTRUCT tvInsert;//表示项目在树中的位置
tvInsert.hParent=TVI_ROOT;//项目插入到视图的根部
tvInsert.hInsertAfter=TVI_LAST;//在列表末插入项目
tvInsert.item=tvItem;//定义将加入到树视图控件的项目的TV_ITEM结构
HTREEITEM hRoot=m_TreeCtrl1.InsertItem(&tvInsert);//得到目录位置
tvItem.pszText="隐层数为1";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="冲量系数eta=0.7";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="学习率alpha=0.9";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="学习精度maxe=0.000001";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="最大干扰值maxep=0.0000001";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="隐层神经元数目为7";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
tvItem.pszText="循环次数cnt_num=20000";
tvItem.cchTextMax=6;//字符大小
tvInsert.hParent=hRoot;//设置父母录为hRoot
tvInsert.item=tvItem;
m_TreeCtrl1.InsertItem(&tvInsert);
//隐层神经元数目相同 均为7
for(i=0;i<nhlayer;i++)
{
nunit[i+1]=7;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -