背包问题(优化后不能正常工作)
Knapsack problem(optimized doesn't work correctly)
我正在编写 Python 代码以解决背包问题。
这是我的代码:
import time
start_time = time.time()
#reading the data:
values = []
weights = []
test = []
with open("test.txt") as file:
W, size = map(int, next(file).strip().split())
for line in file:
value, weight = map(int, line.strip().split())
values.append(int(value))
weights.append(int(weight))
weights = [0] + weights
values = [0] + values
#Knapsack Algorithm:
hash_table = {}
for x in range(0,W +1):
hash_table[(0,x)] = 0
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(i,x)] = hash_table[i - 1,x]
else:
hash_table[(i,x)] = max(hash_table[i - 1,x],hash_table[i - 1,x - weights[i]] + values[i])
print("--- %s seconds ---" % (time.time() - start_time))
这段代码工作正常,但在大文件上我的程序由于 RAM 问题而崩溃。
所以我决定更改以下部分:
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(1,x)] = hash_table[0,x]
#hash_table[(0,x)] = hash_table[1,x]
else:
hash_table[(1,x)] = max(hash_table[0,x],hash_table[0,x - weights[i]] + values[i])
hash_table[(0,x)] = hash_table[(1,x)]
如您所见,我没有使用 n 行,而是只使用了两行(将第二行复制到第一行以重新创建以下代码行 hash_table[(i,x)] = hash_table[i - 1,x]
),这应该可以解决 RAM 问题.
但不幸的是,它给了我一个错误的结果。
我使用了以下测试用例:
190 6
50 56
50 59
64 80
46 64
50 75
5 17
Should get a total value of 150 and total weight of 190 using 3 items:
item with value 50 and weight 75,
item with value 50 and weight 59,
item with value 50 and weight 56,
更多测试用例:https://people.sc.fsu.edu/~jburkardt/datasets/knapsack_01/knapsack_01.html
这里的问题是你需要重置 i 迭代中的所有值,而且还需要 x 索引,所以要这样做,你可以使用另一个循环:
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(1,x)] = hash_table[0,x]
else:
hash_table[(1,x)] = max(hash_table[0,x],hash_table[0,x - weights[i]] + values[i])
for x in range(0, W+1): # Make sure to reset after working on item i
hash_table[(0,x)] = hash_table[(1,x)]
我正在编写 Python 代码以解决背包问题。
这是我的代码:
import time
start_time = time.time()
#reading the data:
values = []
weights = []
test = []
with open("test.txt") as file:
W, size = map(int, next(file).strip().split())
for line in file:
value, weight = map(int, line.strip().split())
values.append(int(value))
weights.append(int(weight))
weights = [0] + weights
values = [0] + values
#Knapsack Algorithm:
hash_table = {}
for x in range(0,W +1):
hash_table[(0,x)] = 0
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(i,x)] = hash_table[i - 1,x]
else:
hash_table[(i,x)] = max(hash_table[i - 1,x],hash_table[i - 1,x - weights[i]] + values[i])
print("--- %s seconds ---" % (time.time() - start_time))
这段代码工作正常,但在大文件上我的程序由于 RAM 问题而崩溃。
所以我决定更改以下部分:
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(1,x)] = hash_table[0,x]
#hash_table[(0,x)] = hash_table[1,x]
else:
hash_table[(1,x)] = max(hash_table[0,x],hash_table[0,x - weights[i]] + values[i])
hash_table[(0,x)] = hash_table[(1,x)]
如您所见,我没有使用 n 行,而是只使用了两行(将第二行复制到第一行以重新创建以下代码行 hash_table[(i,x)] = hash_table[i - 1,x]
),这应该可以解决 RAM 问题.
但不幸的是,它给了我一个错误的结果。
我使用了以下测试用例:
190 6
50 56
50 59
64 80
46 64
50 75
5 17
Should get a total value of 150 and total weight of 190 using 3 items:
item with value 50 and weight 75,
item with value 50 and weight 59,
item with value 50 and weight 56,
更多测试用例:https://people.sc.fsu.edu/~jburkardt/datasets/knapsack_01/knapsack_01.html
这里的问题是你需要重置 i 迭代中的所有值,而且还需要 x 索引,所以要这样做,你可以使用另一个循环:
for i in range(1,size + 1):
for x in range(0,W +1):
if weights[i] > x:
hash_table[(1,x)] = hash_table[0,x]
else:
hash_table[(1,x)] = max(hash_table[0,x],hash_table[0,x - weights[i]] + values[i])
for x in range(0, W+1): # Make sure to reset after working on item i
hash_table[(0,x)] = hash_table[(1,x)]