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

📄 10dlg.cpp

📁 完成BP人工神经网络算法的学习
💻 CPP
字号:
// 10Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "10.h"
#include "10Dlg.h"
#include<stdlib.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()

/////////////////////////////////////////////////////////////////////////////
// CMy10Dlg dialog

CMy10Dlg::CMy10Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy10Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy10Dlg)
		// 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);
}

void CMy10Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy10Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy10Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy10Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy10Dlg message handlers

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

void CMy10Dlg::OnOK() 
{
	// TODO: Add extra validation here
	LinkList L= (LinkList)malloc(sizeof(LNode));
	double w1[6][16],w2[16];

	readexample(&L);           //读取实例
//	Print_L(&L);

   initiarray(w1,w2);         //随机初始权矩阵
 
//	printarray(w1,w2);


		

    learn(L, w1,w2);

	test(w1,w2);
	
}

#define Learn 0.6       //学习率
#define err 0.0000001    //允许最大误差 60

void CMy10Dlg::learn(LinkList L, double w1[][16],double w2[])
{
	int i,j,e;
	long count=0;
	double o3,temp,MAX=1;
	double E[7],E2[16],E3,o1[6],o2[16],I2[16],I3;
	LinkList p;


	while(MAX>err)     //和允许误差比较
	{
		e=1;
		p=L->next;  

		while(p)       //遍历整条链
		{  

		    o1[1]=p->x1;
			o1[2]=p->x2;
			o1[3]=p->x3;
			o1[4]=p->x4;
			o1[5]=p->x5;

			for(i=1;i<=15;i++)
			{
                I2[i]=w1[1][i]*o1[1]+w1[2][i]*o1[2]+w1[3][i]*o1[3]+w1[4][i]*o1[4]+w1[5][i]*o1[5];     //第2层输入
				o2[i]=tanh(I2[i]);   //第2层输出 
			}

            I3=0;
			for(i=1;i<=15;i++)
				I3+=w2[i]*o2[i];    //第3层输入
			o3=tanh(I3);            //第3层输出 
	
			E3=(p->y-o3)*(pow(2/(exp(I3)+exp(-I3)),2));     //输出层误差 


			E[e++]=fabs(E3);		    

			temp=0;
			for(i=1;i<=15;i++)
				temp+=w2[i]*E3;
			for(i=1;i<=15;i++)			
				E2[i]=pow(2/(exp(I2[i])+exp(-I2[i])),2);     //第二层误差		
				 			

              
            for(i=1;i<=15;i++)  				
					w2[i]=w2[i]-Learn*E3*o2[i];    //修改W2矩阵

			for(i=1;i<=5;i++)
				for(j=1;j<=15;j++)
					w1[i][j]=w1[i][j]-Learn*E2[j]*o1[i];     //修改W1矩阵				

			p=p->next;

		}//while(p)


	    count++;

		MAX=MAXE(E,e);          //返回最大误差


	}//while(MAX>err) 

 //   printf("\n******************* BP算法预测预测世界干散货运量 **********************\n");
//	printf("计算允许最大误差Err=%.8f\n",err);	
//	printf("学习产生最大误差MAX=%.8f\n",MAX);
//	printf("学习总次数count=%ld\n",count);
	CString   ss;
	ss.Format("%ld",count);
//	MessageBox(ss);
	SetDlgItemText(IDC_EDIT1,ss); 

}


void CMy10Dlg:: test(double w1[][16],double w2[])
{
	int i;
	double o1[6],o2[16],I2[16],I3,o3,E3,result;


/*	o1[1]=0.1622;    //借用一组实例产生误差E3
	o1[2]=0.1611;
	o1[3]=0.1615;
	o1[4]=0.1685;
	o1[5]=0.1789;
*/
	o1[1]=0.1588;    //借用一组实例产生误差E3
	o1[2]=0.1622;
	o1[3]=0.1611;
	o1[4]=0.1615;
	o1[5]=0.1785;

	
	
	for(i=1;i<=15;i++)
	{
        I2[i]=w1[1][i]*o1[1]+w1[2][i]*o1[2]+w1[3][i]*o1[3]+w1[4][i]*o1[4]+w1[5][i]*o1[5];     //第2层输入
		o2[i]=tanh(I2[i]);   //第2层输出 
	}

     I3=0;
	for(i=1;i<=15;i++)
	    I3+=w2[i]*o2[i];    //第3层输入
	o3=tanh(I3);            //第3层输出 

	E3=(0.1790-o3)*(pow(2/(exp(I3)+exp(-I3)),2));

   	o1[1]=0.1611;          //预测1997年运量
	o1[2]=0.1615;
	o1[3]=0.1685;
	o1[4]=0.1689;
	o1[5]=0.1790;

	
	
	for(i=1;i<=15;i++)
	{
        I2[i]=w1[1][i]*o1[1]+w1[2][i]*o1[2]+w1[3][i]*o1[3]+w1[4][i]*o1[4]+w1[5][i]*o1[5];     //第2层输入
		o2[i]=tanh(I2[i]);   //第2层输出 
	}

    I3=0;
	for(i=1;i<=15;i++)
	    I3+=w2[i]*o2[i];    //第3层输入
	o3=tanh(I3);  //第3层输出
	
//printf("根据已有数据可预测:1997年的运量预测值为%.6f百万吨\n\n",10000*(E3/(pow(2/(exp(I3)+exp(-I3)),2))+o3));

    result=(E3/(pow(2/(exp(I3)+exp(-I3)),2))+o3);

	CString   ss;
	ss.Format("%lf",result);
//	MessageBox(ss);
	SetDlgItemText(IDC_EDIT2,ss); 
}


void CMy10Dlg::readexample(LinkList *L)
{
		FILE *fp;
		LinkList p=*L,r;

		if((fp=fopen("c:\\bp2.txt","r"))==NULL)
		{
		     printf("Open file error!\n");
		     exit(0);
		}

		while(!feof(fp))
		{
			r=(LinkList)malloc(sizeof(LNode));
			r->next=NULL;
			p->next=r;
			fscanf(fp,"%f%f%f%f%f%f",&r->x1,&r->x2,&r->x3,&r->x4,&r->x5,&r->y);

			p=r;
		}

		fclose(fp);
}


void CMy10Dlg::initiarray(double w1[][16],double w2[])
{
	int i,j;
	for(i=1;i<=5;i++)
		for(j=1;j<=15;j++)
			w1[i][j]=rand()%100*1.0/100;

	for(i=1;i<=15;i++)		
			w2[i]=rand()%100*1.0/100;

	
}

double  CMy10Dlg::MAXE(double E[7],int e)
{
	double max=0;
	int i;

	for(i=1;i<e;i++)
		if(max<E[i])
			max=E[i];

		return max;
}
void CMy10Dlg:: printarray(double w1[][16],double w2[])
{
    int i,j;

	for(i=1;i<=5;i++)
		for(j=1;j<=15;j++)
			printf("%f  ",w1[i][j]);
		printf("\n");

	for(i=1;i<=15;i++)
		printf("%f  ",w2[i]);		
	printf("\n");

	
}

⌨️ 快捷键说明

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