Skip to content

Instantly share code, notes, and snippets.

@Thuener
Created March 14, 2019 19:40
Show Gist options
  • Save Thuener/5fd30bda29a84afb126cb5b723574eba to your computer and use it in GitHub Desktop.
Save Thuener/5fd30bda29a84afb126cb5b723574eba to your computer and use it in GitHub Desktop.
using JuMP,CPLEX
const C = 300000
const N = 100
function memuse()
pid = getpid()
return round(Int,parse(Int,read(`ps -p $pid -o rss=`,String))/1024)
end
function addconstraints(m,x,Θ,coef,p)
if p == 1
@constraint(m, Θ .<= coef*x )
elseif p == 2
for i in 1:size(coef,1)
@constraint(m, Θ <= sum(coef[i,j]*x[j] for j = 1:size(coef,2)))
end
elseif p == 3
@constraint(m,[i = 1:size(coef,1)], Θ <= sum(coef[i,j]*x[j] for j = 1:size(coef,2)))
elseif p == 4 # With JuMP 0.18.5
rhs = zeros(C)
coef = hcat(-coef,ones(C))
CPLEX.add_constrs!(m.internalModel.inner, coef, '<', rhs)
elseif p == 5 # With JuMP 0.19.0
rhs = zeros(C)
coef = hcat(-coef,ones(C))
CPLEX.add_constrs!(m.moi_backend.optimizer.model.inner, coef, '<', rhs)
end
end
function test_const(constype; direct=false)
m = if !direct
Model(with_optimizer(CPLEX.Optimizer, CPX_PARAM_SCRIND=0))
else
JuMP.direct_model(CPLEX.Optimizer(CPX_PARAM_SCRIND=0))
end
@variable(m, 0 <= x[1:N] <= 1)
@variable(m, 0 <= Θ <= 1000)
@objective(m, Max, Θ )
optimize!(m)
#solve(m)
coef = rand(C,N)
GC.gc()
m1= memuse()
val, time, = @timed addconstraints(m,x,Θ,coef,constype)
GC.gc()
#sleep(30)
m2 = memuse()
println("Memory consumption $(m2-m1) Mb and Time $(time)")
val, time2, = @timed if constype < 4
optimize!(m)
#solve(m)
elseif constype == 4
CPLEX.optimize!(m.internalModel.inner)
elseif constype == 5
CPLEX.optimize!(m.moi_backend.optimizer.model.inner)
end
println("Time to solve model $(time2)")
end
println("########### Vectorized - WithoutDirect ###########")
test_const(1)
test_const(1)
println("########### Scalar 1 - WithoutDirect ###########")
test_const(2)
test_const(2)
println("########### Scalar 2 - WithoutDirect ###########")
test_const(3)
test_const(3)
println("########### Low-level API ###########")
test_const(5)
test_const(5)
println("########### Vectorized - WithDirect ###########")
test_const(1; direct = true)
test_const(1; direct = true)
println("########### Scalar 1 - WithDirect ########### ")
test_const(2; direct = true)
test_const(2; direct = true)
println("########### Scalar 2 - WithDirect ###########")
test_const(3; direct = true)
test_const(3; direct = true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment