mp.sm_nln_constraint
- class mp.sm_nln_constraint
Bases:
mp.set_manager_opt_modelmp.sm_nln_constraint- MP Set Manager class for nonlinear constraints.nln = mp.sm_nln_constraint() nln = mp.sm_nln_constraint(label)
MP Set Manager class for nonlinear constraints
(13)\[\g(\x) = 0\]or
(14)\[\g(\x) \le 0\]Manages nonlinear constraint sets and their indexing.
By convention,
nln(general),nle(equality), ornli(inequality) are the variable names used formp.sm_nln_constraintobjects.- mp.sm_nln_constraint Methods:
sm_nln_constraint()- constructoradd()- add a subset of nonlinear constraintsparams()- return nonlinear constraint parametersset_params()- modify nonlinear constraint parameter dataeval()- evaluate individual or full set of nonlinear constraintseval_hess()- evaluate “Hessian” for full set of nonlinear constraintsdisplay_soln()- display solution values for nonlinear constraintsget_soln()- fetch solution values for specific named/indexed subsetsparse_soln()- parse solution for nonlinear constraints
See also
mp.set_manager,mp.set_manager_opt_model.- Constructor Summary
- sm_nln_constraint(varargin)
Constructor.
nln = mp.sm_nln_constraint(label)
- Method Summary
- add(var, name, idx, varargin)
Add a subset of nonlinear constraints.
nln.add(var, name, N, fcn, hess); nln.add(var, name, N, fcn, hess, vs); nln.add(var, name, idx_list, N, fcn, hess); nln.add(var, name, idx_list, N, fcn, hess, vs);
Add a named, and possibly indexed, subset of nonlinear constraints \(\g(\x) = 0\) or \(\g(\x) \le 0\) to the set, where \(\x\) is an \(n_x \times 1\) vector made up of the variables specified in the optional
vs(in the order given). This allows the constraint function to be defined in terms of only the relevant variables without the need to manually account for the locations of other variable sets.- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectname (char array or cell array) – name(s) of subset/block of constraints to
add()idx_list (cell array) – (optional) index list for subset/block of constraints to
add()(for an indexed subset)N (integer) – number of constraints in the set, i.e. the dimension \(n\) of constraint function \(\g(\x)\)
fcn (function handle) – handle to function that evaluates the \(n \times 1\) constraint vector \(\g(\x)\), and optionally the corresponding \(n \times n_x\) Jacobian \(\g_\x = \der{\g}{\x}\)
hess (function handle) – handle to function that evaluates the constraint “Hessian” \(\g_{\x\x}(\lam)\) as described below
vs (cell or struct array) – (optional, default
{}) variable set defining vector \(\x\) for this constraint subset; can be either a cell array of names of variable subsets, or a struct array ofname,idxpairs of indexed named subsets of variables; order ofvsdetermines order of blocks in \(\x\); if empty, \(\x\) is assumed to be the full variable vector
Constraint Function Implmentation :
fcnFor a constraint function \(\g(\x)\), the
fcninput should point to a function with the following interface:g = fcn(x) [g, dg] = fcn(x)
- Input:
x (double or cell array of doubles) – variable \(\x\) corresponding to the full variable vector, or to a set of sub-vectors defined by the variable set provided in
vs- Outputs:
g (double) – \(n \times 1\) constraint vector \(\g(\x)\)
dg (double) – \(n \times n_x\) constraint Jacobian \(\g_\x = \der{\g}{\x}\)
Note
The
dgreturn value is the transpose of what is expected from an input function fornlps_master()and friends.
Hessian Function Implmentation :
hessSimilarly, the
hessinput should point to a function that returns an \(n_x \times n_x\) matrix constraint “Hessian” matrix \(\g_{\x\x}(\lam)\) for a given set of multipliers, with the following interface:d2g = hess(x, lam)
- Inputs:
x (double or cell array of doubles) – variable \(\x\) corresponding to the full variable vector, or to a set of sub-vectors defined by the variable set provided in
vslam (double) – \(n \times 1\) vector of multipliers \(\lam\)
- Output:
d2g (double) – \(n_x \times n_x\) “Hessian” matrix \(\g_{\x\x}(\lam) = \der{}{\x}(\trans{\g_\x} \lam)\)
For both functions, the input argument
xtakes one of two forms. If the constraint set is added with varset inputvsempty or missing, thenxwill be the full variable vector. Otherwise it will be a cell array of vectors corresponding to the variable sets specified invs.Special Case
For simple (not indexed) named sets,
namecan be a cell array of constraint set names, in which caseNis a vector specifying the number of constraints in each corresponding set.fcnandhessare each still a single function handle, but the values computed by each correspond to the entire stacked collection of constraint sets together, as if they were a single set.Likewise, if
fcnorhessare empty, it indicates a placeholder in the indexing for a constraint set whose implementation is included in another constraint set. This functionality is only intended to be used internally to handle constraint/gradient and Hessian functions that compute the values for more than one constraint set simultaneously.Examples:
% nonlinear equality constraint with constraint/gradient and % Hessian evaluation functions provided nle.add('var, Qmis', nb, 1, fcn, hess); % nonlinear inequality constraints with indexed named set 'S(i,j)' nli.init_indexed_name('S', {2, 3}); for i = 1:2 for j = 1:3 nli.add('S', {i, j}, N{i,j}, 0, ...); end end
See also
params(),set_params(),eval().
- params(var, name, idx)
Return nonlinear constraint parameters.
N = nln.params(var, name) N = nln.params(var, name, idx_list) [N, fcn] = nln.params(...) [N, fcn, hess] = nln.params(...) [N, fcn, hess, vs] = nln.params(...) [N, fcn, hess, vs, include] = nln.params(...)
Returns the parameters for the general nonlinear constraint subset corresponding to the name or name and index list provided. The parameters are those supplied via the add method when the subset was added.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectname (char array) – name of subset
idx_list (cell array) – (optional) index list for subset
- Outputs:
N (integer) – number of constraints in the set, i.e. the dimension \(n\) of constraint function \(\g(\x)\)
fcn (function handle) – handle to function that evaluates the \(n \times 1\) constraint vector \(\g(\x)\), and optionally the corresponding \(n \times n_x\) Jacobian \(\g_\x = \der{\g}{\x}\)
hess (function handle) – handle to function that evaluates the constraint “Hessian” \(\g_{\x\x}(\lam)\); see
add()for detailsvs (struct array) – variable set,
name,idxpairs specifying the set of variables defining vector \(\x\) for this cost subset; order ofvsdetermines order of blocks in \(\x\)include (struct) – (optional) for constraint sets whose functions compute the constraints for additional sets, struct with fields:
name- cell array of additional set names computedN- array of corresponding dimensions
- set_params(var, name, idx, params, vals)
Modify nonlinear constraint parameter data.
nln.set_params(var, name, params, vals) nln.set_params(var, name, idx_list, params, vals)
This method can be used to modify parameters for an existing subset of nonlinear constraints.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectname (char array) – name of subset/block of nonlinear constraints to modify
idx_list (cell array) – (optional) index list for subset/block of nonlinear constraints to modify (for an indexed subset)
params – can be one of three options:
'all'- indicates thatvalsis a cell array whose elements correspond to the input parameters of theadd()methodname of a parameter -
valis the value of that parametercell array of parameter names -
valsis a cell array of corresponding values
vals – new value or cell array of new values corresponding to
params
Valid parameter names are
N,fcn,hess,vs.Examples:
nln.set_params(var, 'y', {2,3}, {'fcn', 'hess'}, {fcn, hess}); nln.set_params(var, 'Pmis', 'all', {N, fcn, hess, vs});
- eval(var, x, name, idx)
Evaluate individual or full set of nonlinear constraints.
g = nln.eval(var, x) g = nln.eval(var, x, name) g = nln.eval(var, x, name, idx_list) [g, dg] = nln.eval(...)
For a given value of the variable vector \(\x\), this method evaluates the nonlinear constraint function and optionally its derivatives for an individual subset, if name or name and index list are provided, otherwise, for the full set of constraints.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectx (double) – full \(n_x \times 1\) variable vector \(\x\)
name (char array) – name of subset/block of nonlinear constraints to evaluate
idx_list (cell array) – (optional) index list for subset/block of nonlinear constraints to evaluate (for an indexed subset)
- Outputs:
g (double) – \(n \times 1\) constraint vector \(\g(\x)\)
dg (double) – \(n \times n_x\) constraint Jacobian \(\g_\x = \der{\g}{\x}\)
- eval_hess(var, x, lam, name, idx)
Evaluate “Hessian” for full set of nonlinear constraints.
d2g = nln.eval_hess(var, x, lam) % not yet implemented d2g = nln.eval_hess(var, x, lam, name) d2g = nln.eval_hess(var, x, lam, name, idx_list)
For a given value of the variable vector \(\x\), this method evaluates the nonlinear constraint “Hessian” for the full set of constraints.
Instead of evaluating the full 3 dimensional Hessian, it actually evaluates the Jacobian of the vector formed by multiplying the transpose of the constraint Jacobian by a vector \(\lam\) of multipliers.
Note
Evaluation of Hessian for individual subsets not yet implemented.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectx (double) – full \(n_x \times 1\) variable vector \(\x\)
lam (double) – \(n \times 1\) vector of multipliers \(\lam\)
name (char array) – (optional, and not yet implemented) name of subset/block of nonlinear constraints to evaluate
idx_list (cell array) – (optional, and not yet implemente) index list for subset/block of nonlinear constraints to evaluate (for an indexed subset)
- Outputs:
d2g (double) – \(n_x \times n_x\) “Hessian” matrix \(\g_{\x\x}(\lam) = \der{}{\x}(\trans{\g_\x} \lam)\)
- display_soln(var, soln, iseq, varargin)
Display solution values for nonlinear constraints.
nln.display_soln(var, soln, iseq) nln.display_soln(var, soln, iseq, name) nln.display_soln(var, soln, iseq, name, idx_list) nln.display_soln(var, soln, iseq, fid) nln.display_soln(var, soln, iseq, fid, name) nln.display_soln(var, soln, iseq, fid, name, idx_list)
Displays the solution values for all nonlinear constraints (default) or an individual named or named/indexed subset.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectsoln (struct) – full solution struct with these fields (among others):
eflag- exit flag, 1 = success, 0 or negative = solver-specific failure codex- variable valueslambda- constraint shadow prices, struct with fields:eqnonlin- nonlinear equality constraintsineqnonlin- nonlinear inequality constraintsmu_l- linear constraint lower boundsmu_u- linear constraint upper boundslower- variable lower boundsupper- variable upper bounds
iseq (logical) – true for equality constraints, false for inequality constraints
fid (fileID) – fileID of open file to write to (default is 1 for standard output)
name (char array) – (optional) name of individual subset
idx_list (cell array) – (optional) indices of individual subset
- get_soln(var, soln, iseq, varargin)
Fetch solution values for specific named/indexed subsets.
vals = nln.get_soln(var, soln, iseq, name) vals = nln.get_soln(var, soln, iseq, name, idx_list) vals = nln.get_soln(var, soln, iseq, tags, name) vals = nln.get_soln(var, soln, iseq, tags, name, idx_list)
Returns named/indexed nonlinear constraint results for a solved model, evaluated at the solution found.
- Inputs:
var (
mp.sm_variable) – correspondingmp.sm_variableobjectsoln (struct) – full solution struct with these fields (among others):
eflag- exit flag, 1 = success, 0 or negative = solver-specific failure codex- variable valueslambda- constraint shadow prices, struct with fields:eqnonlin- nonlinear equality constraintsineqnonlin- nonlinear inequality constraintsmu_l- linear constraint lower boundsmu_u- linear constraint upper boundslower- variable lower boundsupper- variable upper bounds
iseq (logical) – true for equality constraints, false for inequality constraints
tags (char array or cell array of char arrays) – names of desired outputs, default is
{'g', 'lam', 'dg'}with valid values:'g'or'h'- constraint value \(\g(\x)\)'lam'or'mu'- shadow price \(\lam\) on constraint'dg'or'dh'- constraint Jacobian \(\g_\x = \der{\g}{\x}\)
name (char array) – name of the subset
idx_list (cell array) – (optional) indices of the subset
- Outputs:
Variable number of outputs corresponding to
tagsinput. Iftagsis empty or not specified, the calling context will define the number of outputs, returned in order of default tags.
Example:
[g, lam, dg] = nln.get_soln(var, soln, false, 'flow'); dg_Pmis_5_3 = nln.get_soln(var, soln, true, 'dg', 'Pmis', {5,3});
For a complete set of solution values, using the
parse_soln()method may be more efficient.See also
parse_soln().
- parse_soln(soln, iseq, stash)
Parse solution for nonlinear constraints.
ps = nln.parse_soln(soln, iseq)
Parse a full solution struct into parts corresponding to individual nonlinear constraint subsets.
- Inputs:
soln (struct) – full solution struct with these fields (among others):
x- variable valueslambda- constraint shadow prices, struct with fields:eqnonlin- nonlinear equality constraintsineqnonlin- nonlinear inequality constraintsmu_l- linear constraint lower boundsmu_u- linear constraint upper boundslower- variable lower boundsupper- variable upper bounds
iseq (logical) – true for equality constraints, false for inequality constraints
stash (logical) – if true, store return value in
solnproperty
- Output:
ps (struct) – parsed solution, struct where each field listed below is a struct whos names are the names of the relevant nonlinear constraint subsets and values are scalars for named sets, arrays for named/indexed sets:
lam- equality constraint shadow pricesmu- inequality constraint shadow prices