Package pypower :: Module opf_model :: Class opf_model
[hide private]
[frames] | no frames]

Class opf_model

source code

object --+
         |
        opf_model

This class implements the OPF model object used to encapsulate a given OPF problem formulation. It allows for access to optimization variables, constraints and costs in named blocks, keeping track of the ordering and indexing of the blocks as variables, constraints and costs are added to the problem.


Authors:
Ray Zimmerman (PSERC Cornell), Richard Lincoln
Instance Methods [hide private]
 
__init__(self, ppc)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__repr__(self)
String representation of the object.
source code
 
add_constraints(self, name, AorN, l, u=None, varsets=None)
Adds a set of constraints to the model.
source code
 
add_costs(self, name, cp, varsets)
Adds a set of user costs to the model.
source code
 
add_vars(self, name, N, v0=None, vl=None, vu=None)
Adds a set of variables to the model.
source code
 
build_cost_params(self)
Builds and saves the full generalized cost parameters.
source code
 
compute_cost(self, x, name=None)
Computes a user-defined cost.
source code
 
get_cost_params(self, name=None)
Returns the cost parameter struct for user-defined costs.
source code
 
get_idx(self)
Returns the idx struct for vars, lin/nln constraints, costs.
source code
 
get_ppc(self)
Returns the PYPOWER case dict.
source code
 
getN(self, selector, name=None)
Returns the number of variables, constraints or cost rows.
source code
 
getv(self, name=None)
Returns initial value, lower bound and upper bound for opt variables.
source code
 
linear_constraints(self)
Builds and returns the full set of linear constraints.
source code
 
userdata(self, name, val=None)
Used to save or retrieve values of user data.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
  ppc
PYPOWER case dict used to build the object.
  var
data for optimization variable sets that make up the
  nln
data for nonlinear constraints that make up the
  lin
data for linear constraints that make up the
  cost
data for user-defined costs
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, ppc)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

String representation of the object.

Overrides: object.__repr__

add_constraints(self, name, AorN, l, u=None, varsets=None)

source code 

Adds a set of constraints to the model.

Linear constraints are of the form l <= A * x <= u, where x is a vector made of of the vars specified in varsets (in the order given). This allows the A matrix to be defined only in terms of the relevant variables without the need to manually create a lot of zero columns. If varsets is empty, x is taken to be the full vector of all optimization variables. If l or u are empty, they are assumed to be appropriately sized vectors of -Inf and Inf, respectively.

For nonlinear constraints, the 3rd argument, N, is the number of constraints in the set. Currently, this is used internally by PYPOWER, but there is no way for the user to specify additional nonlinear constraints.

add_costs(self, name, cp, varsets)

source code 

Adds a set of user costs to the model.

Adds a named block of user-defined costs to the model. Each set is defined by the cp dict described below. All user-defined sets of costs are combined together into a single set of cost parameters in a single cp dict by build_cost_params. This full aggregate set of cost parameters can be retrieved from the model by get_cost_params.

Let x refer to the vector formed by combining the specified varsets, and f_u(x, cp) be the cost at x corresponding to the cost parameters contained in cp, where cp is a dict with the following fields:

   N      - nw x nx sparse matrix
   Cw     - nw x 1 vector
   H      - nw x nw sparse matrix (optional, all zeros by default)
   dd, mm - nw x 1 vectors (optional, all ones by default)
   rh, kk - nw x 1 vectors (optional, all zeros by default)

These parameters are used as follows to compute f_u(x, CP):

   R  = N*x - rh

           /  kk(i),  R(i) < -kk(i)
   K(i) = <   0,     -kk(i) <= R(i) <= kk(i)
           \ -kk(i),  R(i) > kk(i)

   RR = R + K

   U(i) =  /  0, -kk(i) <= R(i) <= kk(i)
           \  1, otherwise

   DDL(i) = /  1, dd(i) = 1
            \  0, otherwise

   DDQ(i) = /  1, dd(i) = 2
            \  0, otherwise

   Dl = diag(mm) * diag(U) * diag(DDL)
   Dq = diag(mm) * diag(U) * diag(DDQ)

   w = (Dl + Dq * diag(RR)) * RR

   f_u(x, CP) = 1/2 * w'*H*w + Cw'*w

add_vars(self, name, N, v0=None, vl=None, vu=None)

source code 

Adds a set of variables to the model.

Adds a set of variables to the model, where N is the number of variables in the set, v0 is the initial value of those variables, and vl and vu are the lower and upper bounds on the variables. The defaults for the last three arguments, which are optional, are for all values to be initialized to zero (v0 = 0) and unbounded (VL = -Inf, VU = Inf).

build_cost_params(self)

source code 

Builds and saves the full generalized cost parameters.

Builds the full set of cost parameters from the individual named sub-sets added via add_costs. Skips the building process if it has already been done, unless a second input argument is present.

These cost parameters can be retrieved by calling get_cost_params and the user-defined costs evaluated by calling compute_cost.

