Matlab 优化 - 使用遗传算法最小化 objective 函数
Matlab Optimisation - Minimise objective function using genetic algorithm
我想为包含大约 400 行脚本的函数设置通用算法。脚本本身是一个优化过程,我想使用遗传算法找到优化过程中的最佳输入参数(M
和 OPratio
)。 M
介于 0 and 10^7
和 OPratio
之间 0 and 1
。
脚本的功能是:
NPVtotal = cut_off_optimisation(M,OPratio)
设置遗传算法:
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options.InitialPopulationMatrix = X0;
[M,OPratio,fval] = ga(cut_off_optimisation(M,OPratio),nvars,[],[],[],[],LB,UB)
我收到以下错误:
Undefined function or variable 'M'.
我是优化和遗传算法的新手,非常感谢任何帮助,如果需要更多信息,请告诉我。
首先我假设 objective 是最小化 Objective 函数 cut_off_optimisation
。
现在首先将您的函数更新为如下所示
function y = cut_off_optimisation(x)
M=x(1);
OPratio=x(2);
%
% paste body of your currently used function here
%
y=NPVtotal ;
现在使用此代码最小化您的 objective 函数。
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(@cut_off_optimisation,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
更新:如果你不想更新你的功能。只是 运行 这个主要代码。将函数 NPVtotal = cut_off_optimisation(M,OPratio)
保存在与主代码相同的文件夹中。
objectiveFunction=@(x)cut_off_optimisation(x(1),x(2));
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(objectiveFunction,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
fval
M
OPratio
更新: 用于获取最终种群成员和适应度值。将上面的 ga 函数调用语句替换为下面的语句。
[x,fval,exitflag,output,population,score] = ga(objectiveFunction,nvars,[],[],[],[],LB,UB,[],options);
M=x(1);
OPratio=x(2);
此处 population
将包含最终种群的成员,而 score
将具有最终种群的适应度值。默认人口规模为 20
。所以你将在两个矩阵中都有 20 rows
。 population
中的列数将相当于问题中的 number of variables
,而 score
将是一个列矩阵。您可以通过将选项 PopulationSize
添加到 gaoptimset
.
来更改人口规模
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0,'PopulationSize',30);
了解更多关于 gaoptimset
可用的 options
及其预期 values
和 {default values}
。转到 matlab 帮助并搜索 gaoptimset
。在那里您会找到包含所有这些详细信息的 table。这是来自 matlab 网站 http://in.mathworks.com/help/gads/gaoptimset.html 的 link 。根据您的 matlab 版本,可能会有变化。所以最好在matlab中使用help。
我想为包含大约 400 行脚本的函数设置通用算法。脚本本身是一个优化过程,我想使用遗传算法找到优化过程中的最佳输入参数(M
和 OPratio
)。 M
介于 0 and 10^7
和 OPratio
之间 0 and 1
。
脚本的功能是:
NPVtotal = cut_off_optimisation(M,OPratio)
设置遗传算法:
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options.InitialPopulationMatrix = X0;
[M,OPratio,fval] = ga(cut_off_optimisation(M,OPratio),nvars,[],[],[],[],LB,UB)
我收到以下错误:
Undefined function or variable 'M'.
我是优化和遗传算法的新手,非常感谢任何帮助,如果需要更多信息,请告诉我。
首先我假设 objective 是最小化 Objective 函数 cut_off_optimisation
。
现在首先将您的函数更新为如下所示
function y = cut_off_optimisation(x)
M=x(1);
OPratio=x(2);
%
% paste body of your currently used function here
%
y=NPVtotal ;
现在使用此代码最小化您的 objective 函数。
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(@cut_off_optimisation,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
更新:如果你不想更新你的功能。只是 运行 这个主要代码。将函数 NPVtotal = cut_off_optimisation(M,OPratio)
保存在与主代码相同的文件夹中。
objectiveFunction=@(x)cut_off_optimisation(x(1),x(2));
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(objectiveFunction,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
fval
M
OPratio
更新: 用于获取最终种群成员和适应度值。将上面的 ga 函数调用语句替换为下面的语句。
[x,fval,exitflag,output,population,score] = ga(objectiveFunction,nvars,[],[],[],[],LB,UB,[],options);
M=x(1);
OPratio=x(2);
此处 population
将包含最终种群的成员,而 score
将具有最终种群的适应度值。默认人口规模为 20
。所以你将在两个矩阵中都有 20 rows
。 population
中的列数将相当于问题中的 number of variables
,而 score
将是一个列矩阵。您可以通过将选项 PopulationSize
添加到 gaoptimset
.
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0,'PopulationSize',30);
了解更多关于 gaoptimset
可用的 options
及其预期 values
和 {default values}
。转到 matlab 帮助并搜索 gaoptimset
。在那里您会找到包含所有这些详细信息的 table。这是来自 matlab 网站 http://in.mathworks.com/help/gads/gaoptimset.html 的 link 。根据您的 matlab 版本,可能会有变化。所以最好在matlab中使用help。