-
Notifications
You must be signed in to change notification settings - Fork 2
/
timetabler.rb
287 lines (241 loc) · 6.62 KB
/
timetabler.rb
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require './course'
module Timetabler
MAX_TIMETABLES = 1500
DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
# A timetable is an array of Activities
class Timetable < Array
def earliest_start_time
return @earliest_start_time if @earliest_start_time
earliest = 24
self.each do |a|
a.times.each do |t|
earliest = [earliest, t[1]].min
end
end
return (@earliest_start_time = earliest)
end
def latest_end_time
return @latest_end_time if @latest_end_time
latest = 6
self.each do |a|
a.times.each do |t|
latest = [latest, t[2]].max
end
end
return (@latest_end_time = latest)
end
def hours_at_uni
return @hours_at_uni if @hours_at_uni
hours = 0
start_times = [24]*5
end_times = [0]*5
# find the start/end times for each day
self.each do |a|
a.times.each do |t|
if t[1] < start_times[t[0]]
start_times[t[0]] = t[1]
end
if t[2] > end_times[t[0]]
end_times[t[0]] = t[2]
end
end
end
(0..4).each do |i|
if start_times[i] != 24
hours += end_times[i]-start_times[i]
end
end
return (@hours_at_uni = hours)
end
def days_at_uni
return @days_at_uni if @days_at_uni
at_uni = 0
seen = {}
self.each do |a|
a.times.each do |t|
if not seen.has_key?(t[0])
seen[t[0]] = 1
at_uni += 1
end
end
end
return (@days_at_uni = at_uni)
end
def end_time_earliness
return @end_time_earliness if @end_time_earliness
time = 0
latest = [0]*5
self.each do |a|
a.times.each do |t|
latest[t[0]] = [latest[t[0]], t[2]].max
end
end
latest.each do |t|
time += (24-t) if t > 0
end
return (@end_time_earliness = time)
end
def sleep_in_time
return @sleep_in_time if @sleep_in_time
sleep_in = 0
earliest = [24]*5
self.each do |a|
a.times.each do |t|
earliest[t[0]] = [earliest[t[0]], t[1]].min
end
end
earliest.each do |t|
sleep_in += t if t < 24
end
return (@sleep_in_time = sleep_in)
end
def hours_sd
return @hours_sd if @hours_sd
hours = [0]*5
# find the start/end times for each day
self.each do |a|
a.times.each do |t|
hours[t[0]] += t[2]-t[1]
end
end
mean = hours.reduce(:+) / hours.size
hours.each_with_index do |h, i|
hours[i] = (h - mean)**2
end
return (@hours_sd = Math.sqrt(hours.reduce(:+) / hours.size))
end
def has_course(name, day, start, finish)
has_class = [false]*24
if name != 'NOTHING'
self.each do |a|
a.times.each do |t|
if t[0] == day
has_class[t[1]..t[2]-1] = [true]*(t[2]-t[1])
end
end if a.course == name
end
return has_class[start..finish-1].uniq == [true]
else
self.each do |a|
a.times.each do |t|
if t[0] == day
has_class[t[1]..t[2]-1] = [true]*(t[2]-t[1])
end
end
end
return has_class[start..finish-1].uniq == [false]
end
end
end
def Timetabler.generate(courses=[], options={})
timetables = []
required = []
# required is an array of activity sections
courses.each do |c|
required += c.activities.values
end
# shuffle the required activities
# so we get different timetables each time
required.shuffle!
required.each do |a|
a.shuffle!
end
options[:clash] ||= 0
options[:clash] = 3 if options[:clash] > 3
@@num_generated = 0
generateAux(required, 0, Timetable.new, options[:clash]) do |t|
timetables << t.clone
end
# Look for timetables with a given course in a given timeslot
options[:force_courses].each do |force_course|
if force_course[0] && force_course[1]
if force_course[1] =~ /^(\w+)\s+(\d+)\s*-\s*(\d+)\s*$/
course = force_course[0].upcase
day = DAYS.index($1.capitalize)
start = $2.to_i
finish = $3.to_i
if start < finish && start >= 0 && finish < 24 &&
(0..4).include?(day)
timetables.select!{|t| t.has_course(course, day, start, finish)}
end
end
end
end
if options[:sort_by]
options[:sort_by].split(', ').reverse.each do |s|
case s
# force stable sorting
when 'days' then i = 0; timetables.sort_by!{|x| [x.days_at_uni, i+=1]}
when 'hours' then i = 0; timetables.sort_by!{|x| [x.hours_at_uni, i+=1]}
when 'end_time' then i = 0; timetables.sort_by!{|x| [-x.end_time_earliness, i+=1]}
when 'sleep_in_time' then i = 0; timetables.sort_by!{|x| [-x.sleep_in_time, i+=1]}
when 'hours_sd' then i = 0; timetables.sort_by!{|x| [x.hours_sd, i+=1]}
end
end
end
return timetables
end
private
def Timetabler.generateAux(activities, index, timetable, clash=0, &block)
if @@num_generated >= MAX_TIMETABLES
return
end
if index >= activities.size
@@num_generated += 1
block.call(timetable)
return
end
activities[index].each do |a|
f, c = fits(timetable, a, clash)
if f
timetable << a
generateAux(activities, index+1, timetable, clash-c, &block)
timetable.pop
end
end
end
# returns [true|false, number of clash hours used]
def Timetabler.fits(timetable, activity, clash=0)
current_clash = 0
timetable.each do |a|
a.times.each do |t1|
activity.times.each do |t2|
c = clash(t1, t2)
if current_clash+c <= clash
current_clash += c
else
return false, 0
end
# allow clashes within the same course only if
# they are different activities
# (usually this is because different classes
# run in different weeks)
end if activity.course != a.course
end
end
return true, current_clash
end
# returns the number of clash hours
def Timetabler.clash(t1, t2)
if t1[0] == t2[0]
# if the timeslot that starts later
# starts before the other one ends,
# there is a clash
# TODO: clean this shit up
if t1[1] >= t2[1]
if t1[1] < t2[2]
c = t2[2] - t1[1]
c -= (t2[2]-t1[2]) if t1[2] < t2[2]
return c
end
else
if t2[1] < t1[2]
c = t1[2] - t2[1]
c -= (t1[2]-t2[2]) if t2[2] < t1[2]
return c
end
end
end
return 0
end
end