?? bpdlg.cpp
字號:
// bpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "bp.h"
#include "bpDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CBpDlg dialog
CBpDlg::CBpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBpDlg)
// 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 CBpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBpDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBpDlg, CDialog)
//{{AFX_MSG_MAP(CBpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TRAIN, OnTrain)
ON_BN_CLICKED(IDC_RECOG, OnRecog)
ON_BN_CLICKED(IDC_CLR, OnClr)
ON_BN_CLICKED(IDC_SAMPLE, OnSample)
ON_BN_CLICKED(IDC_CENTER, OnCenter)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBpDlg message handlers
BOOL CBpDlg::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
m_brSquare.CreateSolidBrush (RGB(255,0,0));
//背景色畫刷
m_brBack.CreateSolidBrush (RGB(255,255,255));
//網格線畫刷
m_hPen.CreatePen (PS_SOLID,1,RGB(0,192,192));
m_bInitDraw=true;
ach=true;
//初始化輸入矩陣
for(int i=0;i<LEN;i++)
{
for(int j=0;j<LEN;j++)
{
m_bArray[i][j]=false;
m_InputArray[i][j]=0;
}
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CBpDlg::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 CBpDlg::OnPaint()
{
CWnd *pwnd;
CRect rc;
//CBrush br(RGB(0,0,255));
pwnd=GetDlgItem(IDC_INPUTDLG);
pwnd->GetWindowRect(&rc);
// CClientDC dc(this);
ScreenToClient(&rc);
sizeHeight=rc.Height ()/16;//求出方塊的長
float x=rc.left;
sizeWidth=rc.Width ()/16;//方塊的寬
float y=rc.top;
float newsize=0;
for(int i=0;i<LEN;i++)
{
for(int j=0;j<LEN;j++)
{
m_rectArray[i][j].left =x;
m_rectArray[i][j].right =m_rectArray[i][j].left+sizeWidth;
m_rectArray[i][j].top =y;
m_rectArray[i][j].bottom =m_rectArray[i][j].top+sizeHeight;
//y=y+sizeHeight;
x=x+sizeWidth;
}
//y=rc.top;
//x=x+sizeWidth;
x=rc.left;
y=y+sizeHeight;
}
CClientDC dc(this);
CBrush *pOldBr=dc.SelectObject (&m_brBack);
//dc.Rectangle(&rc);
dc.SelectObject (&m_hPen);
newsize=0;
for(i=0;i<=LEN;i++,newsize+=sizeHeight)
{
dc.MoveTo (rc.left,newsize+rc.top);
dc.LineTo (rc.left+rc.Width (),newsize+rc.top); //畫背景橫向網格
}
newsize=0;
for( i=0;i<=LEN;newsize+=sizeWidth,i++)
{
dc.MoveTo (newsize+rc.left,rc.top);
dc.LineTo (newsize+rc.left,rc.Height ()+rc.top); //畫背景縱向網格
}
dc.SelectObject(&m_brSquare); //這里是開始點小方格的部分
for( i=0;i<LEN;i++)
{
for(int j=0;j<LEN;j++)
{
if(m_bArray[i][j]==true)
{
dc.Rectangle (m_rectArray[i][j]); //填充小方塊
}
}
}
dc.SelectObject (pOldBr);
for( i=0;i<LEN;i++)
{
for(int j=0;j<LEN;j++)
{
if(m_bArray[i][j]==false)
{
dc.Rectangle (m_rectArray[i][j]); //填充小方塊
}
}
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBpDlg::OnTrain()
{
// TODO: Add your control notification handler code here
int dataNum,typeNum,i,j,k;
float sum,dError[10][5][10],dError_output[10],dError_hide1[100],wOffset_intoh[256][100],wOffset_hide1[100][20];
bp.InitWeight();
InitSample();
for (int time=0;time<TIME;time++)
{
for (dataNum=0;dataNum<10;dataNum++)
{
for (typeNum=0;typeNum<5;typeNum++)
{
for (j=0;j<LEN*LEN;j++)
{
bp.output0[j]=temp[dataNum][typeNum][j]; //第0層各神經元的輸出
}
for (i=0;i<M1;i++)
{
sum=0;
for (j=0;j<N;j++)
{
sum+=bp.intoh_w[j][i]*bp.output0[j];
}
bp.output1[i]=FX(sum); //第1層各神經元的輸出
}
for (i=0;i<P;i++)
{
sum=0;
for (j=0;j<M1;j++)
{
sum+=bp.hide1_w[j][i]*bp.output1[j];
}
bp.output[i]=FX(sum); //輸出層的值
dError[dataNum][typeNum][i]=fabs(bp.t[dataNum][i]-bp.output[i]);
dError_output[i]=(bp.t[dataNum][i]-bp.output[i])*bp.output[i]*(1-bp.output[i]);
}
//求取各層誤差值
/* for (j=0;j<M1;j++)
{
sum=0;
for (k=0;k<P;k++)
{
sum+=bp.hide1_w[j][k]*dError_output[k];
}
dError_hide1[j]=bp.output1[j]*(1-bp.output1[j])*sum;
}*/
for (j=0;j<M1;j++)
{
sum=0;
for (k=0;k<P;k++)
{
sum+=bp.hide1_w[j][k]*dError_output[k];
}
dError_hide1[j]=bp.output1[j]*(1-bp.output1[j])*sum;
}
//根據誤差值調整各層的權值
for (i=0;i<N;i++)
{
for (j=0;j<M1;j++)
{
wOffset_intoh[i][j]=Q*dError_hide1[j]*bp.output0[i];
bp.intoh_w[i][j]+=wOffset_intoh[i][j];
}
}
for(i=0;i<M1;i++)
{
for(j=0;j<P;j++)
{
wOffset_hide1[i][j]=Q*dError_output[j]*bp.output1[i];
bp.hide1_w[i][j]+=wOffset_hide1[i][j];
}
}
}
} //所有樣本一次訓練結束
int errortotal=0;
for(i=0;i<10;i++)
for(j=0;j<5;j++)
for(k=0;k<10;k++)
if(dError[i][j][k]<0.3)
errortotal+=1;
if(errortotal==500)
{
bp.bSuccess=true;
char ss[20];
sprintf(ss,"樣本訓練結束!迭代次數:%d",time);
CPaintDC dc(this);
AfxMessageBox(ss);
FILE *output;
output=fopen("weight\\intoh_w.txt","w");
for(i=0;i<256;i++)
for(j=0;j<100;j++)
fprintf(output,"%f ",bp.intoh_w[i][j]);
fclose(output);
output=fopen("weight\\hide1_w.txt","w");
for(i=0;i<100;i++)
for(j=0;j<10;j++)
fprintf(output,"%f ",bp.hide1_w[i][j]);
fclose(output);
break;
}
else if(time==TIME-1)
{
CPaintDC dc(this);
AfxMessageBox("樣本訓練失敗,請重新訓練!");
}
}
}
void CBpDlg::OnRecog()
{
// TODO: Add your control notification handler code here
int i,j,output0[256];
float dError,sum,intoh_w[256][100],hide1_w[100][10],output1[100],output[10];
FILE *out;
out=fopen("weight\\intoh_w.txt","r");
for(i=0;i<256;i++)
for(j=0;j<100;j++)
fscanf(out,"%f ",&intoh_w[i][j]);
fclose(out);
out=fopen("weight\\hide1_w.txt","r");
for(i=0;i<100;i++)
for(j=0;j<10;j++)
fscanf(out,"%f ",&hide1_w[i][j]);
fclose(out);
for (i=0;i<16;i++)
{
for (j=0;j<16;j++)
{
output0[i*LEN+j]=m_InputArray[i][j]; //第0層各神經元的輸出 }
}
}
for (i=0;i<M1;i++)
{
sum=0;
for (j=0;j<N;j++)
{
sum+=intoh_w[j][i]*output0[j];
}
output1[i]=FX(sum); //第1層各神經元的輸出
}
for (i=0;i<P;i++)
{
sum=0;
for (j=0;j<M1;j++)
{
sum+=hide1_w[j][i]*output1[j];
}
output[i]=FX(sum); //第2層各神經元的輸出
}
for (i=0;i<P;i++)
{
dError=0;
sum=0;
for (j=0;j<P;j++)
{
if (fabs(output[j]-bp.t[i][j])<0.3)
{
sum+=1;
}
dError+=fabs(output[j]-bp.t[i][j]);
}
if (sum==10)
{
//SetDlgItemInt(IDC_EDIT1,i);
//char s[10];
//sprintf(s,"%f",dError);
//SetDlgItemText(IDC_EDIT2,s);
char cc[20];
sprintf(cc,"識別結果 %d 誤差 %f",i,dError);
CPaintDC dc(this);
AfxMessageBox(cc);
break;
}
else if (i==9)
{
CPaintDC dc(this);
AfxMessageBox("抱歉,不能識別!");
break;
}
}
}
void CBpDlg::OnClr()
{
// TODO: Add your control notification handler code here
int i,j;
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
m_bArray[i][j]=false;
m_InputArray[i][j]=0;
}
}
this->Invalidate();
this->UpdateWindow();
}
void CBpDlg::OnSample()
{
// TODO: Add your control notification handler code here
FILE *output;
output=fopen("samples\\sample9\\sample4.txt","w");
for (int i=0;i<16;i++)
{
for (int j=0;j<16;j++)
{
fprintf(output,"%d ",m_InputArray[i][j]);
}
}
fclose(output);
}
void CBpDlg::OnCenter()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
int iLeft=LEN,iRight=0,iTop=LEN,iBottom=0;
int i,j;
//調整坐標使輸入居中
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
if(m_bArray[i][j])
{
if (iTop>j)iTop=j; //識別方陣的最左
if(iBottom<j)iBottom=j; //方陣的最右
if(iLeft>i)iLeft=i; //方陣的最上
if(iRight<i)iRight=i; //方陣的最下
}
}
}
int xMiddle=0,yMiddle=0;
xMiddle=(iRight-iLeft)/2+1;
yMiddle=(iBottom-iTop)/2+1;
iLeft=iLeft+xMiddle; //中間點的x
iTop=iTop+yMiddle; //中間點的y
int dx=iLeft-LEN/2; //求出坐標差
int dy=iTop-LEN/2; //求出坐標差
//平移坐標
for(i=0;i<LEN;i++)
for(j=0;j<LEN;j++)
m_InputArray[i][j]=0;
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
if(m_bArray[i][j])
m_InputArray[i-dx][j-dy]=1;
}
}
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
m_bArray[i][j]=false;
}
}
for(i=0;i<LEN;i++)
{
for(j=0;j<LEN;j++)
{
if(m_InputArray[i][j]==1)
m_bArray[i][j]=true;
else m_bArray[i][j]=false;
}
}
this->Invalidate();
this->UpdateWindow();
}
void CBpDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd *pwnd;
CRect rc;
pwnd=GetDlgItem(IDC_INPUTDLG);
pwnd->GetWindowRect(&rc);
ScreenToClient(&rc);
for(int i=0;i<LEN;i++)
{
for(int j=0;j<LEN;j++)
{
if(m_rectArray[i][j].PtInRect (point))
{
//m_InputArray[i][j]=1;
m_InputArray[i][j]=1;
m_bArray[i][j]=!m_bArray[i][j];
//Invalidate();
InvalidateRect(&m_rectArray[i][j],false);
break;
}
}
}
CDialog::OnLButtonDown(nFlags, point);
}
void CBpDlg::InitSample()
{
int i,j,m,p;
int k=0;
CString str;
for(i=0;i<10;i++)
{
for(p=0;p<5;p++)
{
str.Format("samples\\sample%d\\sample%d.txt",i,p);
FILE *output;
output=fopen(str,"r");
for (j=0;j<16;j++)
{
for (k=0;k<16;k++)
{
fscanf(output,"%d",&temp[i][p][j*16+k]);
}
}
fclose(output);
}
}
//CPaintDC dc(this);
//AfxMessageBox("數據導入完成");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -