メインコンテンツ

このページは機械翻訳を使用して翻訳されました。最新版の英語を参照するには、ここをクリックします。

サロゲート最適化と他のソルバーの比較

この例では、surrogateopt を他の 2 つのソルバーと比較します。fmincon は滑らかな問題に推奨されるソルバーであり、patternsearch は滑らかでない問題に推奨されるソルバーです。この例では、2 次元領域で非滑らかな関数を使用します。

type nonSmoothFcn
function [f, g] = nonSmoothFcn(x)
%NONSMOOTHFCN is a non-smooth objective function

%   Copyright 2005 The MathWorks, Inc.

for i = 1:size(x,1)
    if  x(i,1) < -7
        f(i) = (x(i,1))^2 + (x(i,2))^2 ;
    elseif x(i,1) < -3
        f(i) = -2*sin(x(i,1)) - (x(i,1)*x(i,2)^2)/10 + 15 ;
    elseif x(i,1) < 0
        f(i) = 0.5*x(i,1)^2 + 20 + abs(x(i,2))+ patho(x(i,:));
    elseif x(i,1) >= 0
        f(i) = .3*sqrt(x(i,1)) + 25 +abs(x(i,2)) + patho(x(i,:));
    end
end

%Calculate gradient
g = NaN;
if x(i,1) < -7
    g = 2*[x(i,1); x(i,2)];
elseif x(i,1) < -3
    g = [-2*cos(x(i,1))-(x(i,2)^2)/10; -x(i,1)*x(i,2)/5];
elseif x(i,1) < 0
    [fp,gp] = patho(x(i,:));
    if x(i,2) > 0
        g = [x(i,1)+gp(1); 1+gp(2)];
    elseif x(i,2) < 0
        g =  [x(i,1)+gp(1); -1+gp(2)];
    end
elseif x(i,1) >0
    [fp,gp] = patho(x(i,:));
    if x(i,2) > 0
        g = [.15/sqrt(x(i,1))+gp(1); 1+ gp(2)];
    elseif x(i,2) < 0
        g = [.15/sqrt(x(i,1))+gp(1); -1+ gp(2)];
    end
end

function [f,g] = patho(x)
Max = 500;
f = zeros(size(x,1),1);
g = zeros(size(x));
for k = 1:Max  %k 
   arg = sin(pi*k^2*x)/(pi*k^2);
   f = f + sum(arg,2);
   g = g + cos(pi*k^2*x);
end
mplier = 0.1; % Scale the control variable
Objfcn = @(x)nonSmoothFcn(mplier*x); % Handle to the objective function
range = [-6 6;-6 6]/mplier; % Range used to plot the objective function
rng default % Reset the global random number generator
showNonSmoothFcn(Objfcn,range);
title('Nonsmooth Objective Function')
view(-151,44)

Figure contains an axes object. The axes object with title Nonsmooth Objective Function contains 4 objects of type surface, contour.

drawnow

surrogateopt がデフォルトの反復回数内で大域的最小値をどのように正確に見つけられるかを確認します。

lb = -6*ones(1,2)/mplier;
ub = -lb;
[xs,fvals,eflags,outputs] = surrogateopt(Objfcn,lb,ub);

Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: 13, xlabel Iteration, ylabel Function value contains an object of type scatter. This object represents Best function value.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
fprintf("Lowest found value = %g.\r",fvals)
Lowest found value = 13.
figure
showNonSmoothFcn(Objfcn,range);
view(-151,44)
hold on
p1 = plot3(xs(1),xs(2),fvals,'om','MarkerSize',15,'MarkerFaceColor','m');
legend(p1,{'Solution'})
hold off

Figure contains an axes object. The axes object contains 5 objects of type surface, contour, line. One or more of the lines displays its values using only markers This object represents Solution.

patternsearchと比較

境界内のランダムなポイントから開始して、同じ数の関数評価を使用するには、patternsearch オプションを設定します。

rng default
x0 = lb + rand(size(lb)).*(ub - lb);
optsps = optimoptions('patternsearch','MaxFunctionEvaluations',200,'PlotFcn','psplotbestf');
[xps,fvalps,eflagps,outputps] = patternsearch(Objfcn,x0,[],[],[],[],lb,ub,[],optsps);
patternsearch stopped because the mesh size was less than options.MeshTolerance.

Figure Pattern Search contains an axes object. The axes object with title Best Function Value: 13, xlabel Iteration, ylabel Function value contains an object of type scatter.

figure
showNonSmoothFcn(Objfcn,range);
view(-151,44)
hold on
p1 = plot3(x0(1),x0(2),Objfcn(x0),'ob','MarkerSize',12,'MarkerFaceColor','b');
p2 = plot3(xps(1),xps(2),fvalps,'om','MarkerSize',15,'MarkerFaceColor','m');
legend([p1,p2],{'Start Point','Solution'})
hold off

Figure contains an axes object. The axes object contains 6 objects of type surface, contour, line. One or more of the lines displays its values using only markers These objects represent Start Point, Solution.

patternsearchsurrogateopt と同じ解決策を見つけました。

関数評価の回数を制限して再試行してください。

optsurr = optimoptions('surrogateopt','MaxFunctionEvaluations',40);
[xs,fvals,eflags,outputs] = surrogateopt(Objfcn,lb,ub,optsurr);

Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: 13.0003, xlabel Iteration, ylabel Function value contains an object of type scatter. This object represents Best function value.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
optsps.MaxFunctionEvaluations = 40;
[xps,fvalps,eflagps,outputps] = patternsearch(Objfcn,x0,[],[],[],[],lb,ub,[],optsps);
patternsearch stopped because it exceeded options.MaxFunctionEvaluations.

Figure Pattern Search contains an axes object. The axes object with title Best Function Value: 13.0983, xlabel Iteration, ylabel Function value contains an object of type scatter.

再び、両方のソルバーが大域解を迅速に見つけました。

fminconと比較

fmincon は、開始点付近の局所解を効率的に見つけます。ただし、非凸問題や非滑らかな問題では、大域解から遠く離れた場所に簡単に行き詰まる可能性があります。

プロット関数、以前のソルバーと同じ関数評価数、および patternsearch と同じ開始点を使用するように fmincon オプションを設定します。

opts = optimoptions('fmincon','PlotFcn','optimplotfval','MaxFunctionEvaluations',200);
[fmsol,fmfval,eflag,fmoutput] = fmincon(Objfcn,x0,[],[],[],[],lb,ub,[],opts);

Figure Optimization Plot Function contains an axes object. The axes object with title Current Function Value: 30.1703, xlabel Iteration, ylabel Function value contains an object of type scatter.

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the value of the step size tolerance and constraints are 
satisfied to within the value of the constraint tolerance.

<stopping criteria details>
figure
showNonSmoothFcn(Objfcn,range);
view(-151,44)
hold on
p1 = plot3(x0(1),x0(2),Objfcn(x0),'ob','MarkerSize',12,'MarkerFaceColor','b');
p2 = plot3(fmsol(1),fmsol(2),fmfval,'om','MarkerSize',15,'MarkerFaceColor','m');
legend([p1,p2],{'Start Point','Solution'})
hold off

Figure contains an axes object. The axes object contains 6 objects of type surface, contour, line. One or more of the lines displays its values using only markers These objects represent Start Point, Solution.

fmincon は開始点付近で局所的最小値に陥っています。

参考

| |

トピック