//基本遗传算法源程序
//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 =
char* pointer;
for(i = 0, pointer = string + point; i < length; i++, pointer++)
{
decimal += (*pointer - '0') << (length - 1 - i);
}
return (decimal);
}
//未完!!!!
评论