7.2 Callback Functions
The second method, based on defining a set of callback functions, offers several distinct
advantages, especially for more complex scenarios or for adding a feature for others to use,
such as the zonal reserve requirement or the interface flow limits mentioned previously.
This approach makes it possible to:
- define and access variable/constraint/cost sets as individual named blocks
- define constraints, costs only in terms of variables directly involved
- pre-process input data and/or post-process result data
- print and save new result data
- simultaneously use multiple, independently developed extensions (e.g. zonal
reserve requirements and interface flow limits)
With this approach the OPF formulation is modified in the formulation callback,
which is described and illustrated below in Section 7.3.2. The modifications to the
formulation are handled by adding variables, costs and/or constraints to the OPF Model
object (om) using one of the add_* methods. Please see the documentation for each of these
methods for more details.
7.2.1 User-defined Variables
Additional variables are added using the add_var method.
om.add_var('myV', N, myV0, myV_min, myV_max);
Here myV is the name of the variable set, N the number of variables being added (i.e. this is an
vector being appended to the current optimization variable ), and myV0,
myV_min, myV_max are vectors of initial values, lower bounds and upper bounds,
respectively.
7.2.2 User-defined Costs
- Quadratic Costs are added using the add_quad_cost method.
om.add_quad_cost('myQcost', Q, c, k, varsets);
Here myQcost is the name of the cost set, Q, c and k are the , and
parameters from (6.47), respectively, and var_sets is an optional cell array of
variable set names. If var_sets is provided, the dimensions of Q and c will
correspond, not to the full optimization vector , but to the subvector formed by
stacking only the variable sets specified in var_sets.
- General Nonlinear Costs are added using the add_nln_cost method.
om.add_nln_cost('myNonlinCost', 1, fcn, varsets);
Here myNonlinCost is the name of the cost set, fcn is a function handle and var_sets
is an optional cell array of variable set names. The function handle fcn must
point to a function that implements the function from (6.46),
returning the value of the function, along with its gradient and Hessian.
This function has a format similar to that required by MIPS for its cost
function.
If var_sets is provided, instead of the full optimization vector , the x passed to
the function fcn will be a cell array of sub-vectors of corresponding to the
specified variable sets.
- Legacy Costs are added using the add_legacy_cost method.
om.add_legacy_cost('myLegacyCost', cp, varsets);
Here myLegacyCost is the name of the cost set, cp is a struct containing the cost
parameters , , , , , and from (6.48)–(6.51) in fields H, Cw, N,
rh, kk, dd and mm, respectively, and var_sets is an optional cell array of variable set
names. If var_sets is provided, the number of columns in N will correspond, not to
the full optimization vector , but to the subvector formed by stacking only the
variable sets specified in var_sets.
7.2.3 User-defined Constraints
- Linear Constraints are added using the add_lin_constraint method.
om.add_lin_constraint('myLinCons', A, l, u, varsets);
Here myLinCons is the name of the constraint set, A, l and u correspond to additional
rows to add to the , and parameters in (6.38), respectively, and var_sets is
an optional cell array of variable set names. If var_sets is provided, the number
of columns in A will correspond, not to the full optimization vector ,
but to the subvector formed by stacking only the variable sets specified in
var_sets.
- Nonlinear Constraints are added using the add_nln_constraint method.
om.add_nln_constraint('myNonlinCons', N, iseq, fcn, hess, varsets);
Here myNonlinCons is the name of the constraint set, N is the number of constraints
being added, iseq is a true or false value indicating whether this is a set of equality
(or else inequality) constraints, fcn and hess are function handles and var_sets is an
optional cell array of variable set names. The function handle fcn must point to a
function that implements the corresponding constraint function or
and its gradients, and hess to one that evaluates the corresponding
term in the Hessian of the Lagrangian function. This function has a format
similar to that required by MIPS for its cost function. See more details in
Section 7.1.2.
If var_sets is provided, instead of the full optimization vector , the x passed to
the function fcn will be a cell array of sub-vectors of corresponding to the
specified variable sets.