Explore a near-optimal set in Python¶
The web platform is one way to explore a near-optimal set; the mga_compass library is
another, for notebooks and scripts. This guide loads a set, navigates from the cheapest design
to one with more wind, and looks at the cost and the system along the path. The same steps,
with plots, are in the notebook
mga-compass/examples/explore-a-near-optimal-set.ipynb.
You need:
the
mga-compasslibrary, with thenotebookextra for plots:uv sync --package mga-compass --extra notebook(see Install the MGA Compass library);a near-optimal set: a directory, its zip archive, or the URL of a published one. This guide uses the published set of PyPSA’s
model_energyexample, whichscripts/generate_dataset.pybuilds (see Build a dataset for the platform).
Load the set¶
import mga_compass as mc
URL = "https://github.com/EINS-TUDa/MGA-Compass/releases/download/dataset-model-energy-2026-11-07/near-optimal-set.zip"
space = mc.load(URL, "data/model-energy")
space
PyPSA model_energy example
PyPSA's model_energy example (pypsa.examples.model_energy): one electricity node and one
hydrogen node over 2019, at 9-hourly resolution (every third snapshot of the 3-hourly example).
...
Space: 188 near-optimal designs in 4 PoIs, slack 0.08 (set's slack levels: 0.02, 0.05, 0.08)
unit min max optimum description
wind GW 15.35 42.04 25.27 Installed wind power capacity.
solar GW 13.08 55.38 30.53 Installed solar PV capacity.
battery GW 6.632 35.47 16.83 Installed battery storage power (charge/discharge) rating. ...
hydrogen TWh 0.4023 5.349 1.501 Installed hydrogen storage capacity.
TOTEX M€ 6,901 7,453 6,901
A Space holds the set’s designs in the space of its properties of interest (PoIs), with
their cost. space.points has one row per design; space.optimum is the cheapest.
space.plot_ranges() draws each PoI’s range with the optimum marked.
Units: values are in the set’s display units (here capacities in GW and TWh rather than MW and MWh, the cost in M€ rather than €). With
units="model", they stay in the model’s units. Navigation finds the same designs either way.Slack level: by default the space is the set’s largest slack level.
slack=0.02keeps only the designs found within 2% of the optimal cost.Archives: a zip archive or a URL is unpacked into the directory you name, from which outputs are read later. Once unpacked, load it from there:
mc.load("data/model-energy").About the set:
space.titleandspace.descriptionsay what model it comes from, when the set’s manifest gives them.
Look at the path¶
result.traverse is the path from the start to the target. The PoIs move in a straight line;
the cost is the cheapest mix of the set’s designs at each point, an upper bound on the true
cost, and the designs’ duals give a lower bound (see Traverse):
result.traverse
Traverse: 2 breakpoints between start and target; the cost is at most 34.27 M€ above its lower bound
wind solar battery hydrogen TOTEX lower bound
beta
0 25.27 30.53 16.83 1.501 6,901 6,901
0.5 30.04 25.46 16.80 1.190 6,991 6,963
0.75 32.43 22.92 16.78 1.035 7,036 7,007
1 34.82 20.39 16.76 0.8796 7,108 7,099
result.traverse.at(0.3): the design at 30% of the path, with its cost.result.traverse.lower_bound_at(0.3): the cost’s lower bound there.result.traverse.weights_at(0.3): the set’s designs mixed there, heaviest first, with each one’s weight, slack, kind and key (the hash in the name of its result file, see the format).result.traverse.plot(): both bounds against β, with the optimal cost and the budget (the optimal cost plus the slack);plot(beta=0.3)also marks the design there.result.traverse.plot_pois(): bars of the PoIs at the start (pale) and the target, one panel per unit, in the PoIs’ colours;plot_pois(beta=0.3)shows the design at 30% of the path, with lines at the start’s and the target’s values.result.traverse.plot_radar(): the same as a radar chart, each PoI scaled to its range in the space; it takes abetatoo.space.traverse(a, b): the path between any two designs.
Look at the system¶
The set’s detailed outputs are listed by space.outputs(). space.output gives one of them
for a design: a navigation result (its target), the weights of a point along the traverse,
or a design of the set by its id. It reads only the outputs of the designs in the mix.
import pandas as pd
pd.DataFrame({
"start": space.output("capacity", result.traverse.alpha_at(0)),
"halfway": space.output("capacity", result.traverse.alpha_at(0.5)),
"target": space.output("capacity", result),
})
start halfway target
technology
wind 25,271.6 30,043.7 34,815.9
solar 30,534.5 25,460.4 20,386.3
electrolysis 1,709.7 1,450.7 1,312.7
turbine 9,904.6 9,584.5 9,296.5
battery storage 16,828.8 16,795.7 16,762.6
hydrogen storage 1,500,715.2 1,190,180.7 879,645.2
An output is a pandas Series indexed by the output’s sets, with its unit in
series.attrs["unit"]. Unstack it for a table.
The set also declares plots of its outputs, which the web platform shows. space.plots()
lists them, and space.plot draws one for a design the same way, with the set’s labels and
colours:
space.plot("generation", result) # stacked generation per technology over the year
space.plot("capacity", result)
See also¶
mga_compass.spacein the API reference.Navigation and Traverse: how navigation and the traverse work.