📄 nesorsdlg.cpp
字号:
// nesorsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "nesors.h"
#include "nesorsDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CNesorsDlg dialog
CNesorsDlg::CNesorsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNesorsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNesorsDlg)
m_000 = 0.0f;
m_001 = 0.0f;
m_002 = 0.0f;
m_003 = 0.0f;
m_010 = 0.0f;
m_011 = 0.0f;
m_012 = 0.0f;
m_013 = 0.0f;
m_020 = 0.0f;
m_021 = 0.0f;
m_022 = 0.0f;
m_100 = 0.0f;
m_101 = 0.0f;
m_102 = 0.0f;
m_103 = 0.0f;
m_110 = 0.0f;
m_111 = 0.0f;
m_112 = 0.0f;
m_113 = 0.0f;
m_023 = 0.0f;
m_iteration = 0;
m_buchang = 0.0f;
m_wucha = 0.0f;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNesorsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNesorsDlg)
DDX_Text(pDX, IDC_000, m_000);
DDX_Text(pDX, IDC_001, m_001);
DDX_Text(pDX, IDC_002, m_002);
DDX_Text(pDX, IDC_003, m_003);
DDX_Text(pDX, IDC_010, m_010);
DDX_Text(pDX, IDC_011, m_011);
DDX_Text(pDX, IDC_012, m_012);
DDX_Text(pDX, IDC_013, m_013);
DDX_Text(pDX, IDC_020, m_020);
DDX_Text(pDX, IDC_021, m_021);
DDX_Text(pDX, IDC_022, m_022);
DDX_Text(pDX, IDC_100, m_100);
DDX_Text(pDX, IDC_101, m_101);
DDX_Text(pDX, IDC_102, m_102);
DDX_Text(pDX, IDC_103, m_103);
DDX_Text(pDX, IDC_110, m_110);
DDX_Text(pDX, IDC_111, m_111);
DDX_Text(pDX, IDC_112, m_112);
DDX_Text(pDX, IDC_113, m_113);
DDX_Text(pDX, IDC_023, m_023);
DDX_Text(pDX, IDC_TERATION, m_iteration);
DDX_Text(pDX, IDC_BUCHANG, m_buchang);
DDX_Text(pDX, IDC_WUCHA, m_wucha);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNesorsDlg, CDialog)
//{{AFX_MSG_MAP(CNesorsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_READDATA, OnReaddata)
ON_BN_CLICKED(IDC_TRAINDATA, OnTraindata)
ON_BN_CLICKED(IDC_SHIBIE, OnShibie)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNesorsDlg message handlers
BOOL CNesorsDlg::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 CNesorsDlg::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 CNesorsDlg::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 CNesorsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNesorsDlg::OnReaddata()
{
// TODO: Add your control notification handler code here
CStdioFile sfile;
sfile.Open("Iris.txt",CFile::modeRead | CFile::shareDenyNone);
char stringline[90];
int j=0;
while(j<20)
{
sfile.ReadString(stringline,90);
char string[10];
bool flag;
flag=true;
int sum=0;
double num;
for(int t=0;t<5;)
{
flag=true;
int i=0;
for(int m=0;m<10;m++)
string[m]=0;
while(stringline[i+sum]!='\t' )
{
string[i]=stringline[i+sum];
i++;
}
if(stringline[sum]=='\t')
{
i++;
flag=false;
}
sum+=i;
if(flag==true)
{
num=atof(string);
in_num[j][t]=num;
t++;
}
}
//m_input=j+1;
j++;
UpdateData(FALSE);
}
sfile.Close();
CString str;
str.Format("%f %f %f %f %f",in_num[0][0],in_num[0][1],in_num[0][2],in_num[0][3],in_num[0][4]);
MessageBox(str);
}
void CNesorsDlg::OnTraindata()
{
// TODO: Add your control notification handler code here
//将W置初值;
UpdateData(TRUE);
srand((unsigned)time(NULL));
for(int w=0;w<3;w++)
for(int e=0;e<4;e++)
m_w[0][w][e]=2*(float)rand()/(float)RAND_MAX-1;
for(int r=0;r<2;r++)
for(int x=0;x<3;x++)
m_w[1][r][x]=2*(float)rand()/(float)RAND_MAX-1;
/*for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
for(int k=0;k<5;k++)
{
m_w[i][j][k]=(float)rand()/(float)RAND_MAX;
}*/
m_000 = m_w[0][0][0];
m_001 = m_w[0][0][1];
m_002 = m_w[0][0][2];
m_003 = m_w[0][0][3];
m_010 = m_w[0][1][0];
m_011 = m_w[0][1][1];
m_012 = m_w[0][1][2];
m_013 = m_w[0][1][3];
m_020 = m_w[0][2][0];
m_021 = m_w[0][2][1];
m_022 = m_w[0][2][2];
m_023 = m_w[0][2][3];
m_100 = m_w[1][0][0];
m_101 = m_w[1][0][1];
m_102 = m_w[1][0][2];
m_110 = m_w[1][1][0];
m_111 = m_w[1][1][1];
m_112 = m_w[1][1][2];
//m_113 = m_w[1][1][3];
//读取一个训练样本;
int count=0;
float Sum_Epi=0;
do
//最外层循环
{
if(count>19)
{
count=0;
}
else
{
//输入层数据输入:
for(int q=0;q<4;q++)
Op[2][q]=in_num[count][q];
// 计算隐含层输入、输出:
for(int l=0;l<3;l++)
{
Ip[0][l]=m_w[0][l][0]*in_num[count][0]+m_w[0][l][1]*in_num[count][1]+m_w[0][l][2]*in_num[count][2]+m_w[0][l][3]*in_num[count][3];
Op[0][l]=1/(1+exp(-Ip[0][l]));
}
//计算输出层输入、输出;
Ip[1][0]=m_w[1][0][0]*Op[0][0]+m_w[1][0][1]*Op[0][1]+m_w[1][0][2]*Op[0][2];
Op[1][0]=1/(1+exp(-Ip[1][0]));
Ip[1][1]=m_w[1][1][0]*Op[0][0]+m_w[1][1][1]*Op[0][1]+m_w[1][1][2]*Op[0][2];
Op[1][1]=1/(1+exp(-Ip[1][1]));
///////////////////////
//计算输出层误差
float d_expect[2]={0,0};
if(in_num[count][4]==1.0)
{
d_expect[0]=0.9;
d_expect[1]=0.1;
}
else
{
d_expect[0]=0.1;
d_expect[1]=0.9;
}
for(int m=0;m<2;m++)
{
Ep[m]=d_expect[m]-Op[1][m];
Dp[1][m]=2*Ep[m]*Op[1][m]*(1-Op[1][m]);
for(int n=0;n<3;n++)
{
Dw[1][m][n]=m_buchang*Dp[1][m]*Op[0][n];
}
}
//计算隐含层
float sum[3];
for(int p=0;p<3;p++)
{
sum[p]=m_w[1][0][p]*Dp[1][0]+m_w[1][1][p]*Dp[1][1];
Dp[0][p]=sum[p]*Op[0][p]*(1-Op[0][p]);
for(int o=0;o<4;o++)
{
Dw[0][p][o]=m_buchang*Dp[0][p]*Op[2][o];
}
}
//调整w[][][]的取值。
//调整隐含层
for(int w=0;w<3;w++)
for(int e=0;e<4;e++)
m_w[0][w][e]=m_w[0][w][e]+Dw[0][w][e];
for(int r=0;r<2;r++)
for(int x=0;x<3;x++)
m_w[1][r][x]=m_w[1][r][x]+Dw[1][r][x];
Sum_Epi=Ep[0]*Ep[0]+Ep[1]*Ep[1];
m_iteration++;
count++;
}
}while(Sum_Epi>m_wucha);
UpdateData(FALSE);
}
void CNesorsDlg::OnShibie()
{
// TODO: Add your control notification handler code here
//读取数据]
CStdioFile sfile1;
sfile1.Open("IrisData.txt",CFile::modeRead | CFile::shareDenyNone);
char stringline[90];
int j=0;
while(j<100) //read data group;
{
//int t=0;
sfile1.ReadString(stringline,90);
char string[10];
bool flag;
flag=true;
int sum=0;
double num;
for(int t=0;t<5;)
{
flag=true;
int i=0;
for(int m=0;m<10;m++)
string[m]=0;
while(stringline[i+sum]!='\t' )
{
string[i]=stringline[i+sum];
i++;
}
if(stringline[sum]=='\t')
{
i++;
flag=false;
}
sum+=i;
if(flag==true)
{
num=atof(string);
in_num[j][t]=num;
t++;
}
}
//m_input=j;
j++;
//UpdateData(FALSE);
}
sfile1.Close();
//pan duan shu ju leibie;
//float yy=0;
for(int i=0;i<100;i++)
{
for(int j=0;j<4;j++)
in_verify[j]=in_num[i][j];
//////////////////////////
for(int l=0;l<3;l++)
{
Ipp[i][0][l]=m_w[0][l][0]*in_num[i][0]+m_w[0][l][1]*in_num[i][1]+m_w[0][l][2]*in_num[i][2]+m_w[0][l][3]*in_num[i][3];
Opp[i][0][l]=1/(1+exp(-Ipp[i][0][l]));
}
//计算输出层输入、输出;
Ipp[i][1][0]=m_w[1][0][0]*Opp[i][0][0]+m_w[1][0][1]*Opp[i][0][1]+m_w[1][0][2]*Opp[i][0][2];
Opp[i][1][0]=(float)1/(1+exp(-Ipp[i][1][0]));
Ipp[i][1][1]=m_w[1][1][0]*Opp[i][0][0]+m_w[1][1][1]*Opp[i][0][1]+m_w[1][1][2]*Opp[i][0][2];
Opp[i][1][1]=(float)1/(1+exp(-Ipp[i][1][1]));
}
//ouput the result;
CFile file("result.txt",CFile::modeCreate | CFile::modeWrite);
for(int ii=0;ii<100;ii++)
{
for(int jj=0;jj<2;jj++)
{
CString string;
string.Format("%f",Opp[ii][1][jj]);
file.Write(string,strlen(string));
file.Write(" ",strlen(" "));
}
file.Write("\t",strlen("\t"));
}
for(int z=0;z<100;z++)
{
if(Opp[z][1][0]>0.5 && Opp[z][1][1]<0.5)
{
file.Write("0",strlen("0"));
}
else
file.Write("1",strlen("1"));
}
file.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -