I am solving a MILP using Gurobi and Pyomo. I would like to obtain as many optimal solutions as possible. Here is what I have so far:
opt = SolverFactory('gurobi_persistent')
n_of_s = 10
opt.set_instance(model)
opt.set_gurobi_param('PoolSolutions', n_of_s)
opt.set_gurobi_param('PoolSearchMode', 2)
opt.solve(model, tee=True)
opt.set_gurobi_param('SolutionNumber',2)
model.pprint()
At some point Gurobi displays Solution count 3: 5000 5000 5000
. However, when trying to access the value of optimal variables, I can only access one solution.
I have tried using the Xn
attribute, but Gurobi states AttributeError: '_GeneralVarData' object has no attribute 'Xn'
.
m.SolCount
. Then, you can retrieve those solutions using:for sol in range(m.SolCount - 1): m.setParam(GRB.Param.SolutionNumber, sol+1); var.Xn
$\endgroup$