Repository navigation
Closed
Description
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) # rebuildWith 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) # rebuildHence, the performance of resolving with parameter changes can be significantly worse than simply making a new model each time.
Metadata
Metadata
Assignees
Labels
No labels
Activity
guilhermebodin commentedon Jul 8, 2022
Hi @pulsipher unfortunately I don't have Gurobi on my computer but I tried to isolate the problem in Xpress. I used a
direct_modelto interact directly with the solver API and avoided callingoptimize!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.
My results were that with
n=4and
n=30In my opinion, the possible approaches to tackle the problem are:
PProf.jland we could analyze the flame graphDepending on the result we could try to design a solution.
Hope this helps!
joaquimg commentedon Jul 8, 2022
Alternatively,
@pulsipher can try using cplex or xpress if possible.
[-]Resolve Performance Worse than Constructing New Model[/-][+]Resolve Performance Worse than Constructing New Model with Gurobi[/+]pulsipher commentedon Jul 8, 2022
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:
I tried the benchmark as you have it above (no
optimize!and using direct model). With Gurobi, I get (n=30):This is likely because
GRBupdatemodelis not called withoutoptimize!, so nothing is updated.So running the benchmark with
n = 30in direct mode and usingoptimize!, I get: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:
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.
guilhermebodin commentedon Jul 8, 2022
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.pulsipher commentedon Jul 8, 2022
Wow, that's a fast turnaround for starting development on solutions. Running the main benchmark again with the
gb/update_in_batchesbranch I get:So it becomes roughly the same as rebuilding with more allocations. This is definitely a step in the right direction.
joaquimg commentedon Jul 8, 2022
About CPLEX,
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.
pulsipher commentedon Jul 8, 2022
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
masterbranch of POI and got the following:This is similar to the results using Gurobi and the
gb/update_in_batchesbranch. Interestingly, there is a reduction in memory allocation but no speed reduction relative to building a new model. I tried this with larger values ofnand got similar results.Is this the expected behavior of POI for these types of problems?
joaquimg commentedon Jul 8, 2022
POI should cover that gap indeed. POI should be faster than rebuilding.
guilhermebodin commentedon Jul 8, 2022
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.
guilhermebodin commentedon Jul 8, 2022
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.
pulsipher commentedon Jul 8, 2022
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:
Which using CPLEX on
masterwithn = 30gets:So it does make a difference.
Now switching back to the
gb/update_in_batchesbranch with CPLEX, I get:So, yes there is a slight performance degradation. However, switching to Gurobi I get:
Fixing the problem reported in this issue.
Hence, the changes in
gb/update_in_batchesseem to fix the problem.guilhermebodin commentedon Jul 8, 2022
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
Myou have to update in the model.Another possible implementation is to add a new option in Gurobi to avoid updating every time.
pulsipher commentedon Jul 8, 2022
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.
This could be reasonable, @odow will probably have an opinion to give here given his experience with Gurobi.jl.
pulsipher commentedon Jul 14, 2022
For completeness, rerunning the above benchmark using
masterafter #114 I get the following.With Gurobi, I get:
With CPLEX, I get:
So, these are even better than the results obtained using the initial
gb/update_in_batchespatch.Thanks @guilhermebodin for the fast turn around!