📄 ccontroller.cpp
字号:
//run the factory to create a new population
m_vecThePopulation = m_pGA->Epoch(m_vecThePopulation);
//insert the new (hopefully)improved brains back into the sweepers
//and reset their positions etc
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers[i].PutWeights(m_vecThePopulation[i].vecWeights);
m_vecSweepers[i].Reset();
}
}
return true;
}
//------------------------------------Render()--------------------------------------
//
//----------------------------------------------------------------------------------
void CController::Render(HDC surface)
{
//render the stats
string s = "代数: " + itos(m_iGenerations);
TextOut(surface, 5,0, s.c_str(), s.size());
//do not render if running at accelerated speed
if (!m_bFastRender)
{
//display the penalties
m_vecSweepers[0].RenderPenalties(surface);
//changeobjects();
//render the objects
// vector<SPoint>
dk++;
int ar=1;
if(m_iGenerations>50)
ar=2;
if(m_iGenerations>100)
ar=3;
//int a=RandInt(0,50);
int a=RandmInt(0,700/ar,dk);
int b=int(a/(10/ar));
int c=RandmInt(0,2400/ar,dk);
int d=int(c/(10/ar));
m_ObjectsVB[36]=SPoint(350-b, 280);
m_ObjectsVB[37]=SPoint(400-b, 280);
m_ObjectsVB[38]=SPoint(400-b, 280);
m_ObjectsVB[39]=SPoint(400-b, 330);
m_ObjectsVB[40]=SPoint(400-b, 330);
m_ObjectsVB[41]=SPoint(350-b, 330);
m_ObjectsVB[42]=SPoint(350-b, 330);
m_ObjectsVB[43]=SPoint(350-b, 280);
m_ObjectsVB[44]=SPoint(300, 150+d);
m_ObjectsVB[45]=SPoint(350, 150+d);
m_ObjectsVB[46]=SPoint(350, 150+d);
m_ObjectsVB[47]=SPoint(350, 200+d);
m_ObjectsVB[48]=SPoint(350, 200+d);
m_ObjectsVB[49]=SPoint(300, 200+d);
m_ObjectsVB[50]=SPoint(300, 200+d);
m_ObjectsVB[51]=SPoint(300, 150+d);
for (int i=0; i<NumObjectVerts; i+=2)
{
{MoveToEx(surface, m_ObjectsVB[i].x, m_ObjectsVB[i].y, NULL);
LineTo(surface, m_ObjectsVB[i+1].x, m_ObjectsVB[i+1].y);}
}
//record the current pen
m_OldPen = (HPEN)SelectObject(surface, m_GreyPenDotted);
//render the sweepers
for (i=0; i<m_vecSweepers.size(); i++)
{
if (i < CParams::iNumElite)
{
SelectObject(surface, m_OldPen);
}
else
{
SelectObject(surface, m_GreyPenDotted);
/*MoveToEx(surface, 400,305, NULL);
LineTo(surface, 50,305);
MoveToEx(surface, 325,150, NULL);
LineTo(surface, 325,440);*/
}
//render red if collided and elite
if ( m_vecSweepers[i].Collided() && (i < CParams::iNumElite) )
{
SelectObject(surface, m_RedPen);
}
//render dotted red if collided and not elite
if ( m_vecSweepers[i].Collided() && (i > CParams::iNumElite) )
{
SelectObject(surface, m_RedPenDotted);
}
//grab the sweeper vertices
vector<SPoint> sweeperVB = m_SweeperVB;
//transform the vertex buffer
m_vecSweepers[i].WorldTransform(sweeperVB, m_vecSweepers[i].Scale());
//draw the sweeper left track
if(m_iGenerations<50)
{MoveToEx(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y, NULL);
for (int vert=1; vert<4; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y);
//draw the sweeper right track
MoveToEx(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y, NULL);
for (vert=5; vert<8; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y);
MoveToEx(surface, (int)sweeperVB[8].x, (int)sweeperVB[8].y, NULL);
LineTo(surface, (int)sweeperVB[9].x, (int)sweeperVB[9].y);
MoveToEx(surface, (int)sweeperVB[10].x, (int)sweeperVB[10].y, NULL);
for (vert=11; vert<16; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
}
}
//render the sensors
for (i=0; i<CParams::iNumElite; ++i)
{
//grab each sweepers sensor data
vector<SPoint> tranSensors = m_vecSweepers[i].Sensors();
vector<double> SensorReadings = m_vecSweepers[i].SensorReadings();
vector<SPoint> trails=m_vecSweepers[i].trail();
for (int j=0; j<trails.size(); j+=5)
{
RECT rect;
rect.left = trails[j].x - 1;
rect.right = trails[j].x + 1;
rect.top = trails[j].y - 1;
rect.bottom= trails[j].y + 1;
FillRect(surface, &rect, m_BlueBrush);
}
if(m_iGenerations<50)
{for (int sr=0; sr<tranSensors.size(); ++sr)
{
if (SensorReadings[sr] > 0)
{
SelectObject(surface, m_RedPen);
}
else
{
SelectObject(surface, m_GreyPenDotted);
}
//make sure we clip the drawing of the sensors or we will get
//unwanted artifacts appearing
if (!((fabs(m_vecSweepers[i].Position().x - tranSensors[sr].x) >
(CParams::dSensorRange+1))||
(fabs(m_vecSweepers[i].Position().y - tranSensors[sr].y) >
(CParams::dSensorRange+1))))
{
MoveToEx(surface,
(int)m_vecSweepers[i].Position().x,
(int)m_vecSweepers[i].Position().y,
NULL);
LineTo(surface, (int)tranSensors[sr].x, (int)tranSensors[sr].y);
}
}
}
}
SelectObject(surface, m_OldPen);
}//end if
else
{
PlotStats(surface);
}
}
//--------------------------PlotStats-------------------------------------
//
// Given a surface to draw on this function displays stats and a crude
// graph showing best and average fitness
//------------------------------------------------------------------------
void CController::PlotStats(HDC surface)
{
string s = "Best Fitness: " + ftos(m_pGA->BestRawFitness());
TextOut(surface, 5, 20, s.c_str(), s.size());
s = "Average Fitness: " + ftos(m_pGA->AverageRawFitness());
TextOut(surface, 5, 40, s.c_str(), s.size());
//render the graph
float HSlice = (float)cxClient/(m_iGenerations+1);
float VSlice = (float)cyClient/((m_pGA->BestRawFitness()+1)*1.5);
//plot the graph for the best fitness
float x = 0;
m_OldPen = (HPEN)SelectObject(surface, m_RedPen);
MoveToEx(surface, 0, cyClient, NULL);
for (int i=0; i<m_vecBestFitness.size(); ++i)
{
LineTo(surface, x, (cyClient - VSlice*m_vecBestFitness[i]));
x += HSlice;
}
//plot the graph for the average fitness
x = 0;
SelectObject(surface, m_BluePen);
MoveToEx(surface, 0, cyClient, NULL);
for (i=0; i<m_vecAvFitness.size(); ++i)
{
LineTo(surface, x, (cyClient - VSlice*m_vecAvFitness[i]));
x += HSlice;
}
//replace the old pen
SelectObject(surface, m_OldPen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -