首先我假设目标是最小化目标函数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 ;
现在使用此代码最小化您的目标函数。
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。在那里你会找到一张包含所有这些细节的表格。这是来自 matlab 网站http://in.mathworks.com/help/gads/gaoptimset.html 的链接。根据您的 matlab 版本,可能会有所变化。所以最好在matlab中使用帮助。