Metadata-Version: 2.4
Name: einheit
Version: 0.6.0
Requires-Dist: einheit-cli==0.6.0 ; extra == 'cli'
Requires-Dist: pulp>=2.7 ; extra == 'pulp'
Requires-Dist: pyomo>=6.8 ; extra == 'pyomo'
Provides-Extra: cli
Provides-Extra: pulp
Provides-Extra: pyomo
License-File: LICENSE
Summary: Python bindings for Einheit, a linear and mixed-integer programming solver written in Rust.
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://docs.einheit.tech
Project-URL: Homepage, https://einheit.tech

# einheit (Python)

Python bindings for Einheit, a linear and mixed-integer programming solver written in Rust. The
solver runs as native code inside the package; Python builds the model and reads the result. The
package also works as a solver backend for PuLP and Pyomo.

A linear program is solved with the dual simplex. A model with integer variables is solved by a
branch-and-bound search that races its work across cores. **MILP is a preview**: its interfaces
and defaults may change between releases.

## Install

```sh
pip install einheit
```

To use Einheit as a PuLP or Pyomo solver, install the matching extra:

```sh
pip install "einheit[pulp]"
pip install "einheit[pyomo]"
```

Requires Python 3.11 or newer. Wheels are published for Linux x86_64, macOS arm64, and Windows
x86_64. They include the compiled solver, so no Rust toolchain is needed. There is no source
distribution, so pip cannot install Einheit on other platforms yet.

## Quick start

```python
import einheit

lp = einheit.parse_mps(open("afiro.mps").read())
sol = einheit.solve(lp)
if sol.is_optimal:
    print(sol.objective)   # -464.7531428571
    print(sol.x)           # primal solution, length lp.num_cols
```

## API

### Building a model

Three ways to get an `Lp`:

```python
import einheit, math

# 1. MPS text
lp = einheit.parse_mps(open("model.mps").read())

# 2. CPLEX / Gurobi LP-format text
lp = einheit.parse_lp(open("model.lp").read())

# 3. Programmatic builder — no file needed
b = einheit.LpBuilder("demo")
b.sense("maximize")                              # default is "minimize"
x = b.add_var("x", 3.0, 0.0, math.inf)          # returns VarRef
y = b.add_var("y", 2.0, 0.0, math.inf)
b.add_constraint("c1", [(x, 1.0), (y, 1.0)], -math.inf, 4.0)
b.add_constraint("c2", [(x, 1.0), (y, 3.0)], -math.inf, 6.0)
lp = b.build()                                   # raises ValueError on bad bounds / refs
```

### Reference

- `parse_mps(text: str) -> Lp` — parse an MPS model (raises `ValueError` on a bad model).
- `parse_lp(text: str) -> Lp` — parse a CPLEX / Gurobi LP-format model (raises `ValueError`
  on a bad model). Produces the same `Lp` as `parse_mps` — the two readers are interchangeable.
- `LpBuilder(name: str = "") -> LpBuilder` — programmatic model builder.
  - `.sense(sense: str)` — `"minimize"` (default) or `"maximize"`.
  - `.objective_offset(offset: float)` — constant term added to the objective.
  - `.add_var(name, obj, lower, upper, *, integer=False) -> VarRef` — add a variable; returns an
    opaque handle. `integer=True` restricts it to integer values (binary when its bounds are
    `[0, 1]`).
  - `.add_constraint(name, coeffs, lower, upper)` — add a ranged row; `coeffs` is a list of
    `(VarRef, float)` pairs.
  - `.build() -> Lp` — validate and pack; raises `ValueError` on bad bounds or foreign refs.
- `VarRef` — opaque column handle; `.index` gives the column number.
- `Lp` — `num_rows`, `num_cols`, `nnz`, `name`, `col_names`, `row_names`, `has_integer_vars`
  (all read-only).
- `solve(lp, *, time_limit=None, presolve=None, ...) -> Solution` — solve to optimum /
  infeasible / unbounded. Releases the GIL while the solver runs. Every keyword defaults to
  `None`, which keeps the Rust default. A model with integer variables is solved by branch and
  bound; `relax=True` solves its continuous relaxation instead.
  - LP only: `max_iters`, `driver` (`"primal"` / `"dual"`), `edge_weight` (`"dantzig"` /
    `"steepest"`), `dual_pricer`, `backend`.
  - MILP only: `node_limit`, `mip_gap`, `racers`, `brancher`, `heuristics`, `separators`,
    `lazy`.
  - Both: `time_limit`, `presolve`, `observers`, `controllers`, `plugin_settings`.

  An argument for the other kind of model, or an unknown string, raises `ValueError`.
  `help(einheit.solve)` describes each one.
- `Solution` — `status`, `is_optimal`, `objective`, `x`, `iterations`, `wall_secs`, `stop_reason`.
  An LP's `status` is `"optimal"`, `"infeasible"`, `"unbounded"` or `"iteration_limit"`; a
  MILP's adds `"time_limit"`, `"node_limit"`, `"gap_limit"`, `"memory_limit"`, `"stopped"` and
  `"unknown"`, and `nodes`, `dual_bound` and `gap` say how far the proof got (`None` on an LP).

## Modeling with einheit.form (preview)

`einheit.form` is a modeling layer: a model is written once, with no instance data, and bound to
data later; structures such as a knapsack or a network flow come with several formulations and
algorithms, picked per instance. A model is bound for a solver and solved with `einheit.solve`:

```python
import einheit
import einheit.form as ef
from einheit.form.library import Knapsack

m = ef.Model("pack")
k = m.add(Knapsack(), name="k")
m.maximize(k.total)
problem = ef.problem(m, data)            # bound for einheit.mip, the default solver
sol = einheit.solve(problem, time_limit=60)
print(sol.status, sol.objective, sol.realization)
```

`einheit.form` is a preview: its interfaces, its file format and its structure library may change
between releases.

## Links

- Documentation: <https://docs.einheit.tech> — installation, the PuLP and Pyomo guides, and the
  API reference
- Website: <https://einheit.tech>

## License

MIT License. Copyright (c) 2026 Dennis Cost.

