How to fix a bug with quicksum?
回答済みGood afternoon!
When working with Gurobi, the following error occurred:
Traceback (most recent call last):
File "Gurobi.py", line 109, in <module>
model.setObjective(quicksum(x[i, j]*c[i, j] for i, j in A))
File "src/gurobipy/gurobi.pxi", line 3349, in gurobipy.quicksum
File "Gurobi.py", line 109, in <genexpr>
model.setObjective(quicksum(x[i, j]*c[i, j] for i, j in A))
KeyError: (0, 1)
Tell me, please, what should I do? How to fix this error?
I attach my program code below
for name_file1, name_file2 in zip(lst, lst_distance):
sheet = pd.read_csv(name_file1, sep="\t", error_bad_lines=False)
nodes = sheet.values
depot = nodes[len(nodes) - 1]
cities = nodes[:(len(nodes) - 1)]
n_customer = len(nodes)
xc = np.zeros(n_customer)
yc = np.zeros(n_customer)
for i in range(0, len(cities)):
xc[i] = cities[i][0]
yc[i] = cities[i][1]
xc[len(cities)] = depot[0]
yc[len(cities)] = depot[1]
capacity = np.zeros(n_customer)
for i in range(0, len(cities)):
capacity[i] = cities[i][2]
N = [i for i in range(1, n_customer)] # tour без депо
V = [0] + N # tour с депо
A = [(i,j) for i in V for j in V if i != j] # possible arcs
c = {}
sheet_dist = pd.read_csv(name_file2, sep="\t", error_bad_lines=False)
for i,j in A:
# node_1 = (xc[i], yc[i])
# node_2 = (xc[j], yc[j])
for k in range(len(sheet_dist)):
if(xc[i] == sheet_dist.values[k][0] and yc[i] == sheet_dist.values[k][1] and xc[j] == sheet_dist.values[k][2] and yc[j] == sheet_dist.values[k][3]):
c[(i,j)] = sheet_dist.values[k][4]
break
q = {i: capacity[i] for i in N} # number of VRUs at location i to be picked
Q = 50
n = 1
for i in range(len(capacity)):
if(capacity[i] > Q):
Q = Q / n
n += 1
Q *= n
print("Используется грузовиков: ", n)
time_limit = 600
lst = []
lst_time = []
#-----------------------------------------------------------------------------------------------------------------#
model = Model('CVRP')
# Declaration of variables
x = model.addVars(A, vtype= GRB.BINARY)
y = model.addVars(N, vtype= GRB.CONTINUOUS)
# setting the objective function
model.modelSense = GRB.MINIMIZE
model.setObjective(quicksum(x[i, j]*c[i, j] for i, j in A))
# Adding constraints
model.addConstrs(quicksum(x[i,j] for j in V if j!=i) == 1 for i in N)
model.addConstrs(quicksum(x[i,j] for i in V if i!=j) == 1 for j in N)
model.addConstrs((x[i,j] == 1) >> (y[i] + q[j] == y[j]) for (i,j) in A if i != 0 and j != 0)
model.addConstrs(y[i] >= q[i] for i in N)
model.addConstrs(y[k] <= Q for k in N)
# Optimizing the model
model.Params.TimeLimit = time_limit # seconds
model.Params.LogFile= "result_Branch_and_Cut_20_vehicles.txt"
model.optimize()
if model.status == GRB.OPTIMAL:
print('1.Optimal objective: %g' % model.objVal)
print('Optimal cost: %g' % model.objVal)
lst.append(model.objVal)
lst_time.append(model.Runtime)
elif model.status == GRB.INF_OR_UNBD:
print('2.Model is infeasible or unbounded')
res = -1
elif model.status == GRB.INFEASIBLE:
print('3.Model is infeasible')
res = -1
elif model.status == GRB.UNBOUNDED:
print('4.Model is unbounded')
res = -1
else:
print('5.Optimization ended with status %d' % model.status)
print('Optimal cost: %g' % model.objVal)
lst.append(model.objVal)
lst_time.append(time_limit)
0
-
正式なコメント
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 Ivan,
Please always try posting a minimal reproducible example (MRE), so others can actually run your code.
Judging from the code, it looks like c does not contain the correct entries. You should probably implement a default value of 0, to prevent this error.
Cheers,
Matthias0
投稿コメントは受け付けていません。
コメント
2件のコメント