# Adding variables with the Clp solver in MathOptInterface

**URL:** <https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540>\
**Category:** Optimization (Mathematical)\
**Created:** [April 18, 2021, 1:12pm UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540 "2021-04-18T13:12:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [April 18, 2021, 1:12pm UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540/1 "2021-04-18T13:12:16Z")

</div>

Hi,

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

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 🙂

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 18, 2021, 3:56pm UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540/2 "2021-04-18T15:56:25Z")

</div>

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`](https://jump.dev/MathOptInterface.jl/stable/apireference/#Caching-optimizer) (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.

---

<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:** [April 18, 2021, 8:43pm UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540/3 "2021-04-18T20:43:56Z")

</div>

> [@leejm516](#):
>
> in order to revise a package

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: [https://github.com/jump-dev/Clp.jl/blob/d0974213b13a43b7e7f72aa58657ec2c505193d6/src/MOI\_wrapper/MOI\_wrapper.jl#L336-L340](https://github.com/jump-dev/Clp.jl/blob/d0974213b13a43b7e7f72aa58657ec2c505193d6/src/MOI_wrapper/MOI_wrapper.jl#L336-L340).
2. Implement the incremental interface: you write functions like `MOI.add_variable`, `MOI.add_constraint`, etc. This is what GLPK does: [https://github.com/jump-dev/GLPK.jl/blob/b9c225c7226a3a38f903f6909355d6867939e94d/src/MOI\_wrapper/MOI\_wrapper.jl#L458](https://github.com/jump-dev/GLPK.jl/blob/b9c225c7226a3a38f903f6909355d6867939e94d/src/MOI_wrapper/MOI_wrapper.jl#L458)

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

Unfortunately the [Implementing a solver interface](https://jump.dev/MathOptInterface.jl/dev/manual/implementing/) 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](https://jump.dev/MathOptInterface.jl/dev/submodules/Test/overview/).

You can post here, or join the developer chatroom if you have questions: [https://gitter.im/JuliaOpt/JuMP-dev](https://gitter.im/JuliaOpt/JuMP-dev)

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [April 21, 2021, 5:27am UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540/4 "2021-04-21T05:27:33Z")

</div>

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`.

---

<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:** [April 21, 2021, 5:55am UTC](https://discourse.julialang.org/t/adding-variables-with-the-clp-solver-in-mathoptinterface/59540/5 "2021-04-21T05:55:11Z")

</div>

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