遗传算法求函数最值
突然觉得模拟退火不好想,于是我学起了遗传算法。
遗传算法求函数最值
点击查看代码
#include
using namespace std;
const double pi=3.14159265;
const double pcross=0.7;//交叉概率
const double pmutate=0.001;//变异概率
const int len=22;//22位的染色体
const int daishu=500;//更迭代数
const int Size=500;//种群规模
double bestval;//适应值最大值
typedef struct node{//染色体
bool chromo[len];
}node;
node bestchromo;//记录最优个体
node group[Size];//记录种群中的个体的数组
node temp[Size];//记录种群中的个体的临时数组
void gouzao(node &c)
{//对单个染色体随机赋值
for(int i=0;ifitnessval[id])
id=i;
decode(group[id],x);
number=f(x);
return id;
}
void GA(double &x,double &number)
{//遗传算法流程
for(int i=0;i