Make depth first search for Knapsack non-recursive and more efficient. Solves all problems in reasonable time now.
This commit is contained in:
parent
bb9678a493
commit
c21240d9ea
@ -1,4 +0,0 @@
|
||||
3 10
|
||||
45 5
|
||||
48 8
|
||||
35 3
|
@ -3,6 +3,7 @@ from collections import namedtuple
|
||||
Result = namedtuple("Result", ['objective', 'is_optimal', 'xs'])
|
||||
Item = namedtuple("Item", ['index', 'value', 'weight'])
|
||||
Knapsack = namedtuple("Knapsack", ['count', 'capacity', 'items'])
|
||||
Node = namedtuple("Node", ['index', 'value', 'room', 'estimate', 'path'])
|
||||
|
||||
|
||||
def solve_knapsack_greedy(knapsack):
|
||||
@ -62,57 +63,69 @@ def solve_knapsack_dynamic(knapsack):
|
||||
|
||||
|
||||
def solve_knapsack_depth_first_search(knapsack):
|
||||
knapsack.items.sort(key=lambda item: item.value / item.weight,
|
||||
knapsack.items.sort(key=lambda item: item.value / float(item.weight),
|
||||
reverse=True)
|
||||
num_items = len(knapsack.items)
|
||||
result = {"objective": 0,
|
||||
"path": []}
|
||||
|
||||
def get_max_value(from_index, capacity):
|
||||
value = 0
|
||||
for i in range(from_index, num_items):
|
||||
item = knapsack.items[i]
|
||||
if item.weight <= capacity:
|
||||
value += item.value
|
||||
capacity -= item.weight
|
||||
def get_estimate(current_value, current_index, remaining_capacity):
|
||||
estimated_value = current_value
|
||||
for item in knapsack.items[current_index:]:
|
||||
if item.weight <= remaining_capacity:
|
||||
estimated_value += item.value
|
||||
remaining_capacity -= item.weight
|
||||
else:
|
||||
value += int((capacity / item.weight) * item.value)
|
||||
break
|
||||
return value
|
||||
v = int((remaining_capacity / float(item.weight)) * item.value)
|
||||
estimated_value += v
|
||||
return estimated_value
|
||||
return estimated_value
|
||||
|
||||
def search(index, capacity, value, path, result):
|
||||
if value > result["objective"]:
|
||||
result["objective"] = value
|
||||
result["path"] = list(path)
|
||||
estimate = get_estimate(0, 0, knapsack.capacity)
|
||||
nodes = [Node(0, 0, knapsack.capacity, estimate, [])]
|
||||
num_items = len(knapsack.items)
|
||||
best_value = 0
|
||||
best_path = []
|
||||
|
||||
if capacity <= 0:
|
||||
return
|
||||
while nodes:
|
||||
current_node = nodes.pop()
|
||||
current_index = current_node.index
|
||||
|
||||
if index == num_items:
|
||||
return
|
||||
if current_node.room < 0:
|
||||
continue
|
||||
|
||||
# print("--- search")
|
||||
# print("Index: {} capacity: {}, value: {}".format(index, capacity, value))
|
||||
# print("Path {}".format(path))
|
||||
if current_node.estimate < best_value:
|
||||
continue
|
||||
|
||||
# Take current item.
|
||||
max_value = get_max_value(index, capacity)
|
||||
print(max_value)
|
||||
item = knapsack.items[index]
|
||||
# print("max_value: {}".format(max_value))
|
||||
max_value_not = get_max_value(index + 1, capacity)
|
||||
# print("max_value_not: {}".format(max_value_not))
|
||||
if item.weight <= capacity and value + max_value > result["objective"]:
|
||||
path.append(1)
|
||||
search(index + 1, capacity - item.weight,
|
||||
value + item.value, path, result)
|
||||
path.pop()
|
||||
if current_node.value > best_value:
|
||||
best_value = current_node.value
|
||||
best_path = list(current_node.path)
|
||||
|
||||
# Do not take current item.
|
||||
if value + max_value_not > result["objective"]:
|
||||
path.append(0)
|
||||
search(index + 1, capacity, value, path, result)
|
||||
path.pop()
|
||||
if current_index == num_items:
|
||||
continue
|
||||
|
||||
current_item = knapsack.items[current_index]
|
||||
|
||||
# Right child - do not take current_item
|
||||
new_index = current_index + 1
|
||||
new_value = current_node.value
|
||||
new_room = current_node.room
|
||||
new_estimate = get_estimate(new_value, new_index, new_room)
|
||||
if new_estimate > best_value:
|
||||
new_path = current_node.path + [0]
|
||||
r = Node(new_index, new_value, new_room, new_estimate, new_path)
|
||||
nodes.append(r)
|
||||
|
||||
# Left child - take current_item
|
||||
new_index = current_index + 1
|
||||
new_room = current_node.room - current_item.weight
|
||||
if new_room >= 0:
|
||||
new_value = current_node.value + current_item.value
|
||||
new_estimate = get_estimate(new_value, new_index, new_room)
|
||||
new_path = current_node.path + [1]
|
||||
n = Node(new_index, new_value, new_room, new_estimate, new_path)
|
||||
nodes.append(n)
|
||||
|
||||
# If we sort the items by estimate here it becomes
|
||||
# best first search.
|
||||
# nodes.sort(key=lambda node: node.estimate)
|
||||
|
||||
def correct_path(path):
|
||||
path = path + [0] * (num_items - len(path))
|
||||
@ -120,9 +133,7 @@ def solve_knapsack_depth_first_search(knapsack):
|
||||
path = [v[0] for v in sorted(values, key=lambda value: value[1].index)]
|
||||
return path
|
||||
|
||||
search(0, knapsack.capacity, 0, [], result)
|
||||
|
||||
return Result(result["objective"], 1, correct_path(result["path"]))
|
||||
return Result(best_value, 0, correct_path(best_path))
|
||||
|
||||
|
||||
def input_data_to_knapsack(input_data):
|
||||
|
@ -1,15 +1,13 @@
|
||||
#!/usr/bin/python3
|
||||
#!/usr/bin/pypy
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import knapsack
|
||||
|
||||
|
||||
def solve_it(input_data):
|
||||
# Modify this code to run your optimization algorithm
|
||||
k = knapsack.input_data_to_knapsack(input_data)
|
||||
# r = knapsack.solve_knapsack_dynamic(k)
|
||||
r = knapsack.solve_knapsack_depth_first_search(k)
|
||||
# r = knapsack.solve_knapsack_greedy(k)
|
||||
return knapsack.result_to_output_data(r)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user