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

📄 ga_mfcview.cpp

📁 一个利用C++开发遗传算法类
💻 CPP
字号:
// GA_MFCView.cpp : implementation of the CGA_MFCView class
//

#include "stdafx.h"
#include "GA_MFC.h"

#include "GA_MFCDoc.h"
#include "GA_MFCView.h"


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

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView

IMPLEMENT_DYNCREATE(CGA_MFCView, CListView)

BEGIN_MESSAGE_MAP(CGA_MFCView, CListView)
	//{{AFX_MSG_MAP(CGA_MFCView)
	ON_COMMAND(ID_INITIAL, OnInitial)
	ON_COMMAND(ID_RUN_ONE, OnRunOne)
	ON_UPDATE_COMMAND_UI(ID_RUN_ONE, OnUpdateRunOne)
	ON_UPDATE_COMMAND_UI(ID_RUN_TEN, OnUpdateRunTen)
	ON_COMMAND(ID_RUN_TEN, OnRunTen)
	ON_UPDATE_COMMAND_UI(ID_RUN_FIFTY, OnUpdateRunFifty)
	ON_COMMAND(ID_RUN_FIFTY, OnRunFifty)
	ON_COMMAND(ID_CLEAN, OnClean)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView construction/destruction

CGA_MFCView::CGA_MFCView()
{
	// TODO: add construction code here
	this->IsInisitaled=FALSE;
}

CGA_MFCView::~CGA_MFCView()
{
}

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

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView drawing

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

void CGA_MFCView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	this->GetListCtrl().ModifyStyle(LVS_TYPEMASK,LVS_REPORT);

	int i=0;
	this->GetListCtrl().InsertColumn(i++,"个体号",LVCFMT_CENTER,50,-1);
	this->GetListCtrl().InsertColumn(i++,"染色体",LVCFMT_CENTER,200,-1);
	this->GetListCtrl().InsertColumn(i++,"表现型1",LVCFMT_CENTER,60,-1);
	if(1)
		this->GetListCtrl().InsertColumn(i++,"表现型2",LVCFMT_CENTER,60,-1);

	this->GetListCtrl().InsertColumn(i++,"适应度",LVCFMT_CENTER,50,-1);
	this->GetListCtrl().InsertColumn(i++,"选择率",LVCFMT_CENTER,50,-1);
	this->GetListCtrl().InsertColumn(i++,"择率和",LVCFMT_CENTER,50,-1);
	this->GetListCtrl().InsertColumn(i++,"世代",LVCFMT_CENTER,50,-1);
	this->GetListCtrl().InsertColumn(i++,"平均适应度",LVCFMT_CENTER,100,-1);
	


	this->GetDocument()->m_pView=this;
}	

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView diagnostics

#ifdef _DEBUG
void CGA_MFCView::AssertValid() const
{
	CListView::AssertValid();
}

void CGA_MFCView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CGA_MFCView message handlers