compute_cost(self, x, name=None)

source code 

Computes a user-defined cost.

Computes the value of a user defined cost, either for all user defined costs or for a named set of costs. Requires calling build_cost_params first to build the full set of parameters.

Let x be the full set of optimization variables and f_u(x, cp) be the user-defined cost at x, corresponding to the set of cost parameters in the cp dict returned by get_cost_params, where cp is a dict with the following fields:

   N      - nw x nx sparse matrix
   Cw     - nw x 1 vector
   H      - nw x nw sparse matrix (optional, all zeros by default)
   dd, mm - nw x 1 vectors (optional, all ones by default)
   rh, kk - nw x 1 vectors (optional, all zeros by default)

These parameters are used as follows to compute f_u(x, cp):

   R  = N*x - rh

           /  kk(i),  R(i) < -kk(i)
   K(i) = <   0,     -kk(i) <= R(i) <= kk(i)
           \ -kk(i),  R(i) > kk(i)

   RR = R + K

   U(i) =  /  0, -kk(i) <= R(i) <= kk(i)
           \  1, otherwise

   DDL(i) = /  1, dd(i) = 1
            \  0, otherwise

   DDQ(i) = /  1, dd(i) = 2
            \  0, otherwise

   Dl = diag(mm) * diag(U) * diag(DDL)
   Dq = diag(mm) * diag(U) * diag(DDQ)

   w = (Dl + Dq * diag(RR)) * RR

   F_U(X, CP) = 1/2 * w'*H*w + Cw'*w

get_cost_params(self, name=None)

source code 

Returns the cost parameter struct for user-defined costs.

Requires calling build_cost_params first to build the full set of parameters. Returns the full cost parameter struct for all user-defined costs that incorporates all of the named cost sets added via add_costs, or, if a name is provided it returns the cost dict corresponding to the named set of cost rows (N still has full number of columns).

The cost parameters are returned in a dict with the following fields:

   N      - nw x nx sparse matrix
   Cw     - nw x 1 vector
   H      - nw x nw sparse matrix (optional, all zeros by default)
   dd, mm - nw x 1 vectors (optional, all ones by default)
   rh, kk - nw x 1 vectors (optional, all zeros by default)

get_idx(self)

source code 

Returns the idx struct for vars, lin/nln constraints, costs.

Returns a structure for each with the beginning and ending index value and the number of elements for each named block. The 'i1' field (that's a one) is a dict with all of the starting indices, 'iN' contains all the ending indices and 'N' contains all the sizes. Each is a dict whose keys are the named blocks.

Examples:

   [vv, ll, nn] = get_idx(om)

For a variable block named 'z' we have:

       vv['i1']['z'] - starting index for 'z' in optimization vector x
       vv['iN']['z'] - ending index for 'z' in optimization vector x
       vv["N"]    - number of elements in 'z'

To extract a 'z' variable from x:

       z = x(vv['i1']['z']:vv['iN']['z'])

To extract the multipliers on a linear constraint set named 'foo', where mu_l and mu_u are the full set of linear constraint multipliers:

       mu_l_foo = mu_l(ll['i1']['foo']:ll['iN']['foo'])
       mu_u_foo = mu_u(ll['i1']['foo']:ll['iN']['foo'])

The number of nonlinear constraints in a set named 'bar':

       nbar = nn["N"].bar

(note: the following is preferable :

       nbar = getN(om, 'nln', 'bar')

... if you haven't already called get_idx to get nn.)

getN(self, selector, name=None)

source code 

Returns the number of variables, constraints or cost rows.

Returns either the total number of variables/constraints/cost rows or the number corresponding to a specified named block.

Examples:

   N = getN(om, 'var')         : total number of variables
   N = getN(om, 'lin')         : total number of linear constraints
   N = getN(om, 'nln')         : total number of nonlinear constraints
   N = getN(om, 'cost')        : total number of cost rows (in N)
   N = getN(om, 'var', name)   : number of variables in named set
   N = getN(om, 'lin', name)   : number of linear constraints in named set
   N = getN(om, 'nln', name)   : number of nonlinear cons. in named set
   N = getN(om, 'cost', name)  : number of cost rows (in N) in named set

getv(self, name=None)

source code 

Returns initial value, lower bound and upper bound for opt variables.

Returns the initial value, lower bound and upper bound for the full optimization variable vector, or for a specific named variable set.

Examples:

   x, xmin, xmax = getv(om)
   Pg, Pmin, Pmax = getv(om, 'Pg')

linear_constraints(self)

source code 

Builds and returns the full set of linear constraints.

Builds the full set of linear constraints based on those added by add_constraints:

   L <= A * x <= U

userdata(self, name, val=None)

source code 

Used to save or retrieve values of user data.

This function allows the user to save any arbitrary data in the object for later use. This can be useful when using a user function to add variables, constraints, costs, etc. For example, suppose some special indexing is constructed when adding some variables or constraints. This indexing data can be stored and used later to "unpack" the results of the solved case.