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
2 changes: 1 addition & 1 deletion notebooks/01_lp_formulation/goal_programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"reaches a known number of people, costs a known amount, and ties up a known amount of the agency's\n",
"staff. There are ten person-minutes of staff time, and the contract caps radio at six minutes.\n",
"\n",
"Management has two targets: reach at least 45 million people, and spend no more than $100 thousand. Call\n",
"Management has two targets: reach at least 45 million people, and spend no more than \\$100 thousand. Call\n",
"them **goals** rather than constraints, and check first whether any plan can meet both — a\n",
"constraint that cannot be met makes the whole problem infeasible, and goal programming exists for\n",
"that case: *by how little must each target be missed, in order of importance?*\n",
Expand Down
2 changes: 1 addition & 1 deletion notebooks/02_simplex_and_bases/simplex_by_hand.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
"metadata": {},
"source": [
"Enumerating is fine for six. With 50 variables and 30 constraints there are 80 columns and\n",
"$\binom{80}{30}$ choices, about $9 \times 10^{21}$.\n",
"$\\binom{80}{30}$ choices, about $9 \\times 10^{21}$.\n",
"The simplex method visits a handful of them, always moving to a neighbour that is no worse.\n",
"\n",
"## The tableau\n",
Expand Down
2 changes: 1 addition & 1 deletion notebooks/04_networks_and_transport/transportation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@
"## A second instance, where the units are the trap\n",
"\n",
"Three distribution centres, five dealers, distances in miles. Its data are in **units** while trucks\n",
"carry **18** at a time and cost **$25 per mile** — so a factor of 18 and a factor of 25 have to\n",
"carry **18** at a time and cost **\\$25 per mile** — so a factor of 18 and a factor of 25 have to\n",
"appear somewhere, and where they appear is a choice.\n",
"\n",
"The version this came from divided by 18 inside the constraint expressions, as a bare number. Here\n",
Expand Down
14 changes: 7 additions & 7 deletions notebooks/12_energy_systems_pypsa/dispatch_to_pypsa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@
"\n",
"| technology | available this hour | cost to run |\n",
"|---|---|---|\n",
"| solar | 40 MW | $0.00 /MWh |\n",
"| gas | 500 MW | $22.14 /MWh |\n",
"| solar | 40 MW | \\$0.00 /MWh |\n",
"| gas | 500 MW | \\$22.14 /MWh |\n",
"\n",
"**Solve it before you run anything below**: which do you run, how much of each, and what does the\n",
"hour cost? Then write it as a linear program — one variable per technology, a balance row, a\n",
Expand Down Expand Up @@ -740,7 +740,7 @@
"source": [
"Read the table before moving on. The battery column is negative when it charges. Find the hours it\n",
"charges in and what the price is then; the hours it discharges into; and the one hour the peaker\n",
"runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's $22.14, and the\n",
"runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's \\$22.14, and the\n",
"only thing in this system that costs more than gas to run is the peaker, which is off in all four.\n",
"What is setting the price there?"
]
Expand Down Expand Up @@ -1019,7 +1019,7 @@
"## The envelope calculation, and where it goes wrong\n",
"\n",
"Work out by hand why the model would not touch free fuel: one MW of solar makes so many MWh a day,\n",
"each displacing gas at $22.14, and that is worth so much a year. Compare it with the capital cost the\n",
"each displacing gas at \\$22.14, and that is worth so much a year. Compare it with the capital cost the\n",
"table charges."
]
},
Expand Down Expand Up @@ -1425,7 +1425,7 @@
"> **2.** Cut solar to 60 MW. Does the battery still charge at midday? Where does its energy come\n",
"> from now, and what does that do to the evening prices?\n",
">\n",
"> **3.** Raise the peaker's cost to $300. Predict first: does the schedule change, the prices, the\n",
"> **3.** Raise the peaker's cost to \\$300. Predict first: does the schedule change, the prices, the\n",
"> day cost, or all three?"
]
},
Expand Down Expand Up @@ -1664,13 +1664,13 @@
"\n",
"## Where to take this next\n",
"\n",
"- Add a coal plant — 200 MW available at $19.90/MWh — to *both* one-hour models, and confirm they\n",
"- Add a coal plant — 200 MW available at \\$19.90/MWh — to *both* one-hour models, and confirm they\n",
" still agree. Predict, before running, whether the marginal price goes up or down, and why.\n",
"- Set the battery's power to zero in the day model and re-solve. How much of the day's cost was the\n",
" battery saving, and in which hours?\n",
"- Repeat the capital-cost sweep for the peaker instead of solar. Does the model value it above or\n",
" below its energy displacement, and why is the answer the opposite sign to solar's?\n",
"- Cap solar's build at 50 MW and re-run the sweep at $60,000/yr. The model builds all 50. Which is\n",
"- Cap solar's build at 50 MW and re-run the sweep at \\$60,000/yr. The model builds all 50. Which is\n",
" binding — the cap or the price — and how would you tell?"
]
}
Expand Down
4 changes: 2 additions & 2 deletions notebooks/13_supply_chain/sourcing_and_resilience.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@
"source": [
"The zero describes one side only. A shadow price is a derivative, and here the cost has a **kink**:\n",
"upward it is flat, because there is nothing left that is worth refining once demand is met; downward\n",
"it is $3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the\n",
"it is \\$3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the\n",
"right-hand slope. A different solver, or the same one with a different method, may report the left —\n",
"both are correct, and neither is *the* shadow price, because at a kink there isn't one.\n",
"\n",
Expand Down Expand Up @@ -833,7 +833,7 @@
"\n",
"- Put the share cap on the *processors* instead of the mines. Which cap is more expensive to satisfy,\n",
" and what does that say about where the bottleneck in this industry sits?\n",
"- The domestic mine ships to China at $8.0/kt and to the domestic processor at $3.0. Find the ore\n",
"- The domestic mine ships to China at \\$8.0/kt and to the domestic processor at \\$3.0. Find the ore\n",
" tariff at which it enters the uncapped solution on price alone — without re-solving, from the\n",
" reduced costs.\n",
"- The middle-layer structure above — the balance row saying a processor cannot ship what it did not\n",
Expand Down
23 changes: 23 additions & 0 deletions tools/check_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
written in the same cell as the plot (standard Part 3, accessible)
setup cell tag the setup cell clones orteach.paths.TAG, the same release the
package fetches tables from
control characters none in any cell source, and no tab in markdown: the trace of
a "\\b" or "\\t" a seed's string ate ("$\\binom" became
backspace + "inom")
currency dollars a price in markdown is written "\\$22.14", not "$22.14": two on
one line, or one beside real math, pair up and render as LaTeX
"""
from __future__ import annotations

Expand Down Expand Up @@ -60,6 +65,14 @@
CODE_SPAN_RE = re.compile(r"`[^`]*`|\$\$.*?\$\$|\$[^$\n]*\$", re.S)
EXERCISES_RE = re.compile(r"^#+\s*where to take this next.*", re.I | re.M | re.S)
LICENCE_RE = re.compile(r"LicenseID to value|Academic license|Restricted license")
CONTROL_RE = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f]")
# An unescaped "$" opening a number that reads as money: the number ends the line, or is
# followed by punctuation and a space, by a word ("$3 a kilotonne"), or by a per-unit slash
# ("$0.00 /MWh", "$8.0/kt"). Math such as "$2 \times 2$", "$1/\sqrt{n}$", "$1/n$", "$40x_1$"
# or "$0.02$" continues with a backslash, an operator, a letter glued on, a one-letter
# denominator, or the closing "$".
CURRENCY_RE = re.compile(r"(?<!\\)\$(\d[\d,]*(?:\.\d+)?)"
r"(?=$|[.,;:!?)](?:\s|$)|\s+[A-Za-z]|\s*/\s*[A-Za-z]{2})", re.M)
# The release the package fetches tables from, read from source (not imported). The setup cell
# must clone the same one, or Colab runs this notebook against tables from another release.
_TAG_M = re.search(r'^TAG = "([^"]+)"', (ROOT / "src" / "orteach" / "paths.py").read_text(encoding="utf-8"), re.M)
Expand Down Expand Up @@ -193,6 +206,16 @@ def _cell_text(c):
if setup and '"--branch", REF' not in src(setup[0]):
fails.append("setup cell clones without --branch REF, so Colab gets main, not the release")

# no character a string escape ate, and no price that renders as math
bad = [i for i, c in enumerate(cells) if CONTROL_RE.search(src(c))
or (c.get("cell_type") == "markdown" and "\t" in src(c))]
if bad:
fails.append("control character (an eaten \\b or \\t?) in cell(s) %s" % bad)
prices = sorted({m.group(0) for m in CURRENCY_RE.finditer(all_md)})
if prices:
fails.append("unescaped price(s) in markdown; write \\$ so they don't pair as LaTeX: %s"
% prices[:8])

return fails


Expand Down
14 changes: 7 additions & 7 deletions tools/seeds/seed_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def code(t):

| technology | available this hour | cost to run |
|---|---|---|
| solar | 40 MW | $0.00 /MWh |
| gas | 500 MW | $22.14 /MWh |
| solar | 40 MW | \$0.00 /MWh |
| gas | 500 MW | \$22.14 /MWh |

**Solve it before you run anything below**: which do you run, how much of each, and what does the
hour cost? Then write it as a linear program — one variable per technology, a balance row, a
Expand Down Expand Up @@ -339,7 +339,7 @@ def code(t):
md(r"""
Read the table before moving on. The battery column is negative when it charges. Find the hours it
charges in and what the price is then; the hours it discharges into; and the one hour the peaker
runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's $22.14, and the
runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's \$22.14, and the
only thing in this system that costs more than gas to run is the peaker, which is off in all four.
What is setting the price there?
""")
Expand Down Expand Up @@ -460,7 +460,7 @@ def code(t):
## The envelope calculation, and where it goes wrong

Work out by hand why the model would not touch free fuel: one MW of solar makes so many MWh a day,
each displacing gas at $22.14, and that is worth so much a year. Compare it with the capital cost the
each displacing gas at \$22.14, and that is worth so much a year. Compare it with the capital cost the
table charges.
""")
code(r'''
Expand Down Expand Up @@ -667,7 +667,7 @@ def explore_day(battery_mw, battery_hours, solar_mw, peaker_cost, peak_hour):
> **2.** Cut solar to 60 MW. Does the battery still charge at midday? Where does its energy come
> from now, and what does that do to the evening prices?
>
> **3.** Raise the peaker's cost to $300. Predict first: does the schedule change, the prices, the
> **3.** Raise the peaker's cost to \$300. Predict first: does the schedule change, the prices, the
> day cost, or all three?
""")
code(r'''
Expand Down Expand Up @@ -762,13 +762,13 @@ def explore_build(solar_capex, battery_mw):

## Where to take this next

- Add a coal plant — 200 MW available at $19.90/MWh — to *both* one-hour models, and confirm they
- Add a coal plant — 200 MW available at \$19.90/MWh — to *both* one-hour models, and confirm they
still agree. Predict, before running, whether the marginal price goes up or down, and why.
- Set the battery's power to zero in the day model and re-solve. How much of the day's cost was the
battery saving, and in which hours?
- Repeat the capital-cost sweep for the peaker instead of solar. Does the model value it above or
below its energy displacement, and why is the answer the opposite sign to solar's?
- Cap solar's build at 50 MW and re-run the sweep at $60,000/yr. The model builds all 50. Which is
- Cap solar's build at 50 MW and re-run the sweep at \$60,000/yr. The model builds all 50. Which is
binding — the cap or the price — and how would you tell?
""")

Expand Down
2 changes: 1 addition & 1 deletion tools/seeds/seed_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def code(t):
reaches a known number of people, costs a known amount, and ties up a known amount of the agency's
staff. There are ten person-minutes of staff time, and the contract caps radio at six minutes.

Management has two targets: reach at least 45 million people, and spend no more than $100 thousand. Call
Management has two targets: reach at least 45 million people, and spend no more than \$100 thousand. Call
them **goals** rather than constraints, and check first whether any plan can meet both — a
constraint that cannot be met makes the whole problem infeasible, and goal programming exists for
that case: *by how little must each target be missed, in order of importance?*
Expand Down
2 changes: 1 addition & 1 deletion tools/seeds/seed_simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def code(t):

md(r"""
Enumerating is fine for six. With 50 variables and 30 constraints there are 80 columns and
$inom{80}{30}$ choices, about $9 imes 10^{21}$.
$\binom{80}{30}$ choices, about $9 \times 10^{21}$.
The simplex method visits a handful of them, always moving to a neighbour that is no worse.

## The tableau
Expand Down
4 changes: 2 additions & 2 deletions tools/seeds/seed_sourcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def code(t):
md(r"""
The zero describes one side only. A shadow price is a derivative, and here the cost has a **kink**:
upward it is flat, because there is nothing left that is worth refining once demand is met; downward
it is $3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the
it is \$3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the
right-hand slope. A different solver, or the same one with a different method, may report the left —
both are correct, and neither is *the* shadow price, because at a kink there isn't one.

Expand Down Expand Up @@ -395,7 +395,7 @@ def code(t):

- Put the share cap on the *processors* instead of the mines. Which cap is more expensive to satisfy,
and what does that say about where the bottleneck in this industry sits?
- The domestic mine ships to China at $8.0/kt and to the domestic processor at $3.0. Find the ore
- The domestic mine ships to China at \$8.0/kt and to the domestic processor at \$3.0. Find the ore
tariff at which it enters the uncapped solution on price alone — without re-solving, from the
reduced costs.
- The middle-layer structure above — the balance row saying a processor cannot ship what it did not
Expand Down
Loading