文档介绍:智能优化算法第一次作业
洪文杰S151
— ——一-遗传算法
0 0 0 8 5 3
问题:用遗传算法求解f(x) = x sin(10 n大x) + 2、0得最大值,x取[.1, 2 ]、
一、分析:遗传算法基本思路
Pop初始种群
in t Unit_choo s e [NUMBER][3 0 ];〃选择、交叉、变异后得种群
in t Numb e r [NUMBE R]; 〃被选择得个体编号
flo a t F i tness [NUMBER];〃适应度
f 1 oat sele c t_p r o b abi 1 i ty[N UMBE R]; 〃选择概率
f 1 oat accum u 1 a—proba b ility [NUMBER]: 〃积累概率
f 1 oa t f_max=0> 0 ; / /最大值
floa t f_x=0、0;〃最大值对应得自变量
int hwj_ c oding (i n t start, int end);//编码
vo i d hwj_ini t i a l_pop u 1 ation (in t num); //产生初始种群
void hwj_f itness( i nt num);〃适应度计算
v o id hwj_ c h o ose(); 〃选择个体
int h w j _b i n a ry_ s e a rch(in t I, int r, float t emp) ;/ / 查找选择
//voi d hw j _N_M( i nt a[],in t b[], int N, int M);〃从 M 个数中选N个不一样得数 void h wj_cross(int num, f loat cro s s); / /交叉后得得到种群
void hw j _ a berra n ce( i nt num,f 1 oat abe r r a nee) ;/ / 变异后得得到得种群
void hwj_max (in t num);〃找到最适应得个体
int m a in ()(
h n t str a t, e n d ;/ / 区间
in t N u m;〃编码大小
floa t c r os s =0、8;/ / 交叉概率
f 1 oat aberra n ce = 0、0 4;/ /变异概率
int k e y= 1;
cout«"请输入求解区间:"<<e n dl;
cin» s tra t >>end;
Num= h wj_ c od i ng(strat,e n d);
。co u t<< n Num: "<<Num«e n dl;
/ / cou t <C< " ---一 一-一---一 一—一1一 — 一 一 " V Ven d. 1 ;
hwj_ i niti a I_po p ulation (Num);
/ / c o ut«" —- — ——-—— 一-一2 初始种群 ——"«en d. 1 ;
/*"or ( i nt i=0; i〈NUMBER; i+ + ){
for (int j=0; j <N u m;j++)(
c out«U n it[i] [j]«'';
6 0 }
。 c out<< e ndl;
,}
*/
0 whi 1 e( k ey!=GEN E_NUMBER)(
hwj_ f i tness(Num);
//e c outv^^ " -- -- — ~3 - — -"^Cvend 1 ;
/ / fo r ( i nt i =0;i<NUM B ER;i++) {
? cou t « F it n e s s[i]«en d 1 ;
// )
hwj_cho o se ();
cout«"-— ——一—4 被选择得个体一--—-— —-—"« e n dl;
/* for(int i = 0 ; i <NUMBER; i ++)(
?"or(in t j = 0;j<Num; j ++) (
cout<<U n it—c h o o s e[i] [j]«',;
4
d c o ut« e ndl;
*/
。hwj_cros s (Num, cross);
/*ocout<V"-— — --- — -— 5 交叉后得种群 —— ——"«en d 1 ;
a f or( i nt i =0; i <NUMBER;i++) {
oofo r (int j = 0; j<Num;j++)(
c o ut«Uni t [j] [j]«'
}
八 c o u t« e ndl;