📄 antacodlg.cpp
字号:
// AntAcoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AntAcoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern fstream solvefile;
extern GInfo Map;
extern double alpha;
extern double beta;
extern double rou;
extern int iItCount;
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()
/////////////////////////////////////////////////////////////////////////////
// CAntAcoDlg dialog
CAntAcoDlg::CAntAcoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAntAcoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAntAcoDlg)
m_bShowBest = TRUE;
m_bShowTitle = TRUE;
m_bShowTrace = TRUE;
m_strResult = _T("");
m_Result = _T("");
m_ROU = 0.5;
m_ALPHA = 1.0;
m_BETA = 7.5;
m_TotalRound = 1000;
m_StaticStatue = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
mpRe=NULL;
m_DrawArea=CRect(30,30,600,470);
srand(time(NULL));
frompoint.x=0;
frompoint.y=0;
topoint=frompoint;
mb_Start=FALSE;
mb_ResearchEnd=FALSE;
mb_HaveResult=FALSE;
m_bShowTrace=TRUE;
m_bShowBest=FALSE;
m_strResult="^_^***********************************************^_^";
m_Result="^_^智能优化算法课程设计^_^ ";
}
void CAntAcoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAntAcoDlg)
DDX_Control(pDX, IDC_STATIC_STATUE, m_StatueCtrl);
DDX_Control(pDX, IDC_BN_START, m_bnStart);
DDX_Control(pDX, IDC_PROGRESS_THREAD, m_ProgressThread);
DDX_Check(pDX, IDC_CHECK_BEST, m_bShowBest);
DDX_Check(pDX, IDC_CHECK_TITLE, m_bShowTitle);
DDX_Text(pDX, IDC_EDIT_RESULT, m_Result);
DDX_Check(pDX, IDC_CHECK_SHOWTRACE, m_bShowTrace);
DDX_Text(pDX, IDC_EDIT_ROU, m_ROU);
DDV_MinMaxDouble(pDX, m_ROU, 0.1, 1.);
DDX_Text(pDX, IDC_EDIT_ALPHA, m_ALPHA);
DDX_Text(pDX, IDC_EDIT_BETA, m_BETA);
DDX_Text(pDX, IDC_EDIT_ROUND, m_TotalRound);
DDV_MinMaxInt(pDX, m_TotalRound, 1, 100000);
DDX_Text(pDX, IDC_STATIC_STATUE, m_StaticStatue);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAntAcoDlg, CDialog)
//{{AFX_MSG_MAP(CAntAcoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BN_START, OnBnStart)
ON_WM_TIMER()
ON_MESSAGE(USER_RESEARCH_ROUND,OnResearchRound)
ON_BN_CLICKED(IDC_ABOUTBOX, OnAboutbox)
ON_BN_CLICKED(IDC_CHECK_TITLE, OnCheckTitle)
ON_BN_CLICKED(IDC_CHECK_BEST, OnCheckBest)
ON_BN_CLICKED(IDC_BN_LOADV, OnBnLoadv)
ON_BN_CLICKED(IDC_CHECK_SHOWTRACE, OnCheckShowtrace)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAntAcoDlg message handlers
BOOL CAntAcoDlg::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 CAntAcoDlg::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 CAntAcoDlg::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();
}
if(m_bShowTrace==TRUE)
{
DrawTrace();
}
if((mb_HaveResult==TRUE)&&m_bShowBest==TRUE)
{
DrawBestTrace();
}
if(m_bShowTitle==TRUE)
{
DrawTitle();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAntAcoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT CAntAcoDlg::MyThread(LPVOID param)
{
return 0;
}
void CAntAcoDlg::OnResearchRound(int round)
{
m_ProgressThread.SetPos(round);
}
void CAntAcoDlg::OnBnStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(mb_LoadFile==TRUE)
{
if(mb_Start==FALSE)
{
mb_Start=TRUE;
mb_HaveResult=FALSE;
mb_ResearchEnd=FALSE;
alpha=m_ALPHA;
beta=m_BETA;
rou=m_ROU;
iItCount=m_TotalRound;
m_Timer=SetTimer(1,0,NULL);
m_bnStart.SetWindowText("暂停");
m_StaticStatue="Running";
m_StatueCtrl.UpdateWindow();
}
else if(mb_Start==TRUE)
{
mb_Start=FALSE;
KillTimer(1);
m_bnStart.SetWindowText("开始");
m_StatueCtrl.SetWindowText("Ready");
}
}
}
void CAntAcoDlg::OnOK()
{
// TODO: Add extra validation here
KillTimer(1);
CDialog::OnOK();
}
BOOL CAntAcoDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::DestroyWindow();
}
BOOL CAntAcoDlg::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
m_ProgressThread.ModifyStyle(0,2);
m_ProgressThread.SetRange(0,100);
m_ProgressThread.SetStep(0);
return CDialog::PreCreateWindow(cs);
}
void CAntAcoDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString s;
if(nIDEvent==1)
{
CRect r;
r.left=m_DrawArea.left-5;
r.right=m_DrawArea.right+10;
r.top=m_DrawArea.top-5;
r.bottom=m_DrawArea.bottom+10;
InvalidateRect(&r,TRUE);
mRe.StartSearch();
mb_HaveResult=TRUE;
s.Format("运行中...当前最短路径:[%d]:%f :",mRe.m_Round+1,mRe.temp);
m_Result=s;
m_ProgressThread.SetPos(int(((double)mRe.m_Round/(double)iItCount)*100));
UpdateData(FALSE);
if(mRe.EndResearch()==1)
{
KillTimer(m_Timer);
mb_ResearchEnd=TRUE;
solvefile.close();
m_Result.Format("运行结束...当前最短路径:[%d]: %f :",mRe.m_Round+1,mRe.temp);
for(int i=0;i<iCityCount;i++)
{
s.Format(" ->%d ",mRe.temptour[i]);
m_Result+=s;
}
UpdateData(FALSE);
mb_Start=FALSE;
m_ProgressThread.SetPos(0);
m_bnStart.SetWindowText("开始");
m_StatueCtrl.SetWindowText("Ready");
mRe.m_Round=0;
}
}
CDialog::OnTimer(nIDEvent);
}
void CAntAcoDlg::OnBnStop()
{
// TODO: Add your control notification handler code here
KillTimer(1);
mb_Start=FALSE;
mb_HaveResult=FALSE;
}
void CAntAcoDlg::DrawTrace()
{
CDC *ndc;
CPen *pnpen,*oldpen;
ndc=this->GetDC();
double maxtrail=0;
for(int m=0;m<iCityCount;m++)
{
for(int n=0;n<iCityCount;n++)
{
if(Map.m_dTrial[m][n]>maxtrail) maxtrail=Map.m_dTrial[m][n];
}
}
for(int i=0;i<iCityCount;i++)
{
frompoint.x=(int)(((Map.m_CityPos[i][0]))*m_RateX+m_OffsetX);
frompoint.y=(int)(((Map.m_CityPos[i][1]))*m_RateY+m_OffsetY);
for(int j=0;j<iCityCount;j++)
{
int width;
double l=Map.m_dTrial[i][j]/maxtrail;
width=l*6;
if(width>=1)
{
pnpen=new CPen(0,width,COLORREF(RGB(128,214,255)));//创建画笔
oldpen=ndc->SelectObject(pnpen);
ndc->MoveTo(frompoint);
topoint.x=(int)(((Map.m_CityPos[j][0]))*m_RateX+m_OffsetX);
topoint.y=(int)(((Map.m_CityPos[j][1]))*m_RateY+m_OffsetY);
ndc->LineTo(topoint);
ndc->SelectObject(oldpen);
if(pnpen) delete pnpen;
}
}
}
}
void CAntAcoDlg::DrawTitle()
{
CDC *ndc,*odc;
CPen *pnpen,*oldpen;
ndc=this->GetDC();
for(int i=0;i<iCityCount;i++)
{
Map.m_RateX=0.05;
Map.m_RateY=0.12;
frompoint.x=(int)(((Map.m_CityPos[i][0]))*m_RateX+m_OffsetX);
frompoint.y=(int)(((Map.m_CityPos[i][1]))*m_RateY+m_OffsetY);
CString str;
str.Format("%d",i);
TitleOut(ndc,frompoint,str);
}
}
void CAntAcoDlg::DrawBestTrace()
{
CDC *ndc,*odc;
CPen *pnpen,*oldpen;
ndc=this->GetDC();
for(int i=0;i<iCityCount-1;i++)
{
frompoint.x=(int)(((Map.m_CityPos[mRe.temptour[i]][0]))*m_RateX+m_OffsetX);
frompoint.y=(int)(((Map.m_CityPos[mRe.temptour[i]][1]))*m_RateY+m_OffsetY);
pnpen=new CPen(0,2,COLORREF(RGB(230,23,6)));//创建画笔
oldpen=ndc->SelectObject(pnpen);
ndc->MoveTo(frompoint);
topoint.x=(int)(((Map.m_CityPos[mRe.temptour[i+1]][0]))*m_RateX+m_OffsetX);
topoint.y=(int)(((Map.m_CityPos[mRe.temptour[i+1]][1]))*m_RateY+m_OffsetY);
ndc->LineTo(topoint);
ndc->SelectObject(oldpen);
if(pnpen) delete pnpen;
}
frompoint.x=(int)(((Map.m_CityPos[mRe.temptour[iCityCount-1]][0]))*m_RateX+m_OffsetX);
frompoint.y=(int)(((Map.m_CityPos[mRe.temptour[iCityCount-1]][1]))*m_RateY+m_OffsetY);
pnpen=new CPen(0,2,COLORREF(RGB(230,23,6)));//创建画笔
oldpen=ndc->SelectObject(pnpen);
ndc->MoveTo(frompoint);
topoint.x=(int)(((Map.m_CityPos[mRe.temptour[0]][0]))*m_RateX+m_OffsetX);
topoint.y=(int)(((Map.m_CityPos[mRe.temptour[0]][1]))*m_RateY+m_OffsetY);
ndc->LineTo(topoint);
ndc->SelectObject(oldpen);
if(pnpen) delete pnpen;
}
void CAntAcoDlg::TitleOut(CDC* pDC,CPoint p,CString str)
{
CFont font;
font.CreateFont(10,10,0,0,1,0,0,0,0,0,0,0,0,"FONT");
pDC->SelectObject(&font);
pDC->TextOut(p.x,p.y,LPCTSTR(str));
}
void CAntAcoDlg::OnAboutbox()
{
// TODO: Add your control notification handler code here
OnSysCommand(IDM_ABOUTBOX,1);
}
void CAntAcoDlg::OnCheckTitle()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(&m_DrawArea);
}
void CAntAcoDlg::OnCheckBest()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(&m_DrawArea);
}
void CAntAcoDlg::OnCheckShowtrace()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(&m_DrawArea);
}
void CAntAcoDlg::OnBnLoadv()
{
// TODO: Add your control notification handler code here
solvefile.open("solve.txt",ios::out);
mb_LoadFile=TRUE;
mRe.init();
mRe.LoadFile();
mRe.GetAnt();
//计算比例
m_RateX=(double)((m_DrawArea.right-m_DrawArea.left))/(double)(mRe.rightX-mRe.leftX);
m_RateY=(double)((m_DrawArea.bottom-m_DrawArea.top))/(double)(mRe.topY-mRe.bottomY);
m_OffsetX=-(m_RateX*mRe.leftX-m_DrawArea.left);
m_OffsetY=(m_DrawArea.bottom-m_RateY*(mRe.topY));
UpdateData(TRUE);
CString s;
s.Format("文件加载成功...蚂蚁数:%d,城市数:%d,迭代数:%d,Alpha:%f,Beta:%f,rou:%d ",iAntCount,iCityCount,m_TotalRound,m_ALPHA,m_BETA,m_ROU);
m_Result=s;
UpdateData(FALSE);
m_StatueCtrl.SetWindowText("Ready");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -