-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_functions.py
258 lines (202 loc) · 7.04 KB
/
shared_functions.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
functions used by multiple methods
TODO - probably rename the file lets be real 'shared_functions' is hardly clear
"""
import itertools
import collections
import random
def prime_factor_decomposition(val):
"""Reduce any integer into its unique prime decomposition, returning the input integer if the integer itself is
prime.
Parameters
----------
val : int
An integer to be decomposed
Returns
-------
list
List of prime factors of val, or an empty list if val is in [-1, 0, 1]
"""
val = abs(val)
if val not in [0, 1]:
divisors = list(set([d if val % d == 0 else val
for d in range(2, val // 2 + 1)]))
divisors = [d for d in divisors
if all(d % od != 0 for od in divisors if od != d)]
if val in [2, 3]:
divisors = divisors + [val]
return divisors
else:
return []
def get_prime_decomposition_list(
int_list,
text,
):
"""Break a list of integers down into their unique prime factors
Parameters
----------
int_list : list
List of integers to factorise.
text : bool
Set False to avoid printing any statements
Returns
-------
list
List of prime factor decompositions for input list.
"""
if text:
print('\ndecomposing input integers...\n')
prime_decomposition_list = [prime_factor_decomposition(i) for i in int_list
if prime_factor_decomposition(i) != []]
return prime_decomposition_list
def get_sols(prime_decomposition_list):
"""Get list of ints that are definitely within the solution to MinHItSet alg.
Parameters
----------
prime_decomposition_list : list
List of lists of prime numbers
Returns
-------
list
List of unique integers that were previously elements of all sub lists in prime_decomposition_list of length 1.
"""
sols = [decomposition for decomposition in prime_decomposition_list
if len(decomposition) == 1]
sols = list(set(itertools.chain(*sols)))
return sols
def get_remaining(
prime_decomposition_list,
sols,
):
"""Get list of all sub lists that are not included in the solution directly.
steps included:
1. get all decompositions not included in sols.
2. remove all remaining decompositions that are subsets of other decompositions i.e. [2, 3], [3, 2, 7] -> [3, 2, 7]
Parameters
----------
prime_decomposition_list : list
List of lists of prime numbers
sols : list
List of integers which if present as a list in prime_decomposition_list should be dropped.
Returns
-------
list
List of lists where each sublist if of length > 1.
"""
sols = [[i] for i in sols]
remaining = [decomposition for decomposition in prime_decomposition_list
if decomposition not in sols]
remaining_sets = [set(decomposition) for decomposition in remaining]
remaining = [decomposition_list for decomposition_list, decomposition_set in zip(remaining, remaining_sets)
if not any(decomposition_set < other for other in remaining_sets)]
return remaining
def check_if_solved(
remaining,
sols,
):
"""Check if sols is enough to solve MinHitSet alg.
Parameters
----------
remaining : list
List of lists of remaining decompositions to check if sols hit.
sols : list
List of integers that form an at least partial solution to MinHitSet algorithm.
Returns
-------
list
List of remaining solutions not hit by sols.
"""
check = []
for decomposition in remaining:
check.append(any(item in decomposition for item in sols))
if all(check):
return []
else:
indices = [i for i, x in enumerate(check) if x is False]
remaining = [remaining[i] for i in indices]
return remaining
def check_for_single_remaining_sol(
remaining,
sols,
):
"""Check if a single additional int is enough to hit the remaining decompositions.
Parameters
----------
remaining : list
List of lists of remaining decompositions to check if sols hit.
sols : list
List of integers that form an at least partial solution to MinHitSet algorithm.
Returns
-------
list
List of integers representing an at least partial solution to MinHitSet. Adds a random element from the
available sols in the case where multiple single integers will complete the solution.
"""
element_list = list(itertools.chain.from_iterable(remaining))
counts = collections.Counter(element_list)
check = []
for key, value in counts.items():
if value == len(remaining):
check.append(key)
if len(check) > 0:
sols.append(random.choice(check))
return sols
def remove_single_elements(remaining):
"""Remove elements that appear only once in the solution, if they appear in a decomposition containing at least one
other element that appears more than once.
Parameters
---------
remaining : list
List of lists of prime numbers.
Returns
-------
list
List of lists of remaining prime factor decompositions, with single elements removed from each sublist.
"""
element_list = list(itertools.chain.from_iterable(remaining))
counts = collections.Counter(element_list)
solo_elements = [key for key, value in counts.items()
if value == 1]
for decomposition in range(len(remaining)):
if (
any(element in solo_elements for element in remaining[decomposition])
and (not all(element in solo_elements for element in remaining[decomposition]))
):
remaining[decomposition] = [i for i in remaining[decomposition]
if i not in solo_elements]
return remaining
def solution_reduction(
remaining,
sols,
text,
):
"""Run the solution space reduction steps on remaining.
Parameters
----------
remaining : list
List of lists of remaining decompositions to check if sols hit.
sols : list
List of integers that form an at least partial solution to MinHitSet algorithm.
text : bool
Set False to avoid printing any statements
Returns
-------
list
List of either lists or ints depending on whether the reduction is enough to solve the algorithm or not.
"""
if text:
print('checking if problem solved without need for chosen algorithm...\n')
if len(remaining) == 0:
if text:
print('\tMinHitSet complete! solution = {}'.format(sols))
return sols
else:
sols = check_for_single_remaining_sol(remaining, sols)
remaining = check_if_solved(remaining, sols)
if len(remaining) == 0:
if text:
print('\tMinHitSet complete! solution = {}'.format(sols))
return sols
else:
remaining = remove_single_elements(remaining)
return remaining