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

📄 ga_tspview.cpp

📁 与本人上次上传的类别一样
💻 CPP
字号:
// GA_TSPView.cpp : implementation of the CGA_TSPView class
//

#include "stdafx.h"
#include "GA_TSP.h"
#include "math.h"
#include "GA_TSPDoc.h"
#include "GA_TSPView.h"
#include "ExternFile.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView

IMPLEMENT_DYNCREATE(CGA_TSPView, CView)

BEGIN_MESSAGE_MAP(CGA_TSPView, CView)
	//{{AFX_MSG_MAP(CGA_TSPView)
	ON_WM_ERASEBKGND()
	ON_COMMAND(IDM_Start, OnStart)
	ON_WM_SIZE()
	ON_COMMAND(IDM_Cicle, OnCicle)
	ON_COMMAND(IDM_Real, OnReal)
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView construction/destruction

CGA_TSPView::CGA_TSPView()
{
	// TODO: add construction code here

}

CGA_TSPView::~CGA_TSPView()
{
}

BOOL CGA_TSPView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView drawing

void CGA_TSPView::OnDraw(CDC* pDC)
{
	CGA_TSPDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView printing

BOOL CGA_TSPView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGA_TSPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGA_TSPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView diagnostics

#ifdef _DEBUG
void CGA_TSPView::AssertValid() const
{
	CView::AssertValid();
}

void CGA_TSPView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGA_TSPDoc* CGA_TSPView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGA_TSPDoc)));
	return (CGA_TSPDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGA_TSPView message handlers

BOOL CGA_TSPView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CDC mem;

	CBitmap Bmp;
	mem.CreateCompatibleDC(pDC);
	Bmp.CreateCompatibleBitmap(pDC, g_WindowsWidth, g_WindowsHeight);
	mem.SelectObject(&Bmp);
/*
	CRect r(0,0,g_WindowsWidth,g_WindowsHeight);
	CBrush Brush_Bak(RGB(255,255,255));
	CBrush *oldBrush=pDC->SelectObject(&Brush_Bak);
	mem.SelectObject(Brush_Bak);
	mem.FillRect(&r,&Brush_Bak);
*/
	//if(ifCicle)
	{
		Graph.DrawCity(&mem);
		
		if(ifGAing)
		{
			Graph.DrawPath(GenAlg.m_vecPop[GenAlg.m_iFittestGenome].Cities, &mem);
		}
		
		if(ifGAing)
		{
			GenAlg.outputTheData(&mem);
		}
	}

	if(ifReal)
	{
	}

	//(3)拷贝到屏幕
	pDC->BitBlt(0,0,g_WindowsWidth,g_WindowsHeight,&mem,0,0,SRCCOPY);

	return TRUE;

	//return CView::OnEraseBkgnd(pDC);
}

double CGA_TSPView::CalculateDistance(vector <int> Cities)
{
	double TotalDistance = 0;
	double dx,dy;
	for(int i = 1;i < g_numGen;i++)
	{
		int a = Cities[i];
		int b = Cities[i-1];
		dx = Graph.m_CityPoint[Cities[i]].x - Graph.m_CityPoint[Cities[i-1]].x;
		dy = Graph.m_CityPoint[Cities[i]].y - Graph.m_CityPoint[Cities[i-1]].y;
		TotalDistance += sqrt(dx * dx + dy * dy);
	}
	
	dx = Graph.m_CityPoint[Cities[0]].x - Graph.m_CityPoint[Cities[g_numGen-1]].x;
	dy = Graph.m_CityPoint[Cities[0]].y - Graph.m_CityPoint[Cities[g_numGen-1]].y;

	TotalDistance += sqrt(dx * dx + dy * dy);

	return TotalDistance;
}

