當我實作這樣的 MATLAB 代碼進行凸優化時(以下示例類似于此處的MATLAB 官方檔案):
function [xsol,fval,history,searchdir] = runfmincon
clear;clc;
% Set up shared variables with outfun
history.x = [];
history.fval = [];
searchdir = [];
% Call optimization
x0 = [0.1 0.1];
options = optimoptions(@fmincon,'OutputFcn',@outfun,...
'Algorithm','interior-point','Display','iter');
[xsol,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'.
text(x(1) .15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
function f = objfun(x)
f = -x(1)*x(2);
end
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [x(1)^2 x(2)^2 -1];
% Nonlinear equality constraints
ceq = [];
end
end
當我運行上面的代碼時,出現錯誤:
出錯 barrier
出錯 fmincon (line 834)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
出錯 runfmincon (line 15)
[xsol,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);
另外,其他方法,如sqpand active-set,作業正常,只會 interior-point導致錯誤。我已經檢查了初始點,我沒有發現任何問題。如何解決?請幫我!!!謝謝!!!
uj5u.com熱心網友回復:
哦!我解決了!!!在實作interior-point方法的時候,洗掉引數searchdir,以及對應的代碼即可——而對于sqporactive-set方法,保留這個引數不會有任何問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352779.html
下一篇:java.lang.IndexOutOfBoundsException:嘗試從RecyclerView中洗掉URL損壞的專案
