-
Notifications
You must be signed in to change notification settings - Fork 0
/
myelevator.js
151 lines (129 loc) · 4.69 KB
/
myelevator.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
/*
* Available information:
* 1. Request queue
* Simulator.get_instance().get_requests()
* Array of integers representing floors where there are people calling the elevator
* eg: [7,3,2] // There are 3 people waiting for the elevator at floor 7,3, and 2, in that order
*
* 2. Elevator object
* To get all elevators, Simulator.get_instance().get_building().get_elevator_system().get_elevators()
* Array of Elevator objects.
* - Current floor
* elevator.at_floor()
* Returns undefined if it is moving and returns the floor if it is waiting.
* - Destination floor
* elevator.get_destination_floor()
* The floor the elevator is moving toward.
* - Position
* elevator.get_position()
* Position of the elevator in y-axis. Not necessarily an integer.
* - Elevator people
* elevator.get_people()
* Array of people inside the elevator
*
* 3. Person object
* - Floor
* person.get_floor()
* - Destination
* person.get_destination_floor()
* - Get time waiting for an elevator
* person.get_wait_time_out_elevator()
* - Get time waiting in an elevator
* person.get_wait_time_in_elevator()
*
* 4. Time counter
* Simulator.get_instance().get_time_counter()
* An integer increasing by 1 on every simulation iteration
*
* 5. Building
* Simulator.get_instance().get_building()
* - Number of floors
* building.get_num_floors()
*/
Elevator.prototype.decide = function() {
var simulator = Simulator.get_instance();
var building = simulator.get_building();
var num_floors = building.get_num_floors();
var elevators = Simulator.get_instance().get_building().get_elevator_system().get_elevators();
var time_counter = simulator.get_time_counter();
var requests = simulator.get_requests();
var elevator = this;
var people = this.get_people();
var person = people.length > 0 ? people[0] : undefined;
if(elevator) {
elevator.at_floor();
elevator.get_destination_floor();
elevator.get_position();
}
if(person) {
person.get_floor();
}
var target_floors = [];
for (var i = 0;i<people.length;i++){
target_floors.push(people[i].get_destination_floor());
}
var elevator_at_floor = this.get_position()/this.get_height()+1;
for(var i = 0;i < requests.length;i++) {
var handled = false;
for(var j = 0;j < elevators.length;j++) {
if (elevators.indexOf(this) != j){
var other_elevator_at_floor = elevators[j].get_position() / elevators[j].get_height() + 1;
if(elevators[j].get_destination_floor() == requests[i] ) {
handled = true;
break;
}
}
}
if(!handled) {
target_floors.push(requests[i]);
}
}
if (target_floors.length == 0){
return this.commit_decision(Math.floor(num_floors / 2));
}
target_floors.sort(sortNumber);
function sortNumber(a,b) {
return a - b;
}
var direction = this.direction;
if (this.direction == null) {
this.direction = Elevator.DIRECTION_DOWN;
for (var i = 0; i < target_floors.length;i++){
if (target_floors[i] >= elevator_at_floor){
this.direction = Elevator.DIRECTION_UP;
break;
}
}
}
if (this.direction == Elevator.DIRECTION_UP){
this.current_final_destination = target_floors[target_floors.length - 1];
for (var i = 0; i < target_floors.length;i++){
if (target_floors[i] >= elevator_at_floor){
return this.commit_decision(target_floors[i]);
}
}
this.direction = Elevator.DIRECTION_DOWN;
this.current_final_destination = target_floors[0];
for (var i = target_floors.length - 1; i > 0;i--){
if (target_floors[i] <= elevator_at_floor){
return this.commit_decision(target_floors[i]);
}
}
}
else if (this.direction == Elevator.DIRECTION_DOWN) {
this.current_final_destination = target_floors[0];
for (var i = target_floors.length - 1; i > 0;i--){
if (target_floors[i] <= elevator_at_floor){
return this.commit_decision(target_floors[i]);
}
}
this.direction = Elevator.DIRECTION_UP;
this.current_final_destination = target_floors[target_floors.length - 1];
for (var i = 0; i < target_floors.length;i++){
if (target_floors[i] >= elevator_at_floor){
return this.commit_decision(target_floors[i]);
}
}
}
return this.commit_decision(Math.floor(num_floors / 2));
};