⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawcitylineview.h

📁 50个城市以内的TSP问题, 用遗传算法, 算子采用了pmx和ox算子。
💻 H
字号:
// DrawCityLineView.h : interface of the CDrawCityLineView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_DRAWCITYLINEVIEW_H__291F2660_A9B2_48B2_901F_99457AAFA37F__INCLUDED_)
#define AFX_DRAWCITYLINEVIEW_H__291F2660_A9B2_48B2_901F_99457AAFA37F__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include "GA_TSP.h"
#define  USERTIMER1 2001

CGA_TSP tspProblem;

class CDrawCityLineView : public CWindowImpl<CDrawCityLineView>
{
public:
	DECLARE_WND_CLASS(NULL)

	POINT   city[CNUM];
	int     bestPath[CNUM];
	double  lowestDistance;
	int     Generation;

	SYSTEMTIME systime1, systime0;
	


	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

	void ReadDataFromTSP()
	{
		memcpy(city, tspProblem.city, CNUM * sizeof(POINT) );
		memcpy(bestPath, tspProblem.bestPath, CNUM * sizeof(int) );
		lowestDistance = tspProblem.lowestDistance;
	}

	BEGIN_MSG_MAP(CDrawCityLineView)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_TIMER, OnTimer)
		MESSAGE_HANDLER(WM_USER1, OnUser)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		ReadDataFromTSP();
	
		DWORD threadID;
		HANDLE threadHandler;
		
		GetLocalTime(&systime0);
		systime1 = systime0;

		SetTimer(USERTIMER1, 1000);

		threadHandler = CreateThread(NULL, NULL, EvolveProc, m_hWnd, NULL, &threadID);
		SetThreadPriority(threadHandler, THREAD_PRIORITY_BELOW_NORMAL);
		return 0;
	}

	LRESULT OnUser(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		ReadDataFromTSP();
		GetLocalTime(&systime1);
		Generation = wParam;
		
		if(wParam == Gen)
		{
			KillTimer(USERTIMER1);
		}
		else if(wParam == 0)
		{
			KillTimer(USERTIMER1);
			::InvalidateRect(m_hWnd, NULL, true);
//			::MessageBox(NULL, "","", MB_OK);
			SetTimer(USERTIMER1, 1000);
		}
		return 0;
	}

	LRESULT OnTimer(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		int y = 250 + (CNUM/5)*20;
		RECT rt = {615, y, 780, y+40};
		GetLocalTime(&systime1);
		InvalidateRect(&rt, true);
		return 0;
	}

	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		int i;
		char str[100];
		CPaintDC dc(m_hWnd);
		HPEN penCity = CreatePen(PS_SOLID, 2, RGB(60, 60, 0));
		HPEN penLine = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		HPEN penFrm	 = CreatePen(PS_SOLID, 3, RGB(0, 0, 255));
		HPEN penText = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));

		dc.SelectPen(penText);
		sprintf(str, "%d个城市的TSP问题: ", CNUM);
		dc.TextOut(30, 10, str, -1);
		dc.SelectPen(penLine);
		dc.MoveTo(30, 30);
		dc.LineTo(30, 570);
		dc.LineTo(570, 570);
		dc.LineTo(570, 30);
		dc.LineTo(30, 30);	
		
		dc.SelectPen(penFrm);
		dc.MoveTo(600, 0);
		dc.LineTo(600, 600);

		for(i = 0; i < CNUM; i++)
		{
			dc.SelectPen(penText);			
			sprintf(str, "%d", i);
			dc.TextOut(50+city[i].x, 50+city[i].y, 
				str, -1);
		}
		
		for(i = 0; i < CNUM; i++)
		{
			dc.SelectPen(penCity);
			dc.MoveTo(50+city[bestPath[i]].x -1, 50+city[bestPath[i]].y -1);
			dc.LineTo(50+city[bestPath[i]].x +1, 50+city[bestPath[i]].y -1);
			dc.LineTo(50+city[bestPath[i]].x +1, 50+city[bestPath[i]].y +1);
			dc.LineTo(50+city[bestPath[i]].x -1, 50+city[bestPath[i]].y +1);
			dc.LineTo(50+city[bestPath[i]].x -1, 50+city[bestPath[i]].y -1);

			dc.SelectPen(penLine);
			dc.MoveTo(50+city[bestPath[i]].x, 50+city[bestPath[i]].y);
			dc.LineTo(50+city[bestPath[(i+1)%CNUM]].x, 50+city[bestPath[(i+1)%CNUM]].y);
		}

		int y = 30;
		dc.SelectPen(penText);

		sprintf(str, "城市个数: %d", CNUM);
		dc.TextOut(615, y, str, -1);
		y += 30;

		sprintf(str, "种群规模: %d", POP);
		dc.TextOut(615, y, str, -1);
		y += 30;
		
#ifdef  PMX_OPERATER
		dc.TextOut(615, y, "杂交算子: PMX");
#else
		dc.TextOut(615, y, "杂交算子: OX");
#endif
		y += 30;

		if(Generation < 0)
			Generation = 0;
		sprintf(str, "演化代数: %d", Generation);
		dc.TextOut(615, y, str, -1);
		y += 40;

		dc.TextOut(615, y, "路径长度:", -1);
		y += 20;

		sprintf(str, "%f km", lowestDistance);
		dc.TextOut(615, y, str, -1);
		y += 30;
		
		dc.TextOut(615, y, "得到的周游路径为:", -1);
		y += 20;

		for(i = 0; i < CNUM; i += 5, y+=20)
		{
			sprintf(str, "%3d, %3d, %3d, %3d, %3d", bestPath[i],
				bestPath[i+1], bestPath[i+2], bestPath[i+3], bestPath[i+4]);
			dc.TextOut(615, y, str, -1);
		}
		
		y += 20;
		sprintf(str, "开始时间: %2d:%2d:%2d", 
			systime0.wHour, systime0.wMinute, systime0.wSecond);
		dc.TextOut(615, y, str, -1);
		y += 20;
		sprintf(str, "当前时间: %2d:%2d:%2d", 
			systime1.wHour, systime1.wMinute, systime1.wSecond);
		dc.TextOut(615, y, str, -1);

		::DeleteObject(penCity);
		::DeleteObject(penLine);
		::DeleteObject(penText);
		::DeleteObject(penFrm);
		return 0;
	}
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DRAWCITYLINEVIEW_H__291F2660_A9B2_48B2_901F_99457AAFA37F__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -