data_import / syntax error
回答済みHello,
I have a very simple, quick question! I want to use a not complete graph in my MIP model, how can I model the edges?
for example: I can't use E = [(i,j) for i in S for j in T]
and I have a list of edges that I need to use like: E=[(1,16),
(1,23),
(1,25),
(1,26),
(2,17),
...]
Here is the error:
File "model.pxi", line 3332, in gurobipy.Model.addConstrs
File "MIP.py", line 283, in <genexpr> m.addConstrs(x[i,p]+y[j,p] <= 1 + z[i,j,p] for i,j in E for p in K )
KeyError: (17, 1)
The error must come from this part:for i,j in E, how do I define that i , j are the first and second element in E according to the imported data?
Here is the semi_complete code :
S_size =15
T_size =15
k_numb =3
S=[s_i for s_i in range(1,S_size+1) ]
T=[t_j for t_j in range(1,T_size+1) ]
K=[k_nb for k_nb in range(1,k_numb+1) ]
#E = [(i,j) for i in S for j in T]
E=[(1,16),
(1,23),
(1,25),
...]
E_complete = [(1,17),
(1,18),
(1,19),
..]
B = [(i,p) for i in S for p in K]
C = [(j,p) for j in T for p in K]
D = [(i,j,p) for i,j in E_complete for p in K]
m = gp.Model("ILPM")
x = m.addVars(B , name="x")
y = m.addVars(C , name="y")
z = m.addVars(D , name="z")
x.vType = GRB.BINARY
y.vType = GRB.BINARY
z.vType = GRB.BINARY
m.modelSense = GRB.MINIMIZE
m.setObjective(gp.quicksum(z[i,j,p] for i,j in E_complete for p in K))
m.addConstrs(x[i,p]+y[j,p] <= 1 + z[i,j,p] for i,j in E_complete for p in K )
m.addConstrs(gp.quicksum(x[i,p] for p in K)==1 for i in S)
m.addConstrs(x[i,p] <= y[j,p] for i,j in E for p in K )
m.optimize()
m.printAttr('x')
Thank you
-
正式なコメント
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,
You see this error because you are trying to add the variable y[17,1] to a constraint, but this variable does not exist. A y variable is defined for every tuple in the list C:
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3),
(4, 1), (4, 2), (4, 3), (5, 1), (5, 2), (5, 3), (6, 1), (6, 2), (6, 3),
(7, 1), (7, 2), (7, 3), (8, 1), (8, 2), (8, 3), (9, 1), (9, 2), (9, 3),
(10, 1), (10, 2), (10, 3), (11, 1), (11, 2), (11, 3), (12, 1), (12, 2),
(12, 3), (13, 1), (13, 2), (13, 3), (14, 1), (14, 2), (14, 3), (15, 1),
(15, 2), (15, 3)]Note that (17,1) is not in this list. Consider the following constraint set:
m.addConstrs(x[i,p]+y[j,p] <= 1 + z[i,j,p] for i,j in E_complete for p in K)
We encounter the error for the first of these constraints, defined by (i,j) = (1,17) in E_complete and p = 1 in K, because y[j,p] is never defined.
Could you elaborate on what these variables represent and the sets you want to use to define them? Thanks!
Eli
0 -
Hi Eli,
Yes, you are right! I didn't define the sets correctly! that works now!
Thank you so much
Shiva
0
投稿コメントは受け付けていません。
コメント
3件のコメント