Skip to content

Optimization API

The programmatic interface used to build and solve the model. solve is the one-line convenience wrapper over the model classes; load_inputs assembles the input dataset without solving, for inspection.

The mathematical optimization problem — objective function and constraints — is documented separately in the Methodology section.

solve

microgridspy.solve(project_name, *, formulation=None, solver='highs', **solver_kwargs)

Build and solve a project, returning the solved model.

Parameters:

Name Type Description Default
project_name str

the project folder in the active workspace.

required
formulation str | None

"typical_year" or "multi_year"; if None it is read from the project's formulation.json.

None
solver str

"highs" (open source) or "gurobi" (licensed).

'highs'
**solver_kwargs Any

forwarded to TypicalYearModel.solve_single_objective() (e.g. solver_params, problem_fn, log_file_path).

{}

Returns:

Type Description
AnyModel

The solved model instance, ready for results() /

AnyModel

results_summary().

solve_example

One-call end-to-end quick start over the bundled examples — works straight after pip install.

microgridspy.solve_example(name='demo_typical_year', *, solver='highs', dest=None, overwrite=True, **solver_kwargs)

Load a bundled example project and solve it, end-to-end.

A one-call quick start that works straight after pip install (no repository clone needed): it copies the example into the active workspace and solves it.

Parameters:

Name Type Description Default
name str

the bundled example to run (see microgridspy.list_examples()); "demo_typical_year" or "demo_multi_year".

'demo_typical_year'
solver str

"highs" (open source) or "gurobi" (licensed).

'highs'
dest str | None

destination project name; defaults to name.

None
overwrite bool

replace the destination project if it already exists (default True, so the example is re-runnable).

True
**solver_kwargs Any

forwarded to the model's solve_single_objective.

{}

Returns:

Type Description
AnyModel

The solved model instance, ready for results() / results_summary().

load_inputs

microgridspy.load_inputs(project_name, *, formulation=None)

Assemble and return a project's input dataset, without solving.

Builds the sets and data layers (the same inputs a model would use), so you can inspect the assembled xarray Dataset directly.

Parameters:

Name Type Description Default
project_name str

the project to read.

required
formulation str | None

"typical_year" or "multi_year"; auto-detected when None.

None

Returns:

Type Description
Dataset

xr.Dataset: the assembled input dataset.