Metadata-Version: 2.1
Name: pyomof
Version: 0.1.0
Summary: Encode/decode Pyomo models to/from MathOptFormat
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: pyomo >=6.7
Provides-Extra: test
Requires-Dist: pytest ; extra == 'test'

# pyomof

Encode and decode [Pyomo](https://www.pyomo.org/) models to and from
[MathOptFormat](https://jump.dev/MathOptFormat) (MOF), the JSON model format
used by JuMP / MathOptInterface.

Models can be written to an in-memory buffer (to send over the wire) or to a
file, and decoded back into a Pyomo model that keeps the original component
structure: Sets, Blocks, indexed Vars, Constraints and Objectives come back
under their original names and indices.

## Installation

```
pip install pyomof
```

Requires Python 3.8+ and Pyomo 6.7+.

## Quick start

```python
import pyomo.environ as pyo
import pyomof

m = pyo.ConcreteModel()
m.I = pyo.Set(initialize=[1, 2, 3])
m.x = pyo.Var(m.I, bounds=(0, 10))
m.c = pyo.Constraint(m.I, rule=lambda m, i: m.x[i] >= i)
m.obj = pyo.Objective(expr=sum(m.x[i] for i in m.I))

# Encode to an in-memory buffer (optionally gzip-compressed)
buf = pyomof.to_buffer(m, compress=True)
payload = buf.getvalue()          # bytes, ready to send

# Decode it back (gzip is detected automatically)
m2 = pyomof.from_buffer(payload)
m2.x[2].value = 5                 # same names and indices as the original
print(m2.c[3].expr)
```

## API

| Function | Description |
| --- | --- |
| `to_buffer(model, compress=False)` | Return a rewound `io.BytesIO` containing MOF JSON (gzipped if `compress`). |
| `from_buffer(data)` | Decode from `bytes` or a binary stream; gzip is auto-detected. |
| `dump(model, fp)` / `load(fp)` | Write to / read from any text or binary file-like object. |
| `dumps(model)` / `loads(s)` | Convert to / from a JSON string (or bytes). |
| `write(model, path)` / `read(path)` | Write / read a file; paths ending in `.gz` are gzipped. |
| `encode(model)` / `decode(dict)` | Convert to / from a plain MOF `dict`. |

Errors raise `MOFEncodeError` or `MOFDecodeError` (both subclasses of `ValueError`).

## What is supported

- **Variables:** continuous, integer and binary domains (standard Pyomo
  domains such as `NonNegativeReals`, `Binary`, `NonNegativeIntegers`), bounds,
  initial values and fixed values.
- **Constraints:** `<=`, `>=`, `==` and ranged constraints, including
  inactive ones.
- **Objectives:** minimise or maximise. Several objectives may be declared,
  but only one may be active.
- **Expressions:** linear, quadratic and nonlinear (`+ - * / **`, `abs`,
  `exp`, `log`, `log10`, `sqrt` and trigonometric / hyperbolic functions).
- **Structure:** Sets (including empty sets and `RangeSet`), nested and
  indexed Blocks, and components indexed by sets, set products or inline
  lists. Index types are kept, so `1` and `"1"` stay distinct.
- **Fixed variables** stay as variables in expressions, so unfixing one after
  decoding behaves as it would in the original model.

## Interoperability

Pyomo structure is stored in an extra top-level `"pyomo"` field alongside the
standard MOF content. Other MOF readers will usually ignore it, but strict
schema validators may reject it.

Files without this field (for example, written by JuMP) are decoded into a
flat model: variables in `m.x[name]` and constraints in `m.c[name]`, keyed by
their MOF names.

## Limitations

- Params are written as their current numeric values.
- Indexed Sets, rules, `Expression` components (they are inlined), suffixes,
  disjunctions, piecewise components and units are not supported.
- Components indexed by an inline list are rebuilt with an equivalent unnamed
  index set rather than the original object.
- Encoding temporarily unfixes fixed variables and refixes them afterwards, so
  do not encode a model while another thread is using it.

## Development

```
pip install -e .[test]
pytest
```
