-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathsolver.py
executable file
·56 lines (41 loc) · 1.53 KB
/
solver.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import namedtuple
Set = namedtuple("Set", ['index', 'cost', 'items'])
def solve_it(input_data):
# Modify this code to run your optimization algorithm
# parse the input
lines = input_data.split('\n')
parts = lines[0].split()
item_count = int(parts[0])
set_count = int(parts[1])
sets = []
for i in range(1, set_count+1):
parts = lines[i].split()
sets.append(Set(i-1, float(parts[0]), map(int, parts[1:])))
# build a trivial solution
# pick add sets one-by-one until all the items are covered
solution = [0]*set_count
covered = set()
for s in sets:
solution[s.index] = 1
covered |= set(s.items)
if len(covered) >= item_count:
break
# calculate the cost of the solution
obj = sum([s.cost*solution[s.index] for s in sets])
# prepare the solution in the specified output format
output_data = str(obj) + ' ' + str(0) + '\n'
output_data += ' '.join(map(str, solution))
return output_data
import sys
if __name__ == '__main__':
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
input_data_file = open(file_location, 'r')
input_data = ''.join(input_data_file.readlines())
input_data_file.close()
print 'Solving:', file_location
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/sc_6_1)'