📄 gademoview.cpp
字号:
// gademoView.cpp : implementation of the CGademoView class
//
#include "stdafx.h"
#include "gademo.h"
#include "gademoDoc.h"
#include "gademoView.h"
#include <math.h>
#include <ga/GARealGenome.C>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGademoView
IMPLEMENT_DYNCREATE(CGademoView, CView)
BEGIN_MESSAGE_MAP(CGademoView, CView)
//{{AFX_MSG_MAP(CGademoView)
ON_COMMAND(GA_CNTRL_EVOLVE, OnEvolve)
ON_COMMAND(GA_CNTRL_REWIND, OnReset)
ON_COMMAND(GA_CNTRL_STEP, OnStep)
ON_COMMAND(GA_CNTRL_STOP, OnStop)
ON_COMMAND(GA_CNTRL_SOME, OnEvolveSome)
ON_COMMAND(GA_PARAMETERS, OnParameters)
ON_COMMAND(GA_VIEW_GRID, OnSelectViewGrid)
ON_UPDATE_COMMAND_UI(GA_VIEW_GRID, OnUpdateViewGrid)
ON_COMMAND(GA_MALG_CROWDING, OnSelectGACrowding)
ON_UPDATE_COMMAND_UI(GA_MALG_CROWDING, OnUpdateGACrowding)
ON_COMMAND(GA_MALG_SIMPLE, OnSelectGASimple)
ON_UPDATE_COMMAND_UI(GA_MALG_SIMPLE, OnUpdateGASimple)
ON_COMMAND(GA_MALG_SS, OnSelectGASteadyState)
ON_UPDATE_COMMAND_UI(GA_MALG_SS, OnUpdateGASteadyState)
ON_COMMAND(GA_MALG_SS_SHARING, OnSelectGASSSharing)
ON_UPDATE_COMMAND_UI(GA_MALG_SS_SHARING, OnUpdateGASSSharing)
ON_COMMAND(GA_MALG_DEME, OnSelectGADeme)
ON_UPDATE_COMMAND_UI(GA_MALG_DEME, OnUpdateGADeme)
ON_COMMAND(GA_MALG_INCREMENTAL, OnSelectGAIncremental)
ON_UPDATE_COMMAND_UI(GA_MALG_INCREMENTAL, OnUpdateGAIncremental)
ON_COMMAND(GA_MREP_REAL, OnSelectRepReal)
ON_UPDATE_COMMAND_UI(GA_MREP_REAL, OnUpdateRepReal)
ON_COMMAND(GA_MREP_BIN, OnSelectRepBinary)
ON_UPDATE_COMMAND_UI(GA_MREP_BIN, OnUpdateRepBinary)
ON_COMMAND(GA_MREP_BIN8, OnSelectRepBinary8Bit)
ON_UPDATE_COMMAND_UI(GA_MREP_BIN8, OnUpdateRepBinary8Bit)
ON_COMMAND(GA_MFUNC_RIPPLES, OnSelectFuncRipples)
ON_UPDATE_COMMAND_UI(GA_MFUNC_RIPPLES, OnUpdateFuncRipples)
ON_COMMAND(GA_MFUNC_LOAF, OnSelectFuncLoaf)
ON_UPDATE_COMMAND_UI(GA_MFUNC_LOAF, OnUpdateFuncLoaf)
ON_COMMAND(GA_MFUNC_FOXHOLES, OnSelectFuncFoxholes)
ON_UPDATE_COMMAND_UI(GA_MFUNC_FOXHOLES, OnUpdateFuncFoxholes)
ON_COMMAND(GA_MFUNC_SCHWEFEL, OnSelectFuncSchwefel)
ON_UPDATE_COMMAND_UI(GA_MFUNC_SCHWEFEL, OnUpdateFuncSchwefel)
ON_COMMAND(GA_MFUNC_RIPPLE_SHIFT, OnSelectFuncRipplesShifted)
ON_UPDATE_COMMAND_UI(GA_MFUNC_RIPPLE_SHIFT, OnUpdateFuncRipplesShifted)
ON_COMMAND(GA_MREP_OPERATORS, OnOperators)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGademoView construction/destruction
CGademoView::CGademoView(){
_theGenome = 0;
_theGA = 0;
_params = 0;
_ops = 0;
_pmut = (float)0.01;
_pcross = (float)0.9;
_ngen = 150;
_popsize = 200;
_npop = 1;
_enabled = 1;
_running = 0;
_showGrid = 0;
_whichGA = SIMPLE;
_whichFunction = RIPPLE;
_whichRep = REAL;
_whichCross = SINGLE_POINT;
_whichMut = GAUSSIAN;
configure();
}
CGademoView::~CGademoView(){
delete _params;
delete _ops;
delete _theGA;
delete _theGenome;
}
BOOL CGademoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGademoView drawing
void CGademoView::OnDraw(CDC* pDC){
CGademoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
draw(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CGademoView diagnostics
#ifdef _DEBUG
void CGademoView::AssertValid() const
{
CView::AssertValid();
}
void CGademoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGademoDoc* CGademoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGademoDoc)));
return (CGademoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGademoView message handlers
void CGademoView::OnSelectViewGrid() {
_showGrid = (_showGrid ? 0 : 1);
InvalidateRect(NULL);
}
void CGademoView::OnUpdateViewGrid(CCmdUI* pCmdUI) {
if(_showGrid) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnEvolve() {
if(!_running) {
_enabled = -1;
AfxBeginThread(Evolve, this, THREAD_PRIORITY_NORMAL);
}
}
void CGademoView::OnReset() {
_enabled = 0;
while(_running) { ; } // wait for thread to stop
if(_theGA) _theGA->initialize();
InvalidateRect(NULL);
}
void CGademoView::OnStep() {
if(!_running) {
if(_theGA) _enabled = _theGA->generation()+1;
AfxBeginThread(Evolve, this, THREAD_PRIORITY_NORMAL);
}
}
void CGademoView::OnStop() {
_enabled = 0;
}
void CGademoView::OnEvolveSome() {
if(!_running) {
if(_theGA) _enabled = _theGA->generation()+10;
AfxBeginThread(Evolve, this, THREAD_PRIORITY_NORMAL);
}
}
UINT CGademoView::Evolve(LPVOID param) {
CGademoView* ptr = reinterpret_cast<CGademoView*>(param);
if(ptr->_running) return 0;
if(! ptr->_theGA) return 0;
int n = ptr->_enabled;
while(ptr->_enabled != 0) {
ptr->_running = 1;
if((n < 0 && ptr->_theGA->done() == gaFalse) || ptr->_theGA->generation() < n){
ptr->_theGA->step();
ptr->InvalidateRect(NULL);
}
else {
ptr->_enabled = 0;
}
}
ptr->_running = 0;
return 0;
}
void CGademoView::OnSelectFuncRipples() {
_whichFunction = RIPPLE;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectFuncRipplesShifted() {
_whichFunction = RIPPLE_SHIFTED;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectFuncLoaf() {
_whichFunction = LOAF;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectFuncFoxholes() {
_whichFunction = FOXHOLES;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectFuncSchwefel() {
_whichFunction = SCHWEFEL;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnUpdateFuncFoxholes(CCmdUI* pCmdUI) {
if(_whichFunction == FOXHOLES) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateFuncLoaf(CCmdUI* pCmdUI) {
if(_whichFunction == LOAF) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateFuncRipples(CCmdUI* pCmdUI) {
if(_whichFunction == RIPPLE) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateFuncRipplesShifted(CCmdUI* pCmdUI) {
if(_whichFunction == RIPPLE_SHIFTED) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateFuncSchwefel(CCmdUI* pCmdUI) {
if(_whichFunction == SCHWEFEL) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnSelectGASimple() {
_whichGA = SIMPLE;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectGASteadyState() {
_whichGA = STEADY_STATE;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectGASSSharing() {
_whichGA = STEADY_STATE_SHARING;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectGAIncremental() {
_whichGA = INCREMENTAL;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectGACrowding() {
_whichGA = CROWDING;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectGADeme() {
_whichGA = DEME;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnUpdateGASimple(CCmdUI* pCmdUI) {
if(_whichGA == SIMPLE) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateGASteadyState(CCmdUI* pCmdUI) {
if(_whichGA == STEADY_STATE) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateGASSSharing(CCmdUI* pCmdUI) {
if(_whichGA == STEADY_STATE_SHARING) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateGAIncremental(CCmdUI* pCmdUI) {
if(_whichGA == INCREMENTAL) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateGACrowding(CCmdUI* pCmdUI) {
if(_whichGA == CROWDING) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateGADeme(CCmdUI* pCmdUI) {
if(_whichGA == DEME) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateRepBinary(CCmdUI* pCmdUI) {
if(_whichRep == BINARY_4BIT) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateRepBinary8Bit(CCmdUI* pCmdUI) {
if(_whichRep == BINARY_8BIT) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnUpdateRepReal(CCmdUI* pCmdUI) {
if(_whichRep == REAL) pCmdUI->SetCheck(1);
else pCmdUI->SetCheck(0);
}
void CGademoView::OnSelectRepBinary() {
_whichRep = BINARY_4BIT;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectRepBinary8Bit() {
_whichRep = BINARY_8BIT;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnSelectRepReal() {
_whichRep = REAL;
configure();
InvalidateRect(NULL);
}
void CGademoView::OnParameters() {
if(! _params) {
_params = new CGAParametersDialog(this);
_params->m_pcross = pCrossover();
_params->m_pmut = pMutation();
_params->m_ngen = nGenerations();
_params->m_popsize = populationSize();
if(_params->Create() != TRUE) {
delete _params;
_params = 0;
}
}
else {
_params->ShowWindow(SW_RESTORE);
_params->EnableWindow();
_params->UpdateWindow();
// _params->SetActiveWindow();
}
if(_params) {
_params->GetDlgItem(IDC_APPLY)->EnableWindow(FALSE);
_params->GetDlgItem(IDC_REVERT)->EnableWindow(FALSE);
}
}
void CGademoView::OnOperators() {
if(! _ops) {
_ops = new CGAOperatorsDialog(this);
if(_ops->Create() != TRUE) {
delete _ops;
_ops = 0;
}
}
else {
_ops->ShowWindow(SW_RESTORE);
_ops->EnableWindow();
_ops->UpdateWindow();
_ops->SetActiveWindow();
}
if(_ops) {
_ops->GetDlgItem(IDC_OP_APPLY)->EnableWindow(FALSE);
_ops->GetDlgItem(IDC_OP_REVERT)->EnableWindow(FALSE);
_ops->configure();
}
}
float CGademoView::pCrossover(float n) {
if(0.0<=n && n<=1.0) {
_pcross = _theGA->pCrossover(n);
}
return _pcross;
}
float CGademoView::pMutation(float n) {
if(0.0<=n && n<=1.0) {
_pmut = _theGA->pMutation(n);
}
return _pmut;
}
int CGademoView::populationSize(int n) {
return _popsize = _theGA->populationSize(n);
}
int CGademoView::nGenerations(int n) {
return _ngen = _theGA->nGenerations(n);
}
CGademoView::Mutation CGademoView::mutation(Mutation m) {
return _whichMut = m;
}
CGademoView::Crossover CGademoView::crossover(Crossover m) {
return _whichCross = m;
}
// Default values for the user-selectable parameters.
CGademoView::Function CGademoView::_whichFunction = RIPPLE;
CGademoView::Algorithm CGademoView::_whichGA = SIMPLE;
CGademoView::Representation CGademoView::_whichRep = REAL;
CGademoView::Crossover CGademoView::_whichCross = AVERAGING;
CGademoView::Mutation CGademoView::_whichMut = GAUSSIAN;
// This is where we contruct our genome, configure it, construct
// a genetic algorithm, configure that, and set everything up for
// an evolution.
void
CGademoView::configure() {
delete _theGenome;
delete _theGA;
int nbits = 4;
if(_whichRep == BINARY_8BIT) nbits = 8;
// construct a genome. the function matters since that
// determines what the bounding box will be for the plot.
// we also care about which representation has been selected.
switch(_whichFunction) {
case LOAF:
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
GABin2DecPhenotype map;
map.add(nbits, -6, 6);
map.add(nbits, -6, 6);
_theGenome = new GABin2DecGenome(map, Loaf);
}
else _theGenome = new GARealGenome(2, GARealAlleleSet(-6,6), Loaf);
break;
case FOXHOLES:
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
GABin2DecPhenotype map;
map.add(nbits, -60, 60);
map.add(nbits, -60, 60);
_theGenome = new GABin2DecGenome(map, Foxholes);
}
else _theGenome = new GARealGenome(2, GARealAlleleSet(-60,60), Foxholes);
break;
case SCHWEFEL:
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
GABin2DecPhenotype map;
map.add(nbits, -500, 500);
map.add(nbits, -500, 500);
_theGenome = new GABin2DecGenome(map, Schwefel);
}
else _theGenome = new GARealGenome(2, GARealAlleleSet(-500,500), Schwefel);
break;
case RIPPLE_SHIFTED:
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
GABin2DecPhenotype map;
map.add(nbits, -10, 10);
map.add(nbits, -10, 10);
_theGenome = new GABin2DecGenome(map, RipplesShifted);
}
else _theGenome = new GARealGenome(2, GARealAlleleSet(-10,10), RipplesShifted);
break;
case RIPPLE:
default:
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
GABin2DecPhenotype map;
map.add(nbits, -10, 10);
map.add(nbits, -10, 10);
_theGenome = new GABin2DecGenome(map, Ripples);
}
else _theGenome = new GARealGenome(2, GARealAlleleSet(-10,10), Ripples);
break;
}
// set the operators on the genome, depending on what kind
// of genome we have and what operators are desired. note that
// we could just use the defaults that are in GAlib, but by
// doing it here we know exactly what operators we will be
// using. (defaults are listed in the GAlib documentation)
_theGenome->crossover(GenericCrossover);
_theGenome->mutator(GenericMutation);
if(_whichRep == BINARY_4BIT || _whichRep == BINARY_8BIT) {
_theGenome->initializer(GA1DBinaryStringGenome::UniformInitializer);
// _theGenome->comparator();
if(_whichMut != FLIP) _whichMut = FLIP;
if(_whichCross != UNIFORM && _whichCross != SINGLE_POINT &&
_whichCross != TWO_POINT) _whichCross = UNIFORM;
}
else {
_theGenome->initializer(GARealGenome::UniformInitializer);
_theGenome->comparator(RealComparator);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -