decogo.model.model_decomposer¶
This module performs automatic reformulation of the Pyomo model.
Classes
|
This class performs the decomposition of the Pyomo model based on the defined blocks. |
- class decogo.model.model_decomposer.PyomoModelDecomposer(model, settings)[source]¶
This class performs the decomposition of the Pyomo model based on the defined blocks. If for definition of Pyomo model keyword Block was used explicitly, then the reformulation is not performed.
- Parameters:
model (ConcreteModel) – Input pyomo model
settings (Settings) – Settings
- decompose()[source]¶
The function which calls other functions for block detection and reformulation. Reformulates Pyomo model as block-separable with linear objective, with nonlinear local constraints and linear global constraints.
- Returns:
Reformulated model, variables (string names) collected blockwise, original objective sense
- Return type:
(ConcreteModel, list, int)
- _find_connected_components()[source]¶
Construct the graph which describes the sparsity pattern of the Hessian of the Lagrangian. Two nodes are connected if the corresponding variables are contained in the same nonlinear expression.
- _add_edges_to_graph(g, expr, nonlinear_vars)[source]¶
Recursive function for construction of the sparsity graph. The two nodes are connected if respective variables contained in the same nonlinear expression.
- Parameters:
g (Graph) – Graph
expr (Expression) – Expression which is currently processed
nonlinear_vars (set) – List of nonlinear variables
- _compute_initial_statistics()[source]¶
Compute initial statistics such as number of variables and constraints
- _preprocessing()[source]¶
Performs the following preprocessing steps:
deletes trivial constraints (i.e. \(x \leq a\)) and updates the variables bounds;
checks if objective sense is \(\max\); if so, then \(\max f(x) = \min -f(x)\)
estimates bounds of unbounded variables, if the respective setting is set to
True
- _get_blocks()[source]¶
Identifies the blocks if the there are more defined blocks than one (by default in Pyomo each model has at least one block always), i.e. it is used explicitly keyword Block. The variables which appear only in linear blocks are merged into one block.
- _add_copy_constraints()[source]¶
Adds copy constraints in the following case:
If some nonlinear constraints have not all variables in the same block, then the common variable for these constraints is identified. For this variable, a new variable and corresponding copy constraint is created.In the expression trees of the nonlinear constraints, a common variable is replaced by the new variable
- _merge_linear_blocks()[source]¶
Merges linear blocks into one block, nonlinear are left as they are
- Raises:
ValueError – If variable of nonlinear constraint belongs to different blocks
- _compute_blocks()[source]¶
Calls functions depending on the number of defined blocks. If there is only one block detected, then is constructs the sparsity graph depending on the expressions. If there are more than one block, then it simply reads the blocks as defined.
- _determine_block_expr(expr)[source]¶
Assigns the block identifier to the given expression. If the variables in the expression are contained in other blocks, it returns also this blocks
- Parameters:
expr (Expression) – Expression which needs to be assigned to the blocks
- Returns:
Block identifier -> variable names dictionary. If length of dictionary is greater than 1, it means that variables of the expression are contained in the several blocks
- Return type:
dict
- _reformulate()[source]¶
Performs block-separable reformulation for the case if one block is identified. For that case it computes the blocks based on the nonlinear expressions
- _find_separable_sub_expr_and_replace(expr)[source]¶
Recursive function for walking through the expression trees and substituting the nonlinear expressions which belong to the one block by a new variable
- Parameters:
expr (ProductExpression, SumExpression, UnaryFunctionExpression, PowExpression, DivisionExpression, NegationExpression, AbsExpression, MonomialTermExpression, Var) – Expression which is processed
- Returns:
Block id to which this expression belongs. For some subexpressions that should not be substituted None is returned.
- Return type:
int, None
- _compute_number_of_defined_blocks()[source]¶
Gets the number of blocks in the model. This includes also root block (any Pyomo model has at least one block).
- static _comp_bounds_new_vars(expr)[source]¶
Computes bounds of new variables based on expression
- Parameters:
expr (Expression) – Expression for which the bounds are computed
- Returns:
Upper and lower bound of the expression
- Return type:
(int, int)
- _create_new_var(lower, upper, block_id)[source]¶
Creates a new variable and adds it to the block list