model construction gurobipy; working with tupledicts
回答済みHello,
I am new to modeling and am trying to get a basic capacitated max cover facility model running in python using the gurobipy package. The model generates a solution, but it is obviously wrong. I am confused as to how to iteratively multiply the tupledict c[i,j] values by a[i] values.
Here is the code block and solution below. Many thanks for any pointers.
from gurobipy import Model, GRB, quicksum
NeighborhoodSets = [[0,1,2,3],
[0,1,2,3],
[0,1,2,3,4,5],
[0,1,2,3,4,5],
[4,5,6,2,3],
[4,5,6,2,3],
[4,5,6],
[7,8,12,13],
[7,8,12,13,9,10],
[8,9,10,11],
[8,9,10,14,15,11],
[9,10,11,15,16],
[7,8,12,13],
[7,8,12,13],
[10,15,14,19,21],
[10,14,15,11,16],
[11,15,16],
[17,18],
[17,18],
[14,19,20],
[14,19,20],
[21],]
#Capacity K @ j facilities
K=[400, 500, 1000, 1000, 600, 600, 500, 500, 1000, 500, 1500, 1200, 600, 600, 1500, 1500, 500, 500, 500, 500, 500, 150]
#Population a at demand location i
a = [258, 192, 209, 116, 32, 195, 64, 163, 154, 185, 71, 219, 147, 41, 204, 225, 203, 205, 26, 127, 163, 238]
m = Model('CMCLP')
# Define the c[ij] variables -- the location variables--cij =1 if facility is in neighborhood
c = {}
for i in range(22):
for j in range(22):
c[i,j] = m.addVar(obj = 0, vtype=GRB.BINARY, name="cij")
#if dij is <= the service distance cij = 1, 0 otherwise. Use neighborhood sets to determine.
for i in range(22):
for j in range(22):
if j in NeighborhoodSets[i]:
c[i,j]=1
else:
c[i,j]=0
# number of facilities
p = 3
# Define the x[ij] variables -- the location variables--xij =1 if facility @ j
x = {}
for i in range(22):
for j in range(22):
x[i,j] = m.addVar(obj = c[i,j]*a[i], vtype=GRB.BINARY, name="xij")
# Define the y[j] variables -- the location variables--yj = 1 if i served by xij
y = {}
for j in range(22):
y[j] = m.addVar(vtype=GRB.BINARY, name="yj")
m.update()
#Maximize the population assigned to a facility within the coverage distance
m.modelSense = GRB.MAXIMIZE
m.optimize()
Gurobi Optimizer version 9.1.1 build v9.1.1rc0 (win64)
Thread count: 6 physical cores, 12 logical processors, using up to 12 threads
Optimize a model with 0 rows, 990 columns and 0 nonzeros
Model fingerprint: 0xf38dc2e2
Variable types: 0 continuous, 990 integer (990 binary)
Coefficient statistics:
Matrix range [0e+00, 0e+00]
Objective range [3e+01, 3e+02]
Bounds range [1e+00, 1e+00]
RHS range [0e+00, 0e+00]
Loaded MIP start from previous solve with objective -1e+100
Presolve removed 0 rows and 990 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.01 seconds
Thread count was 1 (of 12 available processors)
Solution count 1: 13990
Optimal solution found (tolerance 1.00e-04)
Best objective 1.399000000000e+04, best bound 1.399000000000e+04, gap 0.0000%
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Penelope,
You should have a look at our Python examples and webinars on Optimization with Python. These provide a very good way of getting started with Gurobi and Python.
The Model.write() is also extremely useful to understand what the model looks like that you are building. In your case, you can add the line \(\texttt{m.write("myLP.lp")}\) before the \(\texttt{optimize}\) call and inspect the generated LP file.
Best regards,
Jaromił0 -
Thank you Jaromił Najman. The model write function has helped me make significant progress. Much obliged!
Best,
Penelope
0
投稿コメントは受け付けていません。
コメント
3件のコメント