📄 bpnlayerexedoc.cpp
字号:
// BpNlayerEXEDoc.cpp : implementation of the CBpNlayerEXEDoc class
//
#include "stdafx.h"
#include "BpNlayerEXE.h"
#include "MainFrm.h"
#include "BpNlayerEXEDoc.h"
#include "NewProject.h"
#include "UnitaryDlg.h"
#include "UnUnitaryDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBpNlayerEXEDoc
IMPLEMENT_DYNCREATE(CBpNlayerEXEDoc, CDocument)
BEGIN_MESSAGE_MAP(CBpNlayerEXEDoc, CDocument)
//{{AFX_MSG_MAP(CBpNlayerEXEDoc)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_UNITARY, OnUnitary)
ON_COMMAND(IDM_UNUNITARY, OnUnunitary)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBpNlayerEXEDoc construction/destruction
CBpNlayerEXEDoc::CBpNlayerEXEDoc()
{
// TODO: add one-time construction code here
m_pBPNet=NULL;
m_piLayerCapture=NULL;
m_iLayerNum=0;
m_FunctionType=0;
m_iTrainTimth=0;
m_dStudyRate=0;
m_dMomentum=0;
m_bRunEnable=FALSE;
m_bPauseEnable=FALSE;
m_bStopEnable=FALSE;
m_bSaveEnable=FALSE;
m_bSaveAsEnable=FALSE;
m_bDataOk=FALSE;
m_bPropotinoEnable=FALSE;
m_bSettingEnable=FALSE;
m_bExpendEnable=FALSE;
m_bTreeEnable=FALSE;
m_bAbsError=FALSE;
m_bNew=TRUE;
m_pRsOpen=new CDaoRecordset(&m_DbOpen);
m_pRsSave=new CDaoRecordset(&m_DbSave);
m_pTdOpen=new CDaoTableDef(&m_DbOpen);
m_pTdSave=new CDaoTableDef(&m_DbSave);
m_pFileDlg=NULL;
}
CBpNlayerEXEDoc::~CBpNlayerEXEDoc()
{
delete m_pBPNet;
}
BOOL CBpNlayerEXEDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CBpNlayerEXEDoc serialization
void CBpNlayerEXEDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CBpNlayerEXEDoc diagnostics
#ifdef _DEBUG
void CBpNlayerEXEDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CBpNlayerEXEDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
CBpNlayerEXEDoc *CBpNlayerEXEDoc::GetDoc()
{
//return this;静态成员函数不能访问本类,包括this
CFrameWnd* pFrame=(CFrameWnd*)(AfxGetApp()->m_pMainWnd);
return (CBpNlayerEXEDoc*)pFrame->GetActiveDocument();
}
void CBpNlayerEXEDoc::SetDatabase()
{
if (m_pTdOpen)
{
delete m_pTdOpen;
m_pTdOpen=NULL;
}
if (m_pTdSave)
{
delete m_pTdSave;
m_pTdSave=NULL;
}
if (m_pRsOpen)
{
delete m_pRsOpen;
m_pRsOpen=NULL;
}
if (m_pRsSave)
{
delete m_pRsSave;
m_pRsSave=NULL;
}
if (m_DbOpen.IsOpen())
{
m_DbOpen.Close();
}
if (m_DbSave.IsOpen())
{
m_DbSave.Close();
}
m_pRsOpen=new CDaoRecordset(&m_DbOpen);
m_pRsSave=new CDaoRecordset(&m_DbSave);
m_pTdOpen=new CDaoTableDef(&m_DbOpen);
m_pTdSave=new CDaoTableDef(&m_DbSave);
if (m_pFileDlg)
{
delete m_pFileDlg;
m_pFileDlg=NULL;
}
m_pFileDlg=new CFileDialog(TRUE);
m_pFileDlg->m_ofn.lpstrTitle="打开训练数据库";
m_pFileDlg->m_ofn.lpstrFilter="Access database(*.mdb)\0*.mdb\0Text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
m_pFileDlg->m_ofn.lpstrDefExt="mdb";
/* CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrTitle="打开训练数据库";
dlg.m_ofn.lpstrFilter="Access database(*.mdb)\0*.mdb\0Text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
dlg.m_ofn.lpstrDefExt="mdb";*/
if (IDOK==m_pFileDlg->DoModal())
{
m_DbOpen.Open(m_pFileDlg->GetPathName());
}
else
return;
m_pTdOpen->Open("data");
m_pRsOpen->Open(m_pTdOpen);
if (m_pFileDlg)
{
delete m_pFileDlg;
m_pFileDlg=NULL;
}
m_pFileDlg=new CFileDialog(FALSE);
m_pFileDlg->m_ofn.lpstrTitle="保存训练数据";
m_pFileDlg->m_ofn.lpstrFilter="Access database(*.mdb)\0*.mdb\0Text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
m_pFileDlg->m_ofn.lpstrDefExt="mdb";
CFileFind FileFind;
if (m_bNew)
{
if (IDOK==m_pFileDlg->DoModal())
{
if (!FileFind.FindFile(m_pFileDlg->GetPathName()))
{
m_DbSave.Create(m_pFileDlg->GetPathName());
}
else
{
DeleteFile(m_pFileDlg->GetPathName());
m_DbSave.Create(m_pFileDlg->GetPathName());
}
}
else
{
m_pRsOpen->Close();
m_pTdOpen->Close();
m_DbOpen.Close();
return;
}
//FileFind.Close();
CString str_sql = "create table data(Output0 double)";
m_DbSave.Execute(str_sql);
m_pTdSave->Open("data");
for (int i=1;i<*(m_piLayerCapture+m_iLayerNum-1);i++)
{
str_sql.Format("Output%d",i);
m_pTdSave->CreateField(str_sql,dbDouble,1);
}
m_pRsSave->Open(m_pTdSave);
}
else
{
if (IDOK==m_pFileDlg->DoModal())
{
if (!FileFind.FindFile(m_pFileDlg->GetPathName()))
{
m_DbSave.Create(m_pFileDlg->GetPathName());
}
else
{
m_DbSave.Open(m_pFileDlg->GetPathName());
}
}
else
{
m_pRsOpen->Close();
m_pTdOpen->Close();
m_DbOpen.Close();
return;
}
m_pTdSave->Open("data");
//FileFind.Close();
m_pRsSave->Open(m_pTdSave);
}
//////////////////////////////////////////////////////////////////////////
COleVariant coleTemp;
m_pRsOpen->MoveFirst();
for (int i=0;i<2;i++)
{
m_pRsSave->AddNew();
for (int j=0;j<*(m_piLayerCapture+m_iLayerNum-1);j++)
{
coleTemp=m_pRsOpen->GetFieldValue(*(m_piLayerCapture)+j);
m_pRsSave->SetFieldValue(j,coleTemp.dblVal);
}
m_pRsSave->Update();
m_pRsOpen->MoveNext();
}
m_pRsOpen->MoveFirst();
//////////////////////////////////////////////////////////////////////////
//AfxGetMainWnd()->GetMenu()->GetSubMenu(1)->EnableMenuItem(ID_TRAIN_RUN,MF_BYCOMMAND);
//To get the first view in the list of views:
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = GetNextView(pos);
pView->Invalidate();
}
///////////////////////////////////////////////////////////////////////////
//数据格式检查
int iFieldCount=0,iInputNum=0,iOutputNum=0;
CDaoFieldInfo fieldinfo;
CString strtemp1="",strtemp2="";
iFieldCount=m_pRsOpen->GetFieldCount();
if (iFieldCount!=(m_piLayerCapture[0]+m_piLayerCapture[m_iLayerNum-1]))
{
AfxMessageBox("数据库列名不合要求!请检查数据");
return;
}
for (int i=0;i<iFieldCount;i++)
{
m_pRsOpen->GetFieldInfo(i,fieldinfo,AFX_DAO_PRIMARY_INFO);
strtemp1=fieldinfo.m_strName;
strtemp1=strtemp1.Left(5);
strtemp2=fieldinfo.m_strName;
strtemp2=strtemp2.Left(6);
if (strtemp1!="Input"&&strtemp2!="Output")
{
AfxMessageBox("数据库列名不合要求!请检查数据");
return;
}
else
{
if (strtemp1=="Input")
{
iInputNum+=1;
strtemp1=fieldinfo.m_strName;
strtemp1=strtemp1.Right(strtemp1.GetLength()-5);
if (!IsNum(strtemp1))
{
AfxMessageBox("数据库列名不合要求!请检查数据");
return;
}
}
if (strtemp2=="Output")
{
strtemp2=fieldinfo.m_strName;
strtemp2=strtemp2.Right(strtemp2.GetLength()-6);
if (!IsNum(strtemp2))
{
AfxMessageBox("数据库列名不合要求!请检查数据");
return;
}
iOutputNum+=1;
}
}
}
if (iInputNum!=m_piLayerCapture[0]||iOutputNum!=m_piLayerCapture[m_iLayerNum-1])
{
AfxMessageBox("数据库列名不合要求!请检查数据");
return;
}
m_bRunEnable=TRUE;
m_bDataOk=TRUE;
m_bSaveEnable=TRUE;
m_bSettingEnable=TRUE;
m_bExpendEnable=TRUE;
m_bTreeEnable=TRUE;
//COleVariant coletemp;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBpNlayerEXEDoc commands
void CBpNlayerEXEDoc::OnFileNew()
{
// TODO: Add your command handler code here
CNewProject newproject;
m_iTrainTimth=0;
newproject.DoModal();
}
void CBpNlayerEXEDoc::Train(int times)
{
double *pdInputArray,*pdOutputArray;
pdInputArray=new double[m_piLayerCapture[0]];
pdOutputArray=new double[*(m_piLayerCapture+m_iLayerNum-1)];
double *pdDraw;
pdDraw=new double[*(m_piLayerCapture+m_iLayerNum-1)];
COleVariant ColeTemp;
CString StrTemp;
double dResualt;
int iRecordNum=m_pRsOpen->GetRecordCount();
int iDataScareTimth=1;
m_pRsSave->AddNew();
m_pRsOpen->MoveFirst();
m_pRsOpen->MoveNext();
m_pRsOpen->MoveNext();//the beginning two records are enable;
for (int i=0;i<times;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -