-
Notifications
You must be signed in to change notification settings - Fork 0
/
weights.py
156 lines (120 loc) · 5.2 KB
/
weights.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import copy
import combinations
class Weights(object):
def __init__(self, weight_counts: dict, dumbbell_weight: float = 2.0):
self.available_weights = weight_counts
self.dumbbell_weight = dumbbell_weight
def symmetric_configs(self, max_weights: int = 3):
symmetric_weights = dict()
for k, v in self.available_weights.items():
symmetric_weights[k] = int(v / 2)
available_pairs = []
for k, v in symmetric_weights.items():
for _ in range(v):
available_pairs.append(k)
# print("pairs: {}".format(available_pairs))
all_configs = list(combinations.generate_all_combinations(available_pairs))
limited_configs = []
for combo in all_configs:
# print(combo)
if len(combo) <= max_weights:
# print("adding combo {}".format(combo))
limited_configs.append(combo)
return limited_configs
def weight_configs(self) -> dict:
weight_configs = dict()
configs = self.symmetric_configs()
weight_configs[self.dumbbell_weight] = [[0]]
for config in configs:
weight = sum(config) * 2 + self.dumbbell_weight
if weight not in weight_configs:
weight_configs[weight] = []
weight_configs[weight].append(config)
return weight_configs
def configs_for_weight(self, weight: float):
# print("combos for weight: {}".format(weight))
all_configs = self.weight_configs()
if weight in all_configs:
configs = all_configs[weight]
unique_config = []
for config in configs:
if config not in unique_config:
unique_config.append(config)
# print(unique_combos)
return unique_config
return []
def possible_total_weights(self):
configs = self.weight_configs()
sorted_configs = [k for k in sorted(configs.keys())]
return sorted_configs
def remove_symmetric_config(self, config: [float]):
# print("removing combo from {}: {}".format(self, config))
for weight in config:
if weight > 0:
count = self.available_weights[weight]
if count > 1:
count -= 2
self.available_weights[weight] = count
else:
# print(self.available_weights)
raise ValueError("Not enough weights available!")
def reduced_weight_objects(originals: [Weights], total_weight: float):
reduced_weights = []
# print("num originals: {}".format(len(originals)))
for original in originals:
combos = original.configs_for_weight(total_weight)
tmp_original = copy.deepcopy(original)
# print("num combos: {}".format(len(combos)))
for i, combo in enumerate(combos):
original.remove_symmetric_config(combo)
# print("combo #{:3}: {}".format(i, combo))
reduced_weights.append(copy.deepcopy(original))
original = copy.deepcopy(tmp_original)
# print("num reduced: {}".format(len(reduced_weights)))
return reduced_weights
def possible_double_weights(weight_objects: [Weights]):
cp_weight_objects = [copy.deepcopy(w) for w in weight_objects]
double_weights = []
single_weights = possible_weights(cp_weight_objects)
for w in single_weights:
reduced_weights = reduced_weight_objects([copy.deepcopy(wo) for wo in cp_weight_objects], w)
s_weights = possible_weights(reduced_weights)
if w in s_weights:
double_weights.append(w)
return double_weights
def possible_weights(weight_objects: [Weights]):
possible = []
for weights in weight_objects:
for weight in weights.possible_total_weights():
if weight not in possible:
possible.append(weight)
return [w for w in sorted(possible)]
def get_combos(weight_object: Weights, total_weights: [float]):
print()
print("total weights: {}".format(total_weights))
combo_of_configs = []
original_weight_object = copy.deepcopy(weight_object)
weight = total_weights[0]
configs = weight_object.configs_for_weight(weight)
for config in configs:
weight_object = copy.deepcopy(original_weight_object)
t = (weight, config)
# print("tuple: {}".format(t))
if len(total_weights) == 1:
combo_of_configs.append(t)
return combo_of_configs
weight_object.remove_symmetric_config(config)
combos = get_combos(weight_object, total_weights[1:])
# print("combos: {}".format(combos))
if combos:
for c in combos:
if len(combo_of_configs) < len(total_weights):
combo_of_configs.append(c)
if len(combo_of_configs) < len(total_weights):
combo_of_configs.append(t)
# print("len(combo_of_configs): {}".format(len(combo_of_configs)))
# print("len(total_weights): {}".format(len(total_weights)))
if len(combo_of_configs) == len(total_weights):
# print("returning {}".format(combo_of_configs))
return combo_of_configs
# return combo_of_configs