こんにちは、再帰的で動的にプログラムされたナップサック (0-1) を作成してテストしています (次のコードはメイン ファイルと関数です)。エラーが発生し続けます (コードの後に投稿されます)。エラーは何ですか?どんな助けでも大歓迎です!
メインファイル
import Knapsack_Test
size = 10
W = 2**size
knapsack = Knapsack_Test.Knapsack_Test()
for i in range(1, 10):
knapsack.recursive_knapsack_test(size, W)
size = size + 100*i
W = 2**size
関数呼び出し (クラス Knapsack_Test.py に含まれる)
def recursive_dynamic_knap(self, v, w, i, W):
n = len(v) - 1
if i == n:
if W < w[n]:
return 0
else:
return v
elif W < w[i]:
return self.recursive_dynamic_knap(v, w, i+1, W)
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
エラー
Traceback (most recent call last):
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Main.py", line 9, in <module>
knapsack.recursive_knapsack_test(size, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Test.py", line 19, in recursive_knapsack_test
self.knapsack.recursive_dynamic_knap(v, w, j, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
TypeError: unorderable types: int() > list()