对于这个 0/1 贪心算法,有没有办法打印添加到背包中的内容?
Is there a way to print the contents added to a knapsack for this 0/1 Greedy algorithm?
我最近的实验室作业让我尝试针对 0/1 背包问题实施贪婪算法,并打印出背包的内容以及背包的总价值。到目前为止,我能够让它毫无问题地输出背包的总价值,但我在输出背包中的物品时遇到了问题。
#class definitions for the greedy approach
class Item:
def __init__(self,weight,value):
self.weight = weight
self.value = value
self.price_kg = value / weight
def __repr__(self):
return f"Item(weight={self.weight}, value={self.value},v/w={self.price_kg})\n"
class Knapsack:
def __init__(self,max_weight,items):
self.max_weight = max_weight
self.items = items
self.contents = list()
def fillGreedy(self):
self.items.sort(key=lambda x: x.price_kg, reverse=True)#sorts the items by weight/value
for i in self.items:
self.contents.append(i)#Tries putting the item in the bag
if sum(i.weight for i in self.contents) > self.max_weight:
self.contents.remove(i)#Removes the item it is too heavy for the bag
elif sum(i.weight for i in self.contents) == self.max_weight:#finds an optimal configuration for the bag
return sum(i.value for i in self.contents)
return sum(i.value for i in self.contents)
#main method
max_weights = [10, 13, 15, 30, 30]
weights = [
[4, 5, 7],
[6, 5, 7, 3, 1],
[2, 3, 5, 5, 3, 7],
[10, 13, 17, 15],
[5, 4, 7, 6, 3, 4, 2, 1, 7, 6]
]
values = [
[2, 3, 4],
[7, 3, 4, 4, 3],
[3, 4, 10, 9, 6, 13],
[21, 17, 30, 23],
[3, 1, 3, 2, 1, 3, 2, 3, 1, 4]
]
for i in range(len(max_weights)):
items = list()
for j in range(len(weights[i])):
items.append(Item(weights[i][j], values[i][j])) #adds the contents of the arrays to the Items list
i
ks = Knapsack(max_weights[i], items)
v1 = ks.fillGreedy()
print(f"Total value = {v1}")
#print(items)
到目前为止,我尝试打印出 ks 和 v1 对象的内容,但这只给出了对象的内存地址。我尝试在遍历 fillGreedy 方法后打印出 'items' 列表本身,但它打印出列表的所有内容,而不是背包本身中的内容。我还尝试在 fillGreedy 方法中做一些事情来打印刚刚添加的项目,但最终导致了冲突。我不确定从哪里继续。有没有办法用这种方法打印出背包的物品?
欢迎来到本站。
您已经在 Knapsack 对象中拥有所选项目的集合,因此您可以遍历 ks.contents 并打印出内容或从那里需要的任何内容...
for item in ks.contents:
print(item)
我最近的实验室作业让我尝试针对 0/1 背包问题实施贪婪算法,并打印出背包的内容以及背包的总价值。到目前为止,我能够让它毫无问题地输出背包的总价值,但我在输出背包中的物品时遇到了问题。
#class definitions for the greedy approach
class Item:
def __init__(self,weight,value):
self.weight = weight
self.value = value
self.price_kg = value / weight
def __repr__(self):
return f"Item(weight={self.weight}, value={self.value},v/w={self.price_kg})\n"
class Knapsack:
def __init__(self,max_weight,items):
self.max_weight = max_weight
self.items = items
self.contents = list()
def fillGreedy(self):
self.items.sort(key=lambda x: x.price_kg, reverse=True)#sorts the items by weight/value
for i in self.items:
self.contents.append(i)#Tries putting the item in the bag
if sum(i.weight for i in self.contents) > self.max_weight:
self.contents.remove(i)#Removes the item it is too heavy for the bag
elif sum(i.weight for i in self.contents) == self.max_weight:#finds an optimal configuration for the bag
return sum(i.value for i in self.contents)
return sum(i.value for i in self.contents)
#main method
max_weights = [10, 13, 15, 30, 30]
weights = [
[4, 5, 7],
[6, 5, 7, 3, 1],
[2, 3, 5, 5, 3, 7],
[10, 13, 17, 15],
[5, 4, 7, 6, 3, 4, 2, 1, 7, 6]
]
values = [
[2, 3, 4],
[7, 3, 4, 4, 3],
[3, 4, 10, 9, 6, 13],
[21, 17, 30, 23],
[3, 1, 3, 2, 1, 3, 2, 3, 1, 4]
]
for i in range(len(max_weights)):
items = list()
for j in range(len(weights[i])):
items.append(Item(weights[i][j], values[i][j])) #adds the contents of the arrays to the Items list
i
ks = Knapsack(max_weights[i], items)
v1 = ks.fillGreedy()
print(f"Total value = {v1}")
#print(items)
到目前为止,我尝试打印出 ks 和 v1 对象的内容,但这只给出了对象的内存地址。我尝试在遍历 fillGreedy 方法后打印出 'items' 列表本身,但它打印出列表的所有内容,而不是背包本身中的内容。我还尝试在 fillGreedy 方法中做一些事情来打印刚刚添加的项目,但最终导致了冲突。我不确定从哪里继续。有没有办法用这种方法打印出背包的物品?
欢迎来到本站。
您已经在 Knapsack 对象中拥有所选项目的集合,因此您可以遍历 ks.contents 并打印出内容或从那里需要的任何内容...
for item in ks.contents:
print(item)