选项和输出
使用默认选项运行GA
要使用默认选项运行遗传算法,请致电GA
和the syntax
[x,fval] = ga(@fitnessfun,nvars)
The input arguments toGA
是
@fitnessfun
— A function handle to the file that computes the fitness function.计算目标函数说明如何编写此文件。nvars
- 适应性函数的自变量数量。
输出参数是
X
- 最后一点FVAL
- 健身功能的价值在X
有关其他输入和输出参数的说明,请参阅参考页GA
。
您可以运行描述的示例最小化rastrigin的功能从命令行进入
rng(1,'twister')%可重现性[x,fval] = ga(@rastriginsfcn,2)
这返回
Optimization terminated: average change in the fitness value less than options.FunctionTolerance. x = -1.0421 -1.0018 fval = 2.4385
在命令行设置选项
您可以指定可用的任何选项GA
通过传递选项
作为输入参数GA
使用语法
[x,fval] = ga(@fitnessfun,nvars,[],[],[],[],[],[],[],[],[],options)
该语法未指定任何线性平等,线性不等式或非线性约束。
您创建选项
使用该功能最佳选择
。
options = optimoptions(@ga);
这返回选项
和the default values for its fields.GA
uses these default values if you do not pass in options as an input argument.
每个选项的值存储在一个字段中选项
, 如选项
。您可以通过输入显示这些值选项
其次是一个时期和字段的名称。例如,要显示遗传算法的总体大小,请输入
options.Populionsize ans = '50当numberOfvariobles <= 5,其他200'
去创造选项
具有与默认值不同的字段值 - 例如设置人口化
至100
而不是其默认值50
- 进入
options = optimoptions('ga','superatualsize',100);
这会创造选项
除了所有值设置为默认值人口化
,设置为100
。
如果您现在输入,
ga(@fitnessfun,nvars,[],[],[],[],[],[],[],[],options)
GA
runs the genetic algorithm with a population size of100
。
如果您随后决定更改另一个字段选项
,例如设置plotfcn
至@gaplotbestf
,绘制每一代最佳健身功能价值,请致电最佳选择
和the syntax
options = optimoptions(选项,'plotfcn',@plotbestf);
这保留了所有字段的当前值选项
除了plotfcn
,更改为@plotbestf
。请注意,如果您省略输入参数选项
,,,,最佳选择
重置人口化
其默认值。
您也可以设置两者人口化
andplotfcn
使用单个命令
options = optimoptions('ga','superationsize',100,'plotfcn',@plotbestf);
其他输出参数
要获取有关遗传算法性能的更多信息,您可以致电GA
和the syntax
[X,FVAL,EXITFLAG,输出,人口,分数] = GA(@fitnessfcn,nvars)
除了X
andFVAL
,,,,this function returns the following additional output arguments:
出口
— Integer value corresponding to the reason the algorithm terminated输出
- 结构包含有关算法在每一代的性能的信息人口
- 最终人口scores
- 最终分数
看到GA
参考页面有关这些参数的更多信息。