📄 bpnetdlg.cpp
字号:
// BPnetDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BPnet.h"
#include "BPnetDlg.h"
#include "math.h"
#include "iostream.h"
#include "stdio.h"
#include "fstream.h"
#include "iomanip.h"
#include "stdlib.h"
#include "time.h"
#include "SetDlg.h"
#include "SplashWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int flag;
extern int aa;
extern int bb;
extern int cc;
extern int f1;
extern int f2;
extern int times;
extern double wucha;
extern double a1;
extern double b1;
extern double alpha;
int N=50;
int Nfanhua=50;
int study=0;//训练次数
double c=0;
double cwork=0;
double cerror[10000];
double cerrorwork[10000];
double traininputmax[10];
double traininputmin[10];
double trainteachmax[10];
double trainteachmin[10];
double fanhuainputmax[10];
double fanhuainputmin[10];
double fanhuateachmax[10];
double fanhuateachmin[10];
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
Flagpicture=0;
point.x=0;
point.y=4000;
m_brush.CreateSolidBrush(RGB(100,120,250));
m_brush2.CreateSolidBrush(RGB(255,255,255));
pen1.CreatePen(PS_SOLID,2,RGB(0,0,0));
pen2.CreatePen(PS_SOLID,12,RGB(200,200,250));
pen3.CreatePen(PS_DOT,1,RGB(255,71,170));
pen4.CreatePen(PS_SOLID,1,RGB(255,255,255));
pen5.CreatePen(PS_SOLID,2,RGB(166,69,212));
pen6.CreatePen(PS_SOLID,2,RGB(0,255,0));
font1.CreatePointFont(2500,"宋体",NULL);
font2.CreatePointFont(100,"宋体",NULL);
font3.CreatePointFont(100,"仿宋体",NULL);
IN1=3;
HN=3;
ON=2;
Pre_error=0.1;
buttonflag1=FALSE;
buttonflag2=FALSE;
buttonflag3=FALSE;
buttonflag4=FALSE;
buttonflag5=FALSE;
flaghand1=TRUE;
flaghand2=FALSE;
flaghand3=FALSE;
flaghand4=FALSE;
flaghand6=FALSE;
flaghand7=FALSE;
flagdynamic=FALSE;
flagerrpicture=FALSE;
flaglast=TRUE;
flagsuofang=0;
TIMER=0;
}
void CBPnetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBPnetDlg)
DDX_Control(pDX, IDOK, m_button7);
DDX_Control(pDX, IDC_DELETE, m_button5);
DDX_Control(pDX, IDC_SET, m_button4);
DDX_Control(pDX, IDC_TRAIN, m_button3);
DDX_Control(pDX, IDC_INITIAL, m_button2);
DDX_Control(pDX, IDC_GETDATA, m_button1);
//}}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_GETDATA, OnGetdata)
ON_BN_CLICKED(IDC_INITIAL, OnInitial)
ON_BN_CLICKED(IDC_TRAIN, OnTrain)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_SET, OnSet)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBPnetDlg message handlers
BOOL CBPnetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_button1.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button1.SetFontColor( RGB(2,0,0) );
m_button2.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button2.SetFontColor( RGB(2,0,0) );
m_button3.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button3.SetFontColor( RGB(2,0,0) );
m_button4.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button4.SetFontColor( RGB(2,0,0) );
m_button5.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button5.SetFontColor( RGB(2,0,0) );
m_button7.LoadBitmaps(IDB_BITMAP2,5, 5, 5, 5, 4 );
m_button7.SetFontColor( RGB(2,0,0) );
SetTimer(1,500,NULL);
// 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
CSplashWnd::ShowSplashScreen(3000, IDB_SPLASH24, this);
CRect dlgRect,dlgRect1;
GetWindowRect(dlgRect); //对话框窗口大小,注意:不是客户区窗口大小
GetDesktopWindow()->GetWindowRect(DesktopRect); //取得屏幕窗口大小
dlgDx = 23; //-递增的宽
dlgDy = 14;
GetClientRect(&dlgRect1);//- 递增的高
dlgWidth = 1200;//设置窗口的最终大小
dlgHeight =720;//
MoveWindow
(
( DesktopRect.Width() - dlgRect.Width() )/2,
( DesktopRect.Height() - dlgRect.Height() )/2,
0,
0
);
::SetTimer(this->m_hWnd, 0,40,NULL);
return TRUE; // return TRUE unless you set the focus to a control
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()
{
CPaintDC dc(this);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
// 获取绘制坐标的文本框
CWnd* pWnd = GetDlgItem(IDC_EDIT1);
// 指针
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
CRect rectClient;
pWnd-> GetClientRect(rectClient);
pDC->SelectObject(&pen2);
pDC->SelectObject(&pBrush);
pDC->Rectangle(CRect(rectClient.left,rectClient.top,rectClient.right,rectClient.bottom));
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(12000,1200);
int orgh=rectClient.Width()/9;
int orgz=4*rectClient.Height()/5;
pDC->SetViewportExt(rectClient.right,-rectClient.bottom);
pDC->SetViewportOrg(orgh,orgz);
if(flaglast==TRUE)
{
picture();
}
CString stimes , swucha,sN,sNfanhua;
stimes.Format("%d",times);
swucha.Format("%.2f",wucha);
sN.Format("%d",N);
sNfanhua.Format("%d",Nfanhua);
if(f1==1)
{
pDC->SetTextColor(RGB(79,155,255));
pDC->SelectObject(&font1);
pDC->TextOut(3000,4700,"样本个数:");
pDC->TextOut(4700,4700,sN);
pDC->TextOut(6000,4700,"泛化总数:");
pDC->TextOut(8200,4700,sNfanhua);
if(f2==2)
{
pDC->SelectObject(&font1);
pDC->SetTextColor(RGB(255,0,0));
pDC->TextOut(3000,4300,"预定误差:");
pDC->TextOut(4500,4300,swucha);
pDC->TextOut(6000,4300,"预定训练次数:");
pDC->TextOut(8200,4300,stimes);
}
if(f2==0)
{
pDC->SelectObject(&font1);
pDC->SetTextColor(RGB(0,255,0));
pDC->TextOut(3000,4300,"预定误差:");
pDC->TextOut(4500,4300,swucha);
}
if(f2==1)
{
pDC->SelectObject(&font1);
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(6000,4300,"预定训练次数:");
pDC->TextOut(8200,4300,stimes);
}
if(flagdynamic==TRUE)
{
dynamicpicture1();
dynamicpicture2();
dynamicpicture3();
dynamicpicture4();
}
}
if(flag==1)
{
picture1();
}
if(Flagpicture==1)
{
picture2();
picture3();
picture4();
picture5();
}
if( flaghand1==TRUE)
handpicture1();
if( flaghand2==TRUE)
{handpicture2();}
if( flaghand3==TRUE)
{handpicture3();}
if( flaghand4==TRUE)
{handpicture4();}
if( flaghand6==TRUE)
{handpicture6();}
if( flaghand7==TRUE)
{handpicture7();}
introducepicture();
if(flagerrpicture==TRUE)
{
errpicture();
errworkpicture();
dynamicpicture1();
dynamicpicture2();
dynamicpicture3();
dynamicpicture4();
}
}
// 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::saveWV()
{
for(int i=0;i<HN;i++)
{
for(int j=0;j<IN1;j++)
{
old_W[i][j] = W[i][j];
}
}
for(int ii=0;ii<ON;ii++)
{
for(int jj=0;jj<HN;jj++)
{
old_V[ii][jj] = V[ii][jj];
}
}
}
void CBPnetDlg::intput_P(int m)
{
int i;
for(i=0;i<IN1;i++)
{
P[i]=Study_Data[m].input[i];
}
//获得第m个样本的数据
}
void CBPnetDlg::intput_T(int m)
{
for (int k=0;k<ON;k++)
{
T[k]=Study_Data[m].teach[k];
//子程序input_T(m)结束
}
}
/////////////////////////////////
//隐层各单元输入、输出值子程序///
/////////////////////////////////
void CBPnetDlg::H_I_O()
{
double sigma;
int i,j;
for (j=0;j<HN;j++)
{
sigma=0.0;
for (i=0;i<IN1;i++)
{
sigma+=W[j][i]*P[i];//求隐层内积
}
X[j]=sigma - YU_HN[j];//求隐层净输入
H[j]=1.0/(1.0+exp(-X[j]));//求隐层输出 siglon算法
}
}
void CBPnetDlg::O_I_O()
{
double sigma;
for (int k=0;k<ON;k++)
{
sigma=0.0;
for (int j=0;j<HN;j++)
{
sigma+=V[k][j]*H[j];//求输出层内积
}
Y[k]=sigma-YU_ON[k]; //求输出层净输入
O[k]=1.0/(1.0+exp(-Y[k]));//求输出层输出
}
}
void CBPnetDlg::Err_O_H(int m)
{
double finalerr[10];
for (int k=0;k<ON;k++)
{
abs_err[k]=T[k]-O[k];
d_err[k]=-abs_err[k]*O[k]*(1.0-O[k]);
T[k]=T[k]*(trainteachmax[k]-trainteachmin[k])+trainteachmin[k];
O[k]=O[k]*(trainteachmax[k]-trainteachmin[k])+trainteachmin[k];
finalerr[k]=T[k]-O[k];
finalerr[k]=finalerr[k]/T[k];
finalerr[k]=fabs(finalerr[k]);
err[m]+=finalerr[k];
}
err[m]=err[m]/ON;
}
void CBPnetDlg::Err_H_I()
{
double sigma;
for (int j=0;j<HN;j++)
{
sigma=0.0;
for (int k=0;k<ON;k++)
{
sigma=sigma+d_err[k]*V[k][j];
}
e_err[j]=sigma*H[k]*(1.0-H[k]);
}
}
////////////////////////////////////////////////////////
//输出层至隐层的权值调整、输出层阈值调整计算子程序//////
////////////////////////////////////////////////////////
void CBPnetDlg::Delta_O_H(int m, int n)
{
for (int k=0;k<ON;k++)
{
for (int j=0;j<HN;j++)
{
V[k][j]=V[k][j]-a1*d_err[k]*H[j]+alpha*(V[k][j]-old_V[k][j]);
}
YU_ON[k]=YU_ON[k]+a1*d_err[k];
}
}
void CBPnetDlg::Delta_H_I(int m, int n)
{
for (int j=0;j<HN;j++)
{
for (int i=0;i<IN1;i++)
{
W[j][i]=W[j][i]-b1*e_err[j]*P[i]+alpha*(W[j][i]-old_W[j][i]);
}
YU_HN[j]=YU_HN[j]+b1*e_err[j];
}
}
/////////////////////////////////
//N个样本的全局误差计算子程序////
/////////////////////////////////
void CBPnetDlg::OnGetdata()
{
if( buttonflag1==FALSE)
{
AfxMessageBox("请先设置参数!");
return;
}
else
{
CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
dlg.m_ofn.lpstrTitle="请载入训练样本";
if(dlg.DoModal () == IDOK)
{
inputdatafile = dlg.GetPathName ();
ifstream GetTrainingData;
GetTrainingData.open(inputdatafile);
for(int mm=0;mm<N;mm++)
{
for(int ii=0;ii<IN1;ii++)
{
GetTrainingData>>Study_Data[mm].input[ii]; //取得输入数据
}
for(int jj=0;jj<ON;jj++)
{
GetTrainingData>>Study_Data[mm].teach[jj]; //取得输出数据
}
}
/////以下开始归一化处理
for(int ii=0;ii<IN1;ii++)
{
traininputmax[ii]=Study_Data[0].input[ii];
traininputmin[ii]=Study_Data[0].input[ii];
}
for(int jj=0;jj<ON;jj++)
{
trainteachmin[jj]=Study_Data[0].teach[jj];
trainteachmax[jj]=Study_Data[0].teach[jj];
}
/////////以下是找到训练样本中输入输出的最大最小值
for(mm=0;mm<N;mm++)
{
for(ii=0;ii<IN1;ii++)
{
if(Study_Data[mm].input[ii]>=traininputmax[ii])
traininputmax[ii]=Study_Data[mm].input[ii];
if(Study_Data[mm].input[ii]<=traininputmin[ii])
traininputmin[ii]=Study_Data[mm].input[ii];
}
for(jj=0;jj<ON;jj++)
{
if(Study_Data[mm].teach[jj]>=trainteachmax[jj])
trainteachmax[jj]=Study_Data[mm].teach[jj];
if(Study_Data[mm].teach[jj]<=trainteachmin[jj])
trainteachmin[jj]=Study_Data[mm].teach[jj];
}
}
/////////以上是找到训练样本中输入输出的最大最小值
GetTrainingData.close();
}
CFileDialog dlg1(TRUE);
dlg1.m_ofn.lpstrTitle="请载入泛化样本";
dlg1.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
if(dlg1.DoModal () == IDOK)
{
inputdatafile1 = dlg1.GetPathName ();
ifstream GetWorkData;
GetWorkData.open(inputdatafile1);
buttonflag5=TRUE;
buttonflag2=TRUE;
flaghand2=FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -