# API documentation for gurobi.jl

**URL:** https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [June 13, 2021, 5:41am UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825 "2021-06-13T05:41:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 5:41am UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825/1 "2021-06-13T05:41:26Z")

</div>

I’ve been trying to solve an ILP in Python where the primary bottle neck appears to be model construction. Multiple sources cite that model construction is heavily language dependent (whereas the optimization is language-independent), and I’m trying to see if I can re-implement the same ILP in Julia.

Trouble is, I can’t seem to find an API documentation. Just translating the Python ILP (where it works) into Julia seems to be a huge hassle. Would someone be able to point me in the right direction? Ie:

```julia
# PYTHON
I= 30
J=113000
K=1500
T1=400
T2=25
N = 50
penalty = 1000
model = gp.Model("STT_rnd1")
r_i_j = model.addVars(I, J, vtype=GRB.BINARY, name="r_i_j")
y_i_k = model.addVars(I, K, vtype=GRB.BINARY, name="y_i_k")
z_i_j_k = model.addVars(I, K, vtype=GRB.INTEGER, name="z_i_j_k")
s_j_k = copy.deepcopy(dense_adj_mat) # a numpy matrix

for i in range(I):
    for k in range(K):
        model.addConstr(gp.quicksum(r_i_j[i,j]*y_i_k[i,k]*s_j_k[j,k] for j in range(J))>= T1*y_i_k[i,k])

```

is something I have tested already in Python, but I can’t get past instantiating some of these parameters in Julia:

```julia
# JULIA
model = Model(Gurobi.Optimizer)
Gurobi.add_vars!(model, 0.0, I, J, Gurobi.GRB_BINARY, "r_i_j")

```

throwing the following error:

> MethodError: no method matching add\_vars!(::Model, ::Float64, ::Int64, ::Int64, ::Int8, ::String)  
> Closest candidates are:  
> add\_vars!(::Gurobi.Model, ::Union{Char, Int8, Array{Char,1}, Array{Int8,1}}, ::Array{T,1} where T, ::Union{Array{T,1}, T} where T\<:Real, ::Union{Array{T,1}, T} where T\<:Real) at C:\Users\b2jia.julia\packages\Gurobi\VhpiN\src\grb\_vars.jl:111

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [June 13, 2021, 7:55am UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825/2 "2021-06-13T07:55:18Z")

</div>

All you need should be in the [JuMP documentation](https://jump.dev/JuMP.jl/stable/). Your model does not seem to use gurobi-specific stuff.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [June 13, 2021, 3:12pm UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825/3 "2021-06-13T15:12:20Z")

</div>

There is two ways to use Gurobi in Julia that you are mixing in:

```julia
# JULIA
model = Model(Gurobi.Optimizer)
Gurobi.add_vars!(model, 0.0, I, J, Gurobi.GRB_BINARY, "r_i_j")

```

The first line is using Gurobi through JuMP, which is the recommended way. Then you can create the binary variables with

```julia
@variable(model, r[I, J], Bin)

```

as detailed [in the docs](https://jump.dev/JuMP.jl/stable/manual/variables/).

The second line use the Julia wrapper of the Gurobi C interface. If you want to that interface, without JuMP, then you should not use `Model(Gurobi.Optimizer)` as this would create a JuMP model. You should not use anything from JuMP in that case, Gurobi.jl simply wraps the C-interface as is (the Julia interface is even automatically generated with [Clang.jl](https://github.com/JuliaInterop/Clang.jl)). So check the C-interface documentation in the Gurobi website.  
Again, I would recommend using the JuMP interface directly. It is much easier and will allow you to easily switch to another solver without any change to your code (except changing the line `Model(Gurobi.Optimizer)` of course)

---

<div class="post-metadata">

### Author: ![anon94806364](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@anon94806364](https://discourse.julialang.org/u/anon94806364)
#### Post date: [June 13, 2021, 5:44pm UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825/4 "2021-06-13T17:44:51Z")

</div>

I see that I conflated the two (JuMP, Gurobi). Thank you for pointing this out.

---

<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: [June 13, 2021, 10:04pm UTC](https://discourse.julialang.org/t/api-documentation-for-gurobi-jl/62825/5 "2021-06-13T22:04:08Z")

</div>

> [@anon94806364](#):
>
> where the primary bottle neck appears to be model construction

Note that the Gurobi python wrapper is pretty efficient. You should not expect massive speed gains just by rewriting it into JuMP.

It is worth spending some time to understand why the bottleneck is model construction. (Uncommon, particularly when you are solving MILPs.) Are you solving this problem multiple times with different data? Try modifying an existing model, rather than building a new one every solve.
