-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator.cpp
More file actions
319 lines (284 loc) · 9.69 KB
/
Elevator.cpp
File metadata and controls
319 lines (284 loc) · 9.69 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
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
#include<bits/stdc++.h>
using namespace std;
enum class Direction { UP = 1, DOWN = -1, IDLE = 0};
string DirectionToString(Direction dir){
switch(dir){
case Direction::UP: return "UP";
case Direction::DOWN: return "DOWN";
case Direction::IDLE: return "IDLE";
default: return "UNKNOWN";
}
}
struct Request{
int from;
Direction direction;
Request(int f,Direction d): from(f),direction(d){}
};
class Elevator{
int id;
int currentFloor;
Direction direction;
bool doorOpen;
int capacity;
int currentLoad;
set<int> upStops;
set<int ,greater<>> downStops;
public:
Elevator(int i,int startFloor): id(i),currentFloor(startFloor),direction(Direction::IDLE),doorOpen(false),capacity(10),currentLoad(0){}
int getid(){return id;}
int getcurrentFloor(){return currentFloor;}
Direction getDirection(){return direction;}
bool isIdle(){return direction == Direction::IDLE;}
bool enter(){
if(currentLoad < capacity){
currentLoad++;
return true;
}
return false;
}
bool exit(){
if(currentLoad > 0){
currentLoad--;
return true;
}
return false;
}
void addTarget(int floor){
if(floor == currentFloor){
doorOpen = true;
return;
}
if(floor > currentFloor) upStops.insert(floor);
else downStops.insert(floor);
if(direction == Direction::IDLE){
if(!upStops.empty()) direction = Direction::UP;
else if(!downStops.empty()) direction = Direction::DOWN;
}
}
int distance(int floor){
return abs(currentFloor - floor);
}
void selectFloor(int floor){
addTarget(floor);
}
bool isgoingTowards(int floor,Direction reqDir){
if(direction == Direction::UP && floor>= currentFloor && reqDir == Direction::UP) return true;
if(direction == Direction::DOWN && floor<= currentFloor && reqDir == Direction::DOWN) return true;
return false;
}
string status(){
string s = "Elevator " + to_string(id) + " | Floor: " + to_string(currentFloor) +
" | dir: " + DirectionToString(direction) + " | door: " + (doorOpen ? "Open" : "Closed");
s += " upStops: ";
for(int f :upStops) s+=to_string(f)+ " ";
s+=" downStops: ";
for(int f :downStops) s+=to_string(f)+ " ";
return s;
}
void step(){
if(doorOpen){
doorOpen = false;
removeStop(currentFloor);
updateDirection();
return;
}
if(direction == Direction::UP){
currentFloor++;
if(hasStopAt(currentFloor)) {
doorOpen = true;
}
}
else if(direction == Direction::DOWN){
currentFloor--;
if(hasStopAt(currentFloor)) {
doorOpen = true;
}
}
if(direction != Direction::IDLE) updateDirection();
}
private:
bool hasStopAt(int floor){
return upStops.count(floor) || downStops.count(floor);
}
void removeStop(int floor){
upStops.erase(floor);
downStops.erase(floor);
}
void updateDirection(){
upStops.erase(currentFloor);
downStops.erase(currentFloor);
if(direction == Direction::UP ){
auto it = upStops.lower_bound(currentFloor);
if(it != upStops.end() && *it > currentFloor) {
direction = Direction::UP;
return;
}
if(!downStops.empty()){
direction = Direction::DOWN;
return;
}
if(upStops.empty() && downStops.empty()) direction = Direction::IDLE;
}else if(direction == Direction::DOWN){
if(!downStops.empty()){
// downStops is ordered with greater<>, so lower_bound(currentFloor)
// gives the first element <= currentFloor. We need a strictly lower stop.
auto it = downStops.lower_bound(currentFloor);
if(it != downStops.end() && *it < currentFloor) {
direction = Direction::DOWN;
return;
}
}
if(!upStops.empty()){
direction = Direction::UP;
return;
}
if(upStops.empty() && downStops.empty()) direction = Direction::IDLE;
}else{
if(!upStops.empty()) direction = Direction::UP;
else if(!downStops.empty()) direction = Direction::DOWN;
}
}
};
class ElevatorController{
vector<Elevator> elevators;
int minFloor;
int maxFloor;
queue<Request> pendingRequest;
public:
ElevatorController(int numElevators,int minF,int maxF) : minFloor(minF), maxFloor(maxF){
for(int i=0;i<numElevators;i++){
elevators.push_back(Elevator(i,minF));
}
}
void pickup(int floor,Direction direction){
if(floor< minFloor || floor > maxFloor){
cout<<"Invalid floor request "<<floor<<endl;
return;
}
int bestIdx = -1;
int bestScore = INT_MAX;
// Prefer elevators already moving towards the request, choose the closest among them
for(size_t i=0;i<elevators.size();i++){
auto &e = elevators[i];
if(e.isgoingTowards(floor,direction)){
int dist = e.distance(floor);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(i);
}
}
}
if(bestIdx == -1){
for(size_t i=0;i<elevators.size();i++){
auto &e = elevators[i];
if(e.isIdle()){
int dist = e.distance(floor);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(i);
}
}
}
}
// If still not found, choose the nearest elevator regardless of direction (last resort)
if(bestIdx == -1){
for(size_t i=0;i<elevators.size();i++){
auto &e = elevators[i];
int dist = e.distance(floor);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(i);
}
}
}
if(bestIdx != -1){
elevators[bestIdx].addTarget(floor);
cout<<"Assigned elevator "<<elevators[bestIdx].getid()<<" to pickup at floor "<<floor<<endl;
}else{
pendingRequest.push(Request(floor,direction));
cout<<"Enqueued request at floor "<<floor<<endl;
}
}
void selectFloor(int eid,int floor){
if(eid < 0 || eid >= (int)elevators.size()) return; //out of bounds
if(floor < minFloor || floor > maxFloor) return;
elevators[eid].selectFloor(floor);
}
void step(){
for(auto &e : elevators) e.step();
int pendingCount = pendingRequest.size();
for(int i=0;i<pendingCount;i++){
Request r = pendingRequest.front();
pendingRequest.pop();
int bestIdx = -1;
int bestScore = INT_MAX;
// Try assign using same heuristic as pickup
for(size_t j=0;j<elevators.size();j++){
auto &e = elevators[j];
if(e.isgoingTowards(r.from, r.direction)){
int dist = e.distance(r.from);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(j);
}
}
}
if(bestIdx == -1){
for(size_t j=0;j<elevators.size();j++){
auto &e = elevators[j];
if(e.isIdle()){
int dist = e.distance(r.from);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(j);
}
}
}
}
if(bestIdx == -1){
for(size_t j=0;j<elevators.size();j++){
auto &e = elevators[j];
int dist = e.distance(r.from);
if(dist < bestScore){
bestScore = dist;
bestIdx = static_cast<int>(j);
}
}
}
if(bestIdx != -1){
elevators[bestIdx].addTarget(r.from);
cout<<"Assigned elevator "<<elevators[bestIdx].getid()<<" to pending pickup at floor "<<r.from<<endl;
} else {
// Could not assign now; requeue
pendingRequest.push(r);
}
}
}
void status(){
for(auto &e: elevators){
cout<<e.status()<<endl;
}
cout<<"Pending requests: "<<pendingRequest.size()<<endl;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout<<"=== Elevator LLD Simulation ==="<<endl;
ElevatorController controller(3,0,10);
// Seed some pickup requests
controller.pickup(0, Direction::UP);
controller.pickup(5, Direction::DOWN);
controller.pickup(9, Direction::DOWN);
controller.pickup(3, Direction::UP);
// Run simulation for a number of ticks
for(int t = 0; t < 20; ++t){
cout << "\n--- Tick " << t << " ---\n";
controller.status();
controller.step();
this_thread::sleep_for(chrono::milliseconds(150));
}
cout << "\n--- Final Status ---\n";
controller.status();
return 0;
}