Skip to content

Commit

Permalink
Introduce run.optimization.remove_solution()
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Nov 29, 2024
1 parent 0c4d2ea commit 6ebef60
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ixmp4/core/optimization/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ def __init__(self, run: Run, **kwargs: Backend) -> None:
self.scalars = ScalarRepository(_backend=self.backend, _run=run)
self.tables = TableRepository(_backend=self.backend, _run=run)
self.variables = VariableRepository(_backend=self.backend, _run=run)

def remove_solution(self) -> None:
for equation in self.equations.list():
equation.remove_data()
for variable in self.variables.list():
variable.remove_data()
26 changes: 26 additions & 0 deletions tests/core/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,29 @@ def delete_all_datapoints(self, run: ixmp4.Run) -> None:
run.iamc.remove(cat, type=ixmp4.DataPoint.Type.CATEGORICAL)
if not datetime.empty:
run.iamc.remove(datetime, type=ixmp4.DataPoint.Type.DATETIME)

def test_run_remove_solution(self, platform: ixmp4.Platform) -> None:
run = platform.runs.create("Model", "Scenario")
indexset = run.optimization.indexsets.create("Indexset")
indexset.add(["foo", "bar"])
test_data = {
"Indexset": ["bar", "foo"],
"levels": [2.0, 1],
"marginals": [0, "test"],
}
run.optimization.equations.create(
"Equation",
constrained_to_indexsets=[indexset.name],
).add(test_data)
run.optimization.variables.create(
"Variable",
constrained_to_indexsets=[indexset.name],
).add(test_data)

run.optimization.remove_solution()
# Need to fetch them here even if fetched before because API layer might not
# forward changes automatically
equation = run.optimization.equations.get("Equation")
variable = run.optimization.variables.get("Variable")
assert equation.data == {}
assert variable.data == {}

0 comments on commit 6ebef60

Please sign in to comment.