没有合适的资源?快使用搜索试试~ 我知道了~
本文所述为C++实现的遗传算法的类文件实例。一般来说遗传算法可以解决许多问题,希望本文所述的C++遗传算法类文件,可帮助你解决更多问题,并且代码中为了便于读者更好的理解,而加入了丰富的注释内容,是新手学习遗传算法不可多得的参考代码。 具体代码如下所示: #include stdafx.h #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<ctime>//把日期和时间转换为字符串 using namespace std; //Parametes setting #define POP
资源详情
资源评论
资源推荐
C++遗传算法类文件实例分析遗传算法类文件实例分析
本文所述为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)
weixin_38519619
- 粉丝: 2
- 资源: 905
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0