-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathknapsack_branch_and_bound.py
More file actions
123 lines (92 loc) · 3.25 KB
/
knapsack_branch_and_bound.py
File metadata and controls
123 lines (92 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Branch and Bound solution for the 0/1 Knapsack problem.
This implementation uses a best-first search strategy and prunes
non-promising branches using an upper bound calculated via the
fractional knapsack (greedy) approach.
References:
https://en.wikipedia.org/wiki/Branch_and_bound
https://en.wikipedia.org/wiki/Knapsack_problem
"""
from dataclasses import dataclass
from typing import List
import heapq
@dataclass
class Item:
weight: int
value: int
@dataclass
class Node:
level: int
profit: int
weight: int
bound: float
def calculate_bound(node: Node, capacity: int, items: List[Item]) -> float:
"""
Calculate the upper bound of profit for a node using
the fractional knapsack approach.
"""
if node.weight >= capacity:
return 0.0
profit_bound = node.profit
total_weight = node.weight
index = node.level + 1
while index < len(items) and total_weight + items[index].weight <= capacity:
total_weight += items[index].weight
profit_bound += items[index].value
index += 1
if index < len(items):
profit_bound += (
(capacity - total_weight) * items[index].value / items[index].weight
)
return profit_bound
def knapsack_branch_and_bound(
capacity: int, weights: List[int], values: List[int]
) -> int:
"""
Solve the 0/1 Knapsack problem using the Branch and Bound technique.
>>> knapsack_branch_and_bound(50, [10, 20, 30], [60, 100, 120])
220
"""
items = [Item(w, v) for w, v in zip(weights, values)]
items.sort(key=lambda item: item.value / item.weight, reverse=True)
priority_queue: list[tuple[float, Node]] = []
root = Node(level=-1, profit=0, weight=0, bound=0.0)
root.bound = calculate_bound(root, capacity, items)
heapq.heappush(priority_queue, (-root.bound, root))
max_profit = 0
while priority_queue:
_, current = heapq.heappop(priority_queue)
if current.bound <= max_profit:
continue
next_level = current.level + 1
if next_level >= len(items):
continue
# Include the next item
include_node = Node(
level=next_level,
weight=current.weight + items[next_level].weight,
profit=current.profit + items[next_level].value,
bound=0.0,
)
if include_node.weight <= capacity:
max_profit = max(max_profit, include_node.profit)
include_node.bound = calculate_bound(include_node, capacity, items)
if include_node.bound > max_profit:
heapq.heappush(priority_queue, (-include_node.bound, include_node))
# Exclude the next item
exclude_node = Node(
level=next_level,
weight=current.weight,
profit=current.profit,
bound=0.0,
)
exclude_node.bound = calculate_bound(exclude_node, capacity, items)
if exclude_node.bound > max_profit:
heapq.heappush(priority_queue, (-exclude_node.bound, exclude_node))
return max_profit
if __name__ == "__main__":
# Example usage
capacity_example = 50
weights_example = [10, 20, 30]
values_example = [60, 100, 120]
print(knapsack_branch_and_bound(capacity_example, weights_example, values_example))