登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

基本遗传算法源程序2  

2006-02-25 14:41:29|  分类: 技术积累 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

//计算函数值

//To calulate object value;

//Note: For different problem, user must change these code .

//      This example is dealing with Rosenbrock function.

//      Rosenbrock function is define as:

//      f(x1,x2) = 100 * (x1 ** 2 - x2) ** 2 + (1 - x1) ** 2

//      Its maximal value is:

//      f(-2.048, -2.048) = 3905.926227

void CalculateObjectValue(void)

{

    int i;

    long temp1,temp2;

    double x1, x2;

    for(i = 0; i < PopSize; i++)

    {

        temp1 = DecodeChromosome(population[i].chrom, 0, LENGTH1);

        temp2 = DecodeChromosome(population[i].chrom, LENGTH1, LENGTH2);

        x1 = 4.096 * temp1 / 1023.0 - 2.048;

        x2 = 4.096 * temp2 / 1023.0 - 2.048;

        population[i].value = 100 * (x1*x1 - x2) * (x1*x1 - x2) + (1 - x1) * (1 - x1);

    }

}

 

 

//适应度计算

//To calculate fitness value.

void CalulateFitnessValue(void)

{

    int i;

    double temp;

 

    for(i = 0; i < PopSize; i++)

    {

        if(FunctionMode == MAXIMIZATION)//maximization

        {

            if((population[i].value + Cmin) > 0.0)

            {

                temp = Cmin + population[i].value;

            }

            else

            {

                temp = 0.0;

            }

        }

        else if(FunctionMode == MINIMIZATION)//minimization

        {

            if(population[i].value < Cmax)

            {

                temp = Cmax - population[i].value;

            }

            else

            {

                temp = 0.0;

            }

        }

        population[i].fitness = temp;

    }

}

 

 

//找到最优个体和最差个体

//To find out the best individual so far current generation

void FindBestAndWorstIndividual(void)

{

    int i ;

    double sum = 0.0;

    //find out the best and worst individual of this generation

    bestindividual = population[0];

    worstindividual = population[0];

    for(i = 1; i < 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;

    }

 

    //find out the best individual so far

    if(generation == 0)//initialize the best individual

    {

        currentbest = bestindividual;

    }

    else

    {

        if(bestindividual.fitness > currentbest.fitness)

        {

            currentbest = bestindividual;

        }

    }

}

 

 

//进化操作

//Toperform evolution operation based on elitise modual. Elitist model is to replace the worst

//individual of this generation by the current best one.

void PerformEvolution(void)

{

    if(bestindividual.fitness > currentbest.fitness)

    {

        currentbest = population[best_index];

    }

    else

    {

        population[worst_index] = currentbest;

    }

}

 

 

//基因的选择算子

//Toreplace a chromosome by proportional selection.

void SelectionOperator(void)

{

    int i, index;

    double p,sum = 0.0;

    double cfitness[POPSIZE]; //cumulative fitness value

    struct individual newpopulation[POPSIZE];

 

    //calculate relative fitness

    for(i = 0; i < PopSize; i++)

    {

        sum += population[i].fitness;

    }

    for(i = 0; i < PopSize; i++)

    {

        cfitness[i] = population[i].fitness / sum;

    }

 

    //calculate cumulative fitness

    for(i = 1; i < PopSize; i++)

    {

        cfitness[i] = cfitness[i-1] + cfitness[i];

    }

 

    //selection operation

    for(i = 0; i < PopSize; i++)

    {

        p = rand() % 1000 / 1000.0;

        index = 0;

        while(p > cfitness[index])

            index++;

        newpopulation[i] = population[index];

    }

    for(i = 0; i < PopSize; i++)

    {

        population[i] = newpopulation[i];

    }

}

 

 

  评论这张
 
阅读(1828)| 评论(2)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018