資源簡介
用遺傳算法解決八皇后問題!有比較詳細的注釋!還有有關遺傳算法理論的描述。

代碼片段和文件信息
#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? //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_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? {
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? {?
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? {
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? {
cout< }
cout< cout<<“mum???=?“;
for(i?=?0;?i? {
cout< }
cout< cout<<“baby1?=?“;
for(i?=?0;?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語言 鏈接庫
評論
共有 條評論