Skip to main content

How can I select the candidate interested in the solution pool?

Answered

Comments

5 comments

  • Xianbang Chen
    • Gurobi-versary
    • Conversationalist
    • Curious

    Below is a demo code. How to make ``value.(x)`` always output ``sol3``?

    using JuMP, Gurobi, MathOptInterface

    const MOI = MathOptInterface

    n = 5;

    capacity = 10.0;

    profit = [5.0, 3.0, 3.0, 7.0, 4.0];

    weight = [2.0, 8.0, 8.0, 2.0, 5.0];

    jump_model = Model(Gurobi.Optimizer)

    set_optimizer_attribute(jump_model, "PoolSearchMode", 2)

    set_optimizer_attribute(jump_model, "PoolGap", 0.4)

    @variable(jump_model, x[1:n], Bin)

    @constraint(jump_model, sum(weight[i] * x[i] for i in 1:n) <= capacity)

    @objective(jump_model, Max, sum(profit[i] * x[i] for i in 1:n))

    print(jump_model)

    optimize!(jump_model)

    solution_summary(jump_model)

    all_vars = JuMP.all_variables(jump_model)

    num_solutions = result_count(jump_model)

    on_vars = filter(v -> startswith(JuMP.name(v), "x"), all_vars)

    sol_1 = Dict(JuMP.name(v) => JuMP.value(v; result = 1) for v in on_vars)

    sol_2 = Dict(JuMP.name(v) => JuMP.value(v; result = 2) for v in on_vars)

    sol_3 = Dict(JuMP.name(v) => JuMP.value(v; result = 3) for v in on_vars)

    sol_4 = Dict(JuMP.name(v) => JuMP.value(v; result = 4) for v in on_vars)

    sol_5 = Dict(JuMP.name(v) => JuMP.value(v; result = 5) for v in on_vars)

    value.(x)
    0
  • Riley Clement
    • Gurobi Staff Gurobi Staff

    Hi Xianbang,

    The attributes Xn and SolutionNumber should help here.

    Please see the discussion on Solution Pools for more context.

    If there's any difficulties accessing these through JuMP then please reach out to their discourse channel: https://discourse.julialang.org/tag/jump

    - Riley

    0
  • Xianbang Chen
    • Gurobi-versary
    • Conversationalist
    • Curious

    Hi Riley,

    Thank you for your help. Yes, I read the files and tried a few things. Based on my experience, SolutionNumber doesn’t seem to work in JuMP. If I set SolutionNumber before optimize!, the model only stores the last solution, so SolutionNumber has no effect. If I set it after optimize!, it throws an error.

    I also tried:

    MOI.set(backend_model, MOI.VariablePrimal(), JuMP.index(v), val_3)
    

    to see if I could modify the optimal solution directly in the model, but that failed too.

    My final approach was to add equality constraints to force the binary variables to match the solution I wanted, then run optimize! again.

    Anyway, I appreciate your help.

    Best,
    Xianbang

    0
  • Riley Clement
    • Gurobi Staff Gurobi Staff

    Hi Xianbang,

    If you were using a Gurobi API you would set SolutionNumber after optimization, then use the Xn attribute on variables (or the model) to retrieve the value.  I'm not sure setting SolutionNumber before optimization would have an effect.

    In any case JuMP seems to be doing it's own thing.  Maybe the following page will help? https://jump.dev/JuMP.jl/stable/manual/solutions/#Multiple-solutions

    - Riley

    0
  • Xianbang Chen
    • Gurobi-versary
    • Conversationalist
    • Curious

    Hi Riley,

    I’m using a Julia package where JuMP handles the modeling. It seems JuMP doesn’t support SolutionNumber, but it’s helpful to know that Gurobi does—I’ll try using that feature when I work directly with the Gurobi API.

    For now, I followed the example you shared and wrote something like:

    for i in 1:result_count(model)
        println("Solution $i")
        println(" x = ", value.([x1, x2]; result = i))
    end
    

    This allows me to pull all solutions from the solution pool. Then, I can add constraints like binary_1 = 1 to force the model to use the solution I want as the active one.

    Thanks again for your help.

    Best,
    Xianbang

    0

Please sign in to leave a comment.