⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nn_esdlg.cpp

📁 BP神经网络,可以进行任意发酵过程建模,包括初始化,训练与测试,
💻 CPP
字号:
// NN_ESDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NN_ES.h"
#include "NN_ESDlg.h"
#include "DlgInit.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


HBITMAP hbm;




//*******************************************************
//global.h
extern int gi_SN;
extern int gi_IN;
extern int YGXH;
//The sjwl header file...
//.......................................

//Global variables ...
int const MaxNumber=20;
extern double I[MaxNumber][300];
extern double H[MaxNumber][300];
extern double O[MaxNumber][300];

//...............................

extern double W_S_I[MaxNumber][MaxNumber];
extern double W_I_H[MaxNumber][MaxNumber];
extern double W_H_O[MaxNumber][MaxNumber];
//................................
extern double Thita_I[MaxNumber];//输入神经元阀值
extern double Thita_H[MaxNumber];//隐层神经元阀值
extern double Thita_O[MaxNumber];//输出层神经元阀值
//......................................
extern double Dlta_I[MaxNumber][300];//输入神经元在第PN个样本学习后DLTA值
extern double Dlta_H[MaxNumber][300];//隐层神经元在第PN个样本学习后DLTA值
extern double Dlta_O[MaxNumber][300];//输出层神经元在第PN个样本学习后DLTA值
//......................................
extern double Yita_S_I[MaxNumber][MaxNumber];
extern double Yita_I_H[MaxNumber][MaxNumber];
extern double Yita_H_O[MaxNumber][MaxNumber];
///////////////////////////////////
extern double TD0_I[MaxNumber][MaxNumber];
extern double TD0_H[MaxNumber][MaxNumber];
extern double TD0_O[MaxNumber][MaxNumber];
extern double TD1_I[MaxNumber][MaxNumber];
extern double TD1_H[MaxNumber][MaxNumber];
extern double TD1_O[MaxNumber][MaxNumber];
//////////////////////////////////////
extern double in_val[MaxNumber][300];//训练输入样本空间
extern double out_val[MaxNumber][300];//训练输出样本空间
extern double testin[300][MaxNumber];//测试输入样本空间
extern double testout[300][MaxNumber];//测试输出样本空间
extern double learnin[300][MaxNumber];//训练输入样本空间
extern double learnout[300][MaxNumber];//训练输出样本空间


///////////////////////////////////////
extern double lym_max[MaxNumber];
extern double lym_min[MaxNumber];
extern double Alpha;
extern int Count;
extern int recordnumber;
extern double E;
extern double error[300];
extern double s[300][MaxNumber];
extern double learnerror;
extern double testerror;
extern double getData[300][MaxNumber];
extern double errora,errorb;
extern int testnum;
extern int learnnum;
extern int number;
extern bool mxxz;

extern int BPNN_SN;
extern int BPNN_IN;
extern int BPNN_HN;
extern int BPNN_ON;
extern int BPNN_PN;   //最大样本记录数
extern int BPNN_GDZ;

extern char strInputFileName[1000];
extern char strOutputFileName[1000];





extern bool Compare();
extern void	ForwardSI();
extern void	ForwardIH();
extern void	ForwardHO();
extern void	Counterror();
extern void	BackDlta_O();
extern void	BackDlta_H();
extern void	BackDlta_I();
extern void	totalerror();
extern void	BackTD_O_H();
extern void	BackTD_H_I();
extern void	BackTD_I_S();
extern void	BackYita_O_H();
extern void	BackYita_H_I();
extern void	BackYita_I_S();
extern void	BackW_O_H();
extern void	BackThita_O_H();
extern void	BackW_H_I();
extern void	BackThita_H_I();
extern void	BackW_I_S();
extern void BackThita_I_S();
extern bool mxxz;
extern void test();

//*******************************************************






/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CNN_ESDlg dialog

CNN_ESDlg::CNN_ESDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNN_ESDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNN_ESDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNN_ESDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNN_ESDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNN_ESDlg, CDialog)
	//{{AFX_MSG_MAP(CNN_ESDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDQuit, OnQuit)
	ON_BN_CLICKED(IDDisplay, OnDisplay)
	ON_BN_CLICKED(IDInit, OnInit)
	ON_BN_CLICKED(IDLearn, OnLearn)
	ON_BN_CLICKED(IDTest, OnTest)
	ON_BN_CLICKED(IDC_RADIOYG, OnRadioyg)
	ON_BN_CLICKED(IDC_RADIOCL, OnRadiocl)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNN_ESDlg message handlers

BOOL CNN_ESDlg::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
	
	hbm=(HBITMAP)LoadImage(
		AfxGetInstanceHandle (),
		"d:\\Nn_es5-13\\未命名.bmp",IMAGE_BITMAP,
		0,0,LR_LOADFROMFILE | LR_DEFAULTSIZE);


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNN_ESDlg::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 CNN_ESDlg::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
	{

		CPaintDC dc(this); // device context for painting

		CDC dcBmp;
		dcBmp.CreateCompatibleDC(&dc);
		dcBmp.SelectObject(hbm);
		dc.BitBlt(100,100,400,400,&dcBmp,0,0,SRCCOPY);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNN_ESDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNN_ESDlg::OnQuit() 
{
	// TODO: Add your control notification handler code here
	
	EndDialog(0);
}

void CNN_ESDlg::OnDisplay() 
{
	// TODO: Add your control notification handler code here
	extern void Output();
	Output();
    MessageBox("输出结束");
}

void CNN_ESDlg::OnInit() 
{
	// TODO: Add your control notification handler code here
	CDlgInit dl;
	dl.DoModal();
}

void CNN_ESDlg::OnLearn() 
{
	double learnerr_max=0;
	char buf[200];
	CBrush br;
	CPen   pen;
	int const iSx=50,iEx=540,iSy=50,iEy=400;
	int const iOx=70,iOy=380;
	pen.CreatePen(PS_SOLID,1,RGB(50,50,50));
	br.CreateSolidBrush(RGB(255,255,255));
	CClientDC dc(this);
	dc.SelectObject(&br);
	dc.SelectObject(&pen);
	
	dc.Rectangle(iSx,iSy,iEx,iEy);
	
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iOx,iSy+20);
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iEx-20,iOy);
    
	// TODO: Add your control notification handler code here
	number=0;
	do{
		//每个记录学习一次,并存放
		for(Count=0;Count<learnnum;Count++)
		{
			ForwardSI();
			ForwardIH();
			ForwardHO();
			//........................................
			//计算一个条记录学习后误差
			Counterror();
			//.........................................
			//保存一个样本学习后DELTA值       
			BackDlta_O();
			BackDlta_H();
			BackDlta_I();
		}
		//......................................
        totalerror();
		//......................................    
		if(Compare())
			break;
		else        //否则修正权值与阀值
		BackTD_O_H();
		BackTD_H_I();
		BackTD_I_S();
		//...............
		if(	number==0)
			errora=learnerror;
		if(number>0)
		{
			errorb=learnerror;
			BackYita_O_H();
			BackYita_H_I();
			BackYita_I_S();
			errora=errorb;	
		}	
		//..................
		BackW_O_H();
		BackThita_O_H();
		BackW_H_I();
		BackThita_H_I();
		BackW_I_S();
		BackThita_I_S();
		number++;
		//画线
    if(number==1)
	dc.MoveTo(iOx,iOy-int((iOy-iSy-20)*learnerror/50));
	dc.LineTo(iOx+int((iEx-iOx-20)*number/BPNN_GDZ),iOy-int((iOy-iSy-20)*learnerror/50));
    dc.MoveTo(iOx+int((iEx-iOx-20)*number/BPNN_GDZ),iOy-int((iOy-iSy-20)*learnerror/50));
    if(learnerr_max<learnerror) learnerr_max=learnerror;
	}while(!Compare());
    sprintf(buf,"实际全局均方差为:%f",learnerror);
	dc.TextOut(200,100,buf);
	sprintf(buf,"实际训练次数为:%d 次",number);
	dc.TextOut(200,120,buf);
	//dc.TextOut(20,400,"5000");
    sprintf(buf,"y=%f",learnerr_max);
	dc.TextOut(70,60,buf);
    sprintf(buf,"x=%d",BPNN_GDZ);
	dc.TextOut(490,383,buf);
    dc.TextOut(70,383,"0");
	MessageBox("学习结束");
}

void CNN_ESDlg::OnTest() 
{
	// TODO: Add your control notification handler code here
	double testerr_max=0,temp;
	double testerr_min=100;
	CBrush br;
	CPen   pen;
	int const iSx=50,iEx=540,iSy=50,iEy=400;
	int const iOx=70,iOy=380;
	pen.CreatePen(PS_SOLID,1,RGB(100,100,100));
	br.CreateSolidBrush(RGB(255,255,255));
	CClientDC dc(this);
	dc.SelectObject(&br);
	dc.SelectObject(&pen);
	dc.Rectangle(iSx,iSy,iEx,iEy);
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iOx,iSy+20);
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iEx-20,iOy);
	double sum;
    
/////////////////////////////////
	double realval[40];
	double testval[40];
	for(Count=0;Count<testnum;Count++)
	{
		error[Count]=0.0;	
		for (int i=0;i<BPNN_IN;i++)
		{
			sum=0.0;
			for(int s=0;s<BPNN_SN;s++)
			{
				sum+=testin[Count][s]*W_S_I[s][i];		
			}
			sum=sum-Thita_I[i];
			I[i][Count]=1/(1+exp(-sum));
		}		
		void ForwardIH();
		void ForwardHO();
///////////////////////////////////////////

if(mxxz)
	{
		realval[Count]=(lym_max[YGXH]-lym_min[YGXH])*testin[Count][YGXH]+lym_min[YGXH];
	    testval[Count]=(lym_max[YGXH]-lym_min[YGXH])*O[BPNN_ON-1][Count]+lym_min[YGXH];
        if(Count>1)
		{
        temp=realval[Count]-testval[Count-1];
		if (temp<0)temp=-temp;
		if(testerr_max<temp) testerr_max=temp;	
		if(testerr_min>temp) testerr_min=temp;	
		}
 /////////////////////////////////////////////////
    	if(Count==0)
		{
		dc.MoveTo(iOx,iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));

		dc.Arc(iOx+int((iEx-iOx-20)*(Count+1)/18),
			iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH]),
			iOx+int((iEx-iOx-20)*(Count+1)/18)+3,
			iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH])+3,
			0,0,0,0);
		}
    	else 	   
		{
		dc.LineTo(iOx+int((iEx-iOx-20)*Count/18),iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));
		dc.MoveTo(iOx+int((iEx-iOx-20)*Count/18),iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));
	
		dc.Arc(iOx+int((iEx-iOx-20)*(Count+1)/18),
			iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH]),
			iOx+int((iEx-iOx-20)*(Count+1)/18)+3,
			iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH])+3,
			0,0,0,0);
		}
		}
 ////////////////////////////////////////////////////////////////////////
 //软测量模型测试
  else
	{
		realval[Count]=(lym_max[YGXH]-lym_min[YGXH])*testout[Count][BPNN_ON-1]+lym_min[YGXH];
	    testval[Count]=(lym_max[YGXH]-lym_min[YGXH])*O[BPNN_ON-1][Count]+lym_min[YGXH];
        temp=realval[Count]-testval[Count];
		if(temp<0)temp=-temp;
		if(testerr_max<temp) testerr_max=temp;	
		if(testerr_min>temp) testerr_min=temp;	
 /////////////////////////////////////////////////
    	if(Count==0)
		{
		dc.MoveTo(iOx,iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));
		dc.Arc(iOx+int((iEx-iOx-20)*Count/18),
		iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH]),
		iOx+int((iEx-iOx-20)*Count/18)+3,
		iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH])+3,0,0,0,0);
		}
		else 	   
		{
		dc.LineTo(iOx+int((iEx-iOx-20)*Count/18),iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));
		dc.MoveTo(iOx+int((iEx-iOx-20)*Count/18),iOy-int((iOy-iSy-20)*realval[Count]/lym_max[YGXH]));
		dc.Arc(iOx+int((iEx-iOx-20)*Count/18),
		iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH]),
		iOx+int((iEx-iOx-20)*Count/18)+3,
		iOy-int((iOy-iSy-20)*testval[Count]/lym_max[YGXH])+3,0,0,0,0);
		}
	 }
}
/////////////////////////////for循环结束
    char buf[300];
    sprintf(buf,"y=%f",lym_max[YGXH]);
	dc.TextOut(70,50,buf);
    dc.TextOut(490,383,"x=72小时");
    dc.TextOut(70,383,"0");
    sprintf(buf,"相对误差的最大值为:%f",testerr_max/lym_max[YGXH]);
	dc.TextOut(80,200,buf);
	sprintf(buf,"相对误差的最小值为:% f",testerr_min/lym_max[YGXH]);
	dc.TextOut(80,220,buf);	
	MessageBox("测试结束"); 
}

void CNN_ESDlg::GraphInit()
{
	CBrush br;
	CPen   pen;
	int const iSx=50,iEx=540,iSy=50,iEy=400;
	int const iOx=70,iOy=380;
	pen.CreatePen(PS_SOLID,1,RGB(255,255,255));
	br.CreateSolidBrush(RGB(128,128,128));
	CClientDC dc(this);
	dc.SelectObject(&br);
	dc.SelectObject(&pen);
	
	dc.Rectangle(iSx,iSy,iEx,iEy);
	
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iOx,iSy+20);
	dc.MoveTo(iOx,iOy);
	dc.LineTo(iEx-20,iOy);

}

void CNN_ESDlg::OnRadioyg() 
{
	// TODO: Add your control notification handler code here
	mxxz=true;
}

void CNN_ESDlg::OnRadiocl() 
{
	// TODO: Add your control notification handler code here
		mxxz=false;
}

⌨️ 快捷键说明

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