-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoverSet.py
38 lines (29 loc) · 956 Bytes
/
CoverSet.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
import random
class CoverSet(object):
# class to represent a set and its subsets
def __init__(self, n_range, set_size, n_subsets, max_k):
"""
:param n_range: max value of numbers in set
:param set_size: number of elements in set
:param n_subsets: number of subsets
:param max_k: max number of subsets a chromosome can have
"""
self.K = max_k
possible_numbers = range(n_range)
self._U = set(random.sample(possible_numbers, set_size))
self._S = []
control = set()
for i in range(n_subsets - 1):
sub_size = random.randrange(set_size)
sub = set(random.sample(self._U, sub_size))
self._S += [sub]
control |= sub
rest = self._U - control
if rest:
self._S += [rest]
@property
def U(self):
return self._U
@property
def S(self):
return self._S