-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathScheduleGenerator.ts
330 lines (293 loc) · 11.2 KB
/
ScheduleGenerator.ts
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* @module src/algorithm
* @author Hanzhi Zhou, Zichao Hu, Kaiying Shan
*/
/**
*
*/
import { CourseStatus } from '../config';
import Course from '../models/Course';
import Event from '../models/Event';
import GeneratedSchedule from '../models/GeneratedSchedule';
import ProposedSchedule from '../models/ProposedSchedule';
import { NotiMsg } from '../store/notification';
import { calcOverlap, parseDate } from '../utils';
import ScheduleEvaluator, { EvaluatorOptions } from './ScheduleEvaluator';
/**
* TODO: add description
*/
export type TimeArray = [number[], number[], number[], number[], number[], number[], number[]];
/**
* Start and end date in millisecond (obtained via `Date.getTime`)
*/
export type MeetingDate = [number, number];
/**
* The data structure used in the algorithm to represent a Course that
* possibly has multiple sections combined (occurring at the same time)
*
* 0: key of this course
* 1: an array of section indices
*
* Example:
* ```js
* ["span20205", [0, 1, 2]]
* ```
*/
export type RawAlgoCourse = [string, number[]];
/**
* return true if two [[TimeArray]] objects have overlapping time blocks, false otherwise
* @param timeArray1
* @param timeArray2
* @param step1 the increment step for array 1
* @param step2 the increment step for array 2
* @note use step=2 for time only array, use step=3 for time-room combined array
*/
export function checkTimeConflict(
timeArray1: TimeArray,
timeArray2: TimeArray,
step1 = 2,
step2 = 2
) {
for (let i = 0; i < 7; i++) {
// skip the entire inner loop if needed
const day1 = timeArray1[i],
day2 = timeArray2[i];
if (!day2.length) continue;
for (let j = 0; j < day1.length; j += step1) {
const begin1 = day1[j];
const end1 = day1[j + 1];
for (let k = 0; k < day2.length; k += step2)
if (calcOverlap(begin1, end1, day2[k], day2[k + 1]) > 0) return true;
}
}
return false;
}
/**
* returns an array with all time arrays in `timeArrayList` concatenated together. The offsets
* of time array of section `i` at day k is at `i * 8 + k` position of the resulting array.
*/
function timeArrayToCompact(Module: EMModule, timeArrays: TimeArray[]) {
const numSections = timeArrays.length;
const prefixLen = numSections * 8;
let len = prefixLen;
for (const sec of timeArrays) {
for (const day of sec) {
len += day.length;
}
}
const ptr = Module._malloc(len * 2);
const arr = Module.HEAPU16.subarray(ptr / 2);
len = 0;
for (let i = 0; i < numSections; i++) {
for (let k = 0; k < 7; k++) {
arr[i * 8 + k] = len;
const day = timeArrays[i][k];
arr.set(day, len + prefixLen);
len += day.length;
}
arr[i * 8 + 7] = len;
}
return ptr;
}
/**
* pre-compute `conflictCache` using `timeArrayList` and `dateList`
*/
function computeConflict(
timeArrayList: TimeArray[],
dateList: MeetingDate[],
conflictCache: Uint8Array
) {
const numSections = timeArrayList.length;
// pre-compute the conflict between each pair of sections
for (let i = 0; i < numSections; i++) {
for (let j = i + 1; j < numSections; j++) {
// conflict is symmetric
conflictCache[i * numSections + j] = conflictCache[j * numSections + i] = +(
checkTimeConflict(timeArrayList[i], timeArrayList[j], 3, 3) &&
calcOverlap(dateList[i][0], dateList[i][1], dateList[j][0], dateList[j][1]) !== -1
);
}
}
}
export interface GeneratorOptions {
timeSlots: Event[];
status: CourseStatus[];
sortOptions: EvaluatorOptions;
combineSections: boolean;
maxNumSchedules: number;
}
/**
* The schedule generator generates all possible schedules satisfying the given constraints (filters)
* out of the courses/sections that the user has selected
*/
class ScheduleGenerator {
constructor(
public readonly catalog: Window['catalog'],
public readonly timeMatrix: Window['timeMatrix'],
public readonly options: GeneratorOptions
) {}
/**
* The entrance of the schedule generator
* @returns a [[ScheduleEvaluator]] Object
*/
public getSchedules(
schedule: ProposedSchedule,
refSchedule: GeneratedSchedule['All'] = {}
): NotiMsg<ScheduleEvaluator> {
console.time('algorithm bootstrapping');
// convert events to TimeArrays so that we can easily check for time conflict
const timeSlots: TimeArray[] = schedule.events.map(e => e.toTimeArray());
for (const event of this.options.timeSlots) timeSlots.push(event.toTimeArray());
const classList: RawAlgoCourse[] = [];
const timeArrayList: TimeArray[] = [];
const dateList: MeetingDate[] = [];
const secLens = [0];
const courses = schedule.All;
const msgs: NotiMsg<ScheduleEvaluator>[] = [];
// for each course selected, form an array of sections
for (const key in courses) {
const temp = courses[key];
const allSections = temp === -1 ? ([-1] as const) : temp;
let noSelected = true;
for (let i = 0; i < allSections.length; i++) {
const subgroup = allSections[i];
// ignore empty group
if (subgroup instanceof Set && subgroup.size === 0) continue;
const courseRec = this.catalog.getCourse(key, subgroup);
const [classes, timeArrays, dates, allInvalid] = this.filterSections(
courseRec,
timeSlots
);
noSelected = false;
// give an warning if none of the sections pass the filter
if (classes.length === 0) {
msgs.push({
level: 'warn',
msg: `Not scheduled: ${courseRec.displayName}${
i === 0 || subgroup === -1 ? '' : ' belonging to group ' + i // don't show group idx for default group or Any Section
}. Reason: No sections satisfy your filters and do not conflict with your events`
});
} else {
secLens.push(classes.length);
classList.push(...classes);
timeArrayList.push(...timeArrays);
dateList.push(...dates);
}
if (allInvalid) {
msgs.push({
level: 'warn',
msg: `Warning: No sections of ${courseRec.displayName}${
i === 0 || subgroup === -1 ? '' : ' belonging to group ' + i // don't show group idx for default group or Any Section
} have valid meeting times (e.g. All TBA/TBD/Online Asynchronous). It will not be shown on the schedule grid.`
});
}
}
if (noSelected) {
return {
level: 'error',
msg: `No sections of ${
this.catalog.getCourse(key, -1).displayName
} are selected!`
};
}
}
if (classList.length === 0) {
return {
level: 'error',
msg: 'Given your filter, we cannot generate schedules without overlapping classes'
};
}
// change to prefix array
for (let i = 1; i < secLens.length; i++) {
secLens[i] += secLens[i - 1];
}
const Module = window.NativeModule;
// pointer to the cache for the number of sections in each course
const secLenPtr = Module._malloc(secLens.length * 4);
Module.HEAP32.set(secLens, secLenPtr / 4);
// the side length of the conflict cache matrix
const _size = classList.length ** 2;
const conflictCachePtr = Module._malloc(_size);
// prepare the conflictCache
computeConflict(
timeArrayList,
dateList,
Module.HEAPU8.subarray(conflictCachePtr, conflictCachePtr + _size)
);
console.timeEnd('algorithm bootstrapping');
console.time('running algorithm:');
const size = Module._generate(
secLens.length - 1,
this.options.maxNumSchedules,
secLenPtr,
conflictCachePtr,
timeArrayToCompact(Module, timeArrayList)
);
console.timeEnd('running algorithm:');
if (size < 0)
return {
level: 'error',
msg: 'Out of memory! Please try to reduce the max number of schedules'
};
if (size === 0)
return {
level: 'error',
msg: 'Given your filter, we cannot generate schedules without overlapping classes'
};
const evaluator = new ScheduleEvaluator(
this.options.sortOptions,
schedule.events,
classList,
secLens,
refSchedule,
window.NativeModule
);
evaluator.sort();
let msgString = '';
for (const msg of msgs) msgString += msg.msg + '<br>';
return {
level: msgs.length > 0 ? 'warn' : 'success',
msg: `${msgString}${size} Schedules Generated!`,
payload: evaluator
};
}
private filterSections(courseRec: Course, timeSlots: TimeArray[]) {
const classes: RawAlgoCourse[] = [],
timeArrays: TimeArray[] = [],
dates: MeetingDate[] = [];
// combine all sections of this course occurring at the same time, if enabled
const combined = this.options.combineSections
? Object.values(courseRec.getCombined())
: courseRec.sections.map(s => [s]);
let allInvalid = true;
// for each combined section, form a RawAlgoCourse
outer: for (const sections of combined) {
// only take the time and room info of the first section
// time will be the same for sections in this array
// but rooms..., well this is a compromise
const date = parseDate(sections[0].dates);
if (!date) continue;
const timeArray = sections[0].getTimeRoom();
if (timeArray.some(arr => arr.length > 0)) {
allInvalid = false;
// don't include this combined section if it conflicts with any time filter or event.
for (const td of timeSlots) {
if (checkTimeConflict(td, timeArray, 2, 3)) continue outer;
}
}
const secIndices: number[] = [];
for (const section of sections) {
// filter out sections with unwanted status
if (this.options.status.includes(section.status)) continue;
secIndices.push(section.id);
}
if (secIndices.length) {
classes.push([courseRec.key, secIndices]);
timeArrays.push(timeArray);
dates.push(date);
}
}
return [classes, timeArrays, dates, allInvalid] as const;
}
}
export default ScheduleGenerator;