Skip to content

Instantly share code, notes, and snippets.

@JackDunnNZ
Created November 2, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackDunnNZ/16a5e25d1b654d773d012a0b250d05ef to your computer and use it in GitHub Desktop.
Save JackDunnNZ/16a5e25d1b654d773d012a0b250d05ef to your computer and use it in GitHub Desktop.
Gurobi 7.0 solution pool proof of concept via Gurobi.jl
using Gurobi, JuMP
# New versions of getvalue, getobjectivevalue for solution pool
"Get the variable values for the `solnumber`th solution in the pool"
function grb_get_xn(m::JuMP.Model, solnumber::Int)
g = getrawsolver(m)
Gurobi.set_int_param!(g, "SolutionNumber", solnumber - 1)
Gurobi.get_dblattrarray(g, "Xn", 1, Gurobi.num_vars(g))
end
"As per `getvalue`, but returns the value for the `solnumber`th solution in the pool"
function JuMP.getvalue(x::Variable, solnumber::Int)
if solnumber == 1
return getvalue(x)
end
solution = grb_get_xn(x.m, solnumber)
solution[x.col]
end
function JuMP.getvalue(x::Array{Variable}, solnumber::Int)
if solnumber == 1
return getvalue(x)
end
solution = grb_get_xn(first(x).m, solnumber)
ret = similar(x, Float64)
for I in eachindex(x)
ret[I] = solution[x[I].col]
end
ret
end
"As per `getobjectivevalue`, but returns the value for the `solnumber`th solution in the pool"
function JuMP.getobjectivevalue(m::JuMP.Model, solnumber::Int)
if solnumber == 1
return getobjectivevalue(m)
end
Gurobi.set_int_param!(g, "SolutionNumber", solnumber - 1)
Gurobi.get_dblattr(g, "PoolObjVal")
end
# Example from Gurobi.jl readme
m = Model(solver=GurobiSolver())
@variables(m,begin
0 <= x <= 5
0 <= y <= 10, Int
z, Bin
end)
@objective(m, Max, x + 2y + 5z)
@constraint(m, x + y + z <= 10)
@constraint(m, x + 2y + z <= 15)
solve(m)
# Check number of solutions
g = getrawsolver(m)
solcount = Gurobi.get_sol_count(g)
# Get value of optimal solution - these are the same
getvalue([x, y, z])
getvalue([x, y, z], 1)
# Get value of 2nd solution
getvalue([x, y, z], 2)
# Same but for only one variable
getvalue(x, 1)
getvalue(x, 2)
# Get optimal objective value - these are the same
getobjectivevalue(m)
getobjectivevalue(m, 1)
# Get objective value of 2nd solution
getobjectivevalue(m, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment