-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathks_dp_cython_script.py
More file actions
73 lines (62 loc) · 2.54 KB
/
Copy pathks_dp_cython_script.py
File metadata and controls
73 lines (62 loc) · 2.54 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
import pyximport; pyximport.install()
import ks_dp_cython
import argparse
import numpy as np
import timeit
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Dynamic programming solution '
'of knapsack problem implemented in Cython')
parser.add_argument('-v', action='store_true', default=False,
help='Verbose output. Otherwise only solution value is printed')
parser.add_argument('-f', default='nasdaq100list.csv', metavar='filename',
help='Name of CSV data file (default: %(default)s). '
'Data format: label, weight, value. No header line.')
parser.add_argument('-c', default=1000000, metavar='capacity',
help='Knapsack capacity')
parser.add_argument('-n', default=10, metavar='number',
help='N of repetitions (to measure running time)')
parser.add_argument('-o', action='store_true', default=False,
help='Optimize array indexing')
parser.add_argument('-t', action='store_true', default=False,
help='Print running time (sec)')
args = parser.parse_args()
verbose = args.v
datafilename = args.f
capacity = int(args.c)
n = int(args.n)
optimize_indexing = args.o
print_time = args.t
solver = ks_dp_cython.solve_cython
if optimize_indexing:
solver = ks_dp_cython.solve_cython_optim
labels = []
weights = []
values = []
items = 0
with open(datafilename, 'r') as file:
data = file.read()
for line in data.split('\n'):
symbol, price, target = line.split(',')
labels.append(symbol)
weights.append(int(float(price)*100))
values.append(int(float(target)*100))
items +=1
if verbose:
print ("Got data: {} items".format(items))
timers = np.empty((n), dtype=float)
for i in range(n):
timer0 = timeit.default_timer()
solution_value, solution_weight, taken = \
solver(capacity, items, np.array(weights), np.array(values))
timer1 = timeit.default_timer()
timers[i] = timer1 - timer0
if verbose:
print("Solution_value: {}".format(solution_value))
print("Solution weight: {}\nTook {} items\nItems taken: {}\n".
format(solution_weight, len(taken),
", ".join([labels[i] for i in sorted(taken)])))
else:
print(solution_value)
if print_time:
print("Running time {:.5f} sec.".format(np.mean(timers)))