📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "GA_SA.h"
#include "GA_SAView.h"
#include "GA_SAView.h"
#include "gasatsp.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL IsComputing = FALSE; //计算线程是否在运行
#define MAXOFSAME 40 //MAXOFSAME次得到相同解就终止
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_UPDATE_COMMAND_UI(ID_FILE_OPEN, OnUpdateFileOpen)
ON_COMMAND(ID_FILE_START_COMPUTE, OnFileStartCompute)
ON_UPDATE_COMMAND_UI(ID_FILE_START_COMPUTE, OnUpdateFileStartCompute)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
UINT GASACompution(LPVOID pParam)
{
IsComputing = TRUE;
CString strTemp, strValue;
CGA_SAView *pView = (CGA_SAView*)pParam;
HWND ViewHWND = pView->GetSafeHwnd();
strTemp = "开始计算";
::SendMessage( ViewHWND, WM_INFOVIEWAPPEND, (WPARAM)(&strTemp), (LPARAM)0 );
//CFile cityfile("C:\\gacitysfile.txt", CFile::modeCreate|CFile::modeWrite);
//CFile iterfile("C:\\gaitersfile.txt", CFile::modeCreate|CFile::modeWrite);
int k = 0;
float tk; //初始温度
FormInitialPop(); //初始化群体
SYRouter minRouter, maxRouter;
float minDistance, maxDistance;
minRouter = GetMinRouter();
maxRouter = GetMaxRouter();
minDistance = minRouter.m_fTotalDistance;
maxDistance = maxRouter.m_fTotalDistance;
tk = 20 * (maxDistance - minDistance);
int count=0; //相同最优解出现次数的计数器
while (count != MAXOFSAME && tk > 1e-6)
{
minDistance = GetMinRouter().m_fTotalDistance;
int num = 0; //产生新群体的计数器
srand((unsigned)time(NULL));
while (num != 5*48)
{
AddPop(num, tk);
num++;
}
GenerateNewPop(tk);
double fmindistance = 0.0;
int nowiter = 0;
nowiter = OneIterGACompution(tk);
fmindistance = GetMinRouterFromPop( strTemp );
GetMinRouterFromPop( strTemp );
::SendMessage( ViewHWND, WM_INFOVIEWAPPEND, (WPARAM)(&strTemp), (LPARAM)0 );
if ((minDistance - fmindistance) < 1e-8)
{
count ++;
}
else
count = 0;
tk *= DECRATE;
}
//输出最优解
GetMinRouterFileStringFromPop( strTemp );
::SendMessage( ViewHWND, WM_INFOVIEWAPPEND, (WPARAM)(&strTemp), (LPARAM)0 );
//cityfile.Close();
//iterfile.Close();
IsComputing = FALSE;
return 0;
}
void CMainFrame::OnFileOpen()
{
// TODO: Add your command handler code here
CString strFileName;
char szFilter[200];
strcpy( szFilter, "TXT Files (*.txt)|*.txt||" );
CFileDialog *pFileDialog;
pFileDialog = new CFileDialog( TRUE,
NULL,
NULL,
OFN_HIDEREADONLY,
szFilter,
this );
if( IDOK == pFileDialog->DoModal() )
{
CGA_SAView *pView = (CGA_SAView*)GetActiveView();
pView->ClearInfos();
strFileName = pFileDialog->GetPathName();
int citynumber = OpenDataFile( strFileName );
m_nCityNum = citynumber;
m_nMaxPop = (int)(POP_CITYNUMBER_RATE * citynumber);
CString strTemp, strValue;
if( citynumber > 0 )
{
strTemp = "共读入城市信息";
strValue.Format("%d",citynumber);
strTemp += strValue;
strTemp += "个";
pView->AddString( strTemp );
strTemp = "计算城市距离完成";
pView->AddString( strTemp );
}
else
{
strTemp = "读入数据文件错误";
pView->AddString( strTemp );
}
IsComputing=TRUE;
}
}
void CMainFrame::OnUpdateFileOpen(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if( IsComputing )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
void CMainFrame::OnFileStartCompute()
{
// TODO: Add your command handler code here
CGA_SAView *pView = (CGA_SAView*)GetActiveView();
AfxBeginThread(GASACompution, (LPVOID)pView);
}
void CMainFrame::OnUpdateFileStartCompute(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if( IsComputing )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -