#7楼 得分:0回复于:2003-10-13 19:41:53
/*
* function: generate the first population
*/
inline void CSGAOptimize::GenerateInitialPopulation()
{
register i,j;
// srand( (unsigned)time( 0 ) );
for(i=0; i <params.PopSize; i++)
{
for(j=0; j <CHROMLEN; j++)
{
population[i].chrom[j]=(rand()%2)? '0 ': '1 ';
}
population[i].chrom[CHROMLEN]= '\0 ';
}
}
/*
* function: initialize the first generation
*/
inline void CSGAOptimize::GenerateNextPopulation()
{
SelectionOperator();
CrossoverOperator();
MutationOperator();
}
/*
* function: evaluate population according to certain formula.
*/
inline void CSGAOptimize::EvaluatePopulation() // 适应度计算
{
CalculateObjectValue();
CalculateFitnessValue();
FindBestAndWorstIndividual();
}
/*
* function: to perform evolution operation based on elitise model.
* elitise model is to replace the worst individual of this generation by the current best one
*/
inline void CSGAOptimize::PerformEvolution() // 进化
{
if(bestindividual-> fitness> currentbest-> fitness) // 如果当前代最佳适应度(某个染色体)> 历代最大适应度
{
currentbest=&population[best_index];
}
else // 将当前代最差适应度染色体淘汰
{
population[worst_index]=*currentbest;
}
}
/*
* function: output the results of current population
*/
inline void CSGAOptimize::OutputTextReport(ostream &os) // 输出
{
register i;
double sum; // temporary sum
double average; // average of population object value
// calculate average object value
sum=0.0;
for(i=0; i <params.PopSize; i++)
{
sum+=population[i].value;
}
average=sum/params.PopSize;
// print results of this population
os < <generation < < "\t " < <average < < "\t " < <currentbest-> value < < "\t ";
for(i=0; i <CHROMLEN; i++)
{
os < <currentbest-> chrom[i];
}
os < < "\n ";
}
//--------------------------------------------------
/*
* function: to decode a binary chromosome into a decimal integer
* 计算染色体chrom,从point-> length长度表示的数值
*/
inline const long CSGAOptimize::DecodeChromosome(const char* chrom, const int point, const int length)
{
register 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;
}
/*
* function: to calculate object value
* 从染色体计算优化函数值
*/
inline void CSGAOptimize::CalculateObjectValue()
{
register i;
long y1,y2;
// Rosenbrock function
for(i=0; i <params.PopSize; i++)
{
y1=DecodeChromosome(population[i].chrom,0,LENGTH1);
y2=DecodeChromosome(population[i].chrom,LENGTH1,LENGTH2);
population[i].value=target_function(stipulate_function(y1), stipulate_function(y2));
}
}
/*
* function: to calculate fitness value
* 计算适应度
*/
inline void CSGAOptimize::CalculateFitnessValue()
{
register i;
double sum(0),fitness;
for(i=0; i <params.PopSize; i++)
{
if(FunctionMode==MAXIMIZATION)
{
if((population[i].value+Cmin)> 0.0)
{
fitness=Cmin+population[i].value;
}
else
{
fitness=0.0;
}
}
else if(FunctionMode==MINIMIZATION)
{
if(population[i].value <Cmax)
{
fitness=Cmax-population[i].value;
}
else
{
fitness=0.0;
}
}
population[i].fitness=fitness;
sum+=population[i].fitness;
}
// calculate relative fitness
for(i=0; i <params.PopSize; i++)
{
population[i].except_fitness=/*params.PopSize**/population[i].fitness/sum;
}
}
/*
* function: to find out the best individual so far current generation
* 查找最坏、最好适应度染色体
*/
inline void CSGAOptimize::FindBestAndWorstIndividual()
{
register i;
// double sum=0.0;
bestindividual=&population[0];
worstindividual=&population[0];
for(i=1; i <params.PopSize; i++)
{
if(population[i].fitness> bestindividual-> fitness)
{
bestindividual=&population[i];
best_index=i;
}
else if(population[i].fitness <worstindividual-> fitness)
{
worstindividual=&population[i];
worst_index=i;
}
// sum+=population[i].fitness;
}
if(generation==0) // 如果是初始化的0代,那么当前最佳适应度=本代(0)最佳适应度
{
currentbest=bestindividual;
}
else // 否则比较i-1和i代(i为当前代),取适应度好的染色体
{
if(bestindividual-> fitness> currentbest-> fitness)
{
currentbest=bestindividual;
}
}
}
//--------------------------------------------------
/*
* function: to reproduce a chromosome by proportional selection
* 比例选择算子
*/
inline void CSGAOptimize::SelectionOperator() // 选择复制
{
register i, index;
double p;
individual *newpopulation=new individual[params.PopSize];
// calculate cumulative fitness
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;
}