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

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

基本遗传算法源程序1  

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

  下载LOFTER 我的照片书  |

//基本遗传算法源程序

 

//Simple Genetic Algorithm

//Version 1.0

//Programmed by Jim Zhou, 1994.12.

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

 

//The Definition of Constant

#define POPSIZE 500 //population size

#define MAXIMIZATION 1  //maximization flag

#define MINIMIZATION 2  //minimization flag

 

//The Definition of User Data

//(For different problem, there are some difference.)

#define Cmax    100 //certain maximal value

#define Cmin    0   //certain minimun value

#define LENGTH1 10  //the chromosome length of 1st variable

#define LENGTH2 10  //the chromosome length of 2nd variable

#define CHROMLENGTH LENGTH1 + LENGTH2 //total length of chromosome

int FunctionMode = MAXIMIZATION; //optimization type

int PopSize = 80;   //population size

int MaxGeneration = 200;    //max number of generation

double Pc = 0.6;    //probability of crossover

double Pm = 0.001;  //probability of mutation

 

//The definition of data structure

struct individual   //data structre of individual

{

    char chrom[CHROMLENGTH + 1];    //astring of code representing individual

    double value;   //object value of this individual

    double fitness;     //fitness of this individual

};

 

//The definition of global variables

int generation; //number of generation

int best_index; //index of best individual

int worst_index; //index of worst individual

struct individual bestindividual;   //best individual of current generation

struct individual worstindividual;  //worst individual of current generation

struct individual currentbest;  //best indibidual by now

struct individual population[POPSIZE];  //population

 

//Declaration of Prototype

void GenerateInitialPopulation(void);

void GenerateNextPopulation(void);

void EvaluatePopulation(void);

long DecodeChromosome(char *, int, int);

void CalculateObjectValue(void);

void CalulateFitnessValue(void);

void FindBestAndWorstIndividual(void);

void PerformEvolution(void);

void SelectionOperator(void);

void CrossoverOperator(void);

void MutationOperator(void);

void OutputTextReport(void);

int random(int max);

 

//main program

void main(void)

{

    generation = 0;//设置初始状态的代数

    GenerateInitialPopulation();//生成初始群体

    EvaluatePopulation();//评价群体

    while(generation < MaxGeneration)

    {

        generation ++;

        GenerateNextPopulation();//生成下一状态的群体

        EvaluatePopulation();//评价群体

        PerformEvolution();//进化操作

        OutputTextReport();//打印报告

    }

    printf("[The best answer is 3905.926227] in the abstract.");

}

 

 

//生成初始群体

//Generate the first population.

void GenerateInitialPopulation(void)

{

    int i,j;

    //randomize();

    srand((unsigned int)time(NULL));

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

    {

        for(j = 0; j < CHROMLENGTH; j++)

        {

            population[i].chrom[j] = (random(10) < 5) ? '0' : '1';

        }

        population[i].chrom[CHROMLENGTH] = '\0';

    }

}

 

 

//生成下一代群体

//Initialise the first generation

void GenerateNextPopulation(void)

{

    SelectionOperator();//选择

    CrossoverOperator();//交叉

    MutationOperator();//变异

}

 

 

//群体评价

//Evaluate population according to certain formula.

void EvaluatePopulation(void)

{

    CalculateObjectValue(); //calculate object value;

    CalulateFitnessValue(); //calculate fitness value;

    FindBestAndWorstIndividual();//find the best and worst individual

}

 

 

//基因译码

//To decode a binary chromosome into a decimal interger

//Note: the returend value may be plus, or minus.

//      For different coding metho, this value may be changed into "unsigned int"

long DecodeChromosome(char* string, int point, int length)

{

    int i;

    long decimal = 0L;

    char* pointer;

 

    for(i = 0, pointer = string + point; i < length; i++, pointer++)

    {

        decimal += (*pointer - '0') << (length - 1 - i);

    }

    return (decimal);

}

//未完!!!!

  评论这张
 
阅读(1468)| 评论(4)

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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