logo资料库

C++实现遗传算法.doc

第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
资料共5页,全文预览结束
/* 本程序试用遗传算法来解决 Rosenbrock 函数的全局最大值计算问题: max s.t. f(x1,x2) = 100 (x1^2-x2)^2 + (1-x1)^2 -2.048 ≤ xi ≤ 2.048 (i=1,2)*/ #include #include #include #include using namespace std; const int M=80,T=100; const double pc=0.6,pm=0.01; // M 群体大小,pc 交叉概率,pm 变异概率,T 终止代数 //定义群体结构 struct population {int x[20]; double x1,x2; double fit; double sumfit; }p[M]; void initial(population *); void evaluefitness(population *); void select(population *); void crossover(population *); void mutation(population *); void decoding(population *); void print(population *); int main() {int gen=0; initial(&p[0]); cout<<"initialed!"<
population *po,*po2; for(po=t;po=s) {pt[i]=(*po);break;} else continue; } i++; } for(i=0;i
double q; int d,e,tem[20]; 位置 //用于存一个 0 到 1 的随机数 //d 存放从 1 到 19 的一个随机整数,用来确定交叉的 //e 存放从 0 到 M 的一个随机且与当前 P[i]中 i 不同的整数,用来确定交叉的对 //运算 M 次,避免产生群体中某个体与自己交叉的情况 //随机确定交叉对象 //不能重复 //随机确定交叉位置 象 srand(time(0)); for(po=t;po
//dit=(U(max)-U(min))/n-1 dit=4.096/(n-1); for(po=t;po
分享到:
收藏