Implement dynamic programming solution for Knapsack.
This commit is contained in:
89
knapsack/knapsack.py
Normal file
89
knapsack/knapsack.py
Normal file
@@ -0,0 +1,89 @@
|
||||
from collections import namedtuple
|
||||
|
||||
Result = namedtuple("Result", ['objective', 'is_optimal', 'xs'])
|
||||
Item = namedtuple("Item", ['index', 'value', 'weight'])
|
||||
Knapsack = namedtuple("Knapsack", ['count', 'capacity', 'items'])
|
||||
|
||||
|
||||
def solve_knapsack_greedy(knapsack):
|
||||
remaining_capacity = knapsack.capacity
|
||||
current_value = 0
|
||||
xs = [0] * knapsack.count
|
||||
|
||||
for item in knapsack.items:
|
||||
if remaining_capacity >= item.weight:
|
||||
remaining_capacity -= item.weight
|
||||
current_value += item.value
|
||||
xs[item.index] = 1
|
||||
|
||||
return Result(current_value, 0, xs)
|
||||
|
||||
|
||||
def solve_knapsack_dynamic(knapsack):
|
||||
value_column = [0 for _ in range(knapsack.capacity + 1)]
|
||||
# Keep track for each item for what capacities we take
|
||||
# the respective item. This allows us to backtrack the items
|
||||
# for the optimal solution later.
|
||||
item_taken = {i: set() for i in range(len(knapsack.items))}
|
||||
|
||||
for item in knapsack.items:
|
||||
new_column = list(value_column)
|
||||
for capacity in range(knapsack.capacity + 1):
|
||||
if item.weight <= capacity:
|
||||
# Calculate the new value for the capacity if we take the item.
|
||||
new_value = item.value + value_column[capacity - item.weight]
|
||||
if new_value > value_column[capacity]:
|
||||
new_column[capacity] = new_value
|
||||
item_taken[item.index].add(capacity)
|
||||
|
||||
# There is no else case because new_colum contains the right values
|
||||
# from the previous item so we only have to update when the new
|
||||
# value is better.
|
||||
value_column = new_column
|
||||
|
||||
objective = value_column[-1]
|
||||
# Dynamic programing computes the best posible solution.
|
||||
is_optimal = 1
|
||||
|
||||
# Reconstruct which items are used for the optimal solution.
|
||||
xs = []
|
||||
current_index = knapsack.items[-1].index
|
||||
current_capacity = knapsack.capacity
|
||||
while current_index >= 0:
|
||||
if current_capacity in item_taken[current_index]:
|
||||
current_capacity -= knapsack.items[current_index].weight
|
||||
xs.append(1)
|
||||
else:
|
||||
xs.append(0)
|
||||
current_index -= 1
|
||||
xs.reverse()
|
||||
|
||||
return Result(objective, is_optimal, xs)
|
||||
|
||||
|
||||
def solve_knapsack_depth_first_search(knapsack):
|
||||
objective = 0
|
||||
pass
|
||||
|
||||
|
||||
def input_data_to_knapsack(input_data):
|
||||
lines = input_data.split('\n')
|
||||
item_count, capacity = map(int, lines[0].split())
|
||||
|
||||
items = []
|
||||
for i in range(1, item_count + 1):
|
||||
line = lines[i]
|
||||
value, weight = map(int, line.split())
|
||||
items.append(Item(i-1, value, weight))
|
||||
|
||||
k = Knapsack(item_count, capacity, items)
|
||||
return k
|
||||
|
||||
|
||||
def result_to_output_data(result):
|
||||
# prepare the solution in the specified output format
|
||||
output_data = str(result.objective) + ' ' + str(result.is_optimal) + '\n'
|
||||
output_data += ' '.join(map(str, result.xs))
|
||||
return output_data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user