Keyword: 大域的最適化
概要
本サンプルは大域的最適化を行うサンプルプログラムです。 本サンプルは以下に示される2次元におけるpeaks関数の大域的最小値を求めて出力します。
※本サンプルはnAG Toolbox for MATLAB®が提供する関数 e05jb() のExampleコードです。実行にはMATLAB®本体(他社製品)とnAG Toolbox for MATLAB®が必要です。
本サンプル及び関数の詳細情報は e05jb のマニュアルページをご参照ください。
Mファイル e05jb_monitor.m
function [user,inform] = e05jb_monitor(n,ncall,xbest,icount,ninit,list,numpts,initpt,nbaskt,
xbaskt,boxl,boxu,nstate,user)
inform = int32(0);
if (nstate == 0 || nstate == 1)
disp(sprintf('\n'))
disp('*** Begin monitoring information ***');
disp(sprintf('\n'))
end
if (nstate <= 0)
disp(['Total sub-boxes = ' num2str(icount(1))]);
disp(['Total function evaluations = ' num2str(ncall)]);
disp(['Total function evaluations used in local searches = ' num2str(icount(2))]);
disp(['Total points used in local search = ' num2str(icount(3))]);
disp(['Total sweeps through levels = ' num2str(icount(4))]);
disp(['Total splits by init. list = ' num2str(icount(5))]);
disp(['Lowest level with nonsplit boxes = ' num2str(icount(6))]);
disp(['Number of candidate minima in the ''shopping basket'' = ' num2str(nbaskt)]);
disp('Shopping basket:');
disp(xbaskt);
disp(sprintf('\n'))
disp('*** End monitoring information ***');
disp(sprintf('\n'))
end
- このMファイルでは関数 e05jb_monitor を定義しています。この関数は最小化プロセスを監視するために使用されます。
Mファイル e05jb_objective.m
function [f,user,inform] = e05jb_objective(n,x,nstate,user)
if (n==2)
inform = int32(0);
else
inform = int32(-1);
end
if (inform >= 0)
% We're prepared to evaluate the objective at this point
if (nstate == 1)
disp(sprintf('\n'));
disp('(OBJFUN was just called for the first time)');
end
f = peaks(x(1), x(2));
end
- このMファイルでは関数 e05jb_objective を定義しています。この関数は特定のベクトルxに対する目的関数 F(x)(peaks関数)の値を求めます。
入力データ
% Problem data for peaks function
prob = 'peaks';
xres = 100;
yres = 100;
bl = [-3; -3];
bu = -bl;
fglob = -6.55; % Approx.
xglob = [0.23; -1.63]; % Approx.
% Initialize e05jb
n = int32(length(bl));
[comm, ifail] = e05ja(n);
if (ifail == 0)
% Vanilla call.
disp(sprintf('\n'));
disp('Solve with no options or init.-list data');
ibound = int32(0); % All bounds will be given;
iinit = int32(0); % Default initialization method;
list = zeros(n,3); % Only need to _declare_ the init.-list
numpts = zeros(n, 1, 'int32'); % data: these will be _set_ internally.
initpt = zeros(n, 1, 'int32');
[bl, bu, listOut, numptsOut, initptOut, ...
xbest, obj, comm, userOut, ifail] = ...
e05jb('e05jb_objective', ibound, iinit, bl, bu, list, ...
numpts, initpt, 'e05jb_monitor', comm);
disp(['e05jb (no options) exited with ifail = ' num2str(ifail)]);
if (ifail == 0)
disp('xbest:');
disp(xbest);
disp(['obj = ' num2str(obj)]);
end
% Set some options. No need to reinitialize: n hasn't changed, and we
% didn't set any options above.
disp(sprintf('\n'));
disp('Solve with options and init.-list data');
% Echo the setting of opt. params.
comm = e05jd('List', comm);
comm = e05jd('Function Evaluations Limit = 100000', comm);
comm = e05jf('Static Limit', 3*n, comm);
% Increase infbnd by factor of 10.
infbnd = e05jl('Infinite Bound Size', comm);
comm = e05jg('Infinite Bound Size', 10*infbnd, comm);
comm = e05je('Local Searches', 'on', comm);
% Set the initialization-list data.
iinit = int32(3); % We're providing the data this time:
list = zeros(n, 3);
list(:, 1) = bl; list(:, 3) = bu;
list(:, 2) = [-1; 0];
numpts = int32(3)*ones(n, 1, 'int32'); % 3 splitting points for each dim;
initpt = int32(2)*ones(n, 1, 'int32'); % 2nd pt in each row to be the 'init.' pt.
[bl, bu, listOut, numptsOut, initptOut, ...
xbest, obj, comm, userOut, ifail] = ...
e05jb('e05jb_objective', ibound, iinit, bl, bu, list, ...
numpts, initpt, 'e05jb_monitor', comm);
disp(['e05jb (options) exited with ifail = ' num2str(ifail)]);
if (ifail == 0)
disp('xbest:');
disp(xbest);
disp(['obj = ' num2str(obj)]);
end
end
- 1〜4行目は peaks 関数の問題データを設定しています。
- 6〜9行目は x の下限、上限、目的関数の近似値、x の近似値を指定しています。
- 13〜15行目は 本関数 e05jb や他のオプション・パラメータ用の関数の伝達データの初期化のため e05ja を呼び出す構文を指定しています。
- 23〜27行目は本関数の入力パラメータを設定しています。
- 29〜32行目は本関数を呼び出す構文を指定しています。
- 34〜40行目は本関数の実行結果を出力する構文を指定しています。
- 47〜57行目は本関数のオプション・パラメータを設定しています。
- 59〜65行目は初期化リストのデータを設定しています。
- 67〜70行目は本関数を呼び出す構文を指定しています。
- 72〜78行目は本関数の実行結果を出力する構文を指定しています。
出力結果
Solve with no options or init.-list data
(OBJFUN was just called for the first time)
*** Begin monitoring information ***
Total sub-boxes = 228
Total function evaluations = 196
Total function evaluations used in local searches = 87
Total points used in local search = 13
Total sweeps through levels = 12
Total splits by init. list = 5
Lowest level with nonsplit boxes = 7
Number of candidate minima in the 'shopping basket' = 2
Shopping basket:
-1.3474 0.2283
0.2045 -1.6255
*** End monitoring information ***
e05jb (no options) exited with ifail = 0
xbest:
0.2283
-1.6255
obj = -6.5511
Solve with options and init.-list data
Function Evaluations Limit = 100000
Static Limit 6
Infinite Bound Size 1.1579208923731620E+78
Local Searches on
(OBJFUN was just called for the first time)
*** Begin monitoring information ***
Total sub-boxes = 146
Total function evaluations = 169
Total function evaluations used in local searches = 102
Total points used in local search = 7
Total sweeps through levels = 7
Total splits by init. list = 5
Lowest level with nonsplit boxes = 4
Number of candidate minima in the 'shopping basket' = 2
Shopping basket:
0.2283 -1.3474
-1.6255 0.2045
*** End monitoring information ***
e05jb (options) exited with ifail = 0
xbest:
0.2283
-1.6255
obj = -6.5511
- 1行目は、30行目までがオプション・パラメータを指定せずに解いた結果であることを示しています。
- 7〜23行目は以下に示すモニタリング情報が出力されています。
- サブボックス(下位領域)の数
- ユーザ定義関数 e05jb_objective の呼び出しの累積数
- 局所検索でのユーザ定義関数 e05jb_objective の呼び出しの累積数
- 局所検索の開始点として使用される座標点の数
- 分割のレベルを通じたスイープ(sweep)の累積数
- 初期化リストによる分割の累積数
- 分割していないボックス(領域)を含む、最も低い分割のレベル
- ショッピングバスケット(最小値の候補の格納場所)の最小値の候補の座標点の数
- 最小値の候補が格納されているショッピングバスケットの内容
- 26行目は関数e05jbがエラーを検知せずに終了したことを示しています。
- 27〜29行目のxbestは大域的最適解xの値を示しています。
- 30行目のobjは最終的な関数値を示しています。
- 33〜37行目は、66行目までが以下に示すオプション・パラメータを指定して解いた結果であることを示しています。
- Function Evaluation Limit には関数の呼び出し回数の近似限界を指定しています。
- Static Limit にはこの数のスイープ(sweep)に対して最適な関数値が固定すると計算が終了する、分割のレベルを通じたスイープ(sweep)の数を指定しています。
- Infinite Bound size には問題の制約の定義における無限境界を指定しています。
- Local Searches on は局所探索の開始によって収束を加速させたい場合に"on"を指定します。
- 43〜59行目は以下に示すモニタリング情報が出力されています。
- サブボックス(下位領域)の数
- ユーザ定義関数 e05jb_objective の呼び出しの累積数
- 局所検索でのユーザ定義関数 e05jb_objective の呼び出しの累積数
- 局所検索の開始点として使用される座標点の数
- 分割のレベルを通じたスイープ(sweep)の累積数
- 初期化リストによる分割の累積数
- 分割していないボックス(領域)を含む、最も低い分割のレベル
- ショッピングバスケット(最小値の候補の格納場所)の最小値の候補の座標点の数
- 最小値の候補が格納されているショッピングバスケットの内容
- 62行目は関数e05jbがエラーを検知せずに終了したことを示しています。
- 63〜65行目のxbestは大域的最適解xの値を示しています。
- 66行目のobjは最終的な関数値を示しています。
