-
Notifications
You must be signed in to change notification settings - Fork 16
/
brute_force.py
25 lines (22 loc) · 1016 Bytes
/
brute_force.py
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
#coding: utf-8
from itertools import combinations
def brute_force(number, capacity, weight_cost):
"""Brute force method for solving knapsack problem
:param number: number of existing items
:param capacity: the capacity of knapsack
:param weight_cost: list of tuples like: [(weight, cost), (weight, cost), ...]
:return: tuple like: (best cost, best combination list(contains 1 and 0))
"""
best_cost = None
best_combination = []
# generating combinations by all ways: C by 1 from n, C by 2 from n, ...
for way in range(number):
for comb in combinations(weight_cost, way + 1):
weight = sum([wc[0] for wc in comb])
cost = sum([wc[1] for wc in comb])
if (best_cost is None or best_cost < cost) and weight <= capacity:
best_cost = cost
best_combination = [0] * number
for wc in comb:
best_combination[weight_cost.index(wc)] = 1
return best_cost, best_combination