-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaffer.js
More file actions
82 lines (75 loc) · 2.77 KB
/
Staffer.js
File metadata and controls
82 lines (75 loc) · 2.77 KB
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
function Staffer(availability, timeConflicts, needs) {
this.aMatrix = new Staffing.Matrix(availability.length, availability[0].length, availability, 0);
this.cMatrix = new Staffing.Matrix(timeConflicts.length, timeConflicts.length, timeConflicts, 0);
this.sMatrix = new Staffing.Matrix(availability.length, availability[0].length);
this.needs = needs;
this.timeCount = timeConflicts.length;
this.persCount = availability.length;
}
Staffer.prototype.staffSlot = function(it) {
var slot = it.nextTimeslot();
var pers = it.nextPerson();
for(var j = 0; pers != -1 && j < this.needs[slot]; j++) {
if(this.isStaffable(pers, slot)) {
this.sMatrix.setValue(pers, slot, true);
it.confirmUse(pers);
}
else {
j--;
}
pers = it.nextPerson();
}
}
Staffer.prototype.staff = function() {
var it = new AvailabilityIterator(this.aMatrix);
for(var i = 0; i < this.timeCount; i++) {
this.staffSlot(it);
}
}
Staffer.prototype.swapForMaxed = function(maxRatio, minStaffCount) {
var that = this;
var checked = UTIL.fillArray(null, this.persCount, function(index) {
return false;
});
var ratios = UTIL.fillArray(null, this.persCount, function(index) {
return that.sMatrix.countXVals(index) / that.aMatrix.countXVals(index);
});
var highestIndex = UTIL.greatestIndex(ratios, checked)
while(highestIndex != -1 && ratios[highestIndex] > maxRatio)
{
var ratio = ratios[highestIndex];
checked[highestIndex] = true;
var slots = this.sMatrix.getXVals(highestIndex);
var numStaffed = slots.length;
for(var i = 0; i < slots.length && ratio > maxRatio && numStaffed > minStaffCount; i++) {
var slot = slots[i];
var others = this.aMatrix.getYVals(slot);
for(var j = 0; j < others.length; j++) {
var other = others[j];
if(!this.sMatrix.isSet(other, slot) &&
this.isStaffable(other, slot) &&
(this.sMatrix.countXVals(other) + 1) / this.aMatrix.countXVals(other) < maxRatio)
{
this.sMatrix.setValue(other, slot, true);
this.sMatrix.removeValue(highestIndex, slot);
j = others.length;
ratio = this.sMatrix.countXVals(other) / this.aMatrix.countXVals(other);
ratios[other] = this.sMatrix.countXVals(other) / this.aMatrix.countXVals(other);
ratios[highestIndex] = ratio;
numStaffed--;
}
}
}
highestIndex = UTIL.greatestIndex(ratios, checked);
}
}
Staffer.prototype.isStaffable = function(person, timeslot) {
var staffedTimes = this.sMatrix.getXVals(person);
var ret = true;
var that = this;
UTIL.forEach(staffedTimes, function(otherTime) {
if(that.cMatrix.isSet(otherTime, timeslot) || that.cMatrix.isSet(timeslot, otherTime))
ret = false;
});
return ret;
}