-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathks.py
84 lines (69 loc) · 2.93 KB
/
ks.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
82
83
84
from argparse import ArgumentParser
from Greedy import *
from Memoization import *
from Tabulation import *
import DataReader as dr
from Utilities import *
import time
def args_creator():
parser = ArgumentParser()
parser.add_argument("-d", "--directory", action="store", dest="directory", default="", help="directory (process many files)", metavar="[DIRECTORY]")
parser.add_argument("-f", "--file", action="store", dest="file", default="", help="file (process a single file)", metavar="[FILE]")
parser.add_argument("-b", "--benefit", action="store_true", dest="showBenefit", default=False, help="Display benefit")
parser.add_argument("-r", "--room", action="store_true", dest="showRoom", default=False, help="Display room (unused knapsack weight)")
parser.add_argument("-t", "--time", action="store_true", dest="timer", default=False, help="Display time")
parser.add_argument("-dt", "--display_taken", action="store_true", dest="showTaken", default=False, help="Display identifier of taken items")
parser.add_argument("-sg", "--greedy", action="store_true", dest="greedy", default=False, help="Solve it with Greedy")
parser.add_argument("-sm", "--memoization", action="store_true", dest="memoization", default=False, help="Solve it with Memoization")
parser.add_argument("-st", "--tabulation", action="store_true", dest="tabulation", default=False, help="Solve it with Tabulation")
return parser.parse_args()
def withDirectory(files):
t = time.time()
for f in files:
print("----------" + f + "----------")
withFile(f)
print("<<<< END >>>>")
t = time.time() - t
print("Total execution time = ", t, "s")
exit(0)
def withFile(file):
items, capacity = dr.readFile(file)
t0 = 0
t1 = 0
if args.greedy:
print("Solving it with Greedy")
t0 = time.time()
value, taken = solve_greedy(items, capacity)
t1 = time.time()
elif args.memoization:
print("Solving it with Memoization")
t0 = time.time()
value, taken = memoization(items, capacity)
t1 = time.time()
elif args.tabulation:
print("Solving it with Tabulation")
t0 = time.time()
value, taken = tabulation(items, capacity)
t1 = time.time()
else:
print("Introduzca un metodo, por favor")
exit(-1)
if args.showBenefit:
print("Benefit =", get_total_value(items, taken))
if args.showRoom:
print("Room =", get_left_weight(capacity, items, taken))
if args.showTaken:
print("Take items =", taken_items(items, taken))
if args.timer:
t = (t1 - t0)
print("Time =", round(t, 3), "s")
if __name__ == '__main__':
args = args_creator()
if args.directory != "":
files = dr.readAllFiles(args.directory)
withDirectory(files)
elif args.file != "":
withFile(args.file)
else:
print("Introduzca un fichero, por favor")
exit(-1)