I am fairly new to Pyomo and Gurobi. I'd like to find 20 sets of different solutions that meet the following constraints.
constraints:
10<=x1<=20
12<=x2<=32
5<=x3<=15
5<=x4<=52
x1+x2+x3+x4=100.
From here I learned Gurobi has PoolSolutions, so I decided to have a try.
from gurobipy import GRB
import numpy as np
from pyomo.environ import *
import matplotlib.pyplot as plt
generator={
1 :{'down':10, 'up':20,},
2 :{'down':12, 'up':32,},
3 :{'down':5, 'up':15,},
4 :{'down':5, 'up':52,},
}
G = generator.keys()
# create a new model
m = ConcreteModel()
# create variables
m.P = Var(G, domain=NonNegativeReals)
# set objective
m.OBJ = Objective(expr = 0)
# add constraints
m.c = ConstraintList()
m.c.add(sum(m.P[g] for g in G)== 100)
for g in G:
m.c.add(m.P[g] <= generator[g]['up'])
m.c.add(m.P[g] >= generator[g]['down'])
# try to get 20 sets of different solutions
n_of_s = 20
opt = SolverFactory('gurobi_persistent')
opt.set_instance(m)
opt.set_gurobi_param('PoolSolutions', n_of_s)
opt.set_gurobi_param('PoolSearchMode', 2)
opt.solve()
But it doesn't work properly. Hope somebody could teach me how to modify this and how to show the results of 20 solutions. Any suggestions are welcomed. THX
NonNegativeReals
withNonNegativeIntegers
,opt.get_model_attr('SolCount')
returns20
, so I think 20 basis solutions are found. Do you know how to show all solutions? In Gurobi I can use xn, but how can I do that with pyomo,thanks! $\endgroup$ – ssswokao Jan 20 at 3:58