通过本文主要向大家介绍了遗传算法c++程序,遗传算法c++代码,c++递归算法,c++算法,c++排序算法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文所述为C++实现的遗传算法的类文件实例。一般来说遗传算法可以解决许多问题,希望本文所述的C++遗传算法类文件,可帮助你解决更多问题,并且代码中为了便于读者更好的理解,而加入了丰富的注释内容,是新手学习遗传算法不可多得的参考代码。
具体代码如下所示:
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>//把日期和时间转换为字符串
using namespace std;
//Parametes setting
#define POPSIZE 200 //population size
#define MAXGENS 1000 //max number of generation
#define NVARS 2 //no of problem variables
#define PXOVER 0.75 //probalility of crossover
#define PMUTATION 0.15 //probalility of mutation
#define TRUE 1
#define FALSE 0
#define LBOUND 0
#define UBOUND 12
#define STOP 0.001
int generation; //current generation no
int cur_best; //best individual
double diff;
FILE *galog; //an output file
struct genotype
{
double gene[NVARS]; //a string of variables基因变量
double upper[NVARS]; //individual's variables upper bound 基因变量取值上确界
double lower[NVARS]; //individual's batiables lower bound 基因变量取值下确界
double fitness; //individual's fitness个体适应值
double rfitness; //relative fitness个体适应值占种群适应值比例
double cfitness; //curmulation fitness个体适应值的累加比例
};
struct genotype population[POPSIZE+1];
//population 当前种群 population[POPSIZE]用于存放个体最优值并假设最优个体能存活下去
//在某些遗传算法中最优值个体并不一定能够存活下去
struct genotype newpopulation[POPSIZE+1]; //new population replaces the old generation 子种群
/*Declaration of procedures used by the gentic algorithm*/
void initialize(void); //初始化函数
double randval(double,double); //随机函数
double funtion(double x1,double x2); //目标函数
void evaluate(void); //评价函数
void keep_the_best(void); //保留最优个体
void elitist(void); //当前种群与子代种群最优值比较
void select(void);
void crossover(void); //基因重组函数
void swap(double *,double *); //交换函数
void mutate(void); //基因突变函数
double report(void); //数据记录函数
void initialize(void)
{
int i,j;
for(i=0;i<NVARS;i++)
{
for(j=0;j<POPSIZE+1;j++)
{
if(!i)
{
population[j].fitness=0;
population[j].rfitness=0;
population[j].cfitness=0;
}
population[j].lower[i]=LBOUND;
population[j].upper[i]=UBOUND;
population[j].gene[i]=randval(population[j].lower[i],population[j].upper[i]);
}
}
}
//***************************************************************************
//Random value generator:generates a value within bounds
//***************************************************************************
double randval(double low,double high)
{
double val;
val=((double)(rand()%10000)/10000)*(high-low)+low;
return val;
}
//目标函数
double funtion(double x,double y)
{
double result1=sqrt(x*x+y*y)+sqrt((x-12)*(x-12)+y*y)+sqrt((x-8)*(x-8)+(y-6)*(y-6));
return result1;
}
//***************************************************************************
//Evaluation function:evaluate the individual's fitness.评价函数给出个体适应值
//Each time the function is changes,the code has to be recompl
//***************************************************************************
void evaluate(void)
{
int mem;
int i;
double x[NVARS];
for(mem=0;mem<POPSIZE;mem++)
{
for(i=0;i<NVARS;i++)
x[i]=population[mem].gene[i];
population[mem].fitness=funtion(x[0],x[1]);//将目标函数值作为适应值
}
}
//***************************************************************************************
//Keep_the_best function:This function keeps track of the best member of the population.
//找出种群中的个体最优值并将其移动到最后
//***************************************************************************************
void keep_the_best()
{
int mem;
int i;
cur_best=0;
for(mem=0;mem<POPSIZE;mem++)//找出最高适应值个体
{
if(population[mem].fitness<population[cur_best].fitness)
{
cur_best=mem;
}
}
//将最优个体复制至population[POSIZE]
if(population[cur_best].fitness<=population[POPSIZE].fitness||population[POPSIZE].fitness<1)//防止出现种群基因退化 故保留历史最优个体
{
population[POPSIZE].fitness=population[cur_best].fitness;
for(i=0;i<NVARS;i++)
population[POPSIZE].gene[i]=population[cur_best].gene[i];
}
}
//***************************************************************************
//last in the array.If the best individual from the new populatin is better
//than the best individual from the previous population ,then copy the best
//from the new population;else replace the worst individual from the current
//population with the best one from the previous generation.防止种群最优值退化
//***************************************************************************
void elitist()
{
int i;
double best,worst;//适应值
int best_mem,worst_mem;//序号
best_mem=worst_mem=0;
best=population[best_mem].fitness;//最高适应值初始化
worst=population[worst_mem].fitness;//最低适应值初始化
for(i=1;i<POPSIZE;i++)//找出最高和最低适应值 算法有待改进
{
if(population[i].fitness<best)
{
best=population[i].fitness;
best_mem=i;
}
if(population[i].fitness>worst)
{
worst=population[i].fitness;
worst_mem=i;
}
}
if(best<=population[POPSIZE].fitness)//赋值
{
for(i=0;i<NVARS;i++)
population[POPSIZE].gene[i]=population[best_mem].gene[i];
population[POPSIZE].fitness=population[best_mem].fitness;
}
else
{
for(i=0;i<NVARS;i++)
population[worst_mem].gene[i]=population[POPSIZE].gene[i];
population[worst_mem].fitness=population[POPSIZE].fitness;
}
}
//***************************************************************************
//Select function:Standard proportional selection for maximization problems
//incorporating elitist model--makes sure that the best member survives.筛选函数并产生子代
//***************************************************************************
void select(void)
{
int mem,i,j;
double sum=0;
double p;
for(mem=0;mem<POPSIZE;mem++)//所有适应值求和
{
sum+=population[mem].fitness;
}
for(mem=0;mem<POPSIZE;mem++)
{
population[mem].rfitness=population[mem].fitness/sum;//个人认为还不如建一个种群类 把sum看成类成员
}
population[0].cfitness=population[0].rfitness;
for(mem=1;mem<POPSIZE;mem++)
{
population[mem].cfitness=population[mem-1].cfitness+population[mem].rfitness;
}
for(i=0;i<POPSIZE;i++)
{
p=rand()%1000/1000.0;
if(p<population[0].cfitness)
{
newpopulation[i]=population[0];
}
else
{
for(j=0;j<POPSIZE;j++)
if(p>=population[j].cfitness&&p<population[j+1].cfitness)
newpopulation[i]=population[j+1];
}
}
for(i=0;i<POPSIZE;i++)//子代变父代
population[i]=newpopulation[i];
}
//***************************************************************************
//Crossover:performs crossover of the selected parents.
//***************************************************************************
void Xover(int one,int two)//基因重组函数
{
int i;
int point;
if(NVARS>1)
{
if(NVARS==2)
point=1;
else
point=(rand()%(NVARS-1))+1;//两个都重组吗?
for(i=0;i<point;i++)//只有第一个基因发生重组有待改进
swap(&population[one].gene[i],&population[two].gene[i]);
}
}
//************************************************************

