首先,我假设目标是最小化目标函数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)
保存在与主代码相同的文件夹中。
[email protected](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);
在这里将有最终群体的成员,score
将有拟合值的最终群体。默认人口数量为20
。所以你在矩阵中都会有20 rows
。中的列数将相当于number of variables
中的问题,而score
将成为列矩阵。您可以通过将选项PopulationSize
添加到gaoptimset
来更改人口规模。
options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0,'PopulationSize',30);
要知道更多关于options
可供gaoptimset
及其预期values
及其{default values}
。去matlab帮助和搜索gaoptimset
。在那里你会找到一张包含所有这些细节的表格。这里是来自matlab网站http://in.mathworks.com/help/gads/gaoptimset.html的链接。根据你的matlab版本可能有变化。所以最好在matlab中使用帮助。
谢谢你回答:在函数的函数体中,这里是...你是指函数的完整脚本还是函数? – KiW
另一个问题 - 我必须保存新的功能,或者我可以基本上在脚本中运行你的代码? – KiW
好的,我会更新帖子,使其更容易实施 –