Skip to content

Commit

Permalink
fix(commands): remove the binding constraints that are related to the…
Browse files Browse the repository at this point in the history
… area
  • Loading branch information
laurent-laporte-pro committed Mar 13, 2024
1 parent f8302a4 commit cf74115
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions antarest/study/storage/variantstudy/model/command/remove_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def _remove_area_from_links_in_config(self, study_data_config: FileStudyTreeConf
del study_data_config.areas[area_name].links[link]

def _remove_area_from_sets_in_config(self, study_data_config: FileStudyTreeConfig) -> None:
for id, set in study_data_config.sets.items():
if set.areas and self.id in set.areas:
for id_, set_ in study_data_config.sets.items():
if set_.areas and self.id in set_.areas:
with contextlib.suppress(ValueError):
set.areas.remove(self.id)
study_data_config.sets[id] = set
set_.areas.remove(self.id)
study_data_config.sets[id_] = set_

def _apply_config(self, study_data_config: FileStudyTreeConfig) -> Tuple[CommandOutput, Dict[str, Any]]:
del study_data_config.areas[self.id]
Expand Down Expand Up @@ -78,21 +78,50 @@ def _remove_area_from_links(self, study_data: FileStudy) -> None:
)

def _remove_area_from_binding_constraints(self, study_data: FileStudy) -> None:
binding_constraints = study_data.tree.get(["input", "bindingconstraints", "bindingconstraints"])
"""
Remove the binding constraints that are related to the area.
id_to_remove = {bc_id for bc_id, bc in binding_constraints.items() for key in bc if self.id in key}
Notes:
A binding constraint has properties, a list of terms (which form a linear equation) and
a right-hand side (which is the matrix of the binding constraint).
The terms are of the form `area1%area2` or `area.cluster` where `area` is the ID of the area
and `cluster` is the ID of the cluster.
for bc_id in id_to_remove:
if study_data.config.version < 870:
study_data.tree.delete(["input", "bindingconstraints", binding_constraints[bc_id]["id"]])
else:
for name in ["lt", "gt", "eq"]:
study_data.tree.delete(
["input", "bindingconstraints", f"{binding_constraints[bc_id]['id']}_{name}"]
)
del binding_constraints[bc_id]
When an area is removed, it has an impact on the terms of the binding constraints.
At first, we could decide to remove the terms that are related to the area.
However, this would lead to a linear equation that is not valid anymore.
study_data.tree.save(binding_constraints, ["input", "bindingconstraints", "bindingconstraints"])
Instead, we decide to remove the binding constraints that are related to the area.
"""
# noinspection SpellCheckingInspection
url = ["input", "bindingconstraints", "bindingconstraints"]
binding_constraints = study_data.tree.get(url)

# Collect the binding constraints that are related to the area to remove
# by searching the terms that contain the ID of the area.
bc_to_remove = {}
for bc_index, bc in list(binding_constraints.items()):
for key in bc:
# Term IDs are in the form `area1%area2` or `area.cluster`
if "%" in key:
related_areas = key.split("%")
elif "." in key:
related_areas = key.split(".")[:-1]
else:
# This key belongs to the set of properties, it isn't a term ID, so we skip it
continue
if self.id.lower() in related_areas:
bc_to_remove[bc_index] = binding_constraints.pop(bc_index)
break

matrix_suffixes = ["_lt", "_gt", "_eq"] if study_data.config.version >= 870 else [""]

for bc_index, bc in bc_to_remove.items():
for suffix in matrix_suffixes:
# noinspection SpellCheckingInspection
study_data.tree.delete(["input", "bindingconstraints", f"{bc['id']}{suffix}"])

study_data.tree.save(binding_constraints, url)

def _remove_area_from_hydro_allocation(self, study_data: FileStudy) -> None:
"""
Expand Down

0 comments on commit cf74115

Please sign in to comment.