遗传算法是比较经典的群体智能算法,属于随机搜索算法的一种。遗传算法属于全局性搜索算法,因为存在基因交叉互换和基因突变,因此比较不容易陷入局部最优。遗传算法的本质是随机生成一些合理的解,通过计算代入变量得出的值,优胜劣汰,保存优良解,淘汰不良解,再通过随机地两两交配互换对应变量的值以及改变某解中的某一变量来生成新的解向量,这些过程可以称之为适应度评估,交叉,突变。对于交配对象的分配,本算法使用轮盘赌选择法。
头文件如下
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/*parameter*/
#define POPSIZE 100 /*population size*/
#define VARSNUM 3 /*variable number*/
#define PXOVER 0.7 /*probability of crossover*/
#define PMUTATION 0.07 /*probability of mutation*/
#define MAXGENS 500 /*max number of generations*/
#define TRUE 1
#define FALSE 0
/*global variable*/
int generation; /*current generatons number*/
int current_best; /*current best individual*/
double val_best; /*current best fitness*/
FILE *galog; /*output file*/
FILE *output;
struct popmem{
double gene[VARSNUM]; /*a array to store variables' value*/
double fitness; /*a value to refelct current variable's priority*/
double upboundary[VARSNUM]; /*range of variables*/
double lowboundary[VARSNUM];
double rfitness; /*relative fitness of current generation*/
double cfitness; /*for roulette wheel selection strategy*/
};
struct popmem population[POPSIZE+1]; /*current generation*/
struct popmem newpopulation[POPSIZE+1]; /*next generation*/
void Initial(void); /*initialize the group*/
void Evaluate(void); /*evalute the member's fitness*/
void RepBest(void); /*save the best member*/
void Select(void