Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ repos:
- id: codespell
types_or: [python, rst, markdown]
files: ^(linopy|doc)/
- repo: https://github.com/aflc/pre-commit-jupyter
rev: v1.2.1
- repo: https://github.com/kynan/nbstripout
rev: 0.8.1
hooks:
- id: jupyter-notebook-cleanup
- id: nbstripout
args:
- --extra-keys=cell.metadata.ExecuteTime cell.metadata.execution
exclude: examples/solve-on-remote.ipynb
8 changes: 4 additions & 4 deletions benchmark/notebooks/plot-benchmarks.py.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9a85db47",
"id": "0",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -19,7 +19,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "709bdf49",
"id": "1",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -31,7 +31,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f36897fb",
"id": "2",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -65,7 +65,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c5c93666",
"id": "3",
"metadata": {},
"outputs": [],
"source": [
Expand Down
42 changes: 33 additions & 9 deletions examples/coordinate-alignment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "### Same-Shape Operands: Positional Alignment\n\nWhen two operands have the **same shape** on a shared dimension, linopy uses **positional alignment** by default — coordinate labels are ignored and the left operand's labels are kept. This is a performance optimization but can be surprising:"
"source": [
"### Same-Shape Operands: Positional Alignment\n",
"\n",
"When two operands have the **same shape** on a shared dimension, linopy uses **positional alignment** by default — coordinate labels are ignored and the left operand's labels are kept. This is a performance optimization but can be surprising:"
]
},
{
"cell_type": "code",
Expand All @@ -142,7 +146,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "Even though ``offset_const`` has coordinates ``[5, 6, 7, 8, 9]`` and ``x`` has ``[0, 1, 2, 3, 4]``, the result uses ``x``'s labels. The values are aligned by **position**, not by label. The same applies when adding two variables or expressions of identical shape:"
"source": [
"Even though ``offset_const`` has coordinates ``[5, 6, 7, 8, 9]`` and ``x`` has ``[0, 1, 2, 3, 4]``, the result uses ``x``'s labels. The values are aligned by **position**, not by label. The same applies when adding two variables or expressions of identical shape:"
]
},
{
"cell_type": "code",
Expand All @@ -157,7 +163,11 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "``x`` (time 0–4) and ``z`` (time 5–9) share no coordinate labels, yet the result has 5 entries under ``x``'s coordinates — because they have the same shape, positions are matched directly.\n\nTo force **label-based** alignment, pass an explicit ``join``:"
"source": [
"``x`` (time 0–4) and ``z`` (time 5–9) share no coordinate labels, yet the result has 5 entries under ``x``'s coordinates — because they have the same shape, positions are matched directly.\n",
"\n",
"To force **label-based** alignment, pass an explicit ``join``:"
]
},
{
"cell_type": "code",
Expand All @@ -171,7 +181,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "With ``join=\"outer\"``, the result spans all 10 time steps (union of 0–4 and 5–9), filling missing positions with zeros. This is the correct label-based alignment. The same-shape positional shortcut is equivalent to ``join=\"override\"`` — see below."
"source": [
"With ``join=\"outer\"``, the result spans all 10 time steps (union of 0–4 and 5–9), filling missing positions with zeros. This is the correct label-based alignment. The same-shape positional shortcut is equivalent to ``join=\"override\"`` — see below."
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -271,7 +283,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "**Override** — positional alignment, ignore coordinate labels. The result uses the left operand's coordinates. Here ``a`` has i=[0, 1, 2] and ``b`` has i=[1, 2, 3], so positions are matched as 0↔1, 1↔2, 2↔3:"
"source": [
"**Override** — positional alignment, ignore coordinate labels. The result uses the left operand's coordinates. Here ``a`` has i=[0, 1, 2] and ``b`` has i=[1, 2, 3], so positions are matched as 0↔1, 1↔2, 2↔3:"
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -369,7 +383,11 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "## Practical Example\n\nConsider a generation dispatch model where solar availability follows a daily profile and a minimum demand constraint only applies during peak hours."
"source": [
"## Practical Example\n",
"\n",
"Consider a generation dispatch model where solar availability follows a daily profile and a minimum demand constraint only applies during peak hours."
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -405,7 +423,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "For solar, we build a full 24-hour availability profile — zero at night, sine-shaped during daylight (hours 6–18). Since this covers all hours, standard alignment works directly and solar is properly constrained to zero at night:"
"source": [
"For solar, we build a full 24-hour availability profile — zero at night, sine-shaped during daylight (hours 6–18). Since this covers all hours, standard alignment works directly and solar is properly constrained to zero at night:"
]
},
{
"cell_type": "code",
Expand All @@ -424,7 +444,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "Now suppose a minimum demand of 120 MW must be met, but only during peak hours (8–20). The demand array covers a subset of hours, so we use ``join=\"inner\"`` to restrict the constraint to just those hours:"
"source": [
"Now suppose a minimum demand of 120 MW must be met, but only during peak hours (8–20). The demand array covers a subset of hours, so we use ``join=\"inner\"`` to restrict the constraint to just those hours:"
]
},
{
"cell_type": "code",
Expand All @@ -444,7 +466,9 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "The demand constraint only applies during peak hours (8–20). Outside that range, no minimum generation is required."
"source": [
"The demand constraint only applies during peak hours (8–20). Outside that range, no minimum generation is required."
]
},
{
"cell_type": "markdown",
Expand Down
34 changes: 17 additions & 17 deletions examples/create-a-model-with-coordinates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "4db583af",
"id": "0",
"metadata": {},
"source": [
"# Use Coordinates\n",
Expand All @@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "comparable-talent",
"id": "1",
"metadata": {},
"source": [
"Minimize:\n",
Expand All @@ -36,7 +36,7 @@
},
{
"cell_type": "markdown",
"id": "proprietary-receipt",
"id": "2",
"metadata": {},
"source": [
"In order to formulate the new problem with linopy, we start again by initializing a model."
Expand All @@ -45,7 +45,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "close-maximum",
"id": "3",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -56,7 +56,7 @@
},
{
"cell_type": "markdown",
"id": "positive-appearance",
"id": "4",
"metadata": {},
"source": [
"Again, we define `x` and `y` using the `add_variables` function, but now we are adding a `coords` argument. This automatically creates optimization variables for all coordinates, in this case time-steps."
Expand All @@ -65,7 +65,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "included-religious",
"id": "5",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -83,7 +83,7 @@
},
{
"cell_type": "markdown",
"id": "terminal-ethernet",
"id": "6",
"metadata": {},
"source": [
"Following the previous example, we write the constraints out using the syntax from above, while multiplying the rhs with `t`. Note that the coordinates from the lhs and the rhs have to match. \n",
Expand All @@ -95,7 +95,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c24d120a",
"id": "7",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -106,7 +106,7 @@
},
{
"cell_type": "markdown",
"id": "f09803f4",
"id": "8",
"metadata": {},
"source": [
"It always helps to write out the constraints before adding them to the model. Since they look good, let's assign them."
Expand All @@ -115,7 +115,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "comprehensive-blend",
"id": "9",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -126,7 +126,7 @@
},
{
"cell_type": "markdown",
"id": "induced-professor",
"id": "10",
"metadata": {},
"source": [
"Now, when it comes to the objective, we use the `sum` function of `linopy.LinearExpression`. This stacks all terms all terms of the `time` dimension and writes them into one big expression. "
Expand All @@ -135,7 +135,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "alternate-story",
"id": "11",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -146,7 +146,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "outer-presence",
"id": "12",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -155,7 +155,7 @@
},
{
"cell_type": "markdown",
"id": "495cd082",
"id": "13",
"metadata": {},
"source": [
"In order to inspect the solution. You can go via the variables, i.e. `y.solution` or via the `solution` aggregator of the model, which combines the solution of all variables. This can sometimes be helpful."
Expand All @@ -164,7 +164,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "monthly-census",
"id": "14",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -173,15 +173,15 @@
},
{
"cell_type": "markdown",
"id": "owned-europe",
"id": "15",
"metadata": {},
"source": [
"Alright! Now you learned how to set up linopy variables and expressions with coordinates. In the User Guide, which follows, we are going to see, how the representation of variables with coordinates allows us to formulate more advanced operations."
]
},
{
"cell_type": "markdown",
"id": "4db583af",
"id": "16",
"metadata": {},
"source": [
"## Where to next\n",
Expand Down
Loading
Loading