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()
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.
Stacktrace:
[1] add_variable(model::Clp.Optimizer)
@ MathOptInterface ~\.julia\packages\MathOptInterface\5WwpK\src\variables.jl:37
[2] top-level scope
@ none:1
julia>
Could anyone explain why I got this error? There is no problem with the GLPK solver.
Thank you for reading my question
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.
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.
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.