Skip to content

Commit

Permalink
See #ANT-958 Add Coupling context for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmnz committed Feb 20, 2024
1 parent 3d6990c commit add3654
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/andromede/model/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ class ValueType(Enum):
class ProblemContext(Enum):
operational = 0
investment = 1
coupling = 2
50 changes: 24 additions & 26 deletions src/andromede/simulation/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,21 @@ def _compute_indexing_structure(
return constraint_indexing


def _should_keep_in_model(
problem_type: "OptimizationProblem.Type", context: ProblemContext
) -> bool:
if (problem_type == OptimizationProblem.Type.merged) or (
context == ProblemContext.coupling
):
return True
elif problem_type == OptimizationProblem.Type.master:
return context == ProblemContext.investment
elif problem_type == OptimizationProblem.Type.subproblem:
return context == ProblemContext.operational
else:
return False


def _instantiate_model_expression(
model_expression: ExpressionNode,
component_id: str,
Expand Down Expand Up @@ -667,9 +682,9 @@ class OptimizationProblem:
class Type(Enum):
"""
Class to specify the type of the created problem:
- master: Creates a Xpansion master problem with investment variables and constraints only
- subproblem: Creates Xpansion sub-problems with operational variables and constraints only
- merged: Creates a Antares Simulator/ Xpansion problem with both
- master: Creates a Xpansion master problem with investment and coupling variables and constraints only
- subproblem: Creates Xpansion sub-problems with operational and coupling variables and constraints only
- merged: Creates a Antares Simulator/ Xpansion problem with all
"""

merged = 0
Expand Down Expand Up @@ -732,11 +747,7 @@ def _create_variables(self) -> None:
model = component.model

for model_var in model.variables.values():
if (
self.problem_type == OptimizationProblem.Type.master
and model_var.context == ProblemContext.operational
):
# Xpansion Master Problem only takes investment variables and parameters
if not _should_keep_in_model(self.problem_type, model_var.context):
continue

var_indexing = IndexingStructure(
Expand Down Expand Up @@ -781,18 +792,7 @@ def _create_variables(self) -> None:
def _create_constraints(self) -> None:
for component in self.context.network.all_components:
for constraint in component.model.get_all_constraints():
if (
self.problem_type == OptimizationProblem.Type.master
and constraint.context == ProblemContext.operational
):
# Xpansion Master Problem only takes investment constraints
continue

elif (
self.problem_type == OptimizationProblem.Type.subproblem
and constraint.context == ProblemContext.investment
):
# Xpansion SubProblems only take operational constraints
if not _should_keep_in_model(self.problem_type, constraint.context):
continue

instantiated_expr = _instantiate_model_expression(
Expand Down Expand Up @@ -823,10 +823,9 @@ def _create_objectives(self) -> None:
model = component.model

if (
self.problem_type != OptimizationProblem.Type.master
and model.objective_operational_contribution is not None
model.objective_operational_contribution is not None
and _should_keep_in_model(self.problem_type, ProblemContext.operational)
):
# Xpansion SubProblems only take the operational contribution
_create_objective(
self.solver,
self.context,
Expand All @@ -836,10 +835,9 @@ def _create_objectives(self) -> None:
)

if (
self.problem_type != OptimizationProblem.Type.subproblem
and model.objective_investment_contribution is not None
model.objective_investment_contribution is not None
and _should_keep_in_model(self.problem_type, ProblemContext.investment)
):
# Xpansion Master Problem only takes the investment contribution
_create_objective(
self.solver,
self.context,
Expand Down
5 changes: 3 additions & 2 deletions tests/andromede/test_xpansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

INVESTMENT = ProblemContext.investment
OPERATIONAL = ProblemContext.operational
COUPLING = ProblemContext.coupling

MASTER = OptimizationProblem.Type.master
SUBPBL = OptimizationProblem.Type.subproblem
Expand All @@ -79,7 +80,7 @@ def thermal_candidate() -> Model:
lower_bound=literal(0),
upper_bound=literal(1000),
structure=CONSTANT,
context=INVESTMENT,
context=COUPLING,
),
],
ports=[ModelPort(port_type=BALANCE_PORT_TYPE, port_name="balance_port")],
Expand Down Expand Up @@ -117,7 +118,7 @@ def wind_cluster_candidate() -> Model:
"p_max",
lower_bound=literal(0),
structure=CONSTANT,
context=INVESTMENT,
context=COUPLING,
),
int_variable(
"nb_units",
Expand Down

0 comments on commit add3654

Please sign in to comment.