求解
f
=
−
5
s
i
n
x
1
s
i
n
x
2
s
i
n
x
3
s
i
n
x
4
s
i
n
x
5
−
s
i
n
(
5
x
1
5
x
2
5
x
3
5
x
4
5
x
5
)
+
8
,其中
0
<
=
x
1
,
x
2
,
x
3
,
x
4
,
x
5
<
=
0.9
π
(最小值为
2
点多)
\text{求解}f=-5sinx_1sinx_2sinx_3sinx_4sinx_5-sin(5x_15x_25x_35x_45x_5)+8,{其中}0<=x_1,x_2,x_3,x_4,x_5<=0.9\pi (\text{最小值为}2\text{点多})
求解 f = − 5 s in x 1 s in x 2 s in x 3 s in x 4 s in x 5 − s in ( 5 x 1 5 x 2 5 x 3 5 x 4 5 x 5 ) + 8 , 其中 0 <= x 1 , x 2 , x 3 , x 4 , x 5 <= 0.9 π ( 最小值为 2 点多 )
主函数 Genetic.m
1 初始化参数
clear; clc
maxgen = 100 ;
sizepop = 50 ;
pcross = 0.6 ;
pmutation = 0.01 ;
lenchrom = [ 1 , 1 , 1 , 1 , 1 ] ;
bound = [ 0 , 0.9 * pi ; 0 , 0.9 * pi ; 0 , 0.9 * pi ; 0 , 0.9 * pi ; 0 , 0.9 * pi ] ;
individuals = struct ( 'fitness' , zeros ( 1 , sizepop) , 'chrom' , [ ] ) ;
2 初始化种群
for i = 1 : sizepop
individuals. chrom ( i , : ) = Code ( lenchrom, bound, sizepop) ;
x = individuals. chrom ( i , : ) ;
individuals. fitness ( i ) = fun ( x) ;
end
[ bestfitness, bestindex] = min ( individuals. fitness) ;
bestchrom = individuals. chrom ( bestindex, : ) ;
avgfitness = sum ( individuals. fitness) / sizepop;
3 函数主体:实现搜索过程
trace = zeros ( maxgen, 2 ) ;
for i = 1 : maxgen
individuals = Select ( individuals, sizepop) ;
individuals. chrom = Cross ( pcross, lenchrom, individuals. chrom, sizepop) ;
individuals. chrom = Mutation ( pmutation, lenchrom, individuals. chrom, sizepop, [ i , maxgen] , bound) ;
for j = 1 : sizepop
x = individuals. chrom ( j , : ) ;
individuals. fitness ( j ) = fun ( x) ;
end
[ newbestfitness, newbestindex] = min ( individuals. fitness) ;
[ worestfitness, worestindex] = max ( individuals. fitness) ;
if bestfitness > newbestfitness
bestfitness = newbestfitness;
bestchrom = individuals. chrom ( newbestindex, : ) ;
end
individuals. chrom ( worestindex, : ) = bestchrom;
individuals. fitness ( worestindex) = bestfitness;
avgfitness = sum ( individuals. fitness) / sizepop;
trace ( i , : ) = [ avgfitness, bestfitness] ;
end
4 结果展示
[ r, c] = size ( trace) ;
figure
plot ( ( 1 : r) ', trace(:, 1), ' r- ', (1: r)' , trace ( : , 2 ) , 'b--' ) ;
title ( [ '函数值曲线' , '终止代数=' , num2str ( maxgen) ] , 'fontsize' , 12 ) ;
xlabel ( '进化代数' , 'fontsize' , 12 ) ; ylabel ( '函数值' , 'fontsize' , 12 ) ;
legend ( '各代平均值' , '各代最佳值' ) ;
xlim ( [ 1 , r] ) ; ylim ( [ 1.5 , 8 ] ) ;
grid on
disp ( '函数值 变量' )
disp ( [ bestfitness, individuals. chrom ( bestindex, : ) ] )
适应度函数 fun.m
function y = fun ( x)
y = - 5 * sin ( x ( 1 ) ) * sin ( x ( 2 ) ) * sin ( x ( 3 ) ) * sin ( x ( 4 ) ) * sin ( x ( 5 ) ) ...
- sin ( 5 * x ( 1 ) * 5 * x ( 2 ) * 5 * x ( 3 ) * 5 * x ( 4 ) * 5 * x ( 5 ) ) + 8 ;
生成初始化种群 Code.m
function ret = Code ( lenchrom, bound, sizepop)
for i = 1 : sizepop
pick = rand ( 1 , length ( lenchrom) ) ;
ret = bound ( : , 1 ) ' + (bound(:, 2) - bound(:, 1))' .* pick;
end
选择函数 Select.m
function ret = Select ( individuals, sizepop)
individuals. fitness = 1 ./ individuals. fitness;
sumfitness = sum ( individuals. fitness) ;
sumf = individuals. fitness ./ sumfitness;
index = zeros ( 1 , sizepop) ;
for i = 1 : sizepop
pick = rand;
while pick == 0
pick = rand;
end
for j = 1 : sizepop
pick = pick - sumf ( j ) ;
if pick < 0
index ( i ) = j ;
break ;
end
end
end
individuals. chrom = individuals. chrom ( index, : ) ;
individuals. fitness = individuals. fitness ( index) ;
ret = individuals;
交叉函数 Cross.m
function ret = Cross ( pcross, lenchrom, chrom, sizepop)
for i = 1 : sizepop
pick = rand ( 1 , 2 ) ;
while prod ( pick) == 0
pick = rand ( 1 , 2 ) ;
end
index = ceil ( pick .* sizepop) ;
pick = rand;
while pick == 0
pick = rand;
end
if pick > pcross
continue ;
end
pick = rand;
while pick == 0
pick = rand;
end
pos = ceil ( pick .* sum ( lenchrom) ) ;
pick = rand;
v1 = chrom ( index ( 1 ) , pos) ;
v2 = chrom ( index ( 2 ) , pos) ;
chrom ( index ( 1 ) , pos) = pick * v2 + ( 1 - pick) * v1;
chrom ( index ( 2 ) , pos) = pick * v1 + ( 1 - pick) * v2;
end
ret = chrom;
变异操作 Mutation.m
function ret = Mutation ( pmutation, lenchrom, chrom, sizepop, pop, bound)
for i = 1 : sizepop
pick = rand;
while pick == 0
pick = rand;
end
index = ceil ( pick * sizepop) ;
pick = rand;
if pick > pmutation
continue ;
end
pick = rand;
while pick == 0
pick = rand;
end
pos = ceil ( pick * sum ( lenchrom) ) ;
v = chrom ( index, pos) ;
v1 = v - bound ( pos, 1 ) ;
v2 = bound ( pos, 2 ) - v;
pick = rand;
if pick <= 0.5
delta = v1 * ( 1 - pick * ( ( 1 - pop ( 1 ) / pop ( 2 ) ) ^ 2 ) ) ;
chrom ( index, pos) = v - delta;
else
delta = v2 * ( 1 - pick * ( ( 1 - pop ( 1 ) / pop ( 2 ) ) ^ 2 ) ) ;
chrom ( index, pos) = v + delta;
end
end
ret = chrom;