pyomof
Encode and decode Pyomo models to and from 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
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,sqrtand 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, so1and"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,
Expressioncomponents (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
Metadata
Release files for pyomof 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyomof-0.1.0.tar.gz | 12.2 kB | Without provenance | Details for pyomof-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyomof-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pyomof-0.1.0-py3-none-any.whl |
Total release size: 22.6 kB
Release files / pyomof-0.1.0.tar.gz
| Download URL | pyomof-0.1.0.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc94c8636f164df37f338ab625f6fd00b7ff76f88c548a7dd7da2a6d5c9622b1
|
|
BLAKE2b-256 checksum How to use checksums |
59ddfa1d5c323d4eae24e9b7c068cc6dedcf34e536f6d01b5b1d77f52f100dbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.5
|
Release files / pyomof-0.1.0-py3-none-any.whl
| Download URL | pyomof-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ba3a52d9eecbd54dc9f5629ecb23ef6d7f946dbba336c53c494e3c4f2f2fb6f
|
|
BLAKE2b-256 checksum How to use checksums |
79b177b0b6d35102de19adaf484418663683572354a553e531e0fd857e46afd1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.5
|
Security for pyomof 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.