Python library for building indexed linear and mixed-integer programs in polars; assembles into HiGHS, exportable as MPS.
Project description
polar-high
A Python library for building and solving large linear and mixed-integer optimisation programs, i.e. domain specific language (DSL) for algebraic modelling. Variables and parameters are polars DataFrames, expressions are joined and grouped lazily, and the matrix is assembled directly through HiGHS — or exported as MPS for any other LP/MIP solver. The kernel is intentionally domain-free: it has no opinions about energy systems, supply chains, or any specific application.
Install
pip install polar-high
Requires Python 3.11+. HiGHS ships in highspy, no separate install.
Quickstart
A tiny dispatch LP — wind + coal over three hours, minimise cost subject to capacity and per-hour demand.
import polars as pl
from polar_high import Param, Problem, Sum
p = Problem()
# Index sets — declared once, reused below
unit_index = pl.DataFrame({"unit": ["wind", "coal"]})
time_index = pl.DataFrame({"hour": [1, 2, 3]})
# Decision variable v_production[unit, hour] >= 0
composite_index = unit_index.join(time_index, how="cross")
v_production = p.add_var(
"v_production",
dims=("unit", "hour"),
index=composite_index,
lower=0.0,
)
# Operating cost per unit
cost = Param(
("unit",),
pl.DataFrame({"unit": ["wind", "coal"], "value": [2.0, 8.0]}),
)
# Available capacity per unit per hour — built per-unit, then concatenated
cap_wind = time_index.with_columns(
pl.lit("wind").alias("unit"),
pl.Series("value", [3.0, 1.0, 4.0]), # wind drops in hour 2
)
cap_coal = time_index.with_columns(
pl.lit("coal").alias("unit"),
pl.Series("value", [10.0, 10.0, 10.0]),
)
cap = Param(
("unit", "hour"),
pl.concat([cap_wind, cap_coal]).select("unit", "hour", "value"),
)
# Demand per hour
demand = Param(
("hour",),
time_index.with_columns(pl.Series("value", [5.0, 6.0, 4.0])),
)
# Minimise total cost
p.set_objective(cost * v_production, sense="min")
# v_production[unit, hour] <= cap[unit, hour]
p.add_cstr(
"capacity",
over=composite_index,
lhs_terms={"production": v_production},
sense="<=",
rhs_terms={"cap": cap},
)
# Σ_unit v_production[unit, hour] == demand[hour]
p.add_cstr(
"demand_balance",
over=time_index,
lhs_terms={"production": Sum(v_production, over=("unit",))},
sense="==",
rhs_terms={"demand": demand},
)
sol = p.solve()
print(f"objective: {sol.obj}") # 72.0
print(sol.value("v_production"))
The same code lives at tests/fixtures/quickstart_example.py and is executed in the test suite, so README and docs stay in sync.
Enum dtype handling
polars 1.40 treats pl.Enum as a nominal type and refuses to join
two columns when the Enums carry different categorical vocabularies
— even if one's categories are a strict subset of the other's. That
strict semantics gets in the way of typical LP-DSL workflows, where
one Param may live on a subset of an axis (e.g. only the units
with a finite capacity) while another lives on the full axis.
polar-high auto-aligns Enum-typed join keys at every internal
.join() site:
- same Enum on both sides — no change.
- one side's categories ⊆ the other's — the narrower side is
up-cast to the wider Enum dtype (with
strict=False; any values outside the wider vocab become null, which inner / left joins drop or surface naturally). - Enum vs
pl.Utf8/pl.String— the string side is cast to the Enum dtype. - neither vocab is a subset of the other —
ValueErrorwith actionable guidance (cast topl.Utf8or build a union Enum).
No try/except shims, no Utf8 fallbacks for the disjoint case. The
behaviour is implemented by a generic helper that knows nothing
about specific axis names or vocab contents. See
tests/test_enum_dtype_align.py for the contract.
Documentation
Full docs at https://nodal-tools.fi/polar-high/ — published with MkDocs + mike for per-version reads.
- Concepts —
the indexed-frame mental model (
Var,Param,Sum,Where,Lag, broadcasting/join semantics). - Guide — warm-starting, Lagrangian decomposition, performance tuning, debugging.
- API reference — autogenerated from docstrings.
- Compare — how polar-high relates to Pyomo, JuMP, gurobipy, linopy, and GNU MathProg.
Build locally: pip install -e ".[docs]" && mkdocs serve.
Used by
polar-high is the build engine behind the FlexTool energy-system modelling toolkit (still in dev branch only, 7.5.2026 situation). FlexTool's fleet of system tests (from earlier GNU MathProg to HiGHS implementation) has been used to test polar-high in real modelling use cases. In addition polar-high kernel has its own set of unit and system tests.
Created by
polar-high was created by Juha Kiviluoma of Nodal-Tools using Claude Opus.
License
Apache-2.0 — see LICENSE and NOTICE. Changelog: CHANGELOG.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polar_high-1.3.0.tar.gz.
File metadata
- Download URL: polar_high-1.3.0.tar.gz
- Upload date:
- Size: 107.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d67be945aacb56a351accdee97cdbee014c6805b87409079faad8bda8b1c7fe
|
|
| MD5 |
8a26c4adbd161c2866dd237998f0787e
|
|
| BLAKE2b-256 |
9b4e069915372d0e799b9cd371a95781d6c4f2dc2321f5925de2d0f744910271
|
Provenance
The following attestation bundles were made for polar_high-1.3.0.tar.gz:
Publisher:
release.yml on nodal-tools/polar-high
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polar_high-1.3.0.tar.gz -
Subject digest:
4d67be945aacb56a351accdee97cdbee014c6805b87409079faad8bda8b1c7fe - Sigstore transparency entry: 1603411577
- Sigstore integration time:
-
Permalink:
nodal-tools/polar-high@86eb4d62011471cbe6ed27f7b705b0512f1078d4 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/nodal-tools
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@86eb4d62011471cbe6ed27f7b705b0512f1078d4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file polar_high-1.3.0-py3-none-any.whl.
File metadata
- Download URL: polar_high-1.3.0-py3-none-any.whl
- Upload date:
- Size: 90.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45b0f6032c9c20a586909c7507b0fd6f979ead8c19eb0ac581fd9dc13c96978b
|
|
| MD5 |
51926ce983bc42a008aba97d8d5cf994
|
|
| BLAKE2b-256 |
7cf405b8e2ad3d8594b103cffb87e5c38b47d45e34670ea38c9ecbac7fdce0c5
|
Provenance
The following attestation bundles were made for polar_high-1.3.0-py3-none-any.whl:
Publisher:
release.yml on nodal-tools/polar-high
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polar_high-1.3.0-py3-none-any.whl -
Subject digest:
45b0f6032c9c20a586909c7507b0fd6f979ead8c19eb0ac581fd9dc13c96978b - Sigstore transparency entry: 1603411691
- Sigstore integration time:
-
Permalink:
nodal-tools/polar-high@86eb4d62011471cbe6ed27f7b705b0512f1078d4 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/nodal-tools
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@86eb4d62011471cbe6ed27f7b705b0512f1078d4 -
Trigger Event:
push
-
Statement type: