commonpower.modeling.mip_builder.MIPExpressionBuilder
- class MIPExpressionBuilder(entity: ModelEntity, M: int = 1000.0, eps: float = 1e-05)[source]
Bases:
objectThe expression builder allows to convert logical expression into mixed integer constraints. In the process it also creates all necessary auxiliary variables. The structure of of interface is as follows:
Create expression builder instance.
Generate expressions. Constraints and an output variable are created (or an existing output variable referenced) for each expression. The corresponding ModelElements are internally stored in self.model_elements.
Obtain all generated ModelElements from self.model_elements.
The MIP conversions here are based on
- @article{brown2007formulating,
title={Formulating integer linear programs: A rogues’ gallery}, author={Brown, Gerald G and Dell, Robert F}, journal={INFORMS Transactions on Education}, volume={7}, number={2}, pages={153–159}, year={2007}, publisher={INFORMS}
}
IMPORTANT NOTE: The Integrality Tolerance of the used solver has to be set such that IntFeasTol * M < eps for all the constraints to work correctly.
- Parameters:
entity (ModelEntity) – Entity used to obtain referenced pyomo model elements from.
M (int, optional) – Constant for bigM constraints. Defaults to 1e3.
eps (float, optional) – Slack value for strict inequalities (pyomo only allows for <=, >=, ==). Defaults to 1e-5.
Methods
Generates an always-true constraint for a: val == a
Generates constraints based on: out = 1 if (a and b), out = 0 otherwise.
Generates constraints based on: out = 1 if a >= b, out = 0 otherwise.
Generates constraints based on: out = 1 if a > b, out = 0 otherwise.
Generates a constraint based on: out = 1 - a The MILP formulation is: out == 1 - a
Generates constraints based on: out = 1 if (a or b), out = 0 otherwise.
- _parse_out_var(scenario: ConstraintScenario, var: str | int | float | list[int] | list[float], model: ConcreteModel, t: int) Var | Param | int | float[source]
Helper method to access elements from self.entity’s pyomo model. Enforces that the variable is expanded for robustness.
- Parameters:
scenario (ConstraintScenario) – Scenario to access.
var (Union[str, int, float, list[int], list[float]]) – Either variable name or constant value.
model (ConcreteModel) – Pyomo model to access.
t (int) – Time index.
- Returns:
Union[Union[Var, Param], int, float] – Either pyomo element or constant value.
- _parse_var(scenario: ConstraintScenario, var: str | int | float | list[int] | list[float], model: ConcreteModel, t: int, expand_variable: bool = False) Var | Param | int | float[source]
Helper method to access elements from self.entity’s pyomo model.
- Parameters:
scenario (ConstraintScenario) – Scenario to access.
var (Union[str, int, float, list[int], list[float]]) – Either variable name or constant value.
model (ConcreteModel) – Pyomo model to access.
t (int) – Time index.
expand_variable (bool, optional) – Tells the scenario that this variable should be expanded for robustness. Defaults to False.
- Returns:
Union[Union[Var, Param], int, float] – Either pyomo element or constant value.
- enforce_value(a: str, val: int | float) None[source]
Generates an always-true constraint for a: val == a
- Parameters:
a (str) – Variable 1.
val (Union[int, float]) – value the variable should be set to.
- from_and(a: str, b: str, out: str | None = None, is_new: bool = True) str[source]
Generates constraints based on: out = 1 if (a and b), out = 0 otherwise. The MILP formulation is: out >= a + b - 1 out <= a out <= b
- Parameters:
a (str) – Variable 1 (must be binary).
b (str) – Variable 2 (must be binary).
out (str, optional) – Name of the output variable. If not given, a name is autogenerated.
is_new (bool, optional) – Indicates if the variable is new. If so, a corresponding ModelElement added. Defaults to True.
given (If not)
added. (a name is autogenerated and a corresponding ModelElement)
- Returns:
str – Name of the output variable.
- from_geq(a: str | int | float, b: str | int | float, out: str | None = None, is_new: bool = True, M: int | None = None) str[source]
Generates constraints based on: out = 1 if a >= b, out = 0 otherwise. The MILP formulation using bigM constraints is: a >= b - M*(1-out) a < b + M*out (we use: a + eps <= b + M*out)
- Parameters:
a (Union[str, int, float]) – Left hand side of the inequality.
b (Union[str, int, float]) – Right hand side of the inequality.
out (str, optional) – Name of the output variable. If not given, a name is autogenerated (’aux_’ + 5 hex characters).
is_new (bool, optional) – Indicates if the variable is new. If so, a corresponding ModelElement added. Defaults to True.
M (int, optional) – Constant M for bigM type constraints. If not given, the class’ M is used.
- Returns:
str – Name of the output variable
- from_gt(a: str | int | float, b: str | int | float, out: str | None = None, is_new: bool = True, M: int | None = None) str[source]
Generates constraints based on: out = 1 if a > b, out = 0 otherwise. The MILP formulation using bigM constraints is: a - eps >= b - M*(1-out) a <= b + M*out
- Parameters:
a (Union[str, int, float]) – Left hand side of the inequality.
b (Union[str, int, float]) – Right hand side of the inequality.
out (str, optional) – Name of the output variable. If not given, a name is autogenerated.
is_new (bool, optional) – Indicates if the variable is new. If so, a corresponding ModelElement added. Defaults to True.
M (int, optional) – Constant M for bigM type constraints. If not given, the class’ M is used.
- Returns:
str – Name of the output variable
- from_not(a: str, out: str | None = None, is_new: bool = True) str[source]
Generates a constraint based on: out = 1 - a The MILP formulation is: out == 1 - a
- Parameters:
a (str) – Variable 1 (must be binary)
out (str, optional) – Name of the output variable. If not given, a name is autogenerated.
is_new (bool, optional) – Indicates if the variable is new. If so, a corresponding ModelElement added. Defaults to True.
given (If not)
added. (a name is autogenerated and a corresponding ModelElement)
- Returns:
str – Name of the output variable.
- from_or(a: str, b: str, out: str | None = None, is_new: bool = True) str[source]
Generates constraints based on: out = 1 if (a or b), out = 0 otherwise. The MILP formulation is: out <= a + b out >= a out >= b
- Parameters:
a (str) – Variable 1 (must be binary).
b (str) – Variable 2 (must be binary).
out (str, optional) – Name of the output variable. If not given, a name is autogenerated.
is_new (bool, optional) – Indicates if the variable is new. If so, a corresponding ModelElement added. Defaults to True.
given (If not)
added. (a name is autogenerated and a corresponding ModelElement)
- Returns:
str – Name of the output variable.