This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parent.py
159 lines (149 loc) · 6.14 KB
/
Parent.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
__author__ = 'tpl'
import College
import Course
class Parent(object):
def __init__(self, name, gender, college, course):
self.name = name
self.gender = gender
self.college = college
self.course = course
def set_spouse_preference(self,gender,course,college):
self.spouse_preference = SpousePreference(gender,course,college)
self.spouse_preference.myself = self
def set_child_preference(self, course, college):
self.child_preference = ChildPreference(course, college)
self.child_preference.myself = self
pass
def generate_spouse_ranks(self,parent_list):
spouses = parent_list[:] # shallow copy
spouses.remove(self)
self.spouse_ranks = sorted(spouses, key=lambda p: self.spouse_preference.evaluate(p), reverse=True)
return self.spouse_ranks
class SpousePreference(object):
def __init__(self, gender='Random', course='Close', college = 'Close'):
self.gender_preference = gender
self.course_preference = course
self.college_preference = college
self.myself = None # to be assigned after creation
def evaluate(self, parent):
points = 0
points = points + self._evaluate_gender(parent)
# print( "Gender points" , self._evaluate_gender(parent),"to", parent.name)
points = points + self._evaluate_course(parent)
# print( "Course points" , self._evaluate_course(parent),"to", parent.name)
points = points + self._evaluate_college(parent)
# print( "College points" , self._evaluate_college(parent),"to", parent.name)
return points
def _evaluate_gender(self,parent):
"""
If Random: 10 points (average)
if Opposite: 20 points | 0 points
if Same: 20 points | 0 points
:param parent:
:return:
"""
if self.gender_preference == 'Random':
return 10
if self.gender_preference == 'Opposite':
if self.myself.gender != parent.gender:
return 20
else:
return 0
if self.gender_preference == 'Same':
if self.myself.gender == parent.gender:
return 20
else:
return 0
def _evaluate_college(self,parent):
if self.college_preference == 'Different':
return 10
if self.college_preference == 'Same':
if self.myself.college == parent.college:
return 30
else:
return 0
if self.college_preference == 'Close':
"""
same college : 30 points
colleges at threshold distance: 0 points
colleges further than threshold distance : neg points
"""
distance = College.distance2D(College.COORDINATES [self.myself.college],College.COORDINATES [parent.college])
threshold = College.GIRTONTOHOMERTON / 4.0
return (threshold - distance)/threshold * 30
def _evaluate_course(self,parent):
if self.course_preference == 'Different':
return Course.AVERAGE/Course.MAX * 30
if self.course_preference == 'Same':
if self.myself.course == parent.course:
return 30
else:
return 0
if self.course_preference == 'Close':
"""
Maximally Different Subject = 0
Same subject = 30
"""
distance = Course.distance3D(Course.C_COORDINATES[self.myself.course],Course.C_COORDINATES[parent.course])
return abs(distance - Course.MAX)/Course.MAX * 30
class ChildPreference(object):
def __init__(self, course='Close', college = 'Close'):
self.course_preference = course
self.college_preference = college
self.myself = None # to be assigned after creation
def evaluate(self, parent):
points = 0
points = points + self._evaluate_course(parent)
# print( "Course points" , self._evaluate_course(parent),"to", parent.name)
points = points + self._evaluate_college(parent)
# print( "College points" , self._evaluate_college(parent),"to", parent.name)
return points
def _evaluate_college(self,child):
if self.college_preference == 'Different':
return 10
if self.college_preference == 'Same':
if self.myself.college == child.college:
return 30
else:
return 0
if self.college_preference == 'Close':
"""
same college : 30 points
colleges at threshold distance: 0 points
colleges further than threshold distance : neg points
"""
distance = College.distance2D(College.COORDINATES [self.myself.college],College.COORDINATES [child.college])
threshold = College.GIRTONTOHOMERTON / 4.0
return (threshold - distance)/threshold * 30
def _evaluate_course(self,child):
if self.course_preference == 'Different':
return Course.AVERAGE/Course.MAX * 30
if self.course_preference == 'Same':
if self.myself.course == child.course:
return 30
else:
return 0
if self.course_preference == 'Close':
"""
Maximally Different Subject = 0
Same subject = 30
"""
A = Course.C_COORDINATES[self.myself.course]
Z = Course.C_COORDINATES
B = Course.C_COORDINATES[child.course]
distance = Course.distance3D(A,B)
return abs(distance - Course.MAX)/Course.MAX * 30
class Couple(object):
def __init__(self,par1, par2):
self.par1 = par1
self.par2 = par2
self.name = par1.name + par2.name
def _evaluate(self,child):
points = 0
points = points + self.par1.child_preference.evaluate(child)
points = points + self.par2.child_preference.evaluate(child)
return points
def generate_child_ranks(self,child_list):
children = child_list[:] # shallow copy
self.child_ranks = sorted(children, key=lambda c: self._evaluate(c), reverse=True)
return self.child_ranks