-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Schedule.js
163 lines (135 loc) · 4.71 KB
/
Schedule.js
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
const oferta = require('./oferta.json')['oferta']; // oferta is auto generated by scraper
// format {
// CBF210: {
// name: "",
// schedule: {
// lunes: {
// desde: 0,
// hasta: 1
// }
// ...
// }
// }
// ...
// }
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
function selectRandomSection(code) {
if(!oferta[code]) {
throw "COULD NOT FIND OFFER FOR " + code;
}
if(oferta[code].secciones.length > 0) {
return oferta[code].secciones[getRndInteger(0, oferta[code].secciones.length)];
}
return null;
}
function checkDesiredSectionConflict(curr, desiredSect) {
if(!desiredSect || (desiredSect && desiredSect.includes(curr.sec))) {
return true;
}
return false;
}
function checkDayConflict(day, curr, pool, log) {
let myTime = curr.schedule[day];
if(myTime) {
for(let i=0; i<pool.length; i++) {
let otherTime = pool[i].schedule[day];
if(log) {
console.log('CHECK CONFLICTS');
console.log('me',myTime);
console.log('oth',otherTime);
}
if(otherTime) {
if(myTime.desde < otherTime.hasta && otherTime.desde < myTime.hasta) {
return true;
}
}
}
}
return false;
}
const days = ['lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado'];
module.exports = function Schedule(classCodes, prefill) {
this.classCodes = classCodes;
this.sections = [];
this.fitness = 0;
if(prefill) {
for(let i=0; i<classCodes.length; i++) {
this.sections[i] = selectRandomSection(classCodes[i].code);
}
}
this.getCredits = function() {
let credits = 0;
for(let i=0; i<this.classCodes.length; i++) {
let classData = oferta[this.classCodes[i].code];
if(classData.credits) {
credits+= classData.credits;
}
}
return credits;
}
this.calcFitness = function(log) {
let conflicts = 0;
for(let i=0; i<this.sections.length; i++) {
let sect = this.sections[i];
let desiredSect = this.classCodes[i].section;
let hasDesiredSect = checkDesiredSectionConflict(sect, desiredSect);
if(!hasDesiredSect) {
conflicts++;
}
for(let j=0; j<days.length;j++) {
if(log) {
console.log('check if has conflict for day ' + days[j]);
}
let hasConflict = checkDayConflict(days[j], sect, this.sections.slice(i+1), log);
if(hasConflict)
conflicts++;
}
}
// Calculate score based on MAX number of conflicts - this one's number of conficts
let score = this.sections.length*2 - conflicts;
this.fitness = score/(this.sections.length*2);
if(log) {
console.log('CONFLICTS', conflicts);
console.log('sections', this.sections.length);
console.log('score', score);
console.log('fitness', this.fitness);
}
}
this.crossover = function(partner) {
let child = new Schedule(this.classCodes);
// For crossover, for each class we will have
// a 50% chance of picking the section from either parent.
let swapProbability = 0.5;
for(let i=0; i<this.sections.length;i++) {
if(Math.random() < swapProbability) {
child.sections[i] = this.sections[i];
} else {
child.sections[i] = partner.sections[i];
}
}
return child;
}
this.mutate = function(mutationRate) {
// For each section, there's a mutationRate chance that it will get
// replaced by another random section (of the same class)
for(let i=0; i<this.sections.length; i++) {
if(Math.random() < mutationRate) {
this.sections[i] = selectRandomSection(this.classCodes[i].code);
}
}
}
this.print = function() {
for(let i=0; i<this.sections.length; i++) {
let sect = this.sections[i];
console.log(oferta[this.classCodes[i].code].name + '\r\n-- ' + sect.sec + ' ' + sect.prof);
for(let j=0;j<days.length;j++) {
if(sect.schedule[days[j]]) {
console.log('---- ' + days[j] + ' ' + sect.schedule[days[j]].desde + '-' + sect.schedule[days[j]].hasta);
}
}
console.log();
}
}
}