-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
90 lines (76 loc) · 2.36 KB
/
utils.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
# pylint: disable=invalid-name
def is_valid_solution(D, G, s, rooms):
"""
Checks whether D is a valid mapping of G, by checking every room adheres to the stress budget.
Args:
D: Dictionary mapping student to room
G: networkx.Graph
s: Stress budget
rooms: Number of breakout rooms
Returns:
bool: whether D is a valid solution
"""
room_budget = s / rooms
room_to_student = {}
for k, v in D.items():
room_to_student.setdefault(v, []).append(k)
for k, v in room_to_student.items():
room_stress = calculate_stress_for_room(v, G)
if room_stress > room_budget:
return False
return True
def calculate_happiness(D, G):
"""
Calculates the total happiness in mapping D by summing the happiness of each room.
Args:
D: Dictionary mapping student to room
G: networkx.Graph
s: Stress budget
k: Number of breakout rooms
Returns:
float: total happiness
"""
room_to_s = {}
for k, v in D.items():
room_to_s.setdefault(v, []).append(k)
happiness_total = 0
for k, v in room_to_s.items():
room_happiness = calculate_happiness_for_room(v, G)
happiness_total += room_happiness
return happiness_total
def convert_dictionary(room_to_student):
"""
Converts the dictionary mapping room_to_student to a valid return of the solver
Args:
room_to_student: Dictionary of room to a list of students
Returns:
D: Dictionary mapping student to room
e.g {0: [1,2,3]} ==> {1:0, 2:0, 3:0}
"""
d = {}
for room, lst in room_to_student.items():
for student in lst:
d[student] = room
return d
def calculate_stress_for_room(arr, G):
"""
Given an array of students in to be placed in a room, calculate stress for the room.
Args:
arr: Array of students
G: Original graph
Returns:
room_stress: Stress value of the room
"""
H = G.subgraph(arr)
return H.size("stress")
def calculate_happiness_for_room(arr, G):
"""
Given an array of students in to be placed in a room, calculate happiness for the room.
Args:
arr: Array of students
G: Original graph
Returns:
room_happiness: Happiness value of the room
"""
H = G.subgraph(arr)
return H.size("happiness")