I was trying to solve a simple optimization problem, first via Python.Cvxpy framework and then via Julia.JuMP framework, but Julia.JuMP formulation is 15x slower.
My optimization problem:
# Run: time python this_file.py
import cvxpy as cp
import numpy as np
n = 2
b = np.array([2,3])
c1 = np.array([[3,4],[1,0],[0,1]])
c2 = [1,0,0]
x = cp.Variable(n)
prob = cp.Problem( cp.Minimize(b@x), [ c1@x >= c2 ])
prob.solve(cp.MOSEK) # FOSS alternative: prob.solve(cp.GLPK)
print('Solution:', prob.value)
# Run: time julia this_file.jl
using JuMP
using Mosek, MosekTools # FOSS alternative: using GLPK
function compute()
n = 2
b = [2,3]
c1 = [3 4 ; 1 0 ; 0 1]
c2 = [1,0,0]
prob = Model(optimizer_with_attributes(Mosek.Optimizer))
# FOSS alternative: Model(optimizer_with_attributes(GLPK.Optimizer))
@variable(prob, x[1:n])
@objective(prob, Min, b'*x)
@constraint(prob, c1*x .>= c2)
JuMP.optimize!(prob)
println("Solution: ", JuMP.objective_value(prob))
end;
compute()
Any tips or tricks to fasten the Julia.JuMP code?
More than 1 minute is excessive. Did you update packages or something and recompile?
Here's what I get;
(base) oscar@Oscars-MBP lore % cat ~/Desktop/discourse.jl
@time using JuMP
@time using GLPK
function compute()
n = 2
b = [2,3]
c1 = [3 4 ; 1 0 ; 0 1]
c2 = [1,0,0]
prob = Model(GLPK.Optimizer)
@variable(prob, x[1:n])
@objective(prob, Min, b' * x)
@constraint(prob, c1 * x .>= c2)
optimize!(prob)
println("Solution: ", objective_value(prob))
end
@time compute()
@time compute()
(base) oscar@Oscars-MBP lore % time ~/julia --project=/tmp/jump ~/Desktop/discourse.jl
4.070492 seconds (8.34 M allocations: 599.628 MiB, 4.17% gc time, 0.09% compilation time)
0.280838 seconds (233.24 k allocations: 16.040 MiB, 41.37% gc time)
Solution: 0.6666666666666666
12.746518 seconds (17.74 M allocations: 1.022 GiB, 3.71% gc time, 44.57% compilation time)
Solution: 0.6666666666666666
0.000697 seconds (2.87 k allocations: 209.516 KiB)
~/julia --project=/tmp/jump ~/Desktop/discourse.jl 22.63s user 0.55s system 100% cpu 23.102 total
Breaking it down
using JuMP
We're working on improving the using JuMP
and our "time-to-first-solve" issue, but there are a few things you can do in the meantime.
julia file.jl
. Open Julia once and use the REPL. That avoids the 6sec overhead.