How to use Gurobi variable hints in JuMP 0.21


I was wondering whether it was possible to access the Gurobi variable hint attributes using JuMP, or failing that by accessing a lower level model.

As a concrete working example, suppose that I’m solving a Knapsack problem using JuMP/Gurobi, namely

using JuMP, Gurobi
set_optimizer_attribute(m, "ResultFile", "theHintFile.sol")
@variable(m, z[1:5], Bin)
@objective(m, Min, sum(z))
@constraint(m, sum(z[i]*i for i=1:5)<=5)

where I’ve set the ResultFile parameter so that it writes the optimal solution to disk (and I can similarly write the MPS file to disk using the ResultFile parameter). Is there a defined way to use the sol file to apply variable hints to the model (for e.g. if I were to add a constraint). The closest I have come is using the InputFile parameter in Gurobi to read the .sol file, but that seems to be “command line only”.

The reason that I’m interested in this is that I’m solving a non-convex problem using a decomposition scheme, and Gurobi translates the non-convex constraints to PWL constraints (and introduces some error in the process) so my warm-starts aren’t always feasible. My hope is that I could use variable hints to encourage Gurobi to head in the direction of the warm-start, instead of discarding it if its infeasible. I think more generally it’s also nice to expose as many Gurobi/CPLEX features as possible.

Thanks so much!

You can pass warm-start information to a solver that supports it as explained here:

Importing an initial solution from a file is a solver-specific task (solvers have different solution formats), so you would need a solver-specific implementation.

Finally, with respect to what a solver does with the provided solution, this is even more solver-specific: it may be improved on, it may be discarded, etc…

I should note that the link the OP cites says:

Variables hints and MIP starts are similar in concept, but they behave in very different ways.

Answering the question, as mtanneau says, there is no solver agnostic way (i.e., using just JuMP and with code that will work independent of the solver, if the solver supports the attribute). But there is how to do it with a lower level model. It is not pretty, but you can do something like:

import MathOptInterface
const MOI = MathOptInterface

I did not have time to test it, but it is something in this direction.

1 Like

See the documentation:

julia> using JuMP, Gurobi

julia> model = direct_model(Gurobi.Optimizer())
Academic license - for non-commercial use only
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: DIRECT
Solver name: Gurobi

julia> @variable(model, x)

julia> MOI.set(model, Gurobi.VariableAttribute("VarHintVal"), x, 1)

julia> MOI.get(model, Gurobi.VariableAttribute("VarHintVal"), x)

julia> MOI.set(model, Gurobi.VariableAttribute("VarHintVal"), x, 2.0)

julia> MOI.get(model, Gurobi.VariableAttribute("VarHintVal"), x)
1 Like