/
scoreTimetable.ts
174 lines (145 loc) · 4.58 KB
/
scoreTimetable.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
import { notUndefined } from '../typeHelpers';
import { ClashInfo } from './getClashInfo';
import { TimetableScorerCache } from './TimetableScorerCache';
import { DayLetter, LinkedSession, LinkedStream } from '../state';
export interface TimetableScore {
score: number,
timetable: LinkedSession[],
}
export interface TimetableScoreConfig {
clash: number,
freeDays: number,
times: number,
dayLength: number,
}
export const defaultScoreConfig: TimetableScoreConfig = {
clash: 1,
freeDays: 1,
times: 1,
dayLength: 1,
};
export class TimetableScorer {
private fixedSessions: LinkedSession[];
private clashInfo: ClashInfo;
private fewestClashes: number;
private cache: TimetableScorerCache<number>;
private customWeights: TimetableScoreConfig;
constructor(clashInfo: ClashInfo, fixedSessions: LinkedSession[]) {
this.fixedSessions = fixedSessions;
this.clashInfo = clashInfo;
this.fewestClashes = Infinity;
this.cache = new TimetableScorerCache();
this.customWeights = { ...defaultScoreConfig };
}
score(streams: LinkedStream[], cacheKey?: number[]): number {
if (cacheKey !== undefined) {
const cachedScore = this.cache.get(cacheKey);
if (cachedScore !== undefined) {
return cachedScore;
}
}
const clashes = countClashes(streams, this.clashInfo, this.fewestClashes);
// Quick exit for timetables with more clashes than a previously found timetable
if (clashes > this.fewestClashes) {
return -Infinity;
}
this.fewestClashes = clashes;
const timetable = streams.flatMap(s => s.sessions).concat(this.fixedSessions);
let score = 0;
score += scoreClashes(clashes) * this.customWeights.clash;
score += scoreFreeDays(timetable) * this.customWeights.freeDays;
score += scoreTimes(timetable) * this.customWeights.times;
score += scoreDayLength(timetable) * this.customWeights.dayLength;
score += scoreGGAndTBTDays(timetable);
if (cacheKey) {
this.cache.set(cacheKey, score);
}
return score;
}
updateConfig(newConfig: TimetableScoreConfig) {
this.customWeights = newConfig;
this.cache.clear();
}
}
export function scoreFreeDays(sessions: LinkedSession[]): number {
const scores = { M: 290, T: 250, W: 280, H: 260, F: 300 };
for (let i = 0; i < sessions.length; ++i) {
scores[sessions[i].day] = 0;
}
return scores.M + scores.T + scores.W + scores.H + scores.F;
}
export function scoreTimes(sessions: LinkedSession[]): number {
let total = 0;
for (let i = 0; i < sessions.length; ++i) {
const { start, end } = sessions[i];
const scoreStart = -((start - 14) * (start - 14)) + 9;
const scoreEnd = -((end - 14) * (end - 14)) + 9;
total += Math.min(scoreStart, scoreEnd, 0);
}
return total;
}
export function scoreDayLength(sessions: LinkedSession[]): number {
const perHour = -10;
const starts = { M: 24, T: 24, W: 24, H: 24, F: 24 };
const ends = { M: -1, T: -1, W: -1, H: -1, F: -1 };
for (let i = 0; i < sessions.length; ++i) {
const { day, start, end } = sessions[i];
if (start < starts[day]) {
starts[day] = start;
}
if (end > ends[day]) {
ends[day] = end;
}
}
let total = 0;
if (ends.M > -1) total += ends.M - starts.M;
if (ends.T > -1) total += ends.T - starts.T;
if (ends.W > -1) total += ends.W - starts.W;
if (ends.H > -1) total += ends.H - starts.H;
if (ends.F > -1) total += ends.F - starts.F;
return total * perHour;
}
export function scoreGGAndTBTDays(sessions: LinkedSession[]) {
let tbtDay: DayLetter | null = null;
let ggDay: DayLetter | null = null;
for (let i = 0; i < sessions.length; ++i) {
const s = sessions[i];
if (s.course.code === 'CBS') {
if (s.stream.component === 'The Bible Talks') {
tbtDay = s.day;
if (ggDay) {
break;
}
} else if (s.stream.component === 'Growth Groups') {
ggDay = s.day;
if (tbtDay) {
break;
}
}
}
}
return tbtDay && ggDay && tbtDay === ggDay ? 100 : 0;
}
export function countClashes(
streams: LinkedStream[],
clashInfo: ClashInfo,
maxClash: number,
): number {
let count = 0;
for (let i = 0; i < streams.length; ++i) {
const s1 = streams[i];
const innerMap = notUndefined(clashInfo.get(s1));
for (let j = i + 1; j < streams.length; ++j) {
const s2 = streams[j];
count += notUndefined(innerMap.get(s2));
if (count > maxClash) {
return maxClash + 1;
}
}
}
return count;
}
export function scoreClashes(counts: number): number {
const clashPenalty = -1000;
return counts * clashPenalty;
}