# Setting up gurobi environment inside a module

**URL:** <https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, gurobi\
**Created:** [October 8, 2023, 8:24pm UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736 "2023-10-08T20:24:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 8, 2023, 8:24pm UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/1 "2023-10-08T20:24:13Z")

</div>

I have a module that has some functions that run some optimization problems modeling using the JuMP package and the Gurobi solver.  
a very generic structure of the code is as follows:

```julia
module TestModule

using JuMP
using Gurobi

const GRB_ENV = Ref{Gurobi.Env}()
function __init__ ()
    GRB_ENV[] = Gurobi.Env()
    return
end

function run_optimization()
    model = Model(() -> Gurobi.Optimizer(GRB_ENV))
    set_optimizer_attribute(model, "TimeLimit", 3600.0)
    
    #some constraints, variables, objective modeled using JuMP

   optimize!(model)
end

export run_optimization
end

```

When I call the `run_optimization` function, I get the following error:

```julia
using TestModule

run_optimization() #exported from the module

#Error

ERROR: LoadError: MethodError: no method matching Gurobi.Optimizer(::Base.RefValue{Gurobi.Env})
Closest candidates are:
  Gurobi.Optimizer() at ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:325
  Gurobi.Optimizer(::Union{Nothing, Gurobi.Env}; enable_interrupts) at ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:325

```

I am probably doing something silly here. If someone could point out the correct way to call it, that would be great. Thanks!

---

<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:** [October 8, 2023, 11:59pm UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/2 "2023-10-08T23:59:35Z")

</div>

> [@math\_opt](#):
>
> ```julia
> const GRB_ENV = Ref{Gurobi.Env}()
> function __init__ ()
> GRB_ENV[] = Gurobi.Env()
> return
> end
> 
> ```

Could this just be

```julia
const GRB_ENV = Gurobi.Env()

```

without the ` __init__ ()`?

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 9, 2023, 12:12am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/3 "2023-10-09T00:12:05Z")

</div>

> [@jd-foster](#):
>
> `const GRB_ENV = Gurobi.Env()`

I was doing the same thing before, but apparently, it won’t work inside a package/module as is mentioned [here](https://github.com/jump-dev/Gurobi.jl/issues/424).

---

<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:** [October 9, 2023, 12:30am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/4 "2023-10-09T00:30:05Z")

</div>

Maybe you could just “de-reference” using `[]` here:

```julia
 model = Model(() -> Gurobi.Optimizer(GRB_ENV[]))

```

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 9, 2023, 12:36am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/5 "2023-10-09T00:36:17Z")

</div>

Nope, it doesn’t work. I get the following error:

```julia
ERROR: LoadError: Gurobi Error 10002:
Stacktrace:
  [1] _check_ret
    @ ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:375 [inlined]
  [2] empty!(model::Gurobi.Optimizer)
    @ Gurobi ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:443
  [3] Gurobi.Optimizer(env::Gurobi.Env; enable_interrupts::Bool)
    @ Gurobi ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:348
  [4] Optimizer
    @ ~/.julia/packages/Gurobi/vStLK/src/MOI_wrapper/MOI_wrapper.jl:325 [inlined]

```

---

<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:** [October 9, 2023, 1:12am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/6 "2023-10-09T01:12:11Z")

</div>

This is a different error. The Gurobi Reference Manual says this is:

```julia
10002 NULL input value provided for a required argument

```

But I can’t reproduce that one. This works for me in the REPL:

```julia
module TestModule

using JuMP
using Gurobi

const GRB_ENV = Ref{Gurobi.Env}()
function __init__ ()
    const GRB_ENV[] = Gurobi.Env()
    return
end

function run_optimization()
    model = Model(() -> Gurobi.Optimizer(GRB_ENV[])) # <-- Added []
    set_optimizer_attribute(model, "TimeLimit", 3600.0)
    
    #some constraints, variables, objective modeled using JuMP

   optimize!(model)
end

export run_optimization

end

using .TestModule
run_optimization()
```

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 9, 2023, 1:39am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/7 "2023-10-09T01:39:42Z")

</div>

Totally my fault. I changed `GRB_ENV` to `GRB_ENV[]` at all places but one (I have multiple functions solving optimization problems). It works now; however, I am just curious why does it work now and not before. Also, the person who made the post [here](https://github.com/jump-dev/Gurobi.jl/issues/424) didn’t seem to have any issue with using just `GRB_ENV`. Further, as @odow mentioned in this [comment](https://github.com/jump-dev/Gurobi.jl/issues/424#issuecomment-951374162), any calls to `GRB_ENV ` should be `GRB_ENV[]`. Any leads to why it works now would be helpful. Thanks again!!

---

<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:** [October 9, 2023, 7:26am UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/8 "2023-10-09T07:26:34Z")

</div>

The `[]` is needed to get or set the element inside a `Ref` object.

The ` __init__ ` is needed so that a new environment is created each time you start Julia. Otherwise, the C pointer will be precompiled and it won’t exist the next time you load the model.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 9, 2023, 2:22pm UTC](https://discourse.julialang.org/t/setting-up-gurobi-environment-inside-a-module/104736/9 "2023-10-09T14:22:52Z")

</div>

Oh, I understand now. Thanks for the clarification!
