资源简介
用遗传算法解决八皇后问题!有比较详细的注释!还有有关遗传算法理论的描述。
代码片段和文件信息
#include “CgaQueen.h“
CgaQueen::CgaQueens(double CrossRatedouble MutationRateint ChromoLengthint PopSize)
{
m_dCrossRate = CrossRate;
m_dMutationRate = MutationRate;
m_iChromoLength = ChromoLength;
m_iPopSize = PopSize;
m_dTotalFitnessScore = 0.0;
m_dBestFitnessScore = 1.0;
m_iGeneration = 0;
m_iGeneLength = 0;
m_iGroup = 0;
m_bBusy = true;
m_bSolved = false;
CreateStartPopulation();
}
void CgaQueen::CreateStartPopulation()
{
srand(rand());
CGenome *temp;
for(int i = 0; i < m_iPopSize; i++)
//temp.CGnome(Queen);
{
temp = new CGenome;
temp->CGenom(m_iChromoLength);
m_vecGenomes.push_back(*temp);
}
}
void CgaQueen::ShowChromo()
{
for(m_vecGenomes_it = m_vecGenomes.begin();
m_vecGenomes_it != m_vecGenomes.end();
m_vecGenomes_it++)
{
m_vecGenomes_it->ShowBits();
}
}
bool CgaQueen::UpdateFitnessScores()
{
int i = 0;
int wrong = 0;
m_dTotalFitnessScore = 0;
for( i = 0; i < m_iPopSize; i++)
{
//*
m_vecGenomes[i].UpdateFitnessScore();
if(m_vecGenomes[i].dFitness == m_dBestFitnessScore)
{
m_CSolve = m_vecGenomes[i];
return true;
}
//*/
m_dTotalFitnessScore+=m_vecGenomes[i].dFitness;
}
return false;
}
int CgaQueen::RouletteWheelSelection()
{
int num = 0;
//srand(rand());
double fSlice = (double)(rand()%(int)(m_dTotalFitnessScore*1000))/1000;
double cfTotal = 0;
for(int i = 0; i < m_iPopSize; i++)
{
cfTotal += m_vecGenomes[i].dFitness;
if(cfTotal > fSlice)
{
num = i;
break;
}
}
return num;
}
void CgaQueen::CrossOverPMX(vector&mumvector&dadvector&baby1vector&baby2)
{
//不进行杂交的情况
if((double)(rand()%1000)/1000 < m_dCrossRate || mum == dad)
{
baby1 = dad;
baby2 = mum;
return;
}
//杂交
//srand(rand());
int pos1 = rand()%m_iChromoLength;
int pos2 = rand()%m_iChromoLength;
do
{
pos2 = rand()%m_iChromoLength;
}while(pos2 > pos1);
//沿着染色体的长度随机选择一个点断开染色体
//srand(rand());
int end = rand()%(m_iChromoLength-pos1);
//int end = rand()%m_iChromoLength;
baby1 = dad;
baby2 = mum;
int i = 0j = 0;
for(i = pos1j = pos2; i <= pos1 + end && i < m_iChromoLength; i++j++)
{
int gene1 = dad[i];
int gene2 = mum[j];
if(gene1 != gene2)
{
int *posGene1 = find(baby1.begin()baby1.end()gene1);
int *posGene2 = find(baby1.begin()baby1.end()gene2);
swap(*posGene1*posGene2);
posGene1 = find(baby2.begin()baby2.end()gene1);
posGene2 = find(baby2.begin()baby2.end()gene2);
swap(*posGene1*posGene2);
}
}
/*
cout<<“pos1 = “< cout<<“end = “< cout<<“pos2 = “< cout<<“dad = “;
for(i = 0; i < m_iChromoLength; i++)
{
cout< }
cout< cout<<“mum = “;
for(i = 0; i < m_iChromoLength; i++)
{
cout< }
cout< cout<<“baby1 = “;
for(i = 0; i < m_iChromoLength; i++)
{
cout<<
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1609 2007-07-29 22:02 遗传算法(八皇后)\遗传算法.cpp
文件 5729 2007-07-29 21:50 遗传算法(八皇后)\CgaQueen.cpp
文件 72704 2007-09-01 18:43 遗传算法(八皇后)\遗传算法.opt
文件 1250 2007-07-28 18:51 遗传算法(八皇后)\CgaQueen.h
文件 58368 2007-09-01 18:43 遗传算法(八皇后)\遗传算法.ncb
文件 1298 2007-09-01 18:43 遗传算法(八皇后)\遗传算法.plg
文件 524 2007-07-24 09:34 遗传算法(八皇后)\遗传算法.dsw
文件 6 2007-07-24 10:00 遗传算法(八皇后)\遗传算法.txt.bak
文件 1163 2007-07-24 18:24 遗传算法(八皇后)\遗传算法.txt
文件 3681 2007-07-24 18:24 遗传算法(八皇后)\遗传算法.dsp
文件 417 2007-07-27 09:02 遗传算法(八皇后)\CGenome.h
文件 838 2007-07-27 12:20 遗传算法(八皇后)\CGenome.cpp
目录 0 2007-07-24 09:20 遗传算法(八皇后)
----------- --------- ---------- ----- ----
147587 13
- 上一篇:最新全国行政区域编码
- 下一篇:OPC 64位32位 .net语言 链接库
评论
共有 条评论