NO IMAGE
#include<iostream>
#include<string>
#include<time.h>
#include<sstream>
using namespace std;
//種群總數
const int popSize = 100;
//染色體長度
const int chromosomeSize = 9;
//變異概率
const double Pm = 0.001;
//最多代數
const int MaxGen = 100;
//變異概率
const double Pc = 0.1;
//遺傳個體類
class individual
{
public:
//個體目標值
double ObjectValue;
//個體適應值
double FitValue;
//染色體編碼
string Chromosome;
//建構函式
individual()
{
ObjectValue = 0;
FitValue = 0;
Chromosome = "000000000";
}
};
//進化處理類
class Evaluation
{
private:
//種群
individual Population[popSize];
//進行選擇
void SelectPop();
//進行變異
void VaryPop();
//進行雜交
void CrossPop();
//優化
void OptimizePop();
//初始化種群,隨機構造一個群體
void Initialization();
//找出最優和最差及平均值
void Statistics();
//評價種群
void EvaluatePop();
//最好個體
individual Best;
//最壞個體
individual Worst;
//最壞個體下標
int WorstIndex;
//歷史最佳
individual HistoryBest;
//平均值
double avg;
public:
//建構函式,呼叫初始化函式
Evaluation();
//產生下一代
void NextPopulation();
//列印
void output();
//代數
int generation;
};
//建構函式,呼叫初始化函式
Evaluation::Evaluation()
{
Initialization();
generation = 0;
}
//初始化構造初始種群
void Evaluation::Initialization()
{
//對染色體進行初始化設定,逐顆粒隨機賦值
char temp;
int Index = 0, bitIndex = 0;
for (; Index<popSize; Index  )
{
for (bitIndex = 0; bitIndex<chromosomeSize; bitIndex  )
{
int r = rand() % 2;
if (r == 0)
temp = '0';
else
temp = '1';
Population[Index].Chromosome[bitIndex] = temp;
}
}
//初始化時設定歷史最佳為第一個
HistoryBest = Population[0];
//呼叫目標值和適應值的初始化函式,評價種群
EvaluatePop();
Statistics();
}
//評價函式,就是用函式f(x1,x2)=x1^2 x2^2,因為是數值函式,所以適應值和目標值是一致的
void Evaluation::EvaluatePop()
{
string num1, num2, num3;
int value1, value2, value3;
int Index = 0;
for (; Index<popSize; Index  )
{
num1 = Population[Index].Chromosome.substr(0, 3);
num2 = Population[Index].Chromosome.substr(3, 3);
num3 = Population[Index].Chromosome.substr(6, 3);
//二進位制轉化為十進位制
stringstream ss;
ss << num1;
ss >> value1;
//清空緩衝區
ss.clear();
ss.str("");
ss << num2;
ss >> value2;
//清空緩衝區
ss.clear();
ss.str("");
ss << num3;
ss >> value3;
int a, b, c;
a = value1 / 100;
b = (value1 - a * 100) / 10;
c = value1 - a * 100 - b * 10;
value1 = a * 4   b * 2   c;
a = value2 / 100;
b = (value2 - a * 100) / 10;
c = value2 - a * 100 - b * 10;
value2 = a * 4   b * 2   c;
a = value3 / 100;
b = (value3 - a * 100) / 10;
c = value3 - a * 100 - b * 10;
value3 = a * 4   b * 2   c;
//計算適應值和目標值
Population[Index].FitValue = value1*value1   value2*value2   value3*value3;
Population[Index].ObjectValue = value1*value1   value2*value2   value3*value3;
}
}
//生成下一代
void Evaluation::NextPopulation()
{
SelectPop();
VaryPop();
CrossPop();
EvaluatePop();
Statistics();
OptimizePop();
EvaluatePop();
Statistics();
generation  ;
}
//選擇運算元,輪盤賭
void Evaluation::SelectPop()
{
double FitSum = 0, selection[popSize];
individual newPopulation[popSize];
int index = 0, popindex = 0;
//求適應值的總和
for (; index<popSize; index  )
{
FitSum  = Population[index].FitValue;
}
//確定輪盤分佈
for (index = 0; index<popSize; index  )
{
selection[index] = Population[index].FitValue / FitSum;
}
for (index = 1; index<popSize; index  )
{
selection[index] = selection[index]   selection[index - 1];
}
//用輪盤進行隨機選取,形成新的種群
for (popindex = 0; popindex<popSize; popindex  )
{
double p = (rand() % 100);
p /= 100;
index = 0;
while (p>selection[index])
index  ;
newPopulation[popindex] = Population[index];
}
//將剛產生的群體替換為系統的群體
for (index = 0; index<popSize; index  )
{
Population[index] = newPopulation[index];
}
}
//雜交運算元,隨機選取交叉點
void Evaluation::CrossPop()
{
int Localtion;
int index = 0;
string str1, str2, str3, str4;
//打亂順序
for (; index<popSize; index  )
{
individual temp;
int r = rand() % popSize;
temp = Population[index];
Population[index] = Population[r];
Population[r] = temp;
}
//隨機選取交叉點,將染色體分裂,然後交叉,得到新的染色體
for (index = 0; index<popSize; index  = 2)
{
double temp = rand() % 1000 / 1000.0;
if (temp<Pc)
{
Localtion = rand() % chromosomeSize;
str1 = Population[index].Chromosome.substr(0, Localtion);
str2 = Population[index].Chromosome.substr(Localtion);
str3 = Population[index   1].Chromosome.substr(0, Localtion);
str4 = Population[index   1].Chromosome.substr(Localtion);
Population[index].Chromosome = str1   str4;
Population[index   1].Chromosome = str3   str2;
}
}
}
//變異運算元,對所有染色體每一位的隨機位置以變異概率進行變異
void Evaluation::VaryPop()
{
int index = 0, bitindex = 0;
string str1 = "0";
string str2 = "1";
for (; index<popSize; index  )
{
for (bitindex = 0; bitindex<chromosomeSize; bitindex  )
{
double r = rand() % 1000;
r /= 1000;
if (r<Pm)
{
if (Population[index].Chromosome[bitindex] == str1[0])
Population[index].Chromosome[bitindex] = str2[0];
else
Population[index].Chromosome[bitindex] = str1[0];
}
}
}
}
//優化
void Evaluation::OptimizePop()
{
Population[WorstIndex] = HistoryBest;
}
//統計
void Evaluation::Statistics()
{
Best = Population[0];
Worst = Population[0];
int index = 0;
double sum = 0;
for (; index<popSize; index  )
{
if (Best.FitValue<Population[index].FitValue)
Best = Population[index];
if (Worst.FitValue>Population[index].FitValue)
{
Worst = Population[index];
WorstIndex = index;
}
sum  = Population[index].FitValue;
}
if (HistoryBest.FitValue<Best.FitValue)
HistoryBest = Best;
avg = sum / popSize;
}
//輸出文字
void Evaluation::output()
{
cout << "Generation:" << generation << "   Average:" << avg << "   Best:" << Best.FitValue << "  Chromosome:" << Best.Chromosome << endl;
}
int main()
{
Evaluation eva;
eva.output();
while (eva.generation<MaxGen)
{
eva.NextPopulation();
eva.output();
}
int a;
cin >> a;
}