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:
- In Python.Cvxpy: (runtime: 4 sec)
# 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)
- In Julia.JuMP: (runtime: 1min 7sec)
# 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;
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.