My Model resulting KeyError: 0 please help
ユーザーの入力を待っています。Can someone tell me what's wrong with this code below
import gurobipy as gp
from gurobipy import GRB
import pandas as pd
import time
GUROBI_timelimit = 15*60 # in seconds
presolve_setting = 0 # 0 = Deactivated, 1 = Activated, 2 = Both
model = gp.Model("Penjadwalan Gate")
dm = [1, 2, 3, 4, 5, 6, 7]
data = pd.read_excel(r'ini_file_datatruck.xlsx') # excel file called test.xlsx
dj = range(len(data))
D = {} # dictionary holding the final parameter values
for d in dm:
for i in dj:
D[(d, i + 1)] = data[d][i]
T = pd.read_excel('Tm.xlsx').values.tolist()[0]
H = 60
M = 99999
length_j = 60
length_k = 12
length_m = 7
# Sets
J = [i for i in range(1, length_j+1)]
K = [i for i in range(1, length_k+1)]
M = [i for i in range(1, length_m+1)]
MS = [i for i in range(0, length_m+1)]
ME = [i for i in range(1, length_m+2)]
# Decision Variables
X = model.addVars(M, K, vtype=GRB.BINARY, name='X')
Y = model.addVars(M, J, vtype=GRB.BINARY, name='Y')
Z = model.addVars(M, J, vtype=GRB.BINARY, name='Z')
R = model.addVars(J, K, vtype=GRB.BINARY, name='R')
Q = model.addVars(J, K, vtype=GRB.BINARY, name='Q')
P = model.addVars(J, vtype=GRB.INTEGER, name='P')
indices_U = [*range(3)]
U = model.addVars(indices_U, name="U")
W = model.addVars(J, K, vtype=GRB.BINARY, name=f"W")
V = model.addVars(M, M, K, vtype=GRB.BINARY, name='V')
# Fungsi Tujuan (1)
model.setObjective(U[0] + U[1] + U[2], GRB.MINIMIZE)
# Constraint (2)
model.addConstrs((gp.quicksum(X[m, k] for m in M) == 1 for k in K), name='Constraint 2')
# Constraint (3)
model.addConstrs((j * D[m-1][j-1] <= j_1 * Y[m, j_1] for m in M for j in J for j_1 in J), name='Constraint 3')
# Constraint (4)
model.addConstrs((j * Y[m, j_1] + T[m-1] - 1 == l * Z[m, l] for m in M for j_1 in J for l in J), name='Constraint 4')
# Constraint (5)
model.addConstrs((l * Z[m, l] <= H for m in M for l in L), name='Constraint 5')
# Constraint (6)
model.addConstrs((Y[m, j_1] <= W[j_1, k] + M * (1 - X[m, k]) for m in M for j_1 in J for k in K), name='Constraint 6')
# Constraint (7) (ini bingung)
model.addConstrs((j_2 * W[j_2, k] - j_1 * W[j_1, k] >= M * (1 - R[j_2, k]) for k in K for j_1 in J for j_2 in J if j_1 != j_2), name='Constraint 7')
# Constraint (8)
model.addConstrs((l * W[l, k] - j_2 * W[j_2, k] >= M * (1 - Q[j_2, k]) for k in K for j_2 in J for l in L if j_2 != l), name='Constraint 8')
# Constraint (9)
model.addConstrs((Z[m, l] <= W[l, k] + M * (1 - X[m, k]) for m in M for l in L for k in K), name='Constraint 9')
# Constraint (10)
model.addConstrs((gp.quicksum(V[s, m, k] for m in ME) == 1 for k in K), name='Constraint 10')
# Constraint (11)
model.addConstrs((gp.quicksum(V[m, e, k] for m in MS) == 1 for k in K), name='Constraint 11')
# Constraint (12)
model.addConstrs((gp.quicksum(V[m, m_1, k] for m in MS) == gp.quicksum(V[m_1, m, k] for m in ME)for m_1 in m for k in K), name='Constraint 12')
# Constraint (13)
model.addConstrs((j_1 * Y[m_1, j_1] >= l * Z[m, l] + M * (1 - V[m, m_1, k]) for m in M for m_1 in M for j in J for j_1 in J for l in L for k in K), name='Constraint 13')
# Constraint (14)
model.addConstrs((gp.quicksum(W[j, k] for k in K) == P[j] for j in J), name='Constraint 14')
# Constraint (15)
model.addConstrs((P[j] <= U[0] for j in range(1, 21)), name='Constraint 15')
# Constraint (16)
model.addConstrs((P[j] <= U[1] for j in range(21, 41)), name='Constraint 16')
# Constraint (17)
model.addConstrs((P[j] <= U[2] for j in range(41, 61)), name='Constraint 17')
# Mulai Optimasi
start_time = time.time()
model.reset(0) # Reset model: remove any solutions from previous runs
model.setParam(GRB.Param.TimeLimit, GUROBI_timelimit)
model.setParam(GRB.Param.Presolve, presolve_setting)
model.optimize()
end_time = time.time()
print("Computational time (CPU time) = ", end_time - start_time, "seconds")
print("Total Gate Dibuka =", model.objVal)
print()
print(model.display())
this is the excel file that I use as parameter D

column j from 1 all the way to 60 it represented as j, while as 1, 2, 3, 4, 5, 6, 7 is represented as m
this is the parameter T

This is the error that I recieve:
0
-
Hi Christian,
I guess there are some index issues when accessing your data in these constraints. Probably, you should check whether you can access D[m-1][j-1] for all m and j. Are you sure this works if m and j are the first available values?
Best regards,
Mario0 -
I'm not quite sure if m and j are the first available values. maybe you can check the mathematical model below for that constraints
0 -
I meant that you should check if the indices and corresponding data exist in your data structures, e.g., by accessing and/or printing them before using them in the constraints.
0
サインインしてコメントを残してください。
コメント
3件のコメント