pyomof 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyomof/__init__.py +78 -0
- pyomof/_meta.py +24 -0
- pyomof/decoder.py +259 -0
- pyomof/encoder.py +253 -0
- pyomof-0.1.0.dist-info/METADATA +107 -0
- pyomof-0.1.0.dist-info/RECORD +8 -0
- pyomof-0.1.0.dist-info/WHEEL +5 -0
- pyomof-0.1.0.dist-info/top_level.txt +1 -0
pyomof/__init__.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Encode/decode Pyomo models to/from MathOptFormat (https://jump.dev/MathOptFormat).
|
|
2
|
+
|
|
3
|
+
Streaming API:
|
|
4
|
+
dump(model, fp) -> write MOF JSON to a text or binary stream
|
|
5
|
+
load(fp) -> read a model from a text or binary stream
|
|
6
|
+
to_buffer(model) -> io.BytesIO ready to send over the wire
|
|
7
|
+
write(model, path) -> file (gzip if path ends in .gz)
|
|
8
|
+
read(path) -> model
|
|
9
|
+
"""
|
|
10
|
+
import gzip
|
|
11
|
+
import io
|
|
12
|
+
import json
|
|
13
|
+
|
|
14
|
+
from .decoder import MOFDecodeError, decode
|
|
15
|
+
from .encoder import MOFEncodeError, encode
|
|
16
|
+
|
|
17
|
+
__all__ = ["encode", "decode", "dump", "dumps", "load", "loads", "to_buffer",
|
|
18
|
+
"from_buffer", "write", "read", "MOFEncodeError", "MOFDecodeError"]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def dumps(model, **kw):
|
|
22
|
+
return json.dumps(encode(model), **kw)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def loads(s):
|
|
26
|
+
if isinstance(s, (bytes, bytearray)):
|
|
27
|
+
s = s.decode("utf-8")
|
|
28
|
+
return decode(json.loads(s))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def dump(model, fp, **kw):
|
|
32
|
+
"""Stream MOF JSON into ``fp`` (text or binary file-like object)."""
|
|
33
|
+
if isinstance(fp, io.TextIOBase):
|
|
34
|
+
json.dump(encode(model), fp, **kw)
|
|
35
|
+
return
|
|
36
|
+
w = io.TextIOWrapper(fp, encoding="utf-8")
|
|
37
|
+
try:
|
|
38
|
+
json.dump(encode(model), w, **kw)
|
|
39
|
+
w.flush()
|
|
40
|
+
finally:
|
|
41
|
+
w.detach()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def load(fp):
|
|
45
|
+
"""Read a model from ``fp`` (text or binary file-like object)."""
|
|
46
|
+
return decode(json.load(fp))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def to_buffer(model, compress=False, **kw):
|
|
50
|
+
"""Return a BytesIO (rewound) containing the MOF JSON, optionally gzipped."""
|
|
51
|
+
buf = io.BytesIO()
|
|
52
|
+
if compress:
|
|
53
|
+
with gzip.GzipFile(fileobj=buf, mode="wb") as gz:
|
|
54
|
+
dump(model, gz, **kw)
|
|
55
|
+
else:
|
|
56
|
+
dump(model, buf, **kw)
|
|
57
|
+
buf.seek(0)
|
|
58
|
+
return buf
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def from_buffer(buf):
|
|
62
|
+
"""Decode a model from bytes or a binary stream (auto-detects gzip)."""
|
|
63
|
+
data = buf if isinstance(buf, (bytes, bytearray)) else buf.read()
|
|
64
|
+
if data[:2] == b"\x1f\x8b":
|
|
65
|
+
data = gzip.decompress(data)
|
|
66
|
+
return loads(data)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def write(model, path, **kw):
|
|
70
|
+
opener = gzip.open if str(path).endswith(".gz") else open
|
|
71
|
+
with opener(path, "wt", encoding="utf-8") as f:
|
|
72
|
+
json.dump(encode(model), f, **kw)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def read(path):
|
|
76
|
+
opener = gzip.open if str(path).endswith(".gz") else open
|
|
77
|
+
with opener(path, "rt", encoding="utf-8") as f:
|
|
78
|
+
return load(f)
|
pyomof/_meta.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Metadata helpers shared by the encoder and decoder."""
|
|
2
|
+
|
|
3
|
+
PYOMO_FORMAT = 1
|
|
4
|
+
|
|
5
|
+
_DOMAINS = (
|
|
6
|
+
"Reals", "PositiveReals", "NonPositiveReals", "NegativeReals", "NonNegativeReals",
|
|
7
|
+
"PercentFraction", "UnitInterval", "Integers", "PositiveIntegers",
|
|
8
|
+
"NonPositiveIntegers", "NegativeIntegers", "NonNegativeIntegers", "Boolean", "Binary",
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def enc_key(k):
|
|
13
|
+
"""JSON-safe encoding of a Pyomo index (tuples are tagged, scalars kept)."""
|
|
14
|
+
if isinstance(k, tuple):
|
|
15
|
+
return {"t": [enc_key(i) for i in k]}
|
|
16
|
+
if k is None or isinstance(k, (bool, int, float, str)):
|
|
17
|
+
return k
|
|
18
|
+
raise TypeError("Unsupported index type: %r" % (k,))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def dec_key(k):
|
|
22
|
+
if isinstance(k, dict):
|
|
23
|
+
return tuple(dec_key(i) for i in k["t"])
|
|
24
|
+
return k
|
pyomof/decoder.py
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""Decode MathOptFormat (MOF) dictionaries into Pyomo models."""
|
|
2
|
+
import pyomo.environ as pyo
|
|
3
|
+
|
|
4
|
+
from ._meta import PYOMO_FORMAT, dec_key
|
|
5
|
+
|
|
6
|
+
_UNARY = {n: getattr(pyo, n) for n in (
|
|
7
|
+
"sin", "cos", "tan", "asin", "acos", "atan", "sinh", "cosh", "tanh",
|
|
8
|
+
"asinh", "acosh", "atanh", "exp", "log", "log10", "sqrt")}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MOFDecodeError(ValueError):
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _product(args):
|
|
16
|
+
out = args[0]
|
|
17
|
+
for a in args[1:]:
|
|
18
|
+
out = out * a
|
|
19
|
+
return out
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _nonlinear(f, x):
|
|
23
|
+
nodes = f.get("node_list", [])
|
|
24
|
+
|
|
25
|
+
def build(t):
|
|
26
|
+
if isinstance(t, (int, float)):
|
|
27
|
+
return t
|
|
28
|
+
typ = t["type"]
|
|
29
|
+
if typ == "variable":
|
|
30
|
+
return x[t["name"]]
|
|
31
|
+
if typ == "node":
|
|
32
|
+
return build(nodes[t["index"] - 1])
|
|
33
|
+
if typ == "real":
|
|
34
|
+
return t["value"]
|
|
35
|
+
args = [build(a) for a in t.get("args", [])]
|
|
36
|
+
if typ == "+":
|
|
37
|
+
return sum(args[1:], args[0]) if args else 0
|
|
38
|
+
if typ == "-":
|
|
39
|
+
return -args[0] if len(args) == 1 else args[0] - args[1]
|
|
40
|
+
if typ == "*":
|
|
41
|
+
return _product(args)
|
|
42
|
+
if typ == "/":
|
|
43
|
+
return args[0] / args[1]
|
|
44
|
+
if typ == "^":
|
|
45
|
+
return args[0] ** args[1]
|
|
46
|
+
if typ == "abs":
|
|
47
|
+
return abs(args[0])
|
|
48
|
+
if typ in _UNARY:
|
|
49
|
+
return _UNARY[typ](args[0])
|
|
50
|
+
raise MOFDecodeError("Unsupported nonlinear operator: %s" % typ)
|
|
51
|
+
|
|
52
|
+
return build(f["root"])
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _function(f, x):
|
|
56
|
+
typ = f["type"]
|
|
57
|
+
if typ == "Variable":
|
|
58
|
+
return x[f["name"]]
|
|
59
|
+
if typ == "ScalarAffineFunction":
|
|
60
|
+
return f.get("constant", 0.0) + sum(t["coefficient"] * x[t["variable"]] for t in f["terms"])
|
|
61
|
+
if typ == "ScalarQuadraticFunction":
|
|
62
|
+
e = f.get("constant", 0.0) + sum(t["coefficient"] * x[t["variable"]] for t in f["affine_terms"])
|
|
63
|
+
for t in f["quadratic_terms"]:
|
|
64
|
+
v1, v2 = t["variable_1"], t["variable_2"]
|
|
65
|
+
c = t["coefficient"] / 2 if v1 == v2 else t["coefficient"]
|
|
66
|
+
e = e + c * x[v1] * x[v2]
|
|
67
|
+
return e
|
|
68
|
+
if typ == "ScalarNonlinearFunction":
|
|
69
|
+
return _nonlinear(f, x)
|
|
70
|
+
raise MOFDecodeError("Unsupported function type: %s" % typ)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _relation(expr, s):
|
|
74
|
+
st = s["type"]
|
|
75
|
+
if st == "GreaterThan":
|
|
76
|
+
return expr >= s["lower"]
|
|
77
|
+
if st == "LessThan":
|
|
78
|
+
return expr <= s["upper"]
|
|
79
|
+
if st == "EqualTo":
|
|
80
|
+
return expr == s["value"]
|
|
81
|
+
if st == "Interval":
|
|
82
|
+
return pyo.inequality(s["lower"], expr, s["upper"])
|
|
83
|
+
raise MOFDecodeError("Unsupported set type: %s" % st)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _apply_var_set(v, s):
|
|
87
|
+
st = s["type"]
|
|
88
|
+
if st == "ZeroOne":
|
|
89
|
+
v.domain = pyo.Binary
|
|
90
|
+
elif st == "Integer":
|
|
91
|
+
v.domain = pyo.Integers
|
|
92
|
+
elif st == "GreaterThan":
|
|
93
|
+
v.setlb(s["lower"])
|
|
94
|
+
elif st == "LessThan":
|
|
95
|
+
v.setub(s["upper"])
|
|
96
|
+
elif st == "Interval":
|
|
97
|
+
v.setlb(s["lower"]); v.setub(s["upper"])
|
|
98
|
+
elif st == "EqualTo":
|
|
99
|
+
v.fix(s["value"])
|
|
100
|
+
else:
|
|
101
|
+
raise MOFDecodeError("Unsupported variable set type: %s" % st)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _index_args(index, parent_lookup):
|
|
105
|
+
if index is None:
|
|
106
|
+
return ()
|
|
107
|
+
if "sets" in index:
|
|
108
|
+
return tuple(parent_lookup(p) for p in index["sets"])
|
|
109
|
+
keys = [dec_key(k) for k in index["keys"]]
|
|
110
|
+
return (pyo.Set(initialize=keys, ordered=True, dimen=index.get("dimen")),)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _decode_structured(data, meta):
|
|
114
|
+
if meta.get("format") != PYOMO_FORMAT:
|
|
115
|
+
raise MOFDecodeError("Unsupported pyomo metadata format: %r" % meta.get("format"))
|
|
116
|
+
m = pyo.ConcreteModel(name=data.get("name", "MOFModel"))
|
|
117
|
+
|
|
118
|
+
def find(path):
|
|
119
|
+
c = m if path == "" else m.find_component(path)
|
|
120
|
+
if c is None:
|
|
121
|
+
raise MOFDecodeError("Unknown component path: %s" % path)
|
|
122
|
+
return c
|
|
123
|
+
|
|
124
|
+
starts = {v["name"]: v.get("primal_start") for v in data["variables"]}
|
|
125
|
+
vmap, mof_cons, pending = {}, {}, []
|
|
126
|
+
for con in data.get("constraints", []):
|
|
127
|
+
if con["function"]["type"] != "Variable":
|
|
128
|
+
mof_cons[con["name"]] = con
|
|
129
|
+
|
|
130
|
+
# Pass 1: build Blocks, Sets, Vars and empty Constraint/Objective containers.
|
|
131
|
+
for c in meta["components"]:
|
|
132
|
+
kind, parent = c["kind"], find(c["parent"])
|
|
133
|
+
if kind in ("Set", "RangeSet"):
|
|
134
|
+
if "range" in c:
|
|
135
|
+
comp = pyo.RangeSet(*c["range"])
|
|
136
|
+
else:
|
|
137
|
+
comp = pyo.Set(initialize=[dec_key(k) for k in c["values"]],
|
|
138
|
+
ordered=c["ordered"],
|
|
139
|
+
dimen=c["dimen"])
|
|
140
|
+
parent.add_component(c["name"], comp)
|
|
141
|
+
continue
|
|
142
|
+
args = _index_args(c["index"], find)
|
|
143
|
+
if kind == "Block":
|
|
144
|
+
parent.add_component(c["name"], pyo.Block(*args))
|
|
145
|
+
comp = getattr(parent, c["name"])
|
|
146
|
+
active = {dec_key(k) for k in c["active"]}
|
|
147
|
+
for k, d in comp.items():
|
|
148
|
+
if (k if comp.is_indexed() else None) not in active:
|
|
149
|
+
d.deactivate()
|
|
150
|
+
elif kind == "Var":
|
|
151
|
+
parent.add_component(c["name"], pyo.Var(*args, dense=True))
|
|
152
|
+
comp = getattr(parent, c["name"])
|
|
153
|
+
for item in c["data"]:
|
|
154
|
+
d = comp[dec_key(item["key"])]
|
|
155
|
+
d.domain = getattr(pyo, item["domain"])
|
|
156
|
+
d.value = starts.get(item["name"])
|
|
157
|
+
vmap[item["name"]] = (d, item)
|
|
158
|
+
elif kind == "Constraint":
|
|
159
|
+
parent.add_component(c["name"], pyo.Constraint(*args))
|
|
160
|
+
pending.append((getattr(parent, c["name"]), c))
|
|
161
|
+
elif kind == "Objective":
|
|
162
|
+
parent.add_component(c["name"], pyo.Objective(*args))
|
|
163
|
+
pending.append((getattr(parent, c["name"]), c))
|
|
164
|
+
else:
|
|
165
|
+
raise MOFDecodeError("Unsupported component kind: %s" % kind)
|
|
166
|
+
|
|
167
|
+
# Variable bounds / fixing from MOF, then restore Pyomo-specific state.
|
|
168
|
+
for con in data.get("constraints", []):
|
|
169
|
+
f = con["function"]
|
|
170
|
+
if f["type"] == "Variable":
|
|
171
|
+
if con["set"]["type"] in ("ZeroOne", "Integer"):
|
|
172
|
+
continue # domain restored from metadata
|
|
173
|
+
_apply_var_set(vmap[f["name"]][0], con["set"])
|
|
174
|
+
x = {}
|
|
175
|
+
for n, (d, item) in vmap.items():
|
|
176
|
+
x[n] = d
|
|
177
|
+
if item.get("fixed"):
|
|
178
|
+
d.setlb(item["lb"]); d.setub(item["ub"]); d.fix()
|
|
179
|
+
|
|
180
|
+
# Pass 2: fill constraints and objectives.
|
|
181
|
+
for comp, c in pending:
|
|
182
|
+
for item in c["data"]:
|
|
183
|
+
key = dec_key(item["key"])
|
|
184
|
+
if c["kind"] == "Constraint":
|
|
185
|
+
src = item.get("inactive") or mof_cons[item["name"]]
|
|
186
|
+
expr = _relation(_function(src["function"], x), src["set"])
|
|
187
|
+
else:
|
|
188
|
+
src = item.get("inactive") or data["objective"]
|
|
189
|
+
expr = _function(src["function"], x)
|
|
190
|
+
if comp.is_indexed():
|
|
191
|
+
comp[key] = expr
|
|
192
|
+
d = comp[key]
|
|
193
|
+
else:
|
|
194
|
+
comp.set_value(expr)
|
|
195
|
+
d = comp
|
|
196
|
+
if c["kind"] == "Objective":
|
|
197
|
+
d.sense = pyo.maximize if src["sense"] == "max" else pyo.minimize
|
|
198
|
+
if "inactive" in item:
|
|
199
|
+
d.deactivate()
|
|
200
|
+
return m
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def decode(data):
|
|
204
|
+
"""Build a ConcreteModel from a MOF dict.
|
|
205
|
+
|
|
206
|
+
If the dict carries ``"pyomo"`` structure metadata (written by
|
|
207
|
+
``encode``), the original Sets, Blocks and indexed components are rebuilt
|
|
208
|
+
under their original names. Otherwise variables are stored in ``model.x``
|
|
209
|
+
indexed by MOF name and constraints in ``model.c``.
|
|
210
|
+
"""
|
|
211
|
+
if data.get("version", {}).get("major") != 1:
|
|
212
|
+
raise MOFDecodeError("Unsupported MOF version: %r" % data.get("version"))
|
|
213
|
+
if "pyomo" in data:
|
|
214
|
+
return _decode_structured(data, data["pyomo"])
|
|
215
|
+
m = pyo.ConcreteModel(name=data.get("name", "MOFModel"))
|
|
216
|
+
names = [v["name"] for v in data["variables"]]
|
|
217
|
+
m.x = pyo.Var(pyo.Set(initialize=names, ordered=True, dimen=1), dense=True)
|
|
218
|
+
for v in data["variables"]:
|
|
219
|
+
if "primal_start" in v:
|
|
220
|
+
m.x[v["name"]].value = v["primal_start"]
|
|
221
|
+
|
|
222
|
+
obj = data.get("objective", {"sense": "feasibility"})
|
|
223
|
+
if obj["sense"] != "feasibility":
|
|
224
|
+
m.obj = pyo.Objective(expr=_function(obj["function"], m.x),
|
|
225
|
+
sense=pyo.maximize if obj["sense"] == "max" else pyo.minimize)
|
|
226
|
+
|
|
227
|
+
m.c = pyo.Constraint(pyo.Any)
|
|
228
|
+
for i, con in enumerate(data.get("constraints", [])):
|
|
229
|
+
f, s = con["function"], con["set"]
|
|
230
|
+
st = s["type"]
|
|
231
|
+
if f["type"] == "Variable":
|
|
232
|
+
v = m.x[f["name"]]
|
|
233
|
+
if st == "ZeroOne":
|
|
234
|
+
v.domain = pyo.Binary
|
|
235
|
+
continue
|
|
236
|
+
if st == "Integer":
|
|
237
|
+
v.domain = pyo.Integers
|
|
238
|
+
continue
|
|
239
|
+
if st == "GreaterThan":
|
|
240
|
+
v.setlb(s["lower"]); continue
|
|
241
|
+
if st == "LessThan":
|
|
242
|
+
v.setub(s["upper"]); continue
|
|
243
|
+
if st == "Interval":
|
|
244
|
+
v.setlb(s["lower"]); v.setub(s["upper"]); continue
|
|
245
|
+
if st == "EqualTo":
|
|
246
|
+
v.fix(s["value"]); continue
|
|
247
|
+
expr = _function(f, m.x)
|
|
248
|
+
key = con.get("name", "c%d" % i)
|
|
249
|
+
if st == "GreaterThan":
|
|
250
|
+
m.c[key] = expr >= s["lower"]
|
|
251
|
+
elif st == "LessThan":
|
|
252
|
+
m.c[key] = expr <= s["upper"]
|
|
253
|
+
elif st == "EqualTo":
|
|
254
|
+
m.c[key] = expr == s["value"]
|
|
255
|
+
elif st == "Interval":
|
|
256
|
+
m.c[key] = pyo.inequality(s["lower"], expr, s["upper"])
|
|
257
|
+
else:
|
|
258
|
+
raise MOFDecodeError("Unsupported set type: %s" % st)
|
|
259
|
+
return m
|
pyomof/encoder.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""Encode Pyomo models into MathOptFormat (MOF) dictionaries."""
|
|
2
|
+
from pyomo.common.collections import ComponentSet
|
|
3
|
+
from pyomo.core.base.var import VarData
|
|
4
|
+
from pyomo.core.expr import numeric_expr as ne
|
|
5
|
+
from pyomo.environ import Block, Constraint, Objective, RangeSet, Set, Var, maximize, value
|
|
6
|
+
from pyomo.repn import generate_standard_repn
|
|
7
|
+
|
|
8
|
+
from ._meta import _DOMAINS, PYOMO_FORMAT, enc_key
|
|
9
|
+
|
|
10
|
+
MOF_VERSION = {"major": 1, "minor": 7}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MOFEncodeError(ValueError):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _name(v):
|
|
18
|
+
return v.getname(fully_qualified=True)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _set(lb, ub):
|
|
22
|
+
if lb is not None and ub is not None:
|
|
23
|
+
if lb == ub:
|
|
24
|
+
return {"type": "EqualTo", "value": lb}
|
|
25
|
+
return {"type": "Interval", "lower": lb, "upper": ub}
|
|
26
|
+
if lb is not None:
|
|
27
|
+
return {"type": "GreaterThan", "lower": lb}
|
|
28
|
+
if ub is not None:
|
|
29
|
+
return {"type": "LessThan", "upper": ub}
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _NonlinearWriter:
|
|
34
|
+
def __init__(self):
|
|
35
|
+
self.node_list = []
|
|
36
|
+
|
|
37
|
+
def _push(self, node):
|
|
38
|
+
self.node_list.append(node)
|
|
39
|
+
return {"type": "node", "index": len(self.node_list)}
|
|
40
|
+
|
|
41
|
+
def term(self, e, root=False):
|
|
42
|
+
if e.__class__ in (int, float) or not hasattr(e, "is_expression_type"):
|
|
43
|
+
return float(e)
|
|
44
|
+
if isinstance(e, VarData):
|
|
45
|
+
return {"type": "variable", "name": _name(e)}
|
|
46
|
+
if not e.is_potentially_variable():
|
|
47
|
+
return float(value(e))
|
|
48
|
+
if e.is_named_expression_type():
|
|
49
|
+
return self.term(e.expr, root)
|
|
50
|
+
if isinstance(e, (ne.SumExpression, ne.LinearExpression)):
|
|
51
|
+
op = "+"
|
|
52
|
+
elif isinstance(e, (ne.ProductExpression, ne.MonomialTermExpression)):
|
|
53
|
+
op = "*"
|
|
54
|
+
elif isinstance(e, ne.DivisionExpression):
|
|
55
|
+
op = "/"
|
|
56
|
+
elif isinstance(e, ne.PowExpression):
|
|
57
|
+
op = "^"
|
|
58
|
+
elif isinstance(e, ne.NegationExpression):
|
|
59
|
+
op = "-"
|
|
60
|
+
elif isinstance(e, ne.AbsExpression):
|
|
61
|
+
op = "abs"
|
|
62
|
+
elif isinstance(e, ne.UnaryFunctionExpression):
|
|
63
|
+
op = e.getname()
|
|
64
|
+
else:
|
|
65
|
+
raise MOFEncodeError("Unsupported expression type: %s" % type(e).__name__)
|
|
66
|
+
node = {"type": op, "args": [self.term(a) for a in e.args]}
|
|
67
|
+
return node if root else self._push(node)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _function(expr):
|
|
71
|
+
"""Return a MOF function (constant term included)."""
|
|
72
|
+
f, c = _function_split(expr)
|
|
73
|
+
if f["type"] != "ScalarNonlinearFunction":
|
|
74
|
+
f["constant"] = c
|
|
75
|
+
return f
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _function_split(expr):
|
|
79
|
+
"""Return (mof_function, constant_offset)."""
|
|
80
|
+
repn = generate_standard_repn(expr, quadratic=True, compute_values=True)
|
|
81
|
+
if repn.nonlinear_expr is not None:
|
|
82
|
+
w = _NonlinearWriter()
|
|
83
|
+
root = w.term(expr, root=True)
|
|
84
|
+
if not isinstance(root, dict) or root.get("type") in ("variable", "node"):
|
|
85
|
+
root = {"type": "+", "args": [root]}
|
|
86
|
+
return {"type": "ScalarNonlinearFunction", "root": root,
|
|
87
|
+
"node_list": w.node_list}, 0.0
|
|
88
|
+
affine = [{"coefficient": float(c), "variable": _name(v)}
|
|
89
|
+
for v, c in zip(repn.linear_vars, repn.linear_coefs)]
|
|
90
|
+
const = float(repn.constant)
|
|
91
|
+
if repn.quadratic_vars:
|
|
92
|
+
quad = []
|
|
93
|
+
for (v1, v2), c in zip(repn.quadratic_vars, repn.quadratic_coefs):
|
|
94
|
+
c = float(c)
|
|
95
|
+
# MOF/MOI convention: f = 1/2 x'Qx, so diagonal terms are doubled.
|
|
96
|
+
quad.append({"coefficient": 2 * c if v1 is v2 else c,
|
|
97
|
+
"variable_1": _name(v1), "variable_2": _name(v2)})
|
|
98
|
+
return {"type": "ScalarQuadraticFunction", "affine_terms": affine,
|
|
99
|
+
"quadratic_terms": quad, "constant": 0.0}, const
|
|
100
|
+
return {"type": "ScalarAffineFunction", "terms": affine, "constant": 0.0}, const
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _fnum(x):
|
|
104
|
+
return None if x is None else float(value(x))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _domain_name(v):
|
|
108
|
+
n = getattr(v.domain, "name", None)
|
|
109
|
+
if n not in _DOMAINS:
|
|
110
|
+
raise MOFEncodeError("Unsupported domain for variable %s" % _name(v))
|
|
111
|
+
return n
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _encode_var(v, variables, constraints, is_fixed):
|
|
115
|
+
n = _name(v)
|
|
116
|
+
entry = {"name": n}
|
|
117
|
+
if v.value is not None:
|
|
118
|
+
entry["primal_start"] = float(v.value)
|
|
119
|
+
variables.append(entry)
|
|
120
|
+
fvar = {"type": "Variable", "name": n}
|
|
121
|
+
if v.is_binary():
|
|
122
|
+
constraints.append({"function": fvar, "set": {"type": "ZeroOne"}})
|
|
123
|
+
elif v.is_integer():
|
|
124
|
+
constraints.append({"function": fvar, "set": {"type": "Integer"}})
|
|
125
|
+
elif not v.is_continuous():
|
|
126
|
+
raise MOFEncodeError("Unsupported domain for variable %s" % n)
|
|
127
|
+
if is_fixed:
|
|
128
|
+
s = {"type": "EqualTo", "value": float(v.value)}
|
|
129
|
+
elif v.is_binary():
|
|
130
|
+
s = None
|
|
131
|
+
else:
|
|
132
|
+
s = _set(_fnum(v.lb), _fnum(v.ub))
|
|
133
|
+
if s is not None:
|
|
134
|
+
constraints.append({"function": fvar, "set": s})
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _encode_con(con):
|
|
138
|
+
"""Return (function, set) or None if the constraint is unbounded."""
|
|
139
|
+
s = _set(_fnum(con.lower), _fnum(con.upper))
|
|
140
|
+
return None if s is None else (_function(con.body), s)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _encode_obj(o):
|
|
144
|
+
return {"sense": "max" if o.sense == maximize else "min", "function": _function(o.expr)}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _rel(comp, model):
|
|
148
|
+
return comp.getname(fully_qualified=True, relative_to=model)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _parent(comp, model):
|
|
152
|
+
blk = comp.parent_block()
|
|
153
|
+
return "" if blk is model else _rel(blk, model)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _index_ref(comp, model, set_paths):
|
|
157
|
+
"""Describe a component's index: list of set paths, or inline keys."""
|
|
158
|
+
if not comp.is_indexed():
|
|
159
|
+
return None
|
|
160
|
+
idx = comp.index_set()
|
|
161
|
+
if id(idx) in set_paths:
|
|
162
|
+
return {"sets": [set_paths[id(idx)]]}
|
|
163
|
+
subs = list(idx.subsets()) if hasattr(idx, "subsets") else []
|
|
164
|
+
if len(subs) > 1 and all(id(s) in set_paths for s in subs):
|
|
165
|
+
return {"sets": [set_paths[id(s)] for s in subs]}
|
|
166
|
+
return {"keys": [enc_key(k) for k in idx], "dimen": idx.dimen}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _encode_set(s):
|
|
170
|
+
entry = {"ordered": bool(s.isordered()), "dimen": s.dimen}
|
|
171
|
+
if s.ctype is RangeSet:
|
|
172
|
+
ranges = list(s.ranges())
|
|
173
|
+
if len(ranges) == 1 and ranges[0].step:
|
|
174
|
+
r = ranges[0]
|
|
175
|
+
entry["range"] = [r.start, r.end, r.step]
|
|
176
|
+
return entry
|
|
177
|
+
entry["values"] = [enc_key(k) for k in s]
|
|
178
|
+
return entry
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def encode(model, name=None):
|
|
182
|
+
"""Convert a Pyomo model (or block) into a MOF-compatible dict.
|
|
183
|
+
|
|
184
|
+
A ``"pyomo"`` field records the component structure so ``decode`` can
|
|
185
|
+
rebuild Sets, Blocks, indexed Vars/Constraints/Objectives exactly.
|
|
186
|
+
"""
|
|
187
|
+
variables, constraints, comps, set_paths = [], [], [], {}
|
|
188
|
+
active_obj = []
|
|
189
|
+
fixed = ComponentSet(v for v in model.component_data_objects(Var, descend_into=True) if v.fixed)
|
|
190
|
+
# Unfix temporarily so fixed variables stay symbolic in expressions.
|
|
191
|
+
for v in fixed:
|
|
192
|
+
v.unfix()
|
|
193
|
+
try:
|
|
194
|
+
for comp in model.component_objects((Set, RangeSet, Var, Constraint, Objective, Block),
|
|
195
|
+
descend_into=True):
|
|
196
|
+
if comp.ctype in (Set, RangeSet) and comp.is_indexed():
|
|
197
|
+
raise MOFEncodeError("Indexed Sets are not supported: %s" % comp.name)
|
|
198
|
+
path = _rel(comp, model)
|
|
199
|
+
entry = {"kind": comp.ctype.__name__, "name": comp.local_name,
|
|
200
|
+
"parent": _parent(comp, model)}
|
|
201
|
+
if comp.ctype in (Set, RangeSet):
|
|
202
|
+
entry.update(_encode_set(comp))
|
|
203
|
+
set_paths[id(comp)] = path
|
|
204
|
+
comps.append(entry)
|
|
205
|
+
continue
|
|
206
|
+
entry["index"] = _index_ref(comp, model, set_paths)
|
|
207
|
+
data = []
|
|
208
|
+
for k, d in comp.items():
|
|
209
|
+
item = {"key": enc_key(k)}
|
|
210
|
+
if comp.ctype is Var:
|
|
211
|
+
item["name"] = _name(d)
|
|
212
|
+
item["domain"] = _domain_name(d)
|
|
213
|
+
if d in fixed:
|
|
214
|
+
item["fixed"] = True
|
|
215
|
+
item["lb"], item["ub"] = _fnum(d.lb), _fnum(d.ub)
|
|
216
|
+
_encode_var(d, variables, constraints, d in fixed)
|
|
217
|
+
elif comp.ctype is Constraint:
|
|
218
|
+
fs = _encode_con(d)
|
|
219
|
+
if fs is None:
|
|
220
|
+
continue
|
|
221
|
+
if d.active:
|
|
222
|
+
item["name"] = _name(d)
|
|
223
|
+
constraints.append({"name": item["name"], "function": fs[0], "set": fs[1]})
|
|
224
|
+
else:
|
|
225
|
+
item["inactive"] = {"function": fs[0], "set": fs[1]}
|
|
226
|
+
elif comp.ctype is Objective:
|
|
227
|
+
if d.active:
|
|
228
|
+
active_obj.append(d)
|
|
229
|
+
item["active"] = True
|
|
230
|
+
else:
|
|
231
|
+
item["inactive"] = _encode_obj(d)
|
|
232
|
+
data.append(item)
|
|
233
|
+
if comp.ctype is Block:
|
|
234
|
+
entry["active"] = [enc_key(k) for k, d in comp.items() if d.active]
|
|
235
|
+
else:
|
|
236
|
+
entry["data"] = data
|
|
237
|
+
comps.append(entry)
|
|
238
|
+
|
|
239
|
+
if len(active_obj) > 1:
|
|
240
|
+
raise MOFEncodeError("MOF supports at most one active objective")
|
|
241
|
+
objective = _encode_obj(active_obj[0]) if active_obj else {"sense": "feasibility"}
|
|
242
|
+
finally:
|
|
243
|
+
for v in fixed:
|
|
244
|
+
v.fix()
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
"name": name or model.name,
|
|
248
|
+
"version": dict(MOF_VERSION),
|
|
249
|
+
"variables": variables,
|
|
250
|
+
"objective": objective,
|
|
251
|
+
"constraints": constraints,
|
|
252
|
+
"pyomo": {"format": PYOMO_FORMAT, "components": comps},
|
|
253
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyomof
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Encode/decode Pyomo models to/from MathOptFormat
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: pyomo >=6.7
|
|
8
|
+
Provides-Extra: test
|
|
9
|
+
Requires-Dist: pytest ; extra == 'test'
|
|
10
|
+
|
|
11
|
+
# pyomof
|
|
12
|
+
|
|
13
|
+
Encode and decode [Pyomo](https://www.pyomo.org/) models to and from
|
|
14
|
+
[MathOptFormat](https://jump.dev/MathOptFormat) (MOF), the JSON model format
|
|
15
|
+
used by JuMP / MathOptInterface.
|
|
16
|
+
|
|
17
|
+
Models can be written to an in-memory buffer (to send over the wire) or to a
|
|
18
|
+
file, and decoded back into a Pyomo model that keeps the original component
|
|
19
|
+
structure: Sets, Blocks, indexed Vars, Constraints and Objectives come back
|
|
20
|
+
under their original names and indices.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install pyomof
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Requires Python 3.8+ and Pyomo 6.7+.
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import pyomo.environ as pyo
|
|
34
|
+
import pyomof
|
|
35
|
+
|
|
36
|
+
m = pyo.ConcreteModel()
|
|
37
|
+
m.I = pyo.Set(initialize=[1, 2, 3])
|
|
38
|
+
m.x = pyo.Var(m.I, bounds=(0, 10))
|
|
39
|
+
m.c = pyo.Constraint(m.I, rule=lambda m, i: m.x[i] >= i)
|
|
40
|
+
m.obj = pyo.Objective(expr=sum(m.x[i] for i in m.I))
|
|
41
|
+
|
|
42
|
+
# Encode to an in-memory buffer (optionally gzip-compressed)
|
|
43
|
+
buf = pyomof.to_buffer(m, compress=True)
|
|
44
|
+
payload = buf.getvalue() # bytes, ready to send
|
|
45
|
+
|
|
46
|
+
# Decode it back (gzip is detected automatically)
|
|
47
|
+
m2 = pyomof.from_buffer(payload)
|
|
48
|
+
m2.x[2].value = 5 # same names and indices as the original
|
|
49
|
+
print(m2.c[3].expr)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
| Function | Description |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
| `to_buffer(model, compress=False)` | Return a rewound `io.BytesIO` containing MOF JSON (gzipped if `compress`). |
|
|
57
|
+
| `from_buffer(data)` | Decode from `bytes` or a binary stream; gzip is auto-detected. |
|
|
58
|
+
| `dump(model, fp)` / `load(fp)` | Write to / read from any text or binary file-like object. |
|
|
59
|
+
| `dumps(model)` / `loads(s)` | Convert to / from a JSON string (or bytes). |
|
|
60
|
+
| `write(model, path)` / `read(path)` | Write / read a file; paths ending in `.gz` are gzipped. |
|
|
61
|
+
| `encode(model)` / `decode(dict)` | Convert to / from a plain MOF `dict`. |
|
|
62
|
+
|
|
63
|
+
Errors raise `MOFEncodeError` or `MOFDecodeError` (both subclasses of `ValueError`).
|
|
64
|
+
|
|
65
|
+
## What is supported
|
|
66
|
+
|
|
67
|
+
- **Variables:** continuous, integer and binary domains (standard Pyomo
|
|
68
|
+
domains such as `NonNegativeReals`, `Binary`, `NonNegativeIntegers`), bounds,
|
|
69
|
+
initial values and fixed values.
|
|
70
|
+
- **Constraints:** `<=`, `>=`, `==` and ranged constraints, including
|
|
71
|
+
inactive ones.
|
|
72
|
+
- **Objectives:** minimise or maximise. Several objectives may be declared,
|
|
73
|
+
but only one may be active.
|
|
74
|
+
- **Expressions:** linear, quadratic and nonlinear (`+ - * / **`, `abs`,
|
|
75
|
+
`exp`, `log`, `log10`, `sqrt` and trigonometric / hyperbolic functions).
|
|
76
|
+
- **Structure:** Sets (including empty sets and `RangeSet`), nested and
|
|
77
|
+
indexed Blocks, and components indexed by sets, set products or inline
|
|
78
|
+
lists. Index types are kept, so `1` and `"1"` stay distinct.
|
|
79
|
+
- **Fixed variables** stay as variables in expressions, so unfixing one after
|
|
80
|
+
decoding behaves as it would in the original model.
|
|
81
|
+
|
|
82
|
+
## Interoperability
|
|
83
|
+
|
|
84
|
+
Pyomo structure is stored in an extra top-level `"pyomo"` field alongside the
|
|
85
|
+
standard MOF content. Other MOF readers will usually ignore it, but strict
|
|
86
|
+
schema validators may reject it.
|
|
87
|
+
|
|
88
|
+
Files without this field (for example, written by JuMP) are decoded into a
|
|
89
|
+
flat model: variables in `m.x[name]` and constraints in `m.c[name]`, keyed by
|
|
90
|
+
their MOF names.
|
|
91
|
+
|
|
92
|
+
## Limitations
|
|
93
|
+
|
|
94
|
+
- Params are written as their current numeric values.
|
|
95
|
+
- Indexed Sets, rules, `Expression` components (they are inlined), suffixes,
|
|
96
|
+
disjunctions, piecewise components and units are not supported.
|
|
97
|
+
- Components indexed by an inline list are rebuilt with an equivalent unnamed
|
|
98
|
+
index set rather than the original object.
|
|
99
|
+
- Encoding temporarily unfixes fixed variables and refixes them afterwards, so
|
|
100
|
+
do not encode a model while another thread is using it.
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
pip install -e .[test]
|
|
106
|
+
pytest
|
|
107
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pyomof/__init__.py,sha256=ZepsCa_EdAX8jrIsokj9dLKY8jg7mvG6AI1Sq3TW7xw,2358
|
|
2
|
+
pyomof/_meta.py,sha256=41hOiIhehJAvmpMhk4ovD_HrXU9ka-8CSTHp39J60zo,781
|
|
3
|
+
pyomof/decoder.py,sha256=osvjZAkYVmMQjihsQK0jmpLbQyP_HMMbipvd9bGzNCg,9712
|
|
4
|
+
pyomof/encoder.py,sha256=W2Sb0s4aDcKpEstVdXIAC63V7aLjNoAnkoOdRgI5a1c,9646
|
|
5
|
+
pyomof-0.1.0.dist-info/METADATA,sha256=8y9mBaltXLLc30QVz9-LPLH3aNlTBbV9puHeQXaTdxs,4015
|
|
6
|
+
pyomof-0.1.0.dist-info/WHEEL,sha256=BNRMDyzLkkcmlv0J8ppDQkk2VED33SesJDynr9ED1gc,91
|
|
7
|
+
pyomof-0.1.0.dist-info/top_level.txt,sha256=fterkvtP8a_40DI4C1h_tMIpzKxyJZaqoyvUiL7lg5s,7
|
|
8
|
+
pyomof-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyomof
|