Skip to content

Resolve Performance Worse than Constructing New Model with Gurobi #108

Closed

Description

@pulsipher

I am working on a benchmarking paper that incorporates resolving optimization problems with parameter changes (among other common workflows). Using the facility location problem from the JuMP paper and resolving with different big-M values using POI results in considerably worse performance than simply reconstructing the whole model again.

Here is the basic test script:

using JuMP, Gurobi, ParametricOptInterface
const POI = ParametricOptInterface

function create_model(n, M, use_param = false)
    # Initialize the model object
    if use_param
        model = Model(() -> POI.Optimizer(Gurobi.Optimizer(), evaluate_duals = false))
        @variable(model, Mp in POI.Parameter(M))
    else
        model = Model(Gurobi.Optimizer)
        Mp = M
    end
    set_silent(model)
    set_time_limit_sec(model, 0) # We want to include how long it takes to reach the solver, but that's it.

    # Add the variables
    @variable(model, d)
    @variable(model, 0 ≤ y[1:n, 1:2] ≤ 1)
    @variable(model, z[0:n, 0:n, 1:n], Bin)
    @variable(model, 0 ≤ s[0:n, 0:n, 1:n])
    @variable(model, r[0:n, 0:n, 1:n, 1:2])
    
    # Set the objective
    @objective(model, Min, d)
    
    # Add the constraints
    @constraint(model, [i ∈ 0:n, j ∈ 0:n], sum(z[i,j,f] for f ∈ 1:n) == 1)
    @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], s[i,j,f] == d + Mp*(1 - z[i,j,f]))
    @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1] == i/n - y[f,1])
    @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,2] == j/n - y[f,2])
    @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1]^2 + r[i,j,f,2]^2 ≤ s[i,j,f]^2)

    # Return the model
    return model
end

function poi_resolve_model(n, Ms)
    model = create_model(n, 0.0, true)
    Mp = model[:Mp]
    for M in Ms
        MOI.set(model, POI.ParameterValue(), Mp, M)
        optimize!(model)
    end
end

function reconstruct_resolve_model(n, Ms)
    model = create_model(n, 0.0, false)
    for M in Ms
        model = create_model(n, M, false)
        optimize!(model)
    end
end

# Set test settings
Ms = LinRange(2.0, 2.2, 4) # this specifies the range of big-M values to resolve over
n = 30 # This can vary the problem size

# Account for the jit time 
poi_resolve_model(2, [2.0])
reconstruct_resolve_model(2, [2.0])

# Time resolving using POI
@time poi_resolve_model(n, Ms)

# Time resolving by reconstructing a new model
@time reconstruct_resolve_model(n, Ms)

With n = 4 we get:

0.045777 seconds (45.23 k allocations: 2.511 MiB) # POI
0.045065 seconds (107.45 k allocations: 7.511 MiB) # rebuild

With n = 30 we get:

61.390874 seconds (12.44 M allocations: 678.317 MiB, 0.55% gc time) # POI
4.326594 seconds (28.65 M allocations: 1.953 GiB, 37.67% gc time) # rebuild

Hence, the performance of resolving with parameter changes can be significantly worse than simply making a new model each time.

Activity

  1. guilhermebodin commented on Jul 8, 2022

    @guilhermebodin
    Collaborator

    Hi @pulsipher unfortunately I don't have Gurobi on my computer but I tried to isolate the problem in Xpress. I used a direct_model to interact directly with the solver API and avoided calling optimize! because Xpress does not respect the 0-time limit constraint (just found that there are missing docs in the Xpress.jl interface).

    With all that said, these were my benchmarks and the model is being correctly updated.

    using JuMP, Xpress, ParametricOptInterface
    const POI = ParametricOptInterface
    
    function create_model(n, M, use_param = false)
        # Initialize the model object
        if use_param
            model = direct_model(POI.Optimizer(Xpress.Optimizer(), evaluate_duals = false))
            @variable(model, Mp in POI.Parameter(M))
        else
            model = direct_model(Xpress.Optimizer())
            Mp = M
        end
    
        # Add the variables
        @variable(model, d)
        @variable(model, 0 ≤ y[1:n, 1:2] ≤ 1)
        @variable(model, z[0:n, 0:n, 1:n], Bin)
        @variable(model, 0 ≤ s[0:n, 0:n, 1:n])
        @variable(model, r[0:n, 0:n, 1:n, 1:2])
        
        # Set the objective
        @objective(model, Min, d)
        
        # Add the constraints
        @constraint(model, [i ∈ 0:n, j ∈ 0:n], sum(z[i,j,f] for f ∈ 1:n) == 1)
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], s[i,j,f] == d + Mp*(1 - z[i,j,f]))
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1] == i/n - y[f,1])
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,2] == j/n - y[f,2])
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1]^2 + r[i,j,f,2]^2 ≤ s[i,j,f]^2)
    
        # Return the model
        return model
    end
    
    function poi_resolve_model(n, Ms)
        model = create_model(n, 0.0, true);
        Mp = model[:Mp]
        for M in Ms
            MOI.set(model, POI.ParameterValue(), Mp, M)
            POI.update_parameters!(backend(model))
            # optimize!(model) commented on purpose
        end
    end
    
    function reconstruct_resolve_model(n, Ms)
        model = create_model(n, 0.0, false)
        for M in Ms
            model = create_model(n, M, false)
            # optimize!(model) commented on purpose
        end
    end
    
    # Set test settings
    Ms = LinRange(2.0, 2.2, 4) # this specifies the range of big-M values to resolve over
    n = 30 # This can vary the problem size
    
    # Account for the jit time 
    poi_resolve_model(2, [2.0])
    reconstruct_resolve_model(2, [2.0])
    
    # Time resolving using POI
    @time poi_resolve_model(n, Ms)
    
    # Time resolving by reconstructing a new model
    @time reconstruct_resolve_model(n, Ms)

    My results were that with n=4

    0.004077 seconds (53.78 k allocations: 2.778 MiB) #POI
    0.008554 seconds (162.96 k allocations: 9.178 MiB) #rebuild

    and n=30

    1.304923 seconds (15.39 M allocations: 783.235 MiB, 16.12% gc time) #POI
    3.920162 seconds (45.50 M allocations: 2.501 GiB, 24.05% gc time) #rebuild

    In my opinion, the possible approaches to tackle the problem are:

    Depending on the result we could try to design a solution.

    Hope this helps!

  2. joaquimg commented on Jul 8, 2022

    @joaquimg
    Member

    Alternatively,
    @pulsipher can try using cplex or xpress if possible.

  3. changed the title [-]Resolve Performance Worse than Constructing New Model[/-] [+]Resolve Performance Worse than Constructing New Model with Gurobi[/+] on Jul 8, 2022
  4. pulsipher commented on Jul 8, 2022

    @pulsipher
    Author

    Thanks for the quick responses. I only have access to Xpress via the community license which can only handle small problems and CPLEX is less than ideal (I heard it is effectively no longer developed as most of the maintainers left IBM).

    I simplified the benchmark:

    using JuMP, Gurobi, ParametricOptInterface
    const POI = ParametricOptInterface
    
    function create_model(n, M, use_param = false)
        # Initialize the model object
        if use_param
            model = direct_model(POI.Optimizer(Gurobi.Optimizer(), evaluate_duals = false))
            @variable(model, Mp in POI.Parameter(M))
        else
            model = direct_model(Gurobi.Optimizer())
            Mp = M
        end
        set_silent(model)
        set_time_limit_sec(model, 0)
    
        # Add the variables
        @variable(model, d)
        @variable(model, z[0:n, 0:n, 1:n], Bin)
        @variable(model, 0 ≤ s[0:n, 0:n, 1:n])
        
        # Set the objective
        @objective(model, Min, d)
        
        # Add the constraints
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], s[i,j,f] == d + Mp*(1 - z[i,j,f]))
    
        # Return the model
        return model, Mp
    end
    
    function poi_resolve_model(model, Mp, M)
        MOI.set(model, POI.ParameterValue(), Mp, M)
        optimize!(model)
        return
    end
    
    function reconstruct_resolve_model(n, M)
        model, _ = create_model(n, M, false)
        optimize!(model)
        return
    end
    
    function time_tests(n, M)
        pmodel, Mp = create_model(n, 0.0, true)
        @time poi_resolve_model(pmodel, Mp, M)
        @time reconstruct_resolve_model(n, M)
        return
    end
    
    # Set test settings
    M = 2.0
    n = 30
    
    # Account for the jit time 
    println("\n**JIT Prep**")
    time_tests(2, M)
    
    # Time the operations
    println("\n**Actual Timing**")
    time_tests(n, M)

    I tried the benchmark as you have it above (no optimize! and using direct model). With Gurobi, I get (n=30):

    0.000004 seconds (3 allocations: 48 bytes) # POI
    0.100941 seconds (1.24 M allocations: 70.406 MiB) # rebuild

    This is likely because GRBupdatemodel is not called without optimize!, so nothing is updated.

    So running the benchmark with n = 30 in direct mode and using optimize!, I get:

    6.201263 seconds (1.15 M allocations: 42.657 MiB, 2.91% gc time) # POI
    0.155560 seconds (1.24 M allocations: 70.407 MiB, 26.36% gc time) # rebuild

    I only have access to a community version of Xpress, so I cannot check how that does with optimize! (using a 1s timelimit) on a problem of any appreciable size.

    I profiled with Gurobi via:

    using JuMP, Gurobi, ParametricOptInterface, PProf
    const POI = ParametricOptInterface
    
    function create_model(n)
        # Initialize the model object
        model = direct_model(POI.Optimizer(Gurobi.Optimizer(), evaluate_duals = false))
        @variable(model, Mp in POI.Parameter(0.0))
        set_silent(model)
        set_time_limit_sec(model, 0)
    
        # Add the variables
        @variable(model, d)
        @variable(model, z[0:n, 0:n, 1:n], Bin)
        @variable(model, 0 ≤ s[0:n, 0:n, 1:n])
        
        # Set the objective
        @objective(model, Min, d)
        
        # Add the constraints
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], s[i,j,f] == d + Mp*(1 - z[i,j,f]))
    
        # Return the model
        return model, Mp
    end
    
    function poi_resolve_model(model, Mp, M)
        MOI.set(model, POI.ParameterValue(), Mp, M)
        optimize!(model)
        return
    end
    
    # Account for the jit time 
    model, Mp = create_model(2)
    poi_resolve_model(model, Mp, 2.0)
    
    # Profile resolving using POI
    model, Mp = create_model(30)
    @pprof poi_resolve_model(model, Mp, 2.0)

    The profile file is available at: https://drive.google.com/file/d/1X99YjE0Udofc7iBcqCQj7YUEG3CIj4PV/view?usp=sharing

    Looking at the results seems to confirm that it is taking a long time in the Gurobi update step, so it likely relates to POI not updating Gurobi in a performant way.

  5. guilhermebodin commented on Jul 8, 2022

    @guilhermebodin
    Collaborator

    Hi @pulsipher could you try using POI in the branch gb/update_in_batches. The name of the branch is not really good because it is misleading. I have only separated the get and set inside the updates that your model uses. This can lead to a little worse times in other solvers but might accelerate Gurobi, still evaluating what the best approach is.

  6. pulsipher commented on Jul 8, 2022

    @pulsipher
    Author

    Wow, that's a fast turnaround for starting development on solutions. Running the main benchmark again with the gb/update_in_batches branch I get:

    0.161209 seconds (1.47 M allocations: 49.575 MiB, 21.31% gc time) # POI
    0.154936 seconds (1.24 M allocations: 70.407 MiB, 25.43% gc time) # rebuild

    So it becomes roughly the same as rebuilding with more allocations. This is definitely a step in the right direction.

  7. joaquimg commented on Jul 8, 2022

    @joaquimg
    Member

    About CPLEX,

    I heard it is effectively no longer developed as most of the maintainers left IBM

    I do not think it is unmaintained.
    Even it is, the code is very stable and efficient, I would be very surprised if it is worse than xpress and gurobi.
    I am not saying you should stop using gurobi and use cplex instead.
    I am saying that large part of this slowness is due to a Gurobi.jl implementation decision. and hence it is NOT a fault of POI.

    So using CPLEX has the purpose of benchmarking POI and JuMP as you are trying to do.

  8. pulsipher commented on Jul 8, 2022

    @pulsipher
    Author

    About CPLEX,

    I heard it is effectively no longer developed as most of the maintainers left IBM

    I do not think it is unmaintained. Even it is, the code is very stable and efficient, I would be very surprised if it is worse than xpress and gurobi. I am not saying you should stop using gurobi and use cplex instead. I am saying that large part of this slowness is due to a Gurobi.jl implementation decision. and hence it is NOT a fault of POI.

    So using CPLEX has the purpose of benchmarking POI and JuMP as you are trying to do.

    This is a fair point. Unfortunately, my study is using interfaces in Pyomo and AIMS that currently are limited to Gurobi. JuMP doesn't support mutable parameters for linear/quadratic expressions like Pyomo, so I was hoping to use POI to cover this gap.

    I did, however, go ahead and try CPLEX with the same benchmark on the master branch of POI and got the following:

    0.160076 seconds (1.32 M allocations: 46.168 MiB, 26.70% gc time) # POI
    0.154746 seconds (2.36 M allocations: 96.133 MiB, 31.94% gc time) # rebuild

    This is similar to the results using Gurobi and the gb/update_in_batches branch. Interestingly, there is a reduction in memory allocation but no speed reduction relative to building a new model. I tried this with larger values of n and got similar results.

    Is this the expected behavior of POI for these types of problems?

  9. joaquimg commented on Jul 8, 2022

    @joaquimg
    Member

    POI should cover that gap indeed. POI should be faster than rebuilding.

  10. guilhermebodin commented on Jul 8, 2022

    @guilhermebodin
    Collaborator

    Well, in your benchmarks you only do 4 updates, POI gains more performance when you need to change more times. But indeed we can investigate what are the sources of slowness here.

  11. guilhermebodin commented on Jul 8, 2022

    @guilhermebodin
    Collaborator

    Also, POI is really really good when you have more variables than parameters. If all of your constraints have all terms with parameters, updating the constraints is quite close to building a new one from scratch.

  12. pulsipher commented on Jul 8, 2022

    @pulsipher
    Author

    Yes, I just realized that in the simplified benchmark above (which only does one change) all the constraints are updated (I removed all the others), so in fairness it is changing every constraint.

    Adding back in the other constraints we have:

    using JuMP, Gurobi, ParametricOptInterface, CPLEX
    const POI = ParametricOptInterface
    
    function create_model(n, M, use_param = false)
        # Initialize the model object
        if use_param
            model = direct_model(POI.Optimizer(CPLEX.Optimizer(), evaluate_duals = false))
            @variable(model, Mp in POI.Parameter(M))
        else
            model = direct_model(CPLEX.Optimizer())
            Mp = M
        end
        set_silent(model)
        set_time_limit_sec(model, 0)
    
        # Add the variables
        @variable(model, d)
        @variable(model, 0 ≤ y[1:n, 1:2] ≤ 1)
        @variable(model, z[0:n, 0:n, 1:n], Bin)
        @variable(model, 0 ≤ s[0:n, 0:n, 1:n])
        @variable(model, r[0:n, 0:n, 1:n, 1:2])
        
        # Set the objective
        @objective(model, Min, d)
        
        # Add the constraints
        @constraint(model, [i ∈ 0:n, j ∈ 0:n], sum(z[i,j,f] for f ∈ 1:n) == 1)
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], s[i,j,f] == d + Mp*(1 - z[i,j,f]))
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1] == i/n - y[f,1])
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,2] == j/n - y[f,2])
        @constraint(model, [i ∈ 0:n, j ∈ 0:n, f ∈ 1:n], r[i,j,f,1]^2 + r[i,j,f,2]^2 ≤ s[i,j,f]^2)
    
        # Return the model
        return model, Mp
    end
    
    function poi_resolve_model(model, Mp, M)
        MOI.set(model, POI.ParameterValue(), Mp, M)
        optimize!(model)
        return
    end
    
    function reconstruct_resolve_model(n, M)
        model, _ = create_model(n, M, false)
        optimize!(model)
        return
    end
    
    # Time how long it takes to do one resolve with a parameter change
    function time_tests(n, M) 
        pmodel, Mp = create_model(n, 0.0, true)
        @time poi_resolve_model(pmodel, Mp, M)
        @time reconstruct_resolve_model(n, M)
        return
    end
    
    # Set test settings
    M = 2.0
    n = 30
    
    # Account for the jit time 
    println("\n**JIT Prep**")
    time_tests(2, M)
    
    # Time the operations
    println("\n**Actual Timing**")
    time_tests(n, M)

    Which using CPLEX on master with n = 30 gets:

    0.251625 seconds (1.44 M allocations: 48.810 MiB) # POI
    0.573047 seconds (7.41 M allocations: 389.991 MiB, 19.88% gc time) # rebuild

    So it does make a difference.

    Now switching back to the gb/update_in_batches branch with CPLEX, I get:

    0.392920 seconds (1.76 M allocations: 55.728 MiB, 34.13% gc time) # POI
    0.546639 seconds (7.41 M allocations: 389.991 MiB, 21.22% gc time) # rebuild

    So, yes there is a slight performance degradation. However, switching to Gurobi I get:

    0.162363 seconds (1.47 M allocations: 49.575 MiB, 18.98% gc time)
    0.412273 seconds (5.01 M allocations: 338.767 MiB, 22.28% gc time)

    Fixing the problem reported in this issue.

    Hence, the changes in gb/update_in_batches seem to fix the problem.

  13. guilhermebodin commented on Jul 8, 2022

    @guilhermebodin
    Collaborator

    Well, I think that you could use this branch for now, and in the next week, I will try to write a new version that properly handles this. It should avoid most of the performance degradation seen in CPLEX. In my opinion, in your example with 4 n^3 constraints, POI should do no better than be 4 times faster than rebuilding the problem. Because in the end, you are rebuilding this constraint that accounts for nearly 1/4 of the problem.

    It should converge to 1/4 of the time the more different values of M you have to update in the model.

    Another possible implementation is to add a new option in Gurobi to avoid updating every time.

  14. pulsipher commented on Jul 8, 2022

    @pulsipher
    Author

    Well, I think that you could use this branch for now, and in the next week, I will try to write a new version that properly handles this. It should avoid most of the performance degradation seen in CPLEX. In my opinion, in your example with 4 n^3 constraints, POI should do no better than be 4 times faster than rebuilding the problem. Because in the end, you are rebuilding this constraint that accounts for nearly 1/4 of the problem.

    It should converge to 1/4 of the time the more different values of M you have to update in the model.

    I completely agree that the best case here would be around a 1/4 of the time speed up and POI is doing a pretty good job with these changes relative to that ideal. Thanks again for the fast turn around.

    Another possible implementation is to add a new option in Gurobi to avoid updating every time.

    This could be reasonable, @odow will probably have an opinion to give here given his experience with Gurobi.jl.

  15. pulsipher commented on Jul 14, 2022

    @pulsipher
    Author

    For completeness, rerunning the above benchmark using master after #114 I get the following.

    With Gurobi, I get:

    0.122904 seconds (1.38 M allocations: 46.177 MiB) # POI
    0.421740 seconds (5.01 M allocations: 338.767 MiB, 22.64% gc time) # rebuild

    With CPLEX, I get:

    0.294344 seconds (1.67 M allocations: 52.330 MiB, 15.52% gc time) # POI
    0.561183 seconds (7.41 M allocations: 389.991 MiB, 22.56% gc time) # rebuild

    So, these are even better than the results obtained using the initial gb/update_in_batches patch.

    Thanks @guilhermebodin for the fast turn around!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions