首先,我假設目標是最小化目標函數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
好的,我會更新帖子,使其更容易實施 –