📄 ccontroller.cpp
字号:
#include "CController.h"
//these hold the geometry of the sweepers and the mines
const int NumSweeperVerts = 16;
const SPoint sweeper[NumSweeperVerts] = {SPoint(-1, -1),
SPoint(-1, 1),
SPoint(-0.5, 1),
SPoint(-0.5, -1),
SPoint(0.5, -1),
SPoint(1, -1),
SPoint(1, 1),
SPoint(0.5, 1),
SPoint(-0.5, -0.5),
SPoint(0.5, -0.5),
SPoint(-0.5, 0.5),
SPoint(-0.25, 0.5),
SPoint(-0.25, 1.75),
SPoint(0.25, 1.75),
SPoint(0.25, 0.5),
SPoint(0.5, 0.5)};
const int NumMineVerts = 4;
const SPoint mine[NumMineVerts] = {SPoint(-1, -1),
SPoint(-1, 1),
SPoint(1, 1),
SPoint(1, -1)};
const int NumObjectVerts = 42;
const SPoint objects[NumObjectVerts] = {SPoint(80, 60),
SPoint(200,60),
SPoint(200,60),
SPoint(200,100),
SPoint(200,100),
SPoint(160,100),
SPoint(160,100),
SPoint(160,200),
SPoint(160,200),
SPoint(80,200),
SPoint(80,200),
SPoint(80,60),
SPoint(250,100),
SPoint(300,40),
SPoint(300,40),
SPoint(350,100),
SPoint(350,100),
SPoint(250, 100),
SPoint(220,180),
SPoint(320,180),
SPoint(320,180),
SPoint(320,300),
SPoint(320,300),
SPoint(220,300),
SPoint(220,300),
SPoint(220,180),
SPoint(12,15),
SPoint(380, 15),
SPoint(380,15),
SPoint(380,360),
SPoint(380,360),
SPoint(12,360),
SPoint(12,360),
SPoint(12,340),
SPoint(12,340),
SPoint(100,290),
SPoint(100,290),
SPoint(12,240),
SPoint(12,240),
SPoint(12,15)};
//---------------------------------------constructor---------------------
//
// initilaize the sweepers, their brains and the GA factory
//
//-----------------------------------------------------------------------
CController::CController(HWND hwndMain,
int cxClient,
int cyClient): m_NumSweepers(CParams::iNumSweepers),
m_bFastRender(false),
m_bRenderBest(false),
m_iTicks(0),
m_hwndMain(hwndMain),
m_hwndInfo(NULL),
m_iGenerations(0),
m_cxClient(cxClient),
m_cyClient(cyClient),
m_iViewThisSweeper(0)
{
//let's create the mine sweepers
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers.push_back(CMinesweeper());
}
//and the vector of sweepers which will hold the best performing sweeprs
for (i=0; i<CParams::iNumBestSweepers; ++i)
{
m_vecBestSweepers.push_back(CMinesweeper());
}
m_pPop = new Cga(CParams::iNumSweepers,
CParams::iNumInputs,
CParams::iNumOutputs,
hwndMain,
10,10);
//create the phenotypes
vector<CNeuralNet*> pBrains = m_pPop->CreatePhenotypes();
//assign the phenotypes
for (i=0; i<m_NumSweepers; i++)
{
m_vecSweepers[i].InsertNewBrain(pBrains[i]);
}
//create a pen for the graph drawing
m_BluePen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
m_RedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
m_GreenPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
m_GreyPenDotted = CreatePen(PS_DOT, 1, RGB(100, 100, 100));
m_RedPenDotted = CreatePen(PS_DOT, 1, RGB(200, 0, 0));
m_OldPen = NULL;
//and the brushes
m_BlueBrush = CreateSolidBrush(RGB(0,0,244));
m_RedBrush = CreateSolidBrush(RGB(150,0,0));
//fill the vertex buffers
for (i=0; i<NumSweeperVerts; ++i)
{
m_SweeperVB.push_back(sweeper[i]);
}
for (i=0; i<NumObjectVerts; ++i)
{
m_ObjectsVB.push_back(objects[i]);
}
}
//--------------------------------------destructor-------------------------------------
//
//--------------------------------------------------------------------------------------
CController::~CController()
{
if (m_pPop)
{
delete m_pPop;
}
DeleteObject(m_BluePen);
DeleteObject(m_RedPen);
DeleteObject(m_GreenPen);
DeleteObject(m_OldPen);
DeleteObject(m_GreyPenDotted);
DeleteObject(m_RedPenDotted);
DeleteObject(m_BlueBrush);
DeleteObject(m_RedBrush);
}
//-------------------------------------Update---------------------------------------
//
// This is the main workhorse. The entire simulation is controlled from here.
//
//--------------------------------------------------------------------------------------
bool CController::Update()
{
//run the sweepers through NUM_TICKS amount of cycles. During this loop each
//sweepers NN is constantly updated with the appropriate information from its
//surroundings. The output from the NN is obtained and the sweeper is moved.
if (m_iTicks++ < CParams::iNumTicks)
{
for (int i=0; i<m_NumSweepers; ++i)
{
//update the NN and position
if (!m_vecSweepers[i].Update(m_ObjectsVB))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
}
//update the NNs of the last generations best performers
if (m_iGenerations > 0)
{
for (int i=0; i<m_vecBestSweepers.size(); ++i)
{
//update the NN and position
if (!m_vecBestSweepers[i].Update(m_ObjectsVB))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
}
}
}
//We have completed another generation so now we need to run the GA
else
{
//first add up each sweepers fitness scores. (remember for this task
//there are many different sorts of penalties the sweepers may incur
//and each one has a coefficient)
for (int swp=0; swp<m_vecSweepers.size(); ++swp)
{
m_vecSweepers[swp].EndOfRunCalculations();
}
//increment the generation counter
++m_iGenerations;
//reset cycles
m_iTicks = 0;
//perform an epoch and grab the new brains
vector<CNeuralNet*> pBrains = m_pPop->Epoch(GetFitnessScores());
//insert the new brains back into the sweepers and reset their
//positions
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers[i].InsertNewBrain(pBrains[i]);
m_vecSweepers[i].Reset();
}
//grab the NNs of the best performers from the last generation
vector<CNeuralNet*> pBestBrains = m_pPop->GetBestPhenotypesFromLastGeneration();
//put them into our record of the best sweepers
for (i=0; i<m_vecBestSweepers.size(); ++i)
{
m_vecBestSweepers[i].InsertNewBrain(pBestBrains[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -