📄 sgaoptimize.cpp
字号:
// SGAOptimize.cpp: implementation of the CSGAOptimize class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SGAOptimize.h"
#include "math.h"
#include "individual.h"
//#include "StringResolution.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern int totallength;
extern a[30];
extern CString str;
extern double zdh5[30],LowerVariable[30],r;
extern int VariableNo;
//double best[31];//定义的数组存最优解以及对应的变量值
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSGAOptimize::CSGAOptimize()//:/*Cmax(100),Cmin(0),*/LENGTH1(10),LENGTH2(10),CHROMLEN(LENGTH1+LENGTH2)//,FunctionMode(MAXIMIZATION)
{
generation=0; // 遗传代数
// a=new int[unit.variableno];//存每个变量的码长
currentbest=new individual[params.PopSize];
worstindividual=new individual[params.PopSize];
population=new individual[params.PopSize]; // new array not support special construct => convert to intialize
//动态分配了一数组population,大小为params.popsize
}
CSGAOptimize::~CSGAOptimize()
{
delete[]population;
// delete[]currentbest;
// delete[]worstindividual;
}
void CSGAOptimize::GenerateInitialPopulation()
{
int i,j;
for(i=0; i<params.PopSize; i++ )
{
for(j=0; j<totallength; j++)
{
population[i].chrom[j]=(rand()%2)?'0':'1';
}
population[i].chrom[totallength]='\0';
}
}
void CSGAOptimize::GenerateNextPopulation()
{
SelectionOperator();
CrossoverOperator();
MutationOperator();
}
void CSGAOptimize::EvaluatePopulation()
{
CalculateObjectValue();
FideBestAndWorstIndividual();
}
void CSGAOptimize::PerformEvolution()
{
// if(worstindividual->fitness<currentbest->fitness) // 如果当前代最佳适应度(某个染色体)>历代最大适应度
// currentbest=&population[worst_index];
}
const long CSGAOptimize::DecodeChromosome(const char *chrom, const int point, const int length)
{
int i;
long decimal=0L;
const char *p;
for(i=0,p=chrom+point; i<length; i++,p++)
{
decimal+=(*p-'0')<<(length-1-i);
}
return decimal;
}
void CSGAOptimize::CalculateObjectValue()
{
int i,j,k,h;
double sum=0.0;
long *y;
double *fitness;
y = new long[VariableNo];//存解码后的十进制码
fitness = new double[VariableNo];//存目标函数值
// double *transfer;
// transfer = new double[VariableNo+1];
m_formula1.SetFormula(str.GetBuffer(str.GetLength()));// 重新设置时将所有的清除
// int size=m_formula1.GetVariantTableSize();
for(i=0; i<params.PopSize; i++)
{
for(j=0;j<VariableNo;j++)
{
int length=0;
int sumlength=0;
for(k=0;k<j;k++)
{
sumlength+=a[k];
}
length=a[j];//为了与DecodeChromosome函数声明一致,把指针的内容取出放在已变量中
y[j]=DecodeChromosome(population[i].chrom,sumlength,length);//sumlength解决解码时变量编码长度不一至
}
for(h=0;h<VariableNo;h++)
{
variablenocount=h;//记录的是该变量是第几个变量,供stipulate_function用
fitness[h]=stipulate_function(y[h]);
population[i].realvalue[h]=fitness[h];
}
////////////////////1.21
/* for(int temp=0;temp<VariableNo;temp++)
{
transfer[temp]=fitness[temp];
}
transfer[VariableNo]=0.00000000000000000000000000001;//r
*/
/////////////////////////
/* transfer[0]=0.765;
transfer[1]=0.645;
transfer[2]=1;
//1125
//0120*/
// m_formula1.SetFormula(str.GetBuffer(str.GetLength()));// 重新设置时将所有的清除
// int size=m_formula1.GetVariantTableSize();
//0120
//1125
double k=m_formula1.computer(fitness,VariableNo);
population[i].fitness=k;//target_function(fitness[0],fitness[1]);
sum+=population[i].fitness;
}
for(k=0; k<params.PopSize; k++)
{
population[k].except_fitness=population[k].fitness/sum;
}
delete []y;
delete []fitness;
// delete []transfer;
// m_formula1.Destroy();
}
void CSGAOptimize::FideBestAndWorstIndividual()
{
int i;
// bestindividual=&population[0];//存第0个个体
worstindividual[0]=population[1];
for(i=1; i<params.PopSize; i++)//把每代的每个个体都比较,存所要求的最好
{
if(population[i].fitness<worstindividual[0].fitness)
{
worstindividual[0]=population[i];
// worst_index=i;
}
}
if(generation==0) // 如果是初始化的0代,那么当前最佳适应度=本代(0)最佳适应度
{
currentbest[0]=worstindividual[0];//只是把指针地址赋予currentbest,currentbest本身是一指针
/* best[0]=worstindividual->fitness;
for(int real=0;real<VariableNo;real++)
{
best[real+1]=worstindividual->realvalue[real];
}*/
}
else // 否则比较i-1和i代(i为当前代),取适应度好的染色体
{
if(worstindividual[0].fitness<currentbest[0].fitness)
{
currentbest[0]=worstindividual[0];
/* best[0]=worstindividual->fitness;
for(int real=0;real<VariableNo;real++)
{
best[real+1]=worstindividual->realvalue[real];
}*/
}
}
}
void CSGAOptimize::SelectionOperator()
{
int i, index;
double p;
individual *newpopulation=new individual[params.PopSize];
for(i=1; i<params.PopSize; i++)
{
population[i].except_fitness=population[i-1].except_fitness
+population[i].except_fitness;
}
// selection operation
for(i=0; i<params.PopSize; i++)
{
p=rand()%1000/1000.0;
index=0;
while (p>population[index].except_fitness)
{
index++;
}
newpopulation[i]=population[index];
}
for(i=0; i<params.PopSize; i++)
{
population[i]=newpopulation[i];
}
delete []newpopulation;
}
void CSGAOptimize::CrossoverOperator()
{
int i,j;
int *index=new int[params.PopSize];
int point,temp;
double p;
char ch;
for(i=0; i<params.PopSize; i++)
{
index[i]=i;
}
for(i=0; i<params.PopSize; i++)
{
point = rand()%(params.PopSize-i);
temp = index[i];
index[i] = index[point+i];
index[point+i]=temp;
}
// one - point crossover operation
for(i=0; i<params.PopSize-1; i+=2)
{
p=rand()%1000/1000.0;
if(p<params.blCrossover)
{
point = rand()%(totallength-1)+1;
for(j=point; j<totallength; j++)
{
ch=population[index[i]].chrom[j];
population[index[i]].chrom[j]=population[index[i+1]].chrom[j];
population[index[i+1]].chrom[j]=ch;
}
}
}
delete[]index;
}
void CSGAOptimize::MutationOperator()
{
int i,j;
double p;
// bit mutation
for(i=0; i<params.PopSize; i++)
{
for(j=0; j<totallength; j++)
{
p=rand()%1000/1000.0;
if(p<params.blMutation)
{
population[i].chrom[j]=(population[i].chrom[j]=='0')?'1':'0';
}
}
}
}
double CSGAOptimize::stipulate_function(long yi)
{
double result;
long multiply=1;
double minus;
int length;
length=a[variablenocount];//第几个变量的长度
for(int i=0;i<length;i++)
{
multiply*=2;
}
minus=zdh5[variablenocount]-LowerVariable[variablenocount];
result=minus*yi/(multiply-1)+LowerVariable[variablenocount];
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -