KeyError: 0 in Python
回答済みHi,
I have the following code:
from typing import List
from data_contoh_mod import *
def _pm(i, iterasi):
for col in S[i]:
precedence[iterasi][col] = 1
if col in S:
_pm(col, iterasi)
def generate_pm():
for i in key:
_pm(i, i)
precedence[0][1:] = 1
def _calculate_ranked_position_method(durasi):
a, b, c, d = durasi
up = c**2 + d**2 - (a**2 + b**2) + (c * d) - (a * b)
down = 3 * ((c + d) - (a + b))
return round(up/down, 2)
def _ranked_position_method(*durasi):
to_count = [0, 0, 0, 0]
for dur in durasi:
for x in dur:
to_count[0] += x[0]
to_count[1] += x[1]
to_count[2] += x[2]
to_count[3] += x[3]
return _calculate_ranked_position_method(to_count)
def count_weight(data):
generate_pm()
weights: List[int] = []
to_count = []
for row in range(1, len(data)+1):
tc = []
counter = 0
for col in range(1, len(data)+1):
if precedence[row][col]:
tc.append(data[counter])
counter += 1
tc.insert(0, data[row-1])
to_count.append(tc)
for i in to_count:
ui = _ranked_position_method(i)
weights.append(ui)
weights.insert(0, weights[0])
weights.append(0)
return weights
def _knapsack(W: int, wt: List, val: List, n: int) -> List:
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i - 1] <= w:
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w])
else:
K[i][w] = K[i - 1][w]
res = K[n][W]
w = W
Z = []
for i in range(n, 0, -1):
if res <= 0:
break
if res == K[i - 1][w]:
continue
else:
# This item is included.
Z.append(val[i - 1])
res = res - val[i - 1]
w = w - wt[i - 1]
return Z
# knapsack
def solve_knapsack(R: List, E: List) -> List:
val = []
for e in E:
val.append(u[e])
wt = []
for row in range(len(R)):
temp = []
for e in E:
temp.append(kebutuhan[e][row])
wt.append(temp)
Z_list = []
for w in wt:
counter = 0
z = _knapsack(R[0], w, val, len(val))
Z_list.append(z)
counter += 1
ctr = {}
for v in val:
for Z in Z_list:
if v in Z and v not in ctr:
ctr[E[val.index(v)]] = 1
elif v in Z and ctr[E[val.index(v)]]:
ctr[E[val.index(v)]] =+ 1
to_process = []
for c in ctr:
if ctr[c] == len(R):
to_process.append(c)
return to_process
u = count_weight(data_durasi)
u[n+1] = 1
schedule = {}
def rcpsp(A, F, P, s0, RSk):
if F == A:
return schedule
U = list(set(A).difference(set(F)))
ss = []
for j in U:
if j in S:
for i in S[j]:
ss.append(i)
N = list(set(ss))
E = list(set(U).difference(set(N).union(P)))
to_process = solve_knapsack(RSk, E)
for tp in to_process:
P.append(tp)
for i in to_process:
if i not in schedule:
si = list(s0)
fi = []
di = data_durasi[i-1]
for s in range(len(si)):
fi.append(si[s] + di[s])
schedule[i] = [si, fi]
sigma_r = [0 for x in range(len(RSk))]
for i in to_process:
for k in range(len(kebutuhan[i])):
sigma_r[k] += kebutuhan[i][k]
for index in range(len(RSk)):
RSk[index] = RSk[index] - sigma_r[index]
j = []
j_index = []
for i in P:
j.append(schedule[i][1][-1])
j_index.append(i)
f4 = min(j)
f4_index = j.index(f4)
F = list(set(F).union(set([j_index[f4_index]])))
Ft = j_index[f4_index]
s0 = schedule[Ft][1]
for index in range(len(RSk)):
RSk[index] = RSk[index] + kebutuhan[Ft][index]
P, F = set(P), set(F)
pf = P.intersection(F)
P = list(P.difference(pf))
F = list(F)
if F[-1] == n:
schedule[F[-1] + 1] = schedule[F[-1]]
F.append(F[-1] + 1)
rcpsp(A, F, P, s0, RSk)
rcpsp(A, F, P, s0, RSk)
print(schedule)
D_s = [0, 0, 0, 0]
for i in range(len(b)):
D_s[i] = schedule[A[-1]][1][i] - b[i]
print(D_s)
With input
import numpy as np
n = 7 # aktivitas
A = list(range(0, n + 1 + 1))
K = [2]
R = len(K)
b = (0, 1, 2, 3)
e = (57, 58, 60, 63)data_durasi = [(5, 7, 8, 10),
(8, 10, 15, 18),
(14, 17, 20, 24),
(9, 12, 16, 20),
(3, 5, 7, 9),
(5, 9, 12, 15),
(20, 24, 28, 33)
]
S = {1: [2, 3, 4],
2: [5],
3: [6],
4: [6],
5: [7],
6: [7],
7: [8]}
kebutuhan = {1: [1,2],
2: [1,2],
3: [1,2],
4: [1,2],
5: [1,1],
6: [1,2],
7: [1,1]}key = list(S.keys())
precedence = np.zeros((len(A), len(A))).astype(np.int8)F = [0]
P = []
si = fi = (0, 0, 0, 0)
s0 = f0 = b
RSk = [2,3]
When I run this code, I get this error:
File "/home/support/Pictures/rcpsp/rcpsp.py", line 198, in <module>
rcpsp(A, F, P, s0, RSk)
File "/home/support/Pictures/rcpsp/rcpsp.py", line 177, in rcpsp
f4 = min(j)
ValueError: min() arg is an empty sequence
This code can run for a single RSk. Not sure why this is happening. Any help is appreciated!
Thank you in advance!
-
正式なコメント
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 Hilda,
Your Python code is not indented. Could you please edit the indentation in your code such that it is executable?
Best regards,
Jaromił0 -
updated, thank you! Jaromił Najman
0 -
Hi Hilda,
In your code, the list \(\texttt{j}\) is empty. List \(\texttt{j}\) is filled through list \(\texttt{P}\) which is also empty. List \(\texttt{P}\) depends on list \(\texttt{to_process}\) which is empty as well. List \(\texttt{to_process}\) is computed by the \(\texttt{solve_knapsack}\) function. I guess you have to revise the \(\texttt{solve_knapsack}\) fucntion and check whether it is correct. Simple \(\texttt{print("")}\) statements should be enough to debug this problem.
Additionally, please note that the code as given has nothing to do with Gurobi. One of the stackexchange forums should be better suited for debugging questions.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
4件のコメント