How to run Gurobi throught Julia

To install Julia, we can download it from github

git clone https://github.com/JuliaLang/julia.git
cd julia
sudo apt-get install build-essential libatomic1 python gfortran perl wget m4 cmake pkg-config
make -j8
make install
cd julia-*
cd bin
pwd

Now, export the working directory to your .bashrc file

export PATH="$(pwd)/julia:$PATH"

Run julia and add JuMP (Julia for Mathematical Programming):

Pkg.add("JuMP")
WARNING: Base.Pkg is deprecated, run `using Pkg` instead

Newer versions must use Pkg first as following:

using Pkg
Pkg.add("JuMP")

Other option is install it from the tarball file:

wget https://github.com/JuliaLang/julia/releases/download/v0.6.2/julia-0.6.2-full.tar.gz
tar xzvf julia-0.6.2-full.tar.gz
cd julia-0.6.2
make -j8
make install
cd julia-*
cd bin
pwd

Export to the bashrc file again and install the package:

Pkg.add("JuMP")
Pkg.add("Gurobi")
Pkg.update()

Maybe you can have issues to load gurobi if the environment is not configured:

using Gurobi
INFO: Precompiling module Gurobi.
ERROR: LoadError: Gurobi not properly installed. Please run Pkg.build("Gurobi")

Stacktrace:
 [1] error(::String) at ./error.jl:21
 [2] include_from_node1(::String) at ./loading.jl:576
 [3] include(::String) at ./sysimg.jl:14
 [4] anonymous at ./<missing>:2
while loading /home/dragonx/.julia/v0.6/Gurobi/src/Gurobi.jl, in expression starting on line 8
ERROR: Failed to precompile Gurobi to /home/dragonx/.julia/lib/v0.6/Gurobi.ji.
Stacktrace:
 [1] compilecache(::String) at ./loading.jl:710
 [2] _require(::Symbol) at ./loading.jl:497
 [3] require(::Symbol) at ./loading.jl:405

------------------------------------------------------

julia> Pkg.build("Gurobi")

INFO: Building Gurobi
===============================[ ERROR: Gurobi ]================================

LoadError: Unable to locate Gurobi installation. Note that this must be downloaded separately from gurobi.com
while loading /home/dragonx/.julia/v0.6/Gurobi/deps/build.jl, in expression starting on line 42

================================================================================

================================[ BUILD ERRORS ]================================

WARNING: Gurobi had build errors.

 - packages with build errors remain installed in /home/dragonx/.julia/v0.6
 - build the package(s) and all dependencies with `Pkg.build("Gurobi")`
 - build a single package by running its `deps/build.jl` script

 ---------------------------------------

Check if gurobi environment is configured:

julia> ENV["GUROBI_HOME"]
ERROR: KeyError: key "GUROBI_HOME" not found
Stacktrace:
 [1] access_env(::Base.##288#289, ::String) at ./env.jl:45
 [2] getindex(::Base.EnvHash, ::String) at ./env.jl:78

As you can see, Julia not recognize PATH to Gurobi_Home. Then you need to declare it:

julia> ENV["GUROBI_HOME"] = "/path/to/gurobi/linux64"
julia> Pkg.build("Gurobi")
INFO: Building Gurobi

julia> Pkg.add("Gurobi")
INFO: Package Gurobi is already installed

julia> using Gurobi
INFO: Precompiling module Gurobi.

Now, create an example.jl (julia file) with and example:

using JuMP, Gurobi

m = Model(solver=GurobiSolver(ResultFile="solution.sol",OutputFlag=0))

@variable(m, x >= 5)
@variable(m, y >= 45)

@objective(m, Min, x + y)
@constraint(m, 50x + 24y <= 2400)
@constraint(m, 30x + 33y <= 2100)

status = solve(m)
println(getobjectivevalue(m))

And we run it from julia:

julia example.jl

The output is in solution.sol to import it from any other language like python

Pkg.add("PyCall")
sudo pip3 install julia

HINT: Sometimes is better work with the same package version to avoid incompatibilities like this:

ERROR: LoadError: The solver= keyword is no longer available in JuMP 0.19 and later. See the JuMP documentation (http://www.juliaopt.org/JuMP.jl/latest/) for latest syntax.

To install a specific package version you need tell to Julia the specific version as follow:

Pkg.add(PackageSpec(name="JuMP", version="0.18.4"))

Enjoy it!

Comments

Popular posts from this blog

Exception: Could not find a default OpenFlow controller in Mininet

How to fix Android when developer options are not available for this user

Webcam streaming throught VLC with YUY2 compatibility