-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_dynamic.py
81 lines (63 loc) · 2.25 KB
/
solver_dynamic.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import namedtuple
Item = namedtuple("Item", ['index', 'value', 'weight'])
def solve_it(input_data):
# Modify this code to run your optimization algorithm
# parse the input
lines = input_data.split('\n')
firstLine = lines[0].split()
item_count = int(firstLine[0])
capacity = int(firstLine[1])
items = []
for i in range(1, item_count+1):
line = lines[i]
parts = line.split()
items.append(Item(i-1, int(parts[0]), int(parts[1])))
# a trivial greedy algorithm for filling the knapsack
# it takes items in-order until the knapsack is full
weight = 0
taken = [0]*len(items)
#for item in items:
# if weight + item.weight <= capacity:
# taken[item.index] = 1
# value += item.value
# weight += item.weight
DP_table = []
DP_coloumn = []
wt = 0
while wt <= capacity:
DP_coloumn.append(0)
wt=wt+1
DP_table.append(DP_coloumn)
for item in items:
DP_coloumn = []
wt = 0
while wt <= capacity:
if wt < item.weight:
input_value = DP_table[item.index][wt]
else:
input_value = max(DP_table[item.index][wt],(DP_table[item.index][wt-(item.weight)] + item.value))
DP_coloumn.append(input_value)
wt = wt+1
DP_table.append(DP_coloumn)
value = DP_table[len(items)][capacity]
items = items[::-1]
wt = capacity
for item in items:
if DP_table[(item.index)+1][wt] == (DP_table[item.index][wt - (item.weight)] + item.value):
taken[item.index] = 1
wt = wt - item.weight
# prepare the solution in the specified output format
output_data = str(value) + ' ' + str(0) + '\n'
output_data += ' '.join(map(str, taken))
return output_data
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
with open(file_location, 'r') as input_data_file:
input_data = input_data_file.read()
print(solve_it(input_data))
else:
print('This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)')