//通用演化算法AG.CPP
#include "stdafx.h"
#include "AG.h"
#include <iostream.h>
#include <iomanip.h>
#include <time.h>
#include <math.h>
#include <afxtempl.h>
#include <windows.h>
GA::GA()
{
time_t t;
srand((unsigned) time(&t)); //随机配置种子
nTimes = 0; //进化次数,初始值为0
InitPoputation(); //初始化种群
}
GA::~GA()
{
generList.RemoveAll();
}
//函数InitPoputation:初始化种群
void GA::InitPoputation()
{
Generation firstGener;
for(int i=1; i<=POPUTION_SIZE; i++)
{
Individual indiv;
while(true)
{
for(int j=1; j<=BIT_LENGTH; j++)
{
indiv.x1[j] = rand()%2 + ('1'-1);//随机产生01字符串
indiv.x2[j] = rand()%2 + ('1'-1);
indiv.x3[j] = rand()%2 + ('1'-1);
indiv.x4[j] = rand()%2 + ('1'-1);
indiv.x5[j] = rand()%2 + ('1'-1);
}
if(Restrict(indiv) == true) break;
}
firstGener.indiv[i] = indiv;
}
SortIndiv(firstGener.indiv, POPUTION_SIZE);
generList.AddTail(firstGener);//将初始化后的firstGener添加到种群的列表尾部
}
//函数GetRealX:按照01基因字符串计算出实数值X,其中参数pBit指向01字符串的指针,nType参数类型
double GA::GetRealX(char* pBit, int nType)
{
double ret = 0;
int nDec = 0;
for(int i=1; i<=BIT_LENGTH; i++)
{
nDec = nDec+ (pBit[i] - ('1'-1)) * (int)pow(2,BIT_LENGTH-i);
}
switch (nType)
{
case 1:
ret = 78 + nDec*(102-78)/(pow(2,BIT_LENGTH)-1);
break;
case 2:
ret = 33 + nDec*(45-33)/(pow(2,BIT_LENGTH)-1);
break;
case 3:
case 4:
case 5:
ret = 27 + nDec*(45-27)/(pow(2,BIT_LENGTH)-1);
break;
}
return ret;
}
//函数CalFun:根据个体,算出其适应值
double GA::CalFun(Individual& indiv)
{
double ret;
double x1, x2, x3, x4, x5;
x1 = GetRealX(indiv.x1,1);
x2 = GetRealX(indiv.x2,2);
x3 = GetRealX(indiv.x3,3);
x4 = GetRealX(indiv.x4,4);
x5 = GetRealX(indiv.x5,5);
ret = 5.3578547*x3*x3 + 0.8356891*x1*x5 + 37.293239*x1 - 40792.141;
return ret;
}
//函数Restrict:用约束条件对个体进行约束检测,符合条件的返回TRUE,否则FALSE
bool GA::Restrict(Individual& Indiv)
{
double x1, x2, x3, x4, x5;
x1 = GetRealX(Indiv.x1, 1);
x2 = GetRealX(Indiv.x2, 2);
x3 = GetRealX(Indiv.x3, 3);
x4 = GetRealX(Indiv.x4, 4);
x5 = GetRealX(Indiv.x5, 5);
if(x1<78 || x1>102) return false;
if(x2<33 || x2>45) return false;
if(x3<27 || x3>45) return false;
if(x4<27 || x4>45) return false;
if(x5<27 || x5>45) return false;
double r1, r2, r3;
r1 = 85.334407 + 0.0056858*x2*x5 + 0.0006262*x1*x4 - 0.0022053*x3*x5;
r2 = 80.51249 + 0.0071317*x2*x5 + 0.0029955*x1*x2 + 0.0021813*x3*x3;
r3 = 9.300961 + 0.0047026*x3*x5 + 0.0012547*x1*x3 + 0.0019085*x3*x4;
if(r1<0 || r1>92) return false;
if(r2<90 || r2>110) return false;
if(r3<20 || r3>25) return false;
return true;
}
//函数SwapChar:交换两字符
void GA::SwapChar(char& c1, char& c2)
{
char cTemp = 0;
cTemp = c1;
c1 = c2;
c2 = cTemp;
}
//函数Cross:杂交
void GA::Cross(Individual indiv1, Individual indiv2)
{
int nCrossPos = 0;
while(true)
{
nCrossPos = rand()%(BIT_LENGTH-1) + 1;
for(int i=1; i<=nCrossPos; i++)
{
SwapChar(indiv1.x1[i],indiv2.x1[i]);
SwapChar(indiv1.x2[i],indiv2.x2[i]);
SwapChar(indiv1.x3[i],indiv2.x3[i]);
SwapChar(indiv1.x4[i],indiv2.x4[i]);
SwapChar(indiv1.x5[i],indiv2.x5[i]);
}
if(Restrict(indiv1) == true && Restrict(indiv2) == true) break;
}
tempIndivList.AddTail(indiv1);
tempIndivList.AddTail(indiv2);
}
//函数Mutate:变异
void GA::Mutate(Individual indiv1, Individual indiv2)
{
int nMutatePos = 0;
while(true)
{
nMutatePos = rand()%(BIT_LENGTH) + 1;
indiv1.x1[nMutatePos] = indiv1.x1[nMutatePos]=='1'?'0':'1';
indiv2.x1[nMutatePos] = indiv2.x1[nMutatePos]=='1'?'0':'1';
nMutatePos = rand()%(BIT_LENGTH) + 1;
indiv1.x2[nMutatePos] = indiv1.x2[nMutatePos]=='1'?'0':'1';
indiv2.x2[nMutatePos] = indiv2.x2[nMutatePos]=='1'?'0':'1';
nMutatePos = rand()%(BIT_LENGTH) + 1;
indiv1.x3[nMutatePos] = indiv1.x3[nMutatePos]=='1'?'0':'1';
indiv2.x3[nMutatePos] = indiv2.x3[nMutatePos]=='1'?'0':'1';
nMutatePos = rand()%(BIT_LENGTH) + 1;
indiv1.x4[nMutatePos] = indiv1.x4[nMutatePos]=='1'?'0':'1';
indiv2.x4[nMutatePos] = indiv2.x4[nMutatePos]=='1'?'0':'1';
nMutatePos = rand()%(BIT_LENGTH) + 1;
indiv1.x5[nMutatePos] = indiv1.x5[nMutatePos]=='1'?'0':'1';
indiv2.x5[nMutatePos] = indiv2.x5[nMutatePos]=='1'?'0':'1';
if(Restrict(indiv1) == true && Restrict(indiv2) == true) break;
}
tempIndivList.AddTail(indiv1);
tempIndivList.AddTail(indiv2);
}
//函数Breed:繁殖(根据不同情况选择是杂交,还是变异)
void GA::Breed(Individual& indiv1, Individual& indiv2)
{
if((rand()%1000)/1000.0 < PROBALITITY_MUTATE)
{
Mutate(indiv1,indiv2);
}
else if((rand()%1000)/1000.0 < PROBALITITY_CROSS)
{
Cross(indiv1,indiv2);
}
}
//函数SortIndiv:对一代个体按照适应值排序
void GA::SortIndiv(Individual indiv[], int n)
{
for(int i = n-1; i>=1; i--)
{
for(int j=1; j<i; j++)
{
if(CalFun(indiv[j]) > CalFun(indiv[j+1]))
{
Individual temp;
temp = indiv[j];
indiv[j] = indiv[j+1];
indiv[j+1] = temp;
}
}
}
}
//函数NextGener:向前进化一代
void GA::NextGener()
{
Generation lastGener = generList.GetTail(); //返回列表的末尾元素
double eva[POPUTION_SIZE+1] = {0.0}, evaAll = 0.0;
double rel[POPUTION_SIZE+1] = {0.0};
int N[POPUTION_SIZE+1] = {0}, nAll = 0;
for(int i=1; i<=POPUTION_SIZE; i++)
{
eva[i] = CalFun(lastGener.indiv[i]);
evaAll = evaAll+eva[i];
}
for(int j=1; j<=POPUTION_SIZE; j++)
{
rel[j] = eva[j]/evaAll;
}
for(int k=1; k<=POPUTION_SIZE; k++)
{
N[k] = (int)(rel[k]*POPUTION_SIZE+0.5);
nAll =nAll + N[k];
}
for(int m=1; m<=POPUTION_SIZE; m++)
{
tempIndivList.AddHead(lastGener.indiv[m]); //添加一个元素到列表标题(生成新的标题)
}
Individual* breedPoolIndiv = new Individual[nAll+1];
int nPos = 0;
for(i=1; i<=POPUTION_SIZE; i++)
{
int t = N[i];
while(t>0)
{
breedPoolIndiv[++nPos] = lastGener.indiv[i];
t--;
}
}
nAll = (nAll/2)*2;
for(i=1; i<=nAll; i++)
{
Breed(breedPoolIndiv[i],breedPoolIndiv[nAll-i+1]);
}
for(i=1; i<=POPUTION_SIZE; i++)
{
tempIndivList.AddTail(lastGener.indiv[i]);
}
int nCount = tempIndivList.GetCount(); //返回此列表中的元素数
Individual* tempArrayIndiv = new Individual[nCount+1];
for(i=1; i<=nCount; i++)
{
tempArrayIndiv[i] = tempIndivList.GetHead();
tempIndivList.RemoveHead(); //从列表标题中移走元素
}
SortIndiv(tempArrayIndiv,nCount);
Generation newGener;
for(i=1; i<=POPUTION_SIZE; i++)
{
newGener.indiv[i] = tempArrayIndiv[i];
}
generList.AddTail(newGener);
generList.RemoveHead();
delete tempArrayIndiv;
delete breedPoolIndiv;
}
//函数Evolve:循环进化
void GA::Evolve()
{
for(int i=1; i<=100000000; i++)
{
if(nTimes%200 == 0)
{
Generation gener;
gener = generList.GetTail();
cout<<setiosflags(ios::fixed)<<setprecision(14);
cout<<"The current evolutionary times:"<<nTimes<<endl;
cout<<"The current individual value:"<<endl;
cout<<"x1 = "<<GetRealX(gener.indiv[1].x1, 1)<<endl;
cout<<"x2 = "<<GetRealX(gener.indiv[1].x2, 2)<<endl;
cout<<"x3 = "<<GetRealX(gener.indiv[1].x3, 3)<<endl;
cout<<"x4 = "<<GetRealX(gener.indiv[1].x4, 4)<<endl;
cout<<"x5 = "<<GetRealX(gener.indiv[1].x5, 5)<<endl;
cout<<"The current optimal value:"<<CalFun(gener.indiv[1])<<endl;
cout<<endl;
}
NextGener();
nTimes++;
}
}
//函数DisplayResult:显示计算结果
void GA::DisplayResult()
{
Generation gener;
gener = generList.GetTail();
cout<<"//////////////////////////////////////////////"<<endl;
cout<<"// The total number of evolution:"<<nTimes<<endl;
cout<<"// The individual value:"<<endl;
cout<<"// "<<"x1 = "<<GetRealX(gener.indiv[1].x1 , 1)<<endl;
cout<<"// "<<"x2