当前位置: 编程技术>c/c++/嵌入式
C++遗传算法类文件实例分析
来源: 互联网 发布时间:2014-10-26
本文导语: 本文所述为C++实现的遗传算法的类文件实例。一般来说遗传算法可以解决许多问题,希望本文所述的C++遗传算法类文件,可帮助你解决更多问题,并且代码中为了便于读者更好的理解,而加入了丰富的注释内容,是新手学习遗...
本文所述为C++实现的遗传算法的类文件实例。一般来说遗传算法可以解决许多问题,希望本文所述的C++遗传算法类文件,可帮助你解决更多问题,并且代码中为了便于读者更好的理解,而加入了丰富的注释内容,是新手学习遗传算法不可多得的参考代码。
具体代码如下所示:
#include "stdafx.h"
#include
#include
#include
#include
#include//把日期和时间转换为字符串
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您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。