2016-05-17 179 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

好的,我會更新帖子,使其更容易實施 –