Skip to content

added two example notebooks: energy-resource-build and sudoku#356

Merged
FabianHofmann merged 17 commits into
PyPSA:masterfrom
tpu-brunkhorst:more_examples
Jul 16, 2026
Merged

added two example notebooks: energy-resource-build and sudoku#356
FabianHofmann merged 17 commits into
PyPSA:masterfrom
tpu-brunkhorst:more_examples

Conversation

@tpu-brunkhorst

Copy link
Copy Markdown
Contributor

Hi - I added two notebooks to the examples folder. I didn't add links to these in the TOC or anything - up to you how you want to link them. Thanks for a great project!

@tpu-brunkhorst

Copy link
Copy Markdown
Contributor Author

Closes #348

Sorry if I messed up the formatting stuff on the PR

@aurelije

aurelije commented Feb 21, 2025

Copy link
Copy Markdown
Contributor

What happened to this PR? I took a brief look at sudoku example, it can be made more tidy, not all power of linopy got utilised. Whenever for loop is used it is unnecessary operation in linopy like tools where broadcast and group by operations exists

@tpu-brunkhorst

tpu-brunkhorst commented Feb 21, 2025 via email

Copy link
Copy Markdown
Contributor Author

@codspeed-hq

codspeed-hq Bot commented Jul 14, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 173 untouched benchmarks
⏩ 173 skipped benchmarks1


Comparing tpu-brunkhorst:more_examples (266d103) with master (d701781)

Open in CodSpeed

Footnotes

  1. 173 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Replace the sudoku notebook's loops with idiomatic linopy: groupby for the
square constraints, a broadcast MultiIndex-free mask for the hints, and a
weighted sum for the grid reconstruction (aligns with v1 no-MultiIndex, PyPSA#803).
Add nblink files and toctree entries for the energy-resource-build and sudoku
examples. Square groupby uses use_fallback=True pending PyPSA#823.
Teach the why, not just the what: motivate the binary on/off encoding as a
linearization, map each sum-constraint to its Sudoku rule, explain the groupby
dimension collapse and the hint-mask broadcast, and frame use_fallback=True as
a temporary PyPSA#823 workaround. Drop the unused square_index_3d and define the
variable directly; trim the random-data 3D plot; fix typos and terminology.
@FabianHofmann FabianHofmann added the documentation Improvements or additions to documentation label Jul 14, 2026
@FabianHofmann

FabianHofmann commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Note

Generated by Claude

Pushed follow-up work on top of the two notebooks:

  • Vectorized the sudoku example per @aurelije's earlier feedback — the manual for-loops are gone: groupby for the 3x3-square constraints, a MultiIndex-free broadcast mask for the hints (aligned with the v1 no-MultiIndex direction, feat(v1): drop first-class MultiIndex — feasibility + implementation (#744) #803), and a weighted sum for the grid reconstruction.
  • Wired both notebooks into the docs — added .nblink files and toctree entries for energy-resource-build and sudoku.
  • Reworked the sudoku prose to teach the why (the binary on/off encoding as a linearization, what each sum-constraint enforces, etc.).

⚠️ Follow-up before/after merge

The square-constraint cell currently carries a temporary workaround:

sudoku.groupby(square_index).sum(use_fallback=True) == 1

use_fallback=True is needed because the default grouped-sum fast path reduces a multi-dimensional DataArray grouper over only one of its dims (bug: #823). The fix is #824. Once #824 is merged, drop use_fallback=Truesudoku.groupby(square_index).sum() == 1 will then be correct — and simplify the accompanying note in the notebook. This is noted inline in the notebook markdown as well.

FabianHofmann and others added 8 commits July 15, 2026 20:15
Merged master brings the PyPSA#824 groupby-sum fix, so the square
constraint uses plain .sum() and the temporary note is removed.
Notebook re-executed end-to-end to confirm a valid solution.
- Use freq='ME' instead of deprecated 'M' in pd.date_range
- Replace legacy np.random.seed/randn with np.random.default_rng (NPY002)
Notebook re-executes end-to-end.
@FabianHofmann

Copy link
Copy Markdown
Collaborator

@tpu-brunkhorst I know this took ages but I now got the time to look at this. I will push one more commit on the energy resource nb and then merge this. thanks for the contribution

@FabianHofmann
FabianHofmann merged commit c2607b6 into PyPSA:master Jul 16, 2026
8 of 9 checks passed
@tpu-brunkhorst

tpu-brunkhorst commented Jul 16, 2026 via email

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants