📄 ga---mfcdoc.cpp
字号:
// GA---MFCDoc.cpp : implementation of the CGAMFCDoc class
//
#include "stdafx.h"
#include "GA---MFC.h"
#include "GA---MFCDoc.h"
#include "GA---MFCView.h"
#include "WSInitialGADlg.h"
#include "WSGenAlogrith.h"
#include <process.h>
#include ".\ga---mfcdoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
void WorkingThread(void *p_pView);
BOOL g_Do = FALSE;
BOOL g_Pause = FALSE;
BOOL g_Stop = FALSE;
// CGAMFCDoc
IMPLEMENT_DYNCREATE(CGAMFCDoc, CDocument)
BEGIN_MESSAGE_MAP(CGAMFCDoc, CDocument)
ON_COMMAND(ID_SETTING, &CGAMFCDoc::OnSetting)
ON_COMMAND(ID_DO, &CGAMFCDoc::OnDo)
ON_UPDATE_COMMAND_UI(ID_SETTING, OnUpdateSetting)
ON_UPDATE_COMMAND_UI(ID_DO, OnUpdateDo)
ON_COMMAND(ID_PAUSE, OnPause)
ON_UPDATE_COMMAND_UI(ID_PAUSE, OnUpdatePause)
ON_COMMAND(ID_STOP, OnStop)
ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
END_MESSAGE_MAP()
// CGAMFCDoc construction/destruction
CGAMFCDoc::CGAMFCDoc()
{
// TODO: add one-time construction code here
}
CGAMFCDoc::~CGAMFCDoc()
{
}
BOOL CGAMFCDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
// CGAMFCDoc serialization
void CGAMFCDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
// CGAMFCDoc diagnostics
#ifdef _DEBUG
void CGAMFCDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CGAMFCDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CGAMFCDoc commands
CString g_InputFileName("GADATA.txt");
CString g_OutputFileName("Result.txt");
INT g_Times = 30;
void CGAMFCDoc::OnSetting()
{
// TODO: Add your command handler code here
WSInitialGADlg l_InitialDlg;
if(l_InitialDlg.DoModal() == IDOK)
{
g_InputFileName = l_InitialDlg.m_InputFileName;
g_OutputFileName = l_InitialDlg.m_OutPutFile;
g_Times = l_InitialDlg.m_Times;
}
}
void CGAMFCDoc::OnDo()
{
// TODO: Add your command handler code here
g_Do = TRUE;
CGAMFCView *l_pCurView = (CGAMFCView *)(m_viewList.GetHead());
_beginthread(WorkingThread, NULL, (void *)l_pCurView);
}
char *MakePathStr(char *str)
{
int i = 0;
char *newStr = new char[255];
while(*str)
{
if(*str == '\\')
{
newStr[i++] = '\\';
}
newStr[i++] = *str++;
}
newStr[i] = 0;
return newStr;
}
char *MakePathStr(CString str)
{
int i = 0;
int j = 0;
char *newStr = new char[255];
while(str[j])
{
if(str[j] == '\\')
{
newStr[i++] = '\\';
}
newStr[i++] = str[j++];
}
newStr[i] = 0;
return newStr;
}
WSGeneticAlogrith *l_pGA = NULL;
void WorkingThread(void *p_pView)
{
CGAMFCView *l_pCurView =(CGAMFCView*)p_pView;
char *inputfile = new char[255];
char *outputfile = new char[255];
sprintf(inputfile, "%s", g_InputFileName);//只能得到CString的第一个字符
sprintf(outputfile, "%s", g_OutputFileName);//只能得到CString的第一个字符
//MakePathStr(g_InputFileName);//OK!
//l_pGA->Clear();
l_pGA = new WSGeneticAlogrith();
if(!l_pGA->Initial(
2,
MakePathStr(inputfile),
MakePathStr(outputfile),
g_Times, 0, 0.99, 0.99))
{
AfxMessageBox(_T("配置有误"));
g_Do = FALSE;
delete [] inputfile;
delete [] outputfile;
return ;
}
/////////////////////////////////////////////
INT l_Begin = clock();
INT l_Delta;
int l_max = 0;
for(int i = 0; i < l_pGA->m_BaseCityInfo.m_CityNum; ++i)
{
if(l_max < l_pGA->m_BaseCityInfo.m_pCities[i].x) l_max = l_pGA->m_BaseCityInfo.m_pCities[i].x;
if(l_max < l_pGA->m_BaseCityInfo.m_pCities[i].y) l_max = l_pGA->m_BaseCityInfo.m_pCities[i].y;
}
l_pCurView->m_pTvRate = (1.0*500)/l_max;
CString str;
while(TRUE)
{
while(g_Pause)
{
Sleep(50);
l_Begin += 50;
}
if(g_Stop)
{
break;
}
l_pGA->OneGenerate(3,2,1);
l_pCurView->m_pGA = l_pGA;
l_pCurView->StartDraw();
str.Format(_T("剩时[%3d]秒"), (g_Times-l_Delta/1000));
l_pCurView->TextDraw(0, 0, str);
str.Format(_T("所有的路径总长为:%f"), WSTSP::TSPTotalDistance(&(l_pGA->m_pCityInfoOfMembers[0])));
l_pCurView->TextDraw(100, 0, str);
if( (l_Delta = clock() - l_Begin) > 1000*g_Times )break;
}
l_pCurView->m_pGA = l_pGA;
l_pCurView->StartDraw();
str.Format(_T("完成! ") );
l_pCurView->TextDraw(0, 0, str);
str.Format(_T("所有的路径总长为:%f"), WSTSP::TSPTotalDistance(&(l_pGA->m_pCityInfoOfMembers[0])));
l_pCurView->TextDraw(100, 0, str);
l_pGA->ShowBestRouteInfo();
delete [] inputfile;
delete [] outputfile;
g_Do = TRUE;
g_Stop = TRUE;
g_Pause = TRUE;
//l_pCurView->m_pGA = NULL;
return ;
}
void CGAMFCDoc::OnUpdateSetting(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CGAMFCDoc::OnUpdateDo(CCmdUI *pCmdUI)
{
pCmdUI->Enable(!g_Do);
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CGAMFCDoc::OnPause()
{
g_Pause = !g_Pause;
g_Stop = FALSE;
// TODO: 在此添加命令处理程序代码
}
void CGAMFCDoc::OnUpdatePause(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
if(!g_Do) pCmdUI->Enable(FALSE);
if(g_Pause) pCmdUI->SetText("算法继续");
else pCmdUI->SetText("算法暂停");
}
void CGAMFCDoc::OnStop()
{
// TODO: 在此添加命令处理程序代码
g_Stop = TRUE;
}
void CGAMFCDoc::OnUpdateStop(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
if(!g_Do) pCmdUI->Enable(FALSE);
else if(g_Pause) pCmdUI->Enable(FALSE);
else pCmdUI->Enable(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -