2016-05-17 137 views
0

我想为包含大约400行脚本的函数设置通用算法。脚本本身是一个优化过程,我想使用遗传算法来找到优化过程中的最佳输入参数(MOPratio)。 M介于0 and 1之间的0 and 10^7OPratio之间。 脚本的功能是:Matlab优化 - 使用遗传算法最小化目标函数

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'. 

我是新来优化和遗传算法所以将不胜感激任何帮助,请让我知道如果更多的信息是必要的。

回答

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)保存在与主代码相同的文件夹中。

[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中使用帮助。

+0

谢谢你回答:在函数的函数体中,这里是...你是指函数的完整脚本还是函数? – KiW

+0

另一个问题 - 我必须保存新的功能,或者我可以基本上在脚本中运行你的代码? – KiW

+0

好的,我会更新帖子,使其更容易实施 –

相关问题