-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.py
208 lines (171 loc) · 6.7 KB
/
algorithm.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
"""Roommate generator algorithm."""
import numpy as np
from typing import *
import random
import math
class Matching:
def __init__(
self,
prefs: np.ndarray,
group_size: int = 4,
iter_count: int = 2,
final_iter_count: int = 4,
):
self.group_size = group_size
self.prefs = prefs
self.iter_count = iter_count
self.final_iter_count = final_iter_count
self.num_members = self.prefs.shape[0]
self.num_groups = math.ceil(self.num_members / group_size)
self.ungrouped = [i for i in range(self.num_members)]
self.unfilled = []
self.filled = []
for i in random.sample(range(0, self.num_members), self.num_groups):
self.unfilled.append(Group(self, [i]))
self.ungrouped.remove(i)
super().__init__()
@staticmethod
def from_csv(file_path, r: int = 4):
prefs = np.genfromtxt(file_path, delimiter=",")
return Matching(prefs, r)
def get_mem_pref_for_group(self, mem: int, grp: List[int]) -> int:
pref: int = 0
for i in grp:
pref += self.prefs[mem][i]
pref = pref * (1.0 / len(grp))
return pref
def get_group_pref_for_mem(self, mem: int, grp: List[int]) -> int:
pref: int = 0
for i in grp:
pref += self.prefs[i][mem]
pref = pref * (1.0 / len(grp))
return pref
def get_group_score(self, y: List[int]) -> int:
if len(y) <= 1:
return 0
score: int = 0
for i in y:
for j in y:
if not (i == j):
score += self.prefs[i][j]
score = score * (1.0 / (len(y) ** 2 - len(y)))
return score
def get_net_score(self) -> float:
score = 0
for i in self.filled:
score += self.get_group_score(i.members)
return score / self.num_groups
def solve(self):
while len(self.ungrouped) != 0:
self.add_one_member()
self.filled.extend(self.unfilled)
self.unfilled = []
self.optimize(use_filled=True)
grps = []
for i in self.filled:
grps.append(i.members)
return self.get_net_score(), grps
def optimize(self, use_filled: bool = True):
if use_filled:
grps = self.filled
else:
grps = self.unfilled
iters = self.final_iter_count if use_filled else self.iter_count
for a in range(iters):
for grp1 in grps:
for mem1 in grp1.members:
for grp2 in grps:
if mem1 == -1:
break
if grp2 == grp1:
continue
for mem2 in grp2.members:
if mem1 == -1:
break
if mem2 == mem1:
continue
grp2mem1 = grp2.members.copy()
grp2mem1.remove(mem2)
grp2mem1.append(mem1)
grp1mem2 = grp1.members.copy()
grp1mem2.remove(mem1)
grp1mem2.append(mem2)
grp_one_new_score = self.get_group_score(grp1mem2)
grp_two_new_score = self.get_group_score(grp2mem1)
if (
grp_one_new_score + grp_two_new_score
> self.get_group_score(grp1.members)
+ self.get_group_score(grp2.members)
):
grp1.add_member(mem2)
grp1.remove_member(mem1)
grp2.add_member(mem1)
grp2.remove_member(mem2)
mem1 = -1
def add_one_member(self):
proposed = np.zeros(shape=(len(self.ungrouped), len(self.unfilled)), dtype=bool)
is_temp_grouped = [False for i in range(len(self.ungrouped))]
temp_pref = np.zeros(shape=(len(self.ungrouped), len(self.unfilled)))
temp_pref_order = np.zeros(
shape=(len(self.ungrouped), len(self.unfilled)), dtype=int
)
for i, mem in enumerate(self.ungrouped):
for j, grp in enumerate(self.unfilled):
temp_pref[i][j] = self.get_mem_pref_for_group(mem, grp.members)
for i, mem in enumerate(self.ungrouped):
temp_pref_order[i] = np.argsort(temp_pref[i])[::-1]
while is_temp_grouped.count(False) != 0:
for i, mem in enumerate(self.ungrouped):
if is_temp_grouped[i]:
continue
if np.count_nonzero(proposed[i] == False) == 0:
is_temp_grouped[i] = True
continue
for j in temp_pref_order[i]:
if proposed[i][j]:
continue
grp = self.unfilled[j]
proposed[i][j] = True
pref = self.get_group_pref_for_mem(mem, grp.members)
if pref > grp.tempScore:
if grp.tempMember >= 0:
is_temp_grouped[
self.ungrouped.index(grp.tempMember)
] = False
grp.add_temp(mem)
is_temp_grouped[i] = True
break
for grp in self.unfilled:
if grp.tempMember < 0:
continue
self.ungrouped.remove(grp.tempMember)
grp.add_permanently()
self.optimize(use_filled=False)
for grp in self.unfilled:
if grp.size() >= self.group_size or len(self.ungrouped) == 0:
self.filled.append(grp)
for grp in self.filled:
self.unfilled.remove(grp)
class Group:
def __init__(self, game: Matching, members: List[int] = []):
super().__init__()
self.game = game
self.members = members
self.tempMember = -1
self.tempScore = -1
def add_member(self, x: int):
self.members.append(x)
def remove_member(self, x: int):
self.members.remove(x)
def add_temp(self, x: int) -> int:
self.tempMember = x
self.tempScore = self.game.get_group_pref_for_mem(x, self.members)
return self.tempScore
def add_permanently(self):
if self.tempMember == -1:
return
self.add_member(self.tempMember)
self.tempMember = -1
self.tempScore = -1
def size(self) -> int:
return len(self.members)