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

📄 sgaview.cpp

📁 VC编写的基本遗传算法,建立遗传算子类
💻 CPP
字号:
// SGAView.cpp : implementation of the CSGAView class
//

#include "stdafx.h"
#include "SGA.h"

#include "SGADoc.h"
#include "SGAView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSGAView

IMPLEMENT_DYNCREATE(CSGAView, CScrollView)

BEGIN_MESSAGE_MAP(CSGAView, CScrollView)
	//{{AFX_MSG_MAP(CSGAView)
	ON_COMMAND(ID_INPUTPARAM, OnInputParam)
	ON_COMMAND(ID_RUNGA, OnRunGA)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSGAView construction/destruction

CSGAView::CSGAView()
{

}

CSGAView::~CSGAView()
{
}

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

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSGAView drawing

void CSGAView::OnDraw(CDC* pDC)
{
	CSGADoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	pDC->SetBkMode(TRANSPARENT);

	POSITION pos=MyGA.lsRptData.GetHeadPosition();
	UINT ntxt=0;
	for(int i=0;i<MyGA.lsRptData.GetCount();i++)
	{
		pDC->TextOut(0,ntxt+=tm.tmHeight,MyGA.lsRptData.GetNext(pos));
	}
}

void CSGAView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100; 
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CSGAView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSGAView diagnostics

#ifdef _DEBUG
void CSGAView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSGAView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CSGAView message handlers

void CSGAView::OnInputParam() 
{
	// TODO: Add your command handler code here
	if(ParDlg.DoModal()==IDOK)
		MyGA.InitData(ParDlg.m_nPopSize,ParDlg.m_nChromLen,ParDlg.m_nMaxGen,ParDlg.m_fPc,ParDlg.m_fPm);
}

void CSGAView::OnRunGA() 
{
	MyGA.RunGA();

	FILE *fp;
	if((fp=fopen("SGAReport.txt","w"))==NULL)
		MessageBox("Cannot Open File for Report !");
	POSITION pos=MyGA.lsRptData.GetHeadPosition();
	for(int i=0;i<MyGA.lsRptData.GetCount();i++)
		fprintf(fp,"%s\n",(LPCTSTR)MyGA.lsRptData.GetNext(pos));
	fclose(fp);

	fp=fopen("SGAMaxFit.txt","w");
	for(unsigned j=0;j<=MyGA.nMaxGen;j++)
		fprintf(fp,"%f ",MyGA.MaxFitStat[j]);
	fclose(fp);

	TEXTMETRIC tm;
	CDC *pDC=GetDC();
	pDC->GetTextMetrics(&tm);
	SetScrollSizes(MM_TEXT,CSize(1000,MyGA.lsRptData.GetCount()*tm.tmHeight));

	Invalidate();
}

⌨️ 快捷键说明

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