-
Notifications
You must be signed in to change notification settings - Fork 9
/
elevatoralg.js
684 lines (620 loc) · 26.4 KB
/
elevatoralg.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**
* Notes
* =====
*
* The `function() {...})(` hack
* -----------------------------
* Elevator Saga calls `eval` on the code supplied to it, first placing
* () around the given code. In order to be able to reference functions
* defined inside `init` from `update`, we need `that` to be defined at
* the outer level. Credit to Tom McLaughlin for the idea for this
* workaround :)
*
* Implementation invariants
* -------------------------
*
* - cycling of queues happens only when the elevator stops or if it
* had all empty queues and receives a request
* - only one direction light is on at a time
*
*/
function() {
var that = this;
var obj = {
init: function(elevators, floors) {
/**
* PASS constants
*/
that.CURR_PASS = 0;
that.NEXT_PASS = 1;
that.NEXT_NEXT_PASS = 2;
/**
* Request types
*/
that.PICK_UP = "PICK_UP";
that.DROP_OFF = "DROP_OFF";
/**
* DIRECTION constants
*/
that.UP = "UP";
that.DOWN = "DOWN";
/**
* FLOOR BUTTON constants
*/
that.NO_REQUEST = ""
/**
* An class that represents a request for an elevator.
*
* @param floorNum :: Integer
* @param requestType :: Request type constant
* @param direction :: direction constant
*/
that.RequestFactory = function(floorNum, requestType, direction) {
var that = {};
that.floorNum = floorNum;
that.requestType = requestType;
that.direction = direction;
that.equals = function(other) {
return (that.floorNum === other.floorNum)
&& (that.requestType === other.requestType)
&& (that.direction === other.direction);
}
return that;
}
/**
* An class that represents how serviceable
* an elevator request (up / down pressed) is.
*
* @param pass :: PASS constant - the index of the earliest
* pass the elevator in question could
* handle the request. 0-indexed, so 0
* means the elevator can get the
* request on its current path.
*/
that.ElevatorRequestServiceabilityFactory = function(pass) {
var that = {};
that.pass = pass;
return that;
}
/**
* elevator member functions
*/
_.each(elevators, function(elevator) {
elevator.printQueues = function() {
console.log('E' + elevator.uid + ' queues:');
console.log(
elevator.getQueueFloors(elevator.currQueue)
);
console.log(
elevator.getQueueFloors(elevator.nextQueue)
);
console.log(
elevator.getQueueFloors(elevator.nextNextQueue)
);
}
/**
* @return :: ElevatorRequestServiceability
*/
elevator.getRequestServiceability = function(request) {
if (elevator.floorIsOnCurrPass(request)) {
return that.ElevatorRequestServiceabilityFactory(
that.CURR_PASS
);
}
if (elevator.floorIsOnNextPass(request)) {
return that.ElevatorRequestServiceabilityFactory(
that.NEXT_PASS
);
}
if (elevator.floorIsOnNextNextPass(request)) {
return that.ElevatorRequestServiceabilityFactory(
that.NEXT_NEXT_PASS
);
}
}
/**
* @return :: Bool
*/
elevator.floorIsOnCurrPass = function(request) {
// TODO: decomp
if (request.direction === that.UP) {
if (elevator.goingDownIndicator()) {
return false;
}
if (elevator.currentFloor() === request.floorNum) {
// currentFloor() is discretized, so this'll
// prevent the edge case of elevators turning
// back later
return elevator.getPressedFloors().length === 0;
} else { // going up
return elevator.currentFloor() < request.floorNum;
}
} else {
if (elevator.goingUpIndicator()) {
return false;
}
if (elevator.currentFloor() === request.floorNum) {
// currentFloor() is discretized, so this'll
// prevent the edge case of elevators turning
// back later
return elevator.getPressedFloors().length === 0;
} else { // going down
return elevator.currentFloor() > request.floorNum;
}
}
}
/**
* @return :: Bool
*/
elevator.floorIsOnNextPass = function(request) {
return (request.direction === that.UP)
? elevator.goingDownIndicator()
: elevator.goingUpIndicator();
}
/**
* @return :: Bool
*/
elevator.floorIsOnNextNextPass = function(request) {
return request.direction === that.UP
? elevator.goingUpIndicator()
: elevator.goingDownIndicator();
}
/**
* @param floorNum :: Integer
* @return :: Bool
*/
elevator.isIdleAtFloor = function(floorNum) {
return (elevator.getPressedFloors().length === 0)
&& (elevator.currentFloor() === floorNum);
}
/**
* Gets the floors stored in a queue
*/
elevator.getQueueFloors = function(queue) {
return _.map(queue, function(request) {
return request.floorNum;
});
}
/**
* @param request :: Request
*
* inserts the floor as as a destination for the elevator
* on the current pass
*/
elevator.insertDestinationOnCurrPass = function(request) {
if (elevator.isIdleAtFloor(request.floorNum)) {
return; // Already there
}
elevator.currQueue.push(request);
elevator.sortQueues();
elevator.destinationQueue = elevator.getQueueFloors(
elevator.currQueue
);
elevator.checkDestinationQueue();
};
/**
* @param request :: Request
*
* inserts the floor as as a destination for the elevator
* on the next pass
*/
elevator.insertDestinationOnNextPass = function(request) {
elevator.nextQueue.push(request);
elevator.sortQueues();
};
/**
* @param request :: Request
*
* inserts the floor as as a destination for the elevator
* on the next next pass
*/
elevator.insertDestinationOnNextNextPass = function(request)
{
elevator.nextNextQueue.push(request);
elevator.sortQueues();
};
/**
* @return :: Bool
*/
elevator.queuesAreAllEmpty = function() {
return (elevator.currQueue.length === 0)
&& (elevator.nextQueue.length === 0)
&& (elevator.nextNextQueue.length === 0);
}
/**
* @param request :: Request
*
* inserts the floor as as a destination for the elevator
* on as early a pass as possible
*/
elevator.insertDestination = function(request) {
serviceability = elevator.getRequestServiceability(
request
);
var queuesWereAllEmpty = elevator.queuesAreAllEmpty();
switch (serviceability.pass) {
case that.CURR_PASS:
elevator.insertDestinationOnCurrPass(request);
break;
case that.NEXT_PASS:
elevator.insertDestinationOnNextPass(request);
break;
case that.NEXT_NEXT_PASS:
elevator.insertDestinationOnNextNextPass(
request
);
break;
}
if (queuesWereAllEmpty) {
// insertion might not've happened in `currQueue`,
// which would mean we wouldn't go anywhere.
// Hence, we might need to cycle.
elevator.cycleQueuesIfPossible();
}
}
/**
* sorts all queues: curr, next, and next-next.
*/
elevator.sortQueues = function() {
this.increasingCmp = function(n1, n2)
{ return n1.floorNum - n2.floorNum; }
this.decreasingCmp = function(n1, n2)
{ return n2.floorNum - n1.floorNum; }
if (elevator.goingUpIndicator()) {
elevator.currQueue.sort(this.increasingCmp);
elevator.nextQueue.sort(this.decreasingCmp);
elevator.nextNextQueue.sort(this.increasingCmp);
} else {
elevator.currQueue.sort(this.decreasingCmp);
elevator.nextQueue.sort(this.increasingCmp);
elevator.nextNextQueue.sort(this.decreasingCmp);
}
}
/**
* Flips the elevator's direction indicator
*/
elevator.flipDirection = function() {
if (elevator.goingUpIndicator()) {
console.log('E' + elevator.uid + ' was (↑), now (↓)');
} else {
console.log('E' + elevator.uid + ' was (↓), now (↑)');
}
elevator.goingUpIndicator(
!elevator.goingUpIndicator()
);
elevator.goingDownIndicator(
!elevator.goingDownIndicator()
);
}
/**
* @return :: DIRECTION constant
*/
elevator.getCurrDirection = function() {
return elevator.goingUpIndicator()
? that.UP
: that.DOWN;
}
/**
* let's say this elevator is going (WLOG) up and about
* to drop their only person off at F2. If nextQueue's
* first request to satisfy is a down request at
* Fk for k > 2, then we want to actually insert k
* in the current queue so that we go there while
* facing that direction. Note that this condition only
* makes sense if nextQueue is not the empty array and
* currQueue is the empty array.
*
* @return :: Bool - whether the insertion was performed.
*/
elevator._insertFirstOfNextIfAppropriate = function() {
if ((elevator.currQueue.length === 0) &&
(elevator.nextQueue.length !== 0)
) {
// if we're (WLOG) going up, let's get the
// highest "down" request
var potentialNextDestination
= (elevator.goingUpIndicator())
? _.max(elevator.nextQueue, function(request) { return request.floorNum; })
: _.min(elevator.nextQueue, function(request)
{ return request.floorNum; });
var request = that.RequestFactory(
potentialNextDestination.floorNum,
potentialNextDestination.requestType,
elevator.getCurrDirection() // along curr pass
);
var serviceability = elevator.getRequestServiceability(
request
);
// don't update currQueue if we're currently
// stopped at this floor because it's redundant
// and that redundancy can break some things
// (e.g. stopping at a floor and being about to
// flip direction while picking somebody up there)
if ((serviceability.pass === that.CURR_PASS) &&
!elevator.isIdleAtFloor(request.floorNum)
) {
elevator.currQueue.push(request);
return true;
}
}
return false;
}
/**
* Cycles the three queues and updates the sorting.
*/
elevator.cycleQueues = function() {
elevator.flipDirection();
elevator.currQueue = elevator.nextQueue;
elevator.nextQueue = elevator.nextNextQueue;
elevator.nextNextQueue = [];
// directions have flipped, so we should resort
// in the correct directions.
elevator.sortQueues();
}
/**
* TODO: this doc comment exposes implementation details
*
* If the current pass's queue is empty,
* cycle the queues so that we can start moving.
*/
elevator.cycleQueuesIfPossible = function() {
if (elevator.queuesAreAllEmpty() ||
(elevator.currQueue.length !== 0)
) {
// no point in cycling
return;
}
// see doc comment for this function; it'll become
// clear why it's necessary to call it here
var currQueueUpdated = elevator._insertFirstOfNextIfAppropriate();
if (currQueueUpdated) {
elevator.destinationQueue = elevator.getQueueFloors(
elevator.currQueue
);
elevator.checkDestinationQueue();
// definitely don't do any cycling; currQueue
// will no longer be empty by definition of the
// function so we'd just overwrite that op if we
// continued to do stuff.
return;
}
elevator.cycleQueues();
if (elevator.currQueue.length === 0) {
// happens if the only nonempty queue at the start
// was nextNextQueue. If we don't do something
// here, the elevator'll get stuck because
// currQueue'll be empty. Note that we can say
// that after this op, currQueue will *not* be
// empty, since at the start of the function we
// guarantee that not all queues are empty.
elevator._insertFirstOfNextIfAppropriate();
}
elevator.destinationQueue = elevator.getQueueFloors(
elevator.currQueue
);
elevator.checkDestinationQueue();
}
/**
* @param request :: Request
* @return :: Bool
*/
elevator.requestIsQueued = function(request) {
var eq = function(r) { return r.equals(request); };
return _.any(elevator.currQueue, eq)
|| _.any(elevator.nextQueue, eq)
|| _.any(elevator.nextNextQueue, eq);
}
/**
* The current load factor of this elevator. Note that
* this use of the word "load" is different from the
* notion of the sum of the weights of everyone currently
* in the elevator. Instead, load factor is a notion
* closer to how busy the elevator is.
*/
elevator.getCurrLoad = function() {
var e = elevator;
return (5 * e.loadFactor()) +
(1 * _.uniq(e.getQueueFloors(e.currQueue))
.length) +
(2 * _.uniq(e.getQueueFloors(e.nextQueue))
.length) +
(3 * _.uniq(e.getQueueFloors(e.nextNextQueue))
.length);
}
/**
* The current load factor of this elevator. Note that
* this use of the word "load" is different from the
* notion of the sum of the weights of everyone currently
* in the elevator. Instead, load factor is a notion
* closer to how busy the elevator is.
*/
elevator.getLoadIncrease = function(request) {
serviceability = elevator.getRequestServiceability(
request
);
switch (serviceability.pass) {
case that.CURR_PASS: return 1;
case that.NEXT_PASS: return 2;
case that.NEXT_NEXT_PASS: return 3;
}
}
});
/* ---- elevator initialization ---- */
/**
* All elevators start at floor 0, so
* they start by indicating "up".
*/
_.each(elevators, function(elevator) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
});
/**
* for debugging logging purposes
*/
for (var i=0; i<elevators.length; i++) {
elevators[i].uid = i;
}
/**
* need to be able to queue requests for
* after an elevator changes directions
*/
_.each(elevators, function(elevator) {
elevator.currQueue = [];
elevator.nextQueue = [];
elevator.nextNextQueue = [];
});
/* ---- start of helper functions ---- */
/**
* @param request :: Request
* @return :: Elevator
*/
that.getBestElevatorForRequest = function(request) {
return _.min(elevators, function(elevator) {
currLoad = elevator.getCurrLoad();
loadIncrease = elevator.getLoadIncrease(request);
a = 1.5; // optimal choice
delta = Math.pow(a, currLoad + loadIncrease) - Math.pow(a, currLoad);
return delta;
})
}
/* ---- end of helper functions ---- */
/**
* elevator behavior
*/
_.each(elevators, function(elevator) {
elevator.on("floor_button_pressed", function(floorNum) {
// The person will only have gotten on if the
// indicator was pointing in the direction in which
// they are traveling. Hence, we can guarantee that
// their destination will be on the current pass.
console.log(
'Pressed: F' + floorNum +
' inside E' + elevator.uid
);
elevator.printQueues();
elevator.insertDestinationOnCurrPass(
that.RequestFactory(
floorNum,
that.DROP_OFF,
null
)
);
console.log('---------');
elevator.printQueues();
});
elevator.on("stopped_at_floor", function(floorNum) {
console.log('stopped at F' + floorNum);
elevator.printQueues();
elevator.currQueue = _.filter(
elevator.currQueue,
function(request) {
return request.floorNum !== floorNum;
}
);
elevator.cycleQueuesIfPossible();
elevator.currQueue = _.filter(
elevator.currQueue,
function(request) {
return request.floorNum !== floorNum;
}
);
console.log('--------');
elevator.printQueues();
});
});
/**
* Assign the request to an elevator
*/
that.queueRequest = function(request) {
console.log('Request: F' + request.floorNum + '(↑)');
var bestElevator = that.getBestElevatorForRequest(
request
);
console.log(
'↑ (F' + request.floorNum + '): assigned to E'
+ bestElevator.uid + ' (@ F'
+ bestElevator.currentFloor() + ')'
);
bestElevator.printQueues();
bestElevator.insertDestination(request);
console.log('---------');
bestElevator.printQueues();
}
/**
* floor behavior
*/
_.each(floors, function(floor) {
floor.on("up_button_pressed", function() {
request = that.RequestFactory(
floor.floorNum(),
that.PICK_UP,
that.UP
);
that.queueRequest(request);
});
floor.on("down_button_pressed", function() {
request = that.RequestFactory(
floor.floorNum(),
that.PICK_UP,
that.DOWN
);
that.queueRequest(request);
});
/**
* @return :: Bool
*/
floor.hasOpenRequests = function() {
return (floor.buttonStates.up !== that.NO_REQUEST)
|| (floor.buttonStates.down !== that.NO_REQUEST);
}
/**
* @return :: [Request]
*/
floor.getOpenRequests = function() {
var openRequests = [];
if (floor.buttonStates.up !== that.NO_REQUEST) {
openRequests.push(
that.RequestFactory(
floor.floorNum(),
that.PICK_UP,
that.UP
)
);
}
if (floor.buttonStates.down !== that.NO_REQUEST) {
openRequests.push(
that.RequestFactory(
floor.floorNum(),
that.PICK_UP,
that.DOWN
)
);
}
return openRequests;
}
});
},
/**
* Called regularly.
*/
update: function(dt, elevators, floors) {
_.each(floors, function(floor) {
if (floor.hasOpenRequests()) {
_.each(floor.getOpenRequests(), function(request) {
var requestIsQueued = _.any(elevators, function(e)
{ return e.requestIsQueued(request); }
);
if (!requestIsQueued) {
console.log(
'REQUEST WAS DROPPED; QUEUEING NOW: '
+ request
);
that.queueRequest(request);
}
});
}
});
}
};
return obj;
})(