Matlab优化 - 使用遗传算法最小化目标函数

时间:2016-05-17 17:06:56

标签: matlab genetic-algorithm

我想为包含大约400行脚本的函数设置通用算法。脚本本身是一个优化过程,我想使用遗传算法在优化过程中找到最佳输入参数(MOPratio)。 M介于0 and 10^7OPratio之间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'.

我是优化和遗传算法的新手所以非常感谢任何帮助,如果需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

首先,我假设目标是最小化目标函数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 rowspopulation中的列数相当于问题中的number of variablesscore将是列矩阵。您可以通过向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中使用帮助。

相关问题