Skip to content

Walkthrough

One spec run through every stage of the pipeline: YAML, schema, core AST, logical plan, model tables, LP text and solution. The script prints what each stage produces, then two specs the language refuses and why.

python examples/walkthrough/walkthrough.py

Its output is committed as walkthrough.out, and tests/test_walkthrough.py checks it line for line.

The spec

The dispatch model, plus a macro and a named expression. The macro is gone after the language expands it. The named expression keeps its name, so the last stage reads total_supply back at the solution with evaluate().

examples/walkthrough/walkthrough.yaml
description: >-
  The dispatch model of README.md, plus one macro and one named expression —
  small enough to print in full, complete enough that every pipeline stage in
  examples/walkthrough/walkthrough.py has something to show.

dimensions:
  snapshot:
    description: dispatch periods
    dtype: int
  generator:
    description: >-
      generating units, including oil, which is retired and gets no columns at
      all

parameters:
  p_max: {dims: [generator], description: "installed capacity, zero for a retired unit"}
  load: {dims: [snapshot], description: "demand to be met"}
  cost: {dims: [generator], description: "marginal cost"}

expressions:
  total_supply:
    expression: sum(p, over=generator)
    description: what the whole fleet produces in a snapshot

macros:
  weighted_sum:
    description: an array priced by a second one and summed over a dimension
    args: [array, weights]
    kwargs: [over]
    template: sum(array * weights, over=over)

variables:
  p:
    description: >-
      output of a generator in a snapshot — the `where` drops the retired unit
      entirely, so the built model is smaller than the coordinate product
    dims: [snapshot, generator]
    where: "p_max > 0"
    bounds:
      lower: 0
      upper: p_max

constraints:
  power_balance:
    description: the fleet meets the load exactly in every snapshot
    dims: [snapshot]
    expression: total_supply == load

objective:
  sense: minimize
  description: total cost of generation over the horizon
  expression: sum(weighted_sum(p, cost, over=generator))