Adding variables with the Clp solver in MathOptInterface


I am trying to be familiar with MathOptInterface, in order to revise a package that has been written with MathProgBase.

When I tried to add a variable into the Clp model, I got the following error.

julia> using MathOptInterface, Clp

julia> optimizer = Clp.Optimizer()

julia> x = MOI.add_variable(optimizer)
ERROR: MathOptInterface.AddVariableNotAllowed: Adding variables cannot be performed. You may want to use a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call `reset_optimizer` before doing this operation if the `CachingOptimizer` is in `MANUAL` mode.        
 [1] add_variable(model::Clp.Optimizer)
   @ MathOptInterface ~\.julia\packages\MathOptInterface\5WwpK\src\variables.jl:37
 [2] top-level scope
   @ none:1


Could anyone explain why I got this error? There is no problem with the GLPK solver.
Thank you for reading my question :slight_smile:

Many of the functions defined by MathOptInterface are stubs that the solver packages (i.e., Clp in this case) must extend with a method for the specific optimizer object. It seems like Clp has not extended this function, and the message makes clear that you should try with the Clp optimizer wrapped in a CachingOptimizer (see the linked docs). My guess is that Clp does not support adding the variables one by one, and therefore depends on CachingOptimizer to create a cache of the whole model and then create it in a single go when optimize! is called.

1 Like

Which package?

There are two main ways to interface with MOI:

  1. Implement MOI.copy_to: you write a function that copies a complete model from src into your new optimizer dest. This is what Clp does: Clp.jl/MOI_wrapper.jl at d0974213b13a43b7e7f72aa58657ec2c505193d6 · jump-dev/Clp.jl · GitHub.
  2. Implement the incremental interface: you write functions like MOI.add_variable, MOI.add_constraint, etc. This is what GLPK does: GLPK.jl/MOI_wrapper.jl at b9c225c7226a3a38f903f6909355d6867939e94d · jump-dev/GLPK.jl · GitHub

The capabilities of the underlying solver dictate which approach to take.

Unfortunately the Implementing a solver interface documentation is very sparse. The current suggestion is to find a similar solver, and copy the wrapper. There’s a testing harness that helps check if you implemented everything correctly: Overview · MathOptInterface.

You can post here, or join the developer chatroom if you have questions: JuliaOpt/JuMP-dev - Gitter

1 Like

Thank you for all the answers!

The package that I mentioned is COBRA.jl. This package is based on that from the original MATLAB package COBRA and uses matrices and vectors to formulate an optimization problem.

I have found that cobrapy, another implementation of COBRA into python, formulates the problem with optlang, and I think it would be much better implementing JuMP for problem formulation and optimization instead of MOI.

Ah, I think I misunderstood your question. If you’re just formulating problems, then you should definitely just use JuMP instead.