📄 sgaview.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()
{
float oldmaxfit;
int oldmaxpp;
if(!MyGA.lsRptData.IsEmpty())
MyGA.InitData();
MyGA.InitPop();
MyGA.InitReport();
MyGA.pPop=MyGA.pNewPop;
MyGA.pNewPop=MyGA.pOldPop;
MyGA.StatPop(MyGA.pNewPop);
MyGA.Report(MyGA.nGen);
MyGA.pNewPop=MyGA.pPop;
do
{
MyGA.nGen++;
oldmaxfit=MyGA.fMaxFit;
oldmaxpp=MyGA.nMaxPop;
MyGA.UpdateGen();
MyGA.StatPop(MyGA.pNewPop);
if(MyGA.fMaxFit<oldmaxfit)
{
for(unsigned i=0;i<MyGA.nChromLen;i++)
MyGA.pNewPop[MyGA.nMinPop].chrom[i]=MyGA.pOldPop[oldmaxpp].chrom[i];
MyGA.pNewPop[MyGA.nMinPop].x=MyGA.pOldPop[oldmaxpp].x;
MyGA.pNewPop[MyGA.nMinPop].fitness=MyGA.pOldPop[oldmaxpp].fitness;
MyGA.StatPop(MyGA.pNewPop);
}
MyGA.Report(MyGA.nGen);
MyGA.pPop=MyGA.pOldPop;
MyGA.pOldPop=MyGA.pNewPop;
MyGA.pNewPop=MyGA.pPop;
}while(MyGA.nGen<MyGA.nMaxGen);
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);
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 + -