BOOL CGA_MFCView::InitialRace()
{

	Variable pVar[2]={{20,-5,5},{20,-5,5}};
	CString str;
	CRaceDlg m_RaceDlg;
	int racesize;
	double cross,mutate;
	

	if(this->IsInisitaled==TRUE)
	{
		m_RaceDlg.m_cross=this->m_Race.Prossible_Cross;
		m_RaceDlg.m_mutate=this->m_Race.Prossible_Mutate;
		m_RaceDlg.m_racesize=this->m_Race.RaceSize;
		m_RaceDlg.m_bitnum1=this->m_Race.m_pIdls->pBehaverList->BitNumbers;
		m_RaceDlg.m_bitnum2=this->m_Race.m_pIdls->pBehaverList->pmBehaver->BitNumbers;
		m_RaceDlg.m_maxvalue1=this->m_Race.m_pIdls->pBehaverList->MaxValue;
		m_RaceDlg.m_maxvalue2=this->m_Race.m_pIdls->pBehaverList->pmBehaver->MaxValue;
		m_RaceDlg.m_minvalue1=this->m_Race.m_pIdls->pBehaverList->MinValue;
		m_RaceDlg.m_minvalue2=this->m_Race.m_pIdls->pBehaverList->pmBehaver->MinValue;
	}
	else
	{
		m_RaceDlg.m_cross=0.25;
		m_RaceDlg.m_mutate=0.01;
		m_RaceDlg.m_racesize=20;
		m_RaceDlg.m_bitnum1=20;
		m_RaceDlg.m_bitnum2=20;
		m_RaceDlg.m_maxvalue1=5;
		m_RaceDlg.m_maxvalue2=5;
		m_RaceDlg.m_minvalue1=-5;
		m_RaceDlg.m_minvalue2=-5;
	}


	if(m_RaceDlg.DoModal()==IDOK)
	{
		pVar[0].BitNumbers=m_RaceDlg.m_bitnum1;
		pVar[1].BitNumbers=m_RaceDlg.m_bitnum2;
		pVar[0].MaxValue=m_RaceDlg.m_maxvalue1;
		pVar[1].MaxValue=m_RaceDlg.m_maxvalue2;
		pVar[0].MinValue=m_RaceDlg.m_minvalue1;
		pVar[1].MinValue=m_RaceDlg.m_minvalue2;
		racesize=m_RaceDlg.m_racesize;
		cross=m_RaceDlg.m_cross;
		mutate=m_RaceDlg.m_mutate;

		m_Race.InitializeRace(racesize,cross,mutate,pVar,2);
	}
	else
	{
		return FALSE;
	}
	return TRUE;
}

void CGA_MFCView::OnInitial() 
{
	// TODO: Add your command handler code here
	if(this->InitialRace())
	{
		this->IsInisitaled=TRUE;
		this->m_Race.Generation=0;
		this->m_Race.CaculateFitness();
		this->m_Race.Display(&(this->GetListCtrl()),this->GetListCtrl().GetItemCount());
	}
}

void CGA_MFCView::OnRunOne() 
{
	// TODO: Add your command handler code here
	this->m_Race.RunOnce(1,&(this->GetListCtrl()));
}

void CGA_MFCView::OnUpdateRunOne(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(this->IsInisitaled==FALSE)
		pCmdUI->Enable(FALSE);
}

void CGA_MFCView::OnUpdateRunTen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(this->IsInisitaled==FALSE)
		pCmdUI->Enable(FALSE);	
}

void CGA_MFCView::OnRunTen() 
{
	// TODO: Add your command handler code here
	int i;
	for(i=0;i<10;i++)
		this->m_Race.RunOnce(1,&(this->GetListCtrl()));
	
}


void CGA_MFCView::Serialize(CArchive& ar) 
{
	int i,j,col_num,row_num;
	CString str;
	if (ar.IsStoring())
	{	// storing code
		row_num=this->GetListCtrl().GetItemCount();
		col_num=9;

		for(i=0;i<row_num;i++)
		{
			for(j=0;j<col_num;j++)
			{
				ar.WriteString(this->GetListCtrl().GetItemText(i,j));
				ar.WriteString("  ");
			}
			str.Format("%c%c",13,10);
			ar.WriteString(str);
		}

	}
	else
	{	// loading code
	}
}

void CGA_MFCView::OnUpdateRunFifty(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(this->IsInisitaled==FALSE)
		pCmdUI->Enable(FALSE);	
}

void CGA_MFCView::OnRunFifty() 
{
	// TODO: Add your command handler code here
	int i;
	for(i=0;i<50;i++)
		this->m_Race.RunOnce(0,&(this->GetListCtrl()));
	this->m_Race.Display(&(this->GetListCtrl()),this->GetListCtrl().GetItemCount());

}

void CGA_MFCView::OnClean() 
{
	// TODO: Add your command handler code here
	this->GetListCtrl().DeleteAllItems();	
}

⌨️ 快捷键说明

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