Skip to content

Commit

Permalink
Improve linopy tutorial and helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Aug 29, 2024
1 parent 096b02c commit 1e77ad7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 4 additions & 2 deletions tutorial/transport/dantzig_model_linopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def create_parameter(


def create_dantzig_model(run: Run) -> linopy.Model:
"""Creates new linopy.Model for Dantzig's problem based on ixmp4.Run."""
m = linopy.Model()
i = pd.Index(run.optimization.indexsets.get("i").elements, name="Canning Plants")
j = pd.Index(run.optimization.indexsets.get("j").elements, name="Markets")
Expand Down Expand Up @@ -60,6 +61,7 @@ def create_dantzig_model(run: Run) -> linopy.Model:


def read_dantzig_solution(model: linopy.Model, run: Run) -> None:
"""Reads the Dantzig solution from linopy.Model to ixmp4.Run."""
# Handle objective
# TODO adding fake marginals here until Variables don't require this column anymore
# Can't add units if this column was not declared above. Better stored as Scalar
Expand Down Expand Up @@ -87,15 +89,15 @@ def read_dantzig_solution(model: linopy.Model, run: Run) -> None:
# The following don't seem to be typed correctly by linopy
# Add supply data
supply_data = {
"i": ["seattle", "san-diego"],
"i": run.optimization.indexsets.get("i").elements,
"levels": model.constraints["Observe supply limit at plant i"].data.rhs, # type: ignore
"marginals": model.constraints["Observe supply limit at plant i"].data.dual, # type: ignore
}
run.optimization.equations.get("supply").add(data=supply_data)

# Add demand data
demand_data = {
"j": ["new-york", "chicago", "topeka"],
"j": run.optimization.indexsets.get("j").elements,
"levels": model.constraints["Satisfy demand at market j"].data.rhs, # type: ignore
"marginals": model.constraints["Satisfy demand at market j"].data.dual, # type: ignore
}
Expand Down
14 changes: 7 additions & 7 deletions tutorial/transport/linopy_transport.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"a.add(data=a_data)\n",
"\n",
"# Demand at market j in cases\n",
"b = run.optimization.parameters.create(\"b\", constrained_to_indexsets=\"j\")\n",
"b = run.optimization.parameters.create(\"b\", constrained_to_indexsets=[\"j\"])\n",
"# ... or a pd.DataFrame\n",
"b_data = pd.DataFrame(\n",
" [\n",
Expand Down Expand Up @@ -349,7 +349,7 @@
"source": [
"### Defining `Variable`s and `Equation`s in the scenario\n",
"\n",
"The levels and marginals of these variables and equations will be imported to the scenario when reading the model solution."
"The levels and marginals of these `Variable`s and `Equation`s will be imported to the scenario when reading the model solution."
]
},
{
Expand Down Expand Up @@ -387,7 +387,7 @@
"\n",
"In this tutorial, we solve the tutorial using the ``highs`` solver in linopy. \n",
"\n",
"The ``create_dantzig_model()`` function is a convenience shortcut for setting up a linopy model correctly for the datzig scenario. Please see ``linopy_model.py`` for details.\n",
"The ``create_dantzig_model()`` function is a convenience shortcut for setting up a linopy model correctly for the dantzig scenario. Please see ``linopy_model.py`` for details.\n",
"\n",
"The solution data are stored with the model object automatically. ``store_dantzig_solution()`` then stores them in the ixmp4 objects."
]
Expand All @@ -403,10 +403,10 @@
" read_dantzig_solution,\n",
")\n",
"\n",
"m = create_dantzig_model(run=run)\n",
"m.solve(\"highs\")\n",
"linopy_model = create_dantzig_model(run=run)\n",
"linopy_model.solve(\"highs\")\n",
"\n",
"read_dantzig_solution(model=m, run=run)"
"read_dantzig_solution(model=linopy_model, run=run)"
]
},
{
Expand All @@ -433,7 +433,7 @@
"outputs": [],
"source": [
"# Display the quantities transported from canning plants to demand locations\n",
"x.data"
"pd.DataFrame(x.data)"
]
},
{
Expand Down

0 comments on commit 1e77ad7

Please sign in to comment.