We’ve been busy in the Python energy modelling space developing a declarative YAML language for writing the algebraic part of an optimisation model (mathspec). Think of it as an open-source alternative to the math declaration side of GAMS. We’ve had some conversations with @odow about developing an interface to JuMP, so that it can act as one of the “engines” that runs a math specification. I’d like to now explore how we can make this a reality and ask whether there are Julia developers here who would be interested in supporting us!
Some questions I hope this community can help me answer:
- Is the realm of an extension package?
- Where should Python stop and Julia start (since mathspec is a Python package)? On the scale of pure Python Julia code generator to pure Julia re-implementation of the Python package.
- Has a declarative text interface been proposed before that we could learn from the implementation of?
What is mathspec
It is a language for specifying math problems (dimensions, parameters, variables, constraints, objective) with no data and no solver attached. It is designed to work with multi-dimensional tabular data (e.g. tidy dataframes, YAXArrays) and math is specified with an implicit expectation of dimension broadcasting (so that you don’t have to define the axes of every term in an expression). It works well for quick validation of math before any data is attached and can typeset the math (e.g. to auto-populate docs).
A small example (noting that language is still under active development):
dimensions:
snapshot: { dtype: int }
generator: {}
parameters:
capacity: { dims: [generator] }
load: { dims: [snapshot] }
cost: { dims: [generator] }
variables:
dispatch:
dims: [snapshot, generator]
where: "capacity > 0"
bounds: { lower: 0, upper: capacity }
constraints:
power_balance:
dims: [snapshot]
expression: sum(dispatch, over=generator) == load
objective:
sense: minimize
expression: sum(dispatch * cost)
A possible JuMP integration plan
Rather than reimplement mathspec’s parser and checker in Julia, we could add a renderer to mathspec (Python) that emits plain JuMP code. For the spec above, the output would look something like this (AI-generated):
function build!(model, d)
S, G = d.snapshot, d.generator
@variable(model, 0 <= dispatch[s in S, g in G; d.capacity[g] > 0] <= d.capacity[g])
@constraint(model, power_balance[s in S],
sum(dispatch[s, g] for g in G if d.capacity[g] > 0) == d.load[s])
@objective(model, Min,
sum(dispatch[s, g] * d.cost[g] for s in S, g in G if d.capacity[g] > 0))
return model
end
So Julia users get an ordinary JuMP model that they can go and solve however they like, with no Python dependency at runtime. The same published spec could then be solved from Python and Julia with identical math!