MATLAB,优化函数fmincon解析
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
输入参数:fun要求解的函数值;x0函数fun参数值的初始化;
参数值的线性不等式约束A,b
参数值的等式线性约束Aeq,beq,
参数值的上界和下界lb,ub
非线性约束nonlcon
输出参数:X输出最优参数值
Fval输出fun在X参数的值
Exitflag输出fmincon额外条件值
function [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = fminconFUN,X,A,B,Aeq,Beq,LB,UB,NONLCON,options,varargin)
/*fmincon可以在多元函数中找到最小值
FMINCON attempts to solve problems of the form:
min FX) subject to: A*X <= B, Aeq*X = Beq linear constraints)线性约束
X CX) <= 0, CeqX) = 0 nonlinear constraints)非线性约束
LB <= X <= UB bounds)
*/
/*FMINCON implements four different algorithms: interior point, SQP,
% active set, and trust region reflective. Choose one via the option
% Algorithm: for instance, to choose SQP, set OPTIONS =
% optimoptions'fmincon','Algorithm','sqp'), and then pass OPTIONS to
% FMINCON.
fmincon函数应用四种不同的算法:内点法(interior point);序列二次规划算法SQP);有效集法(active set);信赖域有效算法(trust region reflective)。
如果采用SQP算法可以设置 OPTIONS = optimoptions'fmincon','Algorithm','sqp'),再把OPTIONS赋给fmincon
*/
/*
% X = FMINCONFUN,X0,A,B) starts at X0 and finds a minimum X to the
% function FUN, subject to the linear inequalities A*X <= B. FUN accepts
% input X and returns a scalar function value F evaluated at X. X0 may be
% a scalar, vector, or matrix.
%
% X = FMINCONFUN,X0,A,B,Aeq,Beq) minimizes FUN subject to the linear
% equalities Aeq*X = Beq as well as A*X <= B. Set A=[] and B=[] if no
% inequalities exist.)
%
% X = FMINCONFUN,X0,A,B,Aeq,Beq,LB,UB) defines a set of lower and upper
% bounds on the design variables, X, so that a solution is found in
% the range LB <= X <= UB. Use empty matrices for LB and UB
% if no bounds exist. Set LBi) = -Inf if Xi) is unbounded below;
% set UBi) = Inf if Xi) is unbounded above.
%
% X = FMINCONFUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON) subjects the minimization
% to the constraints defined in NONLCON. The function NONLCON accepts X
% and returns the vectors C and Ceq, representing the nonlinear
% inequalities and equalities respectively. FMINCON minimizes FUN such
% that CX) <= 0 and CeqX) = 0. Set LB = [] and/or UB = [] if no bounds
% exist.)
%
% X = FMINCONFUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS) minimizes with
% the default optimization parameters replaced by values in OPTIONS, an
% argument created with the OPTIMOPTIONS function. See OPTIMOPTIONS for
% details. For a list of options accepted by FMINCON refer to the
% documentation.
%
% X = FMINCONPROBLEM) finds the minimum for PROBLEM. PROBLEM is a
% structure with the function FUN in PROBLEM.objective, the start point
% in PROBLEM.x0, the linear inequality constraints in PROBLEM.Aineq
% and PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and
% PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in
% PROBLEM.ub, the nonlinear constraint function in PROBLEM.nonlcon, the
% options structure in PROBLEM.options, and solver name 'fmincon' in
% PROBLEM.solver. Use this syntax to solve at the command line a problem
% exported from OPTIMTOOL. The structure PROBLEM must have all the fields.
%
% [X,FVAL] = FMINCONFUN,X0,...) returns the value of the objective
% function FUN at the solution X.
%
% [X,FVAL,EXITFLAG] = FMINCONFUN,X0,...) returns an EXITFLAG that
% describes the exit condition of FMINCON. Possible values of EXITFLAG
% and the corresponding exit conditions are listed below. See the
% documentation for a complete description.
% */
/*
% All algorithms:
% 1 First order optimality conditions satisfied.
% 0 Too many function evaluations or iterations.
% -1 Stopped by output/plot function.
% -2 No feasible point found.
% Trust-region-reflective, interior-point, and sqp:
% 2 Change in X too small.
% Trust-region-reflective:
% 3 Change in objective function too small.
% Active-set only:
% 4 Computed search direction too small.
% 5 Predicted change in objective function too small.
% Interior-point and sqp:
% -3 Problem seems unbounded.
所有算法中EXITFLAG返回值涵义
1 满足一阶最优性条件
0 函数计算或迭代太多。无法求解
-1 被输出和绘图功能阻止
-2 找不到可行点
Trust-region-reflective, interior-point, and sqp:三种算法才有的返回值
2 X变化太小
Active-set 算法才有的返回值
4 计算搜索的方向太小
5 目标函数的预测变化太小。
Interior-point and sqp才有的
-3 问题没有边界
*/
/*
% [X,FVAL,EXITFLAG,OUTPUT] = FMINCONFUN,X0,...) returns a structure
% OUTPUT with information such as total number of iterations, and final
% objective function value. See the documentation for a complete list.
返回包含迭代总数和最终目标函数值等信息的结构输出
*/
/*
% [X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = FMINCONFUN,X0,...) returns the
% Lagrange multipliers at the solution X: LAMBDA.lower for LB,
% LAMBDA.upper for UB, LAMBDA.ineqlin is for the linear inequalities,
% LAMBDA.eqlin is for the linear equalities, LAMBDA.ineqnonlin is for the
% nonlinear inequalities, and LAMBDA.eqnonlin is for the nonlinear
% equalities.
返回解x处的拉格朗日乘数:lambda.lower表示lb,lambda.upper表示ub,
lambda.ineqlin表示线性不等式,lambda.eqlin表示线性等式,
lambda.ineqnonlin表示非线性不等式,lambda.eqnonlin表示非线性不等式。
*/
/*
% [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD] = FMINCONFUN,X0,...) returns the
% value of the gradient of FUN at the solution X.
返回解决方案x的fun渐变值。
%*/
/* [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = FMINCONFUN,X0,...)
% returns the value of the exact or approximate Hessian of the Lagrangian
% at X.
返回X的朗格朗日精确解或者近似Hessian矩阵
*/
/* Examples
% FUN can be specified using @:
% X = fmincon@humps,...)
% In this case, F = humpsX) returns the scalar function value F of
% the HUMPS function evaluated at X.
%
% FUN can also be an anonymous function:
% X = fmincon@x) 3*sinx1))+expx2)),[1;1],[],[],[],[],[0 0])
% returns X = [0;0].
%
% If FUN or NONLCON are parameterized, you can use anonymous functions to
% capture the problem-dependent parameters. Suppose you want to minimize
% the objective given in the function myfun, subject to the nonlinear
% constraint mycon, where these two functions are parameterized by their
% second argument a1 and a2, respectively. Here myfun and mycon are
% MATLAB file functions such as
%
% function f = myfunx,a1)
% f = x1)^2 + a1*x2)^2;
%
% function [c,ceq] = myconx,a2)
% c = a2/x1) - x2);
% ceq = [];
%
% To optimize for specific values of a1 and a2, first assign the values
% to these two parameters. Then create two one-argument anonymous
% functions that capture the values of a1 and a2, and call myfun and
% mycon with two arguments. Finally, pass these anonymous functions to
% FMINCON:
%
% a1 = 2; a2 = 1.5; % define parameters first
% options = optimoptions'fmincon','Algorithm','interior-point'); % run interior-point algorithm
% x = fmincon@x) myfunx,a1),[1;2],[],[],[],[],[],[],@x) myconx,a2),options)
%
% See also OPTIMOPTIONS, OPTIMTOOL, FMINUNC, FMINBND, FMINSEARCH, @, FUNCTION_HANDLE.
% Copyright 1990-2013 The MathWorks, Inc.
*/
defaultopt = struct ...
'Algorithm','interior-point', ...
'AlwaysHonorConstraints','bounds', ...
'DerivativeCheck','off', ...
'Diagnostics','off', ...
'DiffMaxChange',Inf, ...
'DiffMinChange',0, ...
'Display','final', ...
'FinDiffRelStep', [], ...
'FinDiffType','forward', ...
'FunValCheck','off', ...
'GradConstr','off', ...
'GradObj','off', ...
'HessFcn',[], ...
'Hessian',[], ...
'HessMult',[], ...
'HessPattern','sparseonesnumberOfVariables))', ...
'InitBarrierParam',0.1, ...
'InitTrustRegionRadius','sqrtnumberOfVariables)', ...
'MaxFunEvals',[], ...
'MaxIter',[], ...
'MaxPCGIter','max1,floornumberOfVariables/2))', ...
'MaxProjCGIter','2*numberOfVariables-numberOfEqualities)', ...
'MaxSQPIter','10*maxnumberOfVariables,numberOfInequalities+numberOfBounds)', ...
'ObjectiveLimit',-1e20, ...
'OutputFcn',[], ...
'PlotFcns',[], ...
'PrecondBandWidth',0, ...
'RelLineSrchBnd',[], ...
'RelLineSrchBndDuration',1, ...
'ScaleProblem','none', ...
'SubproblemAlgorithm','ldl-factorization', ...
'TolCon',1e-6, ...
'TolConSQP',1e-6, ...
'TolFun',1e-6, ...
'TolPCG',0.1, ...
'TolProjCG',1e-2, ...
'TolProjCGAbs',1e-10, ...
'TolX',[], ...
'TypicalX','onesnumberOfVariables,1)', ...
'UseParallel',false ...
);
% If just 'defaults' passed in, return the default options in X
if nargin==1 && nargout <= 1 && strcmpiFUN,'defaults')
X = defaultopt;
return
end
if nargin < 10
options = [];
if nargin < 9
NONLCON = [];
if nargin < 8
UB = [];
if nargin < 7
LB = [];
if nargin < 6
Beq = [];
if nargin < 5
Aeq = [];
end
end
end
end
end
end
problemInput = false;
if nargin == 1
if isaFUN,'struct')
problemInput = true;
[FUN,X,A,B,Aeq,Beq,LB,UB,NONLCON,options] = separateOptimStructFUN);
else % Single input and non-structure.
errormessage'optimlib:fmincon:InputArg'));
end
end
% Prepare the options for the solver
[options, optionFeedback] = prepareOptionsForSolveroptions, 'fmincon');
if nargin < 4 && ~problemInput
errormessage'optimlib:fmincon:AtLeastFourInputs'))
end
if isemptyNONLCON) && isemptyA) && isemptyAeq) && isemptyUB) && isemptyLB)
errormessage'optimlib:fmincon:ConstrainedProblemsOnly'))
end
% Check for non-double inputs
msg = isoptimargdbl'FMINCON', {'X0','A','B','Aeq','Beq','LB','UB'}, ...
X, A, B, Aeq, Beq, LB, UB);
if ~isemptymsg)
error'optimlib:fmincon:NonDoubleInput',msg);
end
if nargout > 4
computeLambda = true;
else
computeLambda = false;
end
activeSet = 'medium-scale: SQP, Quasi-Newton, line-search';
sqp = 'sequential quadratic programming';
trustRegionReflective = 'trust-region-reflective';
interiorPoint = 'interior-point';
[sizes.xRows,sizes.xCols] = sizeX);
XOUT = X:);
sizes.nVar = lengthXOUT);
% Check for empty X
if sizes.nVar == 0
errormessage'optimlib:fmincon:EmptyX'));
end
display = optimgetoptions,'Display',defaultopt,'fast');
flags.detailedExitMsg = ~isemptystrfinddisplay,'detailed'));
switch display
case {'off','none'}
verbosity = 0;
case {'notify','notify-detailed'}
verbosity = 1;
case {'final','final-detailed'}
verbosity = 2;
case {'iter','iter-detailed'}
verbosity = 3;
case 'testing'
verbosity = 4;
otherwise
verbosity = 2;
end
% Set linear constraint right hand sides to column vectors
% in particular, if empty, they will be made the correct
% size, 0-by-1)
B = B:);
Beq = Beq:);
% Check for consistency of linear constraints, before evaluating
% potentially expensive) user functions
% Set empty linear constraint matrices to the correct size, 0-by-n
if isemptyAeq)
Aeq = reshapeAeq,0,sizes.nVar);
end
if isemptyA)
A = reshapeA,0,sizes.nVar);
end
[lin_eq,Aeqcol] = sizeAeq);
[lin_ineq,Acol] = sizeA);
% These sizes checks assume that empty matrices have already been made the correct size
if Aeqcol ~= sizes.nVar
errormessage'optimlib:fmincon:WrongNumberOfColumnsInAeq', sizes.nVar))
end
if lin_eq ~= lengthBeq)
errormessage'optimlib:fmincon:AeqAndBeqInconsistent'))
end
if Acol ~= sizes.nVar
errormessage'optimlib:fmincon:WrongNumberOfColumnsInA', sizes.nVar))
end
if lin_ineq ~= lengthB)
errormessage'optimlib:fmincon:AeqAndBinInconsistent'))
end
% End of linear constraint consistency check
Algorithm = optimgetoptions,'Algorithm',defaultopt,'fast');
% Option needed for processing initial guess
AlwaysHonorConstraints = optimgetoptions,'AlwaysHonorConstraints',defaultopt,'fast');
% Determine algorithm user chose via options. We need this now
% to set OUTPUT.algorithm in case of early termination due to
% inconsistent bounds.)
if strcmpiAlgorithm,'active-set')
OUTPUT.algorithm = activeSet;
elseif strcmpiAlgorithm,'sqp')
OUTPUT.algorithm = sqp;
elseif strcmpiAlgorithm,'interior-point')
OUTPUT.algorithm = interiorPoint;
elseif strcmpiAlgorithm,'trust-region-reflective')
OUTPUT.algorithm = trustRegionReflective;
else
errormessage'optimlib:fmincon:InvalidAlgorithm'));
end
[XOUT,l,u,msg] = checkboundsXOUT,LB,UB,sizes.nVar);
if ~isemptymsg)
EXITFLAG = -2;
[FVAL,LAMBDA,GRAD,HESSIAN] = deal[]);
OUTPUT.iterations = 0;
OUTPUT.funcCount = 0;
OUTPUT.stepsize = [];
if strcmpiOUTPUT.algorithm,activeSet) || strcmpiOUTPUT.algorithm,sqp)
OUTPUT.lssteplength = [];
else % trust-region-reflective, interior-point
OUTPUT.cgiterations = [];
end
if strcmpiOUTPUT.algorithm,interiorPoint) || strcmpiOUTPUT.algorithm,activeSet) || ...
strcmpiOUTPUT.algorithm,sqp)
OUTPUT.constrviolation = [];
end
OUTPUT.firstorderopt = [];
OUTPUT.message = msg;
X:) = XOUT;
if verbosity > 0
dispmsg)
end
return
end
% Get logical list of finite lower and upper bounds
finDiffFlags.hasLBs = isfinitel);
finDiffFlags.hasUBs = isfiniteu);
lFinite = lfinDiffFlags.hasLBs);
uFinite = ufinDiffFlags.hasUBs);
% Create structure of flags and initial values, initialize merit function
% type and the original shape of X.
flags.meritFunction = 0;
initVals.xOrigShape = X;
diagnostics = strcmpioptimgetoptions,'Diagnostics',defaultopt,'fast'),'on');
funValCheck = strcmpioptimgetoptions,'FunValCheck',defaultopt,'fast'),'on');
derivativeCheck = strcmpioptimgetoptions,'DerivativeCheck',defaultopt,'fast'),'on');
% Gather options needed for finitedifferences
% Write checked DiffMaxChange, DiffMinChage, FinDiffType, FinDiffRelStep,
% GradObj and GradConstr options back into struct for later use
options.DiffMinChange = optimgetoptions,'DiffMinChange',defaultopt,'fast');
options.DiffMaxChange = optimgetoptions,'DiffMaxChange',defaultopt,'fast');
if options.DiffMinChange >= options.DiffMaxChange
errormessage'optimlib:fmincon:DiffChangesInconsistent', sprintf '%0.5g', options.DiffMinChange ), sprintf '%0.5g', options.DiffMaxChange )))
end
% Read in and error check option TypicalX
[typicalx,ME] = getNumericOrStringFieldValue'TypicalX','onesnumberOfVariables,1)', ...
onessizes.nVar,1),'a numeric value',options,defaultopt);
if ~isemptyME)
throwME)
end
checkoptionsize'TypicalX', sizetypicalx), sizes.nVar);
options.TypicalX = typicalx;
options.FinDiffType = optimgetoptions,'FinDiffType',defaultopt,'fast');
options = validateFinDiffRelStepsizes.nVar,options,defaultopt);
options.GradObj = optimgetoptions,'GradObj',defaultopt,'fast');
options.GradConstr = optimgetoptions,'GradConstr',defaultopt,'fast');
flags.grad = strcmpioptions.GradObj,'on');
% Notice that defaultopt.Hessian = [], so the variable "hessian" can be empty
hessian = optimgetoptions,'Hessian',defaultopt,'fast');
% If calling trust-region-reflective with an unavailable Hessian option value,
% issue informative error message
if strcmpiOUTPUT.algorithm,trustRegionReflective) && ...
~ isemptyhessian) || strcmpihessian,'on') || strcmpihessian,'user-supplied') || ...
strcmpihessian,'off') || strcmpihessian,'fin-diff-grads') )
errormessage'optimlib:fmincon:BadTRReflectHessianValue'))
end
if ~iscellhessian) && strcmpihessian,'user-supplied') || strcmpihessian,'on') )
flags.hess = true;
else
flags.hess = false;
end
if isemptyNONLCON)
flags.constr = false;
else
flags.constr = true;
end
% Process objective function
if ~isemptyFUN) % will detect empty string, empty matrix, empty cell array
% constrflag in optimfcnchk set to false because we're checking the objective, not constraint
funfcn = optimfcnchkFUN,'fmincon',lengthvarargin),funValCheck,flags.grad,flags.hess,false,Algorithm);
else
errormessage'optimlib:fmincon:InvalidFUN'));
end
% Process constraint function
if flags.constr % NONLCON is non-empty
flags.gradconst = strcmpioptions.GradConstr,'on');
% hessflag in optimfcnchk set to false because hessian is never returned by nonlinear constraint
% function
%
% constrflag in optimfcnchk set to true because we're checking the constraints
confcn = optimfcnchkNONLCON,'fmincon',lengthvarargin),funValCheck,flags.gradconst,false,true);
else
flags.gradconst = false;
confcn = {'','','','',''};
end
[rowAeq,colAeq] = sizeAeq);
if strcmpiOUTPUT.algorithm,activeSet) || strcmpiOUTPUT.algorithm,sqp)
% See if linear constraints are sparse and if user passed in Hessian
if issparseAeq) || issparseA)
warningmessage'optimlib:fmincon:ConvertingToFull', Algorithm))
end
if flags.hess % conflicting options
flags.hess = false;
warningmessage'optimlib:fmincon:HessianIgnoredForAlg', Algorithm));
if strcmpifunfcn{1},'fungradhess')
funfcn{1}='fungrad';
elseif strcmpifunfcn{1},'fun_then_grad_then_hess')
funfcn{1}='fun_then_grad';
end
end
elseif strcmpiOUTPUT.algorithm,trustRegionReflective)
% Look at constraint type and supplied derivatives, and determine if
% trust-region-reflective can solve problem
isBoundedNLP = isemptyNONLCON) && isemptyA) && isemptyAeq); % problem has only bounds and no other constraints
isLinEqNLP = isemptyNONLCON) && isemptyA) && isemptylFinite) ...
&& isemptyuFinite) && colAeq > rowAeq;
if isBoundedNLP && flags.grad
% if only l and u then call sfminbx
elseif isLinEqNLP && flags.grad
% if only Aeq beq and Aeq has more columns than rows, then call sfminle
else
if ~isBoundedNLP && ~isLinEqNLP
errormessage'optimlib:fmincon:ConstrTRR', ...
addLink 'Choosing the Algorithm', 'choose_algorithm' )))
else
% The user has a problem that satisfies the TRR constraint
% restrictions but they haven't supplied gradients.
errormessage'optimlib:fmincon:GradOffTRR', ...
addLink 'Choosing the Algorithm', 'choose_algorithm' )))
end
end
end
lenvlb = lengthl);
lenvub = lengthu);
% Process initial point
shiftedX0 = false; % boolean that indicates if initial point was shifted
if strcmpiOUTPUT.algorithm,activeSet)
%
% Ensure starting point lies within bounds
%
i=1:lenvlb;
lindex = XOUTi)<li);
if anylindex)
XOUTlindex)=llindex);
shiftedX0 = true;
end
i=1:lenvub;
uindex = XOUTi)>ui);
if anyuindex)
XOUTuindex)=uuindex);
shiftedX0 = true;
end
X:) = XOUT;
elseif strcmpiOUTPUT.algorithm,trustRegionReflective)
%
% If components of initial x not within bounds, set those components
% of initial point to a "box-centered" point
%
if isemptyAeq)
arg = u >= 1e10); arg2 = l <= -1e10);
uarg) = inf;
larg2) = -inf;
xinitOutOfBounds_idx = XOUT < l | XOUT > u;
if anyxinitOutOfBounds_idx)
shiftedX0 = true;
XOUT = startxu,l,XOUT,xinitOutOfBounds_idx);
X:) = XOUT;
end
else
% Phase-1 for sfminle nearest feas. pt. to XOUT. Don't print a
% message for this change in X0 for sfminle.
XOUT = feasiblAeq,Beq,XOUT);
X:) = XOUT;
end
elseif strcmpiOUTPUT.algorithm,interiorPoint)
% Variables: fixed, finite lower bounds, finite upper bounds
xIndices = classifyBoundsOnVarsl,u,sizes.nVar,true);
% If honor bounds mode, then check that initial point strictly satisfies the
% simple inequality bounds on the variables and exactly satisfies fixed variable
% bounds.
if strcmpiAlwaysHonorConstraints,'bounds') || strcmpiAlwaysHonorConstraints,'bounds-ineqs')
violatedFixedBnds_idx = XOUTxIndices.fixed) ~= lxIndices.fixed);
violatedLowerBnds_idx = XOUTxIndices.finiteLb) <= lxIndices.finiteLb);
violatedUpperBnds_idx = XOUTxIndices.finiteUb) >= uxIndices.finiteUb);
if anyviolatedLowerBnds_idx) || anyviolatedUpperBnds_idx) || anyviolatedFixedBnds_idx)
XOUT = shiftInitPtToInteriorsizes.nVar,XOUT,l,u,Inf);
X:) = XOUT;
shiftedX0 = true;
end
end
else % SQP
% Classify variables: finite lower bounds, finite upper bounds
xIndices = classifyBoundsOnVarsl,u,sizes.nVar,false);
% SQP always honors the bounds. Check that initial point
% strictly satisfies the bounds on the variables.
violatedLowerBnds_idx = XOUTxIndices.finiteLb) < lxIndices.finiteLb);
violatedUpperBnds_idx = XOUTxIndices.finiteUb) > uxIndices.finiteUb);
if anyviolatedLowerBnds_idx) || anyviolatedUpperBnds_idx)
finiteLbIdx = findxIndices.finiteLb);
finiteUbIdx = findxIndices.finiteUb);
XOUTfiniteLbIdxviolatedLowerBnds_idx)) = lfiniteLbIdxviolatedLowerBnds_idx));
XOUTfiniteUbIdxviolatedUpperBnds_idx)) = ufiniteUbIdxviolatedUpperBnds_idx));
X:) = XOUT;
shiftedX0 = true;
end
end
% Display that x0 was shifted in order to honor bounds
if shiftedX0
if verbosity >= 3
if strcmpiOUTPUT.algorithm,interiorPoint)
fprintfgetStringmessage'optimlib:fmincon:ShiftX0StrictInterior')));
fprintf'\n');
else
fprintfgetStringmessage'optimlib:fmincon:ShiftX0ToBnds')));
fprintf'\n');
end
end
end
% Evaluate function
initVals.g = zerossizes.nVar,1);
HESSIAN = [];
switch funfcn{1}
case 'fun'
try
initVals.f = fevalfunfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:ObjectiveError', ...
getStringmessage'optimlib:fmincon:ObjectiveError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
case 'fungrad'
try
[initVals.f,initVals.g] = fevalfunfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:ObjectiveError', ...
getStringmessage'optimlib:fmincon:ObjectiveError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
case 'fungradhess'
try
[initVals.f,initVals.g,HESSIAN] = fevalfunfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:ObjectiveError', ...
getStringmessage'optimlib:fmincon:ObjectiveError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
case 'fun_then_grad'
try
initVals.f = fevalfunfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:ObjectiveError', ...
getStringmessage'optimlib:fmincon:ObjectiveError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
try
initVals.g = fevalfunfcn{4},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:GradientError', ...
getStringmessage'optimlib:fmincon:GradientError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
case 'fun_then_grad_then_hess'
try
initVals.f = fevalfunfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:ObjectiveError', ...
getStringmessage'optimlib:fmincon:ObjectiveError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
try
initVals.g = fevalfunfcn{4},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:GradientError', ...
getStringmessage'optimlib:fmincon:GradientError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
try
HESSIAN = fevalfunfcn{5},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:HessianError', ...
getStringmessage'optimlib:fmincon:HessianError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
otherwise
errormessage'optimlib:fmincon:UndefinedCallType'));
end
% Check that the objective value is a scalar
if numelinitVals.f) ~= 1
errormessage'optimlib:fmincon:NonScalarObj'))
end
% Check that the objective gradient is the right size
initVals.g = initVals.g:);
if numelinitVals.g) ~= sizes.nVar
error'optimlib:fmincon:InvalidSizeOfGradient', ...
getStringmessage'optimlib:commonMsgs:InvalidSizeOfGradient',sizes.nVar)));
end
% Evaluate constraints
switch confcn{1}
case 'fun'
try
[ctmp,ceqtmp] = fevalconfcn{3},X,varargin{:});
catch userFcn_ME
if strcmpi'MATLAB:maxlhs',userFcn_ME.identifier)
errormessage'optimlib:fmincon:InvalidHandleNonlcon'))
else
optim_ME = MException'optimlib:fmincon:NonlconError', ...
getStringmessage'optimlib:fmincon:NonlconError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
end
initVals.ncineq = ctmp:);
initVals.nceq = ceqtmp:);
initVals.gnc = zerossizes.nVar,lengthinitVals.ncineq));
initVals.gnceq = zerossizes.nVar,lengthinitVals.nceq));
case 'fungrad'
try
[ctmp,ceqtmp,initVals.gnc,initVals.gnceq] = fevalconfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:NonlconError', ...
getStringmessage'optimlib:fmincon:NonlconError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
initVals.ncineq = ctmp:);
initVals.nceq = ceqtmp:);
case 'fun_then_grad'
try
[ctmp,ceqtmp] = fevalconfcn{3},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:NonlconError', ...
getStringmessage'optimlib:fmincon:NonlconError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
initVals.ncineq = ctmp:);
initVals.nceq = ceqtmp:);
try
[initVals.gnc,initVals.gnceq] = fevalconfcn{4},X,varargin{:});
catch userFcn_ME
optim_ME = MException'optimlib:fmincon:NonlconFunOrGradError', ...
getStringmessage'optimlib:fmincon:NonlconFunOrGradError')));
userFcn_ME = addCauseuserFcn_ME,optim_ME);
rethrowuserFcn_ME)
end
case ''
% No nonlinear constraints. Reshaping of empty quantities is done later
% in this file, where both cases, i) no nonlinear constraints and ii)
% nonlinear constraints that have one type missing equalities or
% inequalities), are handled in one place
initVals.ncineq = [];
initVals.nceq = [];
initVals.gnc = [];
initVals.gnceq = [];
otherwise
errormessage'optimlib:fmincon:UndefinedCallType'));
end
% Check for non-double data typed values returned by user functions
if ~isempty isoptimargdbl'FMINCON', {'f','g','H','c','ceq','gc','gceq'}, ...
initVals.f, initVals.g, HESSIAN, initVals.ncineq, initVals.nceq, initVals.gnc, initVals.gnceq) )
error'optimlib:fmincon:NonDoubleFunVal',getStringmessage'optimlib:commonMsgs:NonDoubleFunVal','FMINCON')));
end
sizes.mNonlinEq = lengthinitVals.nceq);
sizes.mNonlinIneq = lengthinitVals.ncineq);
% Make sure empty constraint and their derivatives have correct sizes not 0-by-0):
if isemptyinitVals.ncineq)
initVals.ncineq = reshapeinitVals.ncineq,0,1);
end
if isemptyinitVals.nceq)
initVals.nceq = reshapeinitVals.nceq,0,1);
end
if isemptyinitVals.gnc)
initVals.gnc = reshapeinitVals.gnc,sizes.nVar,0);
end
if isemptyinitVals.gnceq)
initVals.gnceq = reshapeinitVals.gnceq,sizes.nVar,0);
end
[cgrow,cgcol] = sizeinitVals.gnc);
[ceqgrow,ceqgcol] = sizeinitVals.gnceq);
if cgrow ~= sizes.nVar || cgcol ~= sizes.mNonlinIneq
errormessage'optimlib:fmincon:WrongSizeGradNonlinIneq', sizes.nVar, sizes.mNonlinIneq))
end
if ceqgrow ~= sizes.nVar || ceqgcol ~= sizes.mNonlinEq
errormessage'optimlib:fmincon:WrongSizeGradNonlinEq', sizes.nVar, sizes.mNonlinEq))
end
if diagnostics
% Do diagnostics on information so far
diagnose'fmincon',OUTPUT,flags.grad,flags.hess,flags.constr,flags.gradconst,...
XOUT,sizes.mNonlinEq,sizes.mNonlinIneq,lin_eq,lin_ineq,l,u,funfcn,confcn);
end
% Create default structure of flags for finitedifferences:
% This structure will temporarily) ignore some of the features that are
% algorithm-specific e.g. scaling and fault-tolerance) and can be turned
% on later for the main algorithm.
finDiffFlags.fwdFinDiff = strcmpioptions.FinDiffType,'forward');
finDiffFlags.scaleObjConstr = false; % No scaling for now
finDiffFlags.chkFunEval = false; % No fault-tolerance yet
finDiffFlags.chkComplexObj = false; % No need to check for complex values
finDiffFlags.isGrad = true; % Scalar objective
% Check derivatives
if derivativeCheck && ... % User wants to check derivatives...
flags.grad || ... % of either objective or ...
flags.gradconst && sizes.mNonlinEq+sizes.mNonlinIneq > 0) % nonlinear constraint function.
validateFirstDerivativesfunfcn,confcn,X, ...
l,u,options,finDiffFlags,sizes,varargin{:});
end
% call algorithm
if strcmpiOUTPUT.algorithm,activeSet) % active-set
defaultopt.MaxIter = 400; defaultopt.MaxFunEvals = '100*numberofvariables'; defaultopt.TolX = 1e-6;
defaultopt.Hessian = 'off';
problemInfo = []; % No problem related data
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
nlconstfunfcn,X,l,u,fullA),B,fullAeq),Beq,confcn,options,defaultopt, ...
finDiffFlags,verbosity,flags,initVals,problemInfo,optionFeedback,varargin{:});
elseif strcmpiOUTPUT.algorithm,trustRegionReflective) % trust-region-reflective
if strcmpifunfcn{1}, 'fun_then_grad_then_hess') || strcmpifunfcn{1}, 'fungradhess'))
Hstr = [];
elseif strcmpifunfcn{1}, 'fun_then_grad') || strcmpifunfcn{1}, 'fungrad'))
n = lengthXOUT);
Hstr = optimgetoptions,'HessPattern',defaultopt,'fast');
if ischarHstr)
if strcmpiHstr,'sparseonesnumberofvariables))')
Hstr = sparseonesn));
else
errormessage'optimlib:fmincon:InvalidHessPattern'))
end
end
checkoptionsize'HessPattern', sizeHstr), n);
end
defaultopt.MaxIter = 400; defaultopt.MaxFunEvals = '100*numberofvariables'; defaultopt.TolX = 1e-6;
defaultopt.Hessian = 'off';
% Trust-region-reflective algorithm does not compute constraint
% violation as it progresses. If the user requests the output structure,
% we need to calculate the constraint violation at the returned
% solution.
if nargout > 3
computeConstrViolForOutput = true;
else
computeConstrViolForOutput = false;
end
if isemptyAeq)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN] = ...
sfminbxfunfcn,X,l,u,verbosity,options,defaultopt,computeLambda,initVals.f,initVals.g, ...
HESSIAN,Hstr,flags.detailedExitMsg,computeConstrViolForOutput,optionFeedback,varargin{:});
else
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN] = ...
sfminlefunfcn,X,sparseAeq),Beq,verbosity,options,defaultopt,computeLambda,initVals.f, ...
initVals.g,HESSIAN,Hstr,flags.detailedExitMsg,computeConstrViolForOutput,optionFeedback,varargin{:});
end
elseif strcmpiOUTPUT.algorithm,interiorPoint)
defaultopt.MaxIter = 1000; defaultopt.MaxFunEvals = 3000; defaultopt.TolX = 1e-10;
defaultopt.Hessian = 'bfgs';
mEq = lin_eq + sizes.mNonlinEq + nnzxIndices.fixed); % number of equalities
% Interior-point-specific options. Default values for lbfgs memory is 10, and
% ldl pivot threshold is 0.01
options = getIpOptionsoptions,sizes.nVar,mEq,flags.constr,defaultopt,10,0.01);
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrierfunfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
initVals.f,initVals.g,initVals.ncineq,initVals.nceq,initVals.gnc,initVals.gnceq,HESSIAN, ...
xIndices,options,optionFeedback,finDiffFlags,varargin{:});
else % sqp
defaultopt.MaxIter = 400; defaultopt.MaxFunEvals = '100*numberofvariables';
defaultopt.TolX = 1e-6; defaultopt.Hessian = 'bfgs';
% Validate options used by sqp
options = getSQPOptionsoptions,defaultopt,sizes.nVar);
optionFeedback.detailedExitMsg = flags.detailedExitMsg;
% Call algorithm
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpLineSearchfunfcn,X,fullA),fullB),fullAeq),fullBeq), ...
fulll),fullu),confcn,initVals.f,fullinitVals.g),fullinitVals.ncineq),fullinitVals.nceq), ...
fullinitVals.gnc),fullinitVals.gnceq),xIndices,options,finDiffFlags,verbosity,optionFeedback,varargin{:});
end