# Integrating MathOptInterface into an industry-scale project (e.g. constraint management)

**URL:** <https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 23, 2021, 1:57pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943 "2021-11-23T13:57:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pboes](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@pboes](https://discourse.julialang.org/u/pboes)\
**Post date:** [November 23, 2021, 1:57pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/1 "2021-11-23T13:57:59Z")

</div>

Hi,  
I have been tasked by my employer to think about a general “Operations Research Framework” that guides future operations research projects, both at the level of project management (how to organise this in an agile way, processes, etc.) but also at the level of best practices for the codebase. The types of optimization problem that this should cover are quite general (sometimes simple LPs but also sometimes highly nonlinear problems with black-box character for which anytime algos or metaheuristics would be the right choice) and MOI seems like the right level of abstraction to formulate how models should be written down, constraints documented and annotated, etc.

Part of the requirement will be that it should very simple to present one’s current understanding of the optimization problem to stakeholders, in a language that they understand and including giving a full list of all constraints that are in place. For this reason I’m thinking about how to integrate MOI into some kind of constraint management system. For instance, there could be Constraint structs with human-readable metadata and descriptions but also fields that correspond to the MOI ConstraintIndex and then these could be stored an SQL database or something like this. It would be great if this could be done without dislinking the “stakeholder-facing” version of the problem from the “solver-facing” version, in order to avoid the two versions moving out of sync, etc.

I find it hard to get started on this, moving the model definition away from the simple enumeration of variables-constraints-objective in a single file, and am wondering about how other people manage the OR problems in practice, whether tools for this exist, etc. Any help on this is much appreciated.

Cheers, Paul

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 23, 2021, 8:10pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/2 "2021-11-23T20:10:24Z")

</div>

> MOI seems like the right level of abstraction to formulate

No. You should use JuMP instead. MOI is too complicated, and lacks good support for nonlinear problems.

> Part of the requirement will be that it should very simple to present one’s current understanding of the optimization problem to stakeholders, in a language that they understand and including giving a full list of all constraints that are in place.

Use JuMP. The algebraic model can be simple to present the understanding of the optimization model to stakeholders.

> moving the model definition away from the simple enumeration of variables-constraints-objective in a single file

Many projects have success in a functional form with method dispatch to create different sets of variables or constraints.

PowerSimulations is probably the largest example of this:

> **[GitHub - NREL-Sienna/PowerSimulations.jl: Julia for optimization simulation...](https://github.com/NREL-Sienna/PowerSimulations.jl)**
>
> Julia for optimization simulation and modeling of PowerSystems. Part of the Scalable Integrated Infrastructure Planning Initiative at the National Renewable Energy Lab. - GitHub - NREL-Sienna/Power...

A concrete example might look like:

```julia
struct Object
    weight::Float64 
    profit::Float64
end

struct KnapsackData
    objects::Vector{Object}   
    capacity::Float64 
end

abstract type AbstractKnapsack end

struct BinaryKnapsack <: AbstractKnapsack end
struct IntegerKnapsack <: AbstractKnapsack end

function add_knapsack_variables(model, data::KnapsackData, ::BinaryKnapsack)
    @variable(model, x[1:length(data.objects)], Bin)
    return
end

function add_knapsack_variables(model, data::KnapsackData, ::IntegerKnapsack)
    @variable(model, x[1:length(data.objects)] >= 0, Int)
    return
end

function add_capacity_constraint(model, data::KnapsackData, ::AbstractKnapsack)
    x = model[:x]
    N = length(x)
    @constraint(
        model, 
        capacity, 
        sum(data.objects[i].weight * x[i] for i in 1:N) <= data.capacity,
    )
    return
end

function add_profit_objective(model, data::KnapsackData, ::AbstractKnapsack)
    x = model[:x]
    N = length(x)
    @objective(model, Max, sum(data.objects[i].profit * x[i] for i in 1:N))
    return
end

function knapsack_model(data::KnapsackData, config::AbstractKnapsack)
    model = Model()    
    add_knapsack_variables(model, data, config)
    add_capacity_constraint(model, data, config)
    add_profit_objective(model, data, config)
    return model
end

data = KnapsackData([Object(rand(), rand()) for i in 1:20], 10.0)
binary_model = knapsack_model(data, BinaryKnapsack())
integer_model = knapsack_model(data, IntegerKnapsack())

```

A key benefit of using JuMP is that you can write the algebraic model in a readable form, and then combine it with the power of Julia to build a framework that suits you. A downside of the generality is that there aren’t off-the-shelf solutions.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 23, 2021, 8:13pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/3 "2021-11-23T20:13:18Z")

</div>

I should actually turn this into a tutorial. It’s a useful approach to constructing larger problems.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [November 24, 2021, 10:23am UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/4 "2021-11-24T10:23:47Z")

</div>

Right, there could be a blog post about “patterns of composition in optimization modeling”!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [November 24, 2021, 10:08pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/5 "2021-11-24T22:08:01Z")

</div>

I would contribute to writing that!

---

<div class="post-metadata">

**Author:** ![pboes](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@pboes](https://discourse.julialang.org/u/pboes)\
**Post date:** [November 25, 2021, 5:25pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/6 "2021-11-25T17:25:43Z")

</div>

Thanks @odow, that was very helpful.

> MOI is too complicated, and lacks good support for nonlinear problems.

Interesting. I thought it was the other way around: Since the models you can formulate in MOI is a strict superset of those that you formulate in JuMP, the solvers that support MOI would be a strict superset of those that support JuMP, but I must be mistaken.

If I can be of help writing such a tutorial or blog post, I’m more than happy to contribute. Of course, as you can see from the fact that I asked the question, I don’t bring experience but see that the approach is basically straightforward.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 25, 2021, 5:29pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/7 "2021-11-25T17:29:01Z")

</div>

JuMP is in some sense “mathoptinterface with nicer syntax.”

There is no difference between the supported solvers or the problem types. In fact, JuMP makes it easier to specify nonlinear problems; the nonlinear interface at the moi level is difficult to implement.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 25, 2021, 6:36pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/8 "2021-11-25T18:36:58Z")

</div>

I’ll make a start with the tutorial this morning. Once I have a draft I’ll post it here and people can comment with feedback.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 25, 2021, 9:09pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/9 "2021-11-25T21:09:00Z")

</div>

Comments appreciated: [https://github.com/jump-dev/JuMP.jl/pull/2812](https://github.com/jump-dev/JuMP.jl/pull/2812)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [November 25, 2021, 11:15pm UTC](https://discourse.julialang.org/t/integrating-mathoptinterface-into-an-industry-scale-project-e-g-constraint-management/71943/10 "2021-11-25T23:15:31Z")

</div>

On the question relating to broader structuring and presentation of models, I thought that there were a number of nice examples in this year’s [JuMP-dev conference](https://jump.dev/meetings/juliacon2021/)

I recall an interesting example described in [AnyMOD.jl: A Julia package for creating energy system models - Leonard Göke](https://www.youtube.com/watch?v=QE_tNDER0F4&list=PLP8iPy9hna6Rf8_oh9oR2bNeBO-Rw8pzc&index=24) of using DataFrames to manage constraints.

Also very worthwhile: [A Brief Introduction to InfrastructureModels - Carleton Coffrin](https://www.youtube.com/watch?v=POOt1FCA8LI&list=PLP8iPy9hna6Rf8_oh9oR2bNeBO-Rw8pzc&index=26)

And to back up @odow’s recommendation of PowerSimulations, see this talk from JuliaCon:  
[[https://live.juliacon.org/talk/88EDGD](https://live.juliacon.org/talk/88EDGD)]
