-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorythms.py
138 lines (102 loc) · 3.85 KB
/
algorythms.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Author: Avo-Catto
Page: algorythms.py
Note: containing the algorythms
"""
from itertools import combinations
import json, os
# functions
def get_weight_worth(item):
item_list = json.load(open("./items.json", "r")) # "Key":[weight, worth]
weight = 0.0
worth = 0.0
try:
_ = item_list["zero"]
except:
item_list.update({"zero":[0.0, 0.0]})
for i in item:
weight += item_list[i][0]
worth += item_list[i][1]
return weight, worth
# algorythms
def recursion(items:tuple, max_weight:float, best_comb:list=[0.0, 0.0, None], count:int=1) -> tuple:
overflow = True
combs = tuple(combinations(items, count))
for item in combs:
foo = get_weight_worth(item)
if all((
foo[0] <= best_comb[0], # best_comb = [weight, worth, items]
foo[1] > best_comb[1]
)) or all((
foo[1] > best_comb[1],
foo[0] <= max_weight
)):
best_comb[0] = foo[0]
best_comb[1] = foo[1]
best_comb[2] = item
if foo[0] < max_weight:
overflow = False
if not overflow:
return recursion(items=items, max_weight=max_weight, best_comb=best_comb, count=count + 1)
else:
return best_comb
def recursively_algorythm(weight_limit):
item_list = json.load(open("./items.json", "r")) # "Key":[weight, worth]
try: del item_list['zero']
except: ...
backpack = recursion(items=item_list, max_weight=weight_limit)
return backpack
def greedy_algorythm(weight_limit):
item_list = json.load(open("./items.json", "r")) # "Key":[weight, worth]
try:
_ = item_list["zero"]
except:
item_list.update({"zero":[0.0, 0.0]})
backpack = []
overflow = False
weight = 0.0
worth = 0.0
while not overflow:
best_item = "zero"
for item in item_list:
if item_list[item][1] > item_list[best_item][1] and item_list[item][0] <= (weight_limit - weight) and item not in backpack:
best_item = item
if best_item == "zero":
overflow = True
else:
backpack.append(best_item)
weight += item_list[best_item][0]
worth += item_list[best_item][1]
return (backpack, worth, weight)
def smart_greedy_algorithm(weight_limit):
item_list = json.load(open("./items.json", "r")) # "Key":[weight, worth]
try:
_ = item_list["zero"]
except:
item_list.update({"zero":[0.0, 0.0]})
backpack = []
overflow = False
weight = 0.0
worth = 0.0
while not overflow:
best_item = ["zero", 0, False] # [key, distance between weight and worth, True if worth > weight else False]
overflow = True
for item in item_list:
if item_list[item][0] < item_list[item][1] and (item_list[item][1] - item_list[item][0]) > best_item[1] and item_list[item][0] <= (weight_limit - weight) and item not in backpack:
best_item = [item, item_list[item][1] - item_list[item][0], True]
overflow = False
elif item_list[item][0] > item_list[item][1] and not best_item[2] and (item_list[item][0] - item_list[item][1]) < best_item[1] and item_list[item][0] <= (weight_limit - weight) and item not in backpack:
best_item = [item, item_list[item][1] - item_list[item][0], False]
overflow = False
else:
pass
if not overflow:
weight += item_list[best_item[0]][0]
worth += item_list[best_item[0]][1]
backpack.append(best_item[0])
return (backpack, worth, weight)
# file check
def check_file():
if not os.path.exists("./items.json"):
with open("./items.json", "w+") as file:
file.write('{"zero":[0.0, 0.0]}')