游戏编程中的人工智能技术-神经网络入门(四)
2016-07-19 22:25
645 查看
接下来到了最后一个类:CController控制器类了。
CController可以算是所有类的一个综合,将所有类结合起来,成为了一个可以工作的系统,其重要性不言而喻。不过由于作者对windows程序编程实在不了解,只能挑一部分介绍。
class CController
{
private:
//storage for the population of genomes
vector<SGenome> m_vecThePopulation; //表示整个染色体种群,向量里存放的是染色体
//and the minesweepers
vector<CMinesweeper> m_vecSweepers;//整个扫雷机总和,向量里存放的是单个扫雷机
//and the mines
vector<SVector2D> m_vecMines;//整个地雷总和,向量里存放的是单个地雷
//pointer to the GA
CGenAlg* m_pGA;//定义了一个CGenAlg类的实例m_pGA
int m_NumSweepers;
int m_NumMines;
int m_NumWeightsInNN;
//vertex buffer for the sweeper shape's vertices
vector<SPoint> m_SweeperVB;
//vertex buffer for the mine shape's vertices
vector<SPoint> m_MineVB;
//stores the average fitness per generation for use
//in graphing.
vector<double> m_vecAvFitness;//每一代的平均适应度,是用来显示的
//stores the best fitness per generation
vector<double> m_vecBestFitness;//每一代的最高适应度,也是用来显示的
//pens we use for the stats
HPEN m_RedPen;
HPEN m_BluePen;
HPEN m_GreenPen;
HPEN m_OldPen;
//handle to the application window
HWND m_hwndMain;
//toggles the speed at which the simulation runs
bool m_bFastRender;
//cycles per generation
//int m_iTicks;
//generation counter
int m_iGenerations;
//window dimensions
int cxClient, cyClient;
//this function plots a graph of the average and best fitnesses
//over the course of a run
void PlotStats(HDC surface);
public:
CController(HWND hwndMain);
~CController();
void Render(HDC surface);
void WorldTransform(vector<SPoint> &VBuffer,
SVector2D vPos);
bool Update();//这个是关键中的关键函数
int m_iTicks;
//accessor methods
bool FastRender()const {return m_bFastRender;}
void FastRender(bool arg){m_bFastRender = arg;}
void FastRenderToggle() {m_bFastRender = !m_bFastRender;}
};本文只介绍最关键的update()函数,其他应用于画图的函数就不介绍了。
bool CController::Update()
{
//run the sweepers through CParams::iNumTicks 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 it encounters a mine its fitness is
//updated appropriately,
if (m_iTicks++ < CParams::iNumTicks)//帧数,每代运行的时间,默认为2000
{
for (int i=0; i<m_NumSweepers; ++i)
{
//update the NN and position
if (!m_vecSweepers[i].Update(m_vecMines))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
//see if it's found a mine 检查是否碰撞到地雷,GrabHit表示碰撞到的地雷编号,若是-1表示没有碰到地雷
int GrabHit = m_vecSweepers[i].CheckForMine(m_vecMines,
CParams::dMineScale);
if (GrabHit >= 0)//如果碰到地雷
{
//we have discovered a mine so increase fitness
m_vecSweepers[i].IncrementFitness();//适应度函数加1
//mine found so replace the mine with another at a random
//position 另外随机的放一个地雷,编号就是GrabHit
m_vecMines[GrabHit] = SVector2D(RandFloat() * cxClient,
RandFloat() * cyClient);
}
//update the chromos fitness score 遍历每个扫雷机,得到其适应度函数
//并放入遗传算法类里。关于 m_vecThePopulation的定义请参阅CController的构造函数
m_vecThePopulation[i].dFitness = m_vecSweepers[i].Fitness();
}
}
//Another generation has been completed.
//Time to run the GA and update the sweepers with their new NNs
else//帧数到了,一代结束了
{
//update the stats to be used in our stat window
m_vecAvFitness.push_back(m_pGA->AverageFitness());//将每一代的平均适应度和最高适应度存储下来
m_vecBestFitness.push_back(m_pGA->BestFitness());
//increment the generation counter
++m_iGenerations;//代数加1
//reset cycles
m_iTicks = 0;//重置帧数
//run the GA 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;
}
CController可以算是所有类的一个综合,将所有类结合起来,成为了一个可以工作的系统,其重要性不言而喻。不过由于作者对windows程序编程实在不了解,只能挑一部分介绍。
class CController
{
private:
//storage for the population of genomes
vector<SGenome> m_vecThePopulation; //表示整个染色体种群,向量里存放的是染色体
//and the minesweepers
vector<CMinesweeper> m_vecSweepers;//整个扫雷机总和,向量里存放的是单个扫雷机
//and the mines
vector<SVector2D> m_vecMines;//整个地雷总和,向量里存放的是单个地雷
//pointer to the GA
CGenAlg* m_pGA;//定义了一个CGenAlg类的实例m_pGA
int m_NumSweepers;
int m_NumMines;
int m_NumWeightsInNN;
//vertex buffer for the sweeper shape's vertices
vector<SPoint> m_SweeperVB;
//vertex buffer for the mine shape's vertices
vector<SPoint> m_MineVB;
//stores the average fitness per generation for use
//in graphing.
vector<double> m_vecAvFitness;//每一代的平均适应度,是用来显示的
//stores the best fitness per generation
vector<double> m_vecBestFitness;//每一代的最高适应度,也是用来显示的
//pens we use for the stats
HPEN m_RedPen;
HPEN m_BluePen;
HPEN m_GreenPen;
HPEN m_OldPen;
//handle to the application window
HWND m_hwndMain;
//toggles the speed at which the simulation runs
bool m_bFastRender;
//cycles per generation
//int m_iTicks;
//generation counter
int m_iGenerations;
//window dimensions
int cxClient, cyClient;
//this function plots a graph of the average and best fitnesses
//over the course of a run
void PlotStats(HDC surface);
public:
CController(HWND hwndMain);
~CController();
void Render(HDC surface);
void WorldTransform(vector<SPoint> &VBuffer,
SVector2D vPos);
bool Update();//这个是关键中的关键函数
int m_iTicks;
//accessor methods
bool FastRender()const {return m_bFastRender;}
void FastRender(bool arg){m_bFastRender = arg;}
void FastRenderToggle() {m_bFastRender = !m_bFastRender;}
};本文只介绍最关键的update()函数,其他应用于画图的函数就不介绍了。
bool CController::Update()
{
//run the sweepers through CParams::iNumTicks 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 it encounters a mine its fitness is
//updated appropriately,
if (m_iTicks++ < CParams::iNumTicks)//帧数,每代运行的时间,默认为2000
{
for (int i=0; i<m_NumSweepers; ++i)
{
//update the NN and position
if (!m_vecSweepers[i].Update(m_vecMines))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
//see if it's found a mine 检查是否碰撞到地雷,GrabHit表示碰撞到的地雷编号,若是-1表示没有碰到地雷
int GrabHit = m_vecSweepers[i].CheckForMine(m_vecMines,
CParams::dMineScale);
if (GrabHit >= 0)//如果碰到地雷
{
//we have discovered a mine so increase fitness
m_vecSweepers[i].IncrementFitness();//适应度函数加1
//mine found so replace the mine with another at a random
//position 另外随机的放一个地雷,编号就是GrabHit
m_vecMines[GrabHit] = SVector2D(RandFloat() * cxClient,
RandFloat() * cyClient);
}
//update the chromos fitness score 遍历每个扫雷机,得到其适应度函数
//并放入遗传算法类里。关于 m_vecThePopulation的定义请参阅CController的构造函数
m_vecThePopulation[i].dFitness = m_vecSweepers[i].Fitness();
}
}
//Another generation has been completed.
//Time to run the GA and update the sweepers with their new NNs
else//帧数到了,一代结束了
{
//update the stats to be used in our stat window
m_vecAvFitness.push_back(m_pGA->AverageFitness());//将每一代的平均适应度和最高适应度存储下来
m_vecBestFitness.push_back(m_pGA->BestFitness());
//increment the generation counter
++m_iGenerations;//代数加1
//reset cycles
m_iTicks = 0;//重置帧数
//run the GA 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;
}
相关文章推荐
- Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
- 如何解决linux下apache启动时httpd: apr_sockaddr_info_get() failed for 报错
- HDU - 1021 Fibonacci Again
- 游戏编程中的人工智能技术-神经网络入门(三)
- 2016 Multi-University Training Contest 1
- Container With Most Water
- HDU 5733 tetrahedron (2016 Multi-University Training Contest 1 计算几何)
- dojo布局BorderContainer
- hdu 5178 pairs(二分,思路)
- 2016 Multi-University Training Contest 1 1011 tetrahedron
- 2016 Multi-University Training Contest 1 1002 Chess
- 2016 Multi-University Training Contest 1
- 1014. Waiting in Line (30)
- testNg之PowerEmailableReporter
- UVA 253-Cube painting
- hdu 4300 Clairewd’s message
- hdu 5726 GCD(2016 Multi-University Training Contest 1线段树)
- hdu 5733 tetrahedron(2016 Multi-University Training Contest 1计算几何)
- UVA 10494 If We Were a Child Again
- 2016 Multi-University Training Contest 1题解报告