void CGA_TSPView::StartGenAlg()
{
	srand( (unsigned)time( NULL ) );

	GenAlg.init(g_popsize,
                 g_dMutationRate,
                 g_dCrossoverRate,
                 g_numGen);

	m_population.clear();
	m_population = GenAlg.m_vecPop;

	for(int Generation = 1;Generation <= g_Generation;Generation++)
	{
		for(int i=0;i<g_popsize;i++)//里面是对每一条基因组操作
		{
			//计算适应性
			m_population[i].dFitness = CalculateDistance(m_population[i].Cities);
		}
		GenAlg.Epoch(m_population);
		ifGAing = 1;
		Invalidate();
		if(ifStopThread == 1)
		{
			CDC *pDC = GetDC();
			OnEraseBkgnd(pDC);
			ifGAing = 0;
			return;
		}
	}
	Invalidate();
	ifStopThread = 1;
	ifGAing = 0;
}

DWORD WINAPI ThreadForStartGenAlg(
  LPVOID lpParameter   // thread data
)
{
	((CGA_TSPView *)lpParameter)->StartGenAlg();
	return 0;
}

void CGA_TSPView::OnStart()
{
	// TODO: Add your command handler code here

	CWnd* pParent = GetParent();
	CMenu* pSubMenu = pParent->GetMenu();

	if(ifCicle)
	{
		if(ifStopThread == 1)
		{
			ifSize = 0;
			
			ifStopThread = 0;
			
			CGA_TSPView * pView = (CGA_TSPView *)((CMainFrame*)AfxGetApp()->m_pMainWnd)->GetActiveView(); 
			
			m_hThread = CreateThread(NULL,0,ThreadForStartGenAlg,pView,0,NULL);
			
			pSubMenu->ModifyMenu(IDM_Start,MF_BYCOMMAND,IDM_Start,_T("结束"));
		}
		else
		{
			ifStopThread = 1;
			ifSize = 1;
			ifGAing = 0;
			pSubMenu->ModifyMenu(IDM_Start,MF_BYCOMMAND,IDM_Start,_T("开始"));
		}
	}
	else if(ifReal)
	{
		ifClick = 0;
		if(ifStopThread == 1)
		{
			ifSize = 0;
			
			ifStopThread = 0;
			
			CGA_TSPView * pView = (CGA_TSPView *)((CMainFrame*)AfxGetApp()->m_pMainWnd)->GetActiveView(); 
			
			m_hThread = CreateThread(NULL,0,ThreadForStartGenAlg,pView,0,NULL);
			
			pSubMenu->ModifyMenu(IDM_Start,MF_BYCOMMAND,IDM_Start,_T("结束"));
		}
		else
		{
			ifStopThread = 1;
			ifSize = 1;
			ifGAing = 0;
			pSubMenu->ModifyMenu(IDM_Start,MF_BYCOMMAND,IDM_Start,_T("开始"));
		}
	}

	DrawMenuBar();
	Invalidate();
}

void CGA_TSPView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	CDC *pDC = GetDC();
	Graph.init(pDC);

	// TODO: Add your specialized code here and/or call the base class
	
}

void CGA_TSPView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	static int ifOnce = 0;

	
	if(ifStopThread == 1)
	{
		g_WindowsWidth = cx;
		g_WindowsHeight = cy;
		CDC *pDC = GetDC();
		Graph.init(pDC);
		Invalidate();
	}
	

	
	// TODO: Add your message handler code here
	
}

void CGA_TSPView::OnCicle() 
{
	// TODO: Add your command handler code here
	ifCicle = 1;
	ifReal = 0;
	ifClick = 0;

	g_numGen = g_NumCity;
	CDC *pDC = GetDC();
	Graph.init(pDC);
	Invalidate();
}

void CGA_TSPView::OnReal() 
{
	// TODO: Add your command handler code here
	ifCicle = 0;
	ifReal = 1;
	ifClick = 1;
	g_numGen = 0;
	CDC *pDC = GetDC();
	Graph.init2(pDC);
	Invalidate();
}

void CGA_TSPView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CGA_TSPView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(ifClick)
	{
		Graph.m_CityPoint[g_numGen].x = point.x;
		Graph.m_CityPoint[g_numGen].y = point.y;
		g_numGen++;
		CDC *pDC = GetDC();
		Graph.init2(pDC);
		Invalidate();
	}
	CView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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