-
一、
实验目的和意义
用遗传算法解决下面函数的极小值问题:
1
30
2
30
?
x
i
?
f
(
X
)
?<
/p>
x
?
cos
|<
/p>
x
i
|
?
600
?
?
?
1
,
?
i
?
400
0
i
?
1
i<
/p>
?
?
i
?
1
遗传算法的具体实施策略不限,最好用
MA
TLAB
,上交内容包括源程序和运行结果
二、
实验原理
目标函数是一个含有
30
维变量的复杂型超越函数,决定使用
MA
TLAB
下的遗传函数工具箱来寻找函
数最小值。
三、
详细设计步骤
1
、函数编辑
在
m
文件下编辑目标函数:
function
y
=fun(x);
y =
zeros(size(x,1),1);
%
产生一个列向量
< br>,(
种群数
)*1,
作为预留解
空间
[xSize, Dim] = size(x);
%xSize:
种群数
;
Dim:
变量
x
的维数
indices = repmat(1:Dim, xSize, 1);
%
生成函数中
i
的空间矩阵
,(
种群数
)*(
变量维数
)
y = sum(((x.^2) / 4000)')' - prod(cos(x
./
sqrt(indices))')' + 1;
%
函数表达式
,
此处均采用矩
阵点乘的形式
2
、遗传算法参数设置
通过
gatool(
遗传算法工具箱
)
设置参数,运行程序,选择合适参数。
Number of
variables(
变量维数
):30
Population size:100
Initial
range:[-600;600]
Selection function
(
选择函数
):
tournament(
锦标赛
);
tournament size:2
Mutation
function (
变异函数
): Uniform();
Rate:0.05
Crossover
function: Intermediate;
Ratio: 0.05
Generrations:
5000;
Stall
generations: Inf;
Stall time limite: Inf
Fitness function is vectorized: on
其余均为工具箱默认参数。
3
、运行程序
将设置好参数的算法程序运行
30
次运行,最佳运行结果为:
3.4937e-010
x1
~
p>
x30
取值:
1.0e-004 *
-0.0097
0.0017
0.0587
-0.0250
0.0765
0.0391
-0.0824
0.0653
-0.0539
-0.3470
-0.1078
0.4388
0.0223
0.0363
0.0003
0.0228
-0.0481
-0.4491
-0.5006
-0.2514
0.0608
-0.1022
0.3454
0.1146
0.0768
-0.0203
0.0910
-0.0571
0.0065
-0.1384
1
4
、保存遗传算法程序
将在工具箱中编辑好的算法导出为
m
函数并保存:<
/p>
function
[X,FVAL,REASON,OUTPUT,POPULATION,SCORES] =
untitled
%% This is an
auto generated M file to do optimization with the
Genetic Algorithm and
%
Direct Search Toolbox. Use GAOPTIMSET for default
GA options structure.
%%Fitness
function
fitnessFunction =
@fun;
%%Number of
Variables
nvars =
30;
%Start with default
options
options =
gaoptimset;
%%Modify some
parameters
options = gaoptim
set(options,
'PopInitRange'
,[-600 600 ]);
options = g
aoptimset(options,
'PopulationSize'
,100);
options = gaop
timset(options,
'Generations'
,5000);
options = gaoptimset
(options,
'StallGenLimit'
,Inf);
options = gaoptimset(
options,
'StallTimeLimit'
,Inf);
options = gaoptimset(
options,
'PlotInterval'
,30);
options = gaoptimset(o
ptions,
'SelectionFcn'
,{
@selectiontournament 2 });
options = gaoptimset(options,
'CrossoverFcn'
,{
@crossoverintermediate 0.05 });
2
-
-
-
-
-
-
-
-
-
上一篇:古诗常见意象及含义
下一篇:什么是屌丝?回答你屌丝是什么意思