-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathelevator.c
515 lines (457 loc) · 13.6 KB
/
elevator.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CAPACITY 10
#define MAX_ELEVATORS 16
#define DEFAULT_TOP 32
#define MAX_TOP 128
#define QUEUE_SZ 100
#define ST_IDLE 0
#define ST_GOING_UP 1
#define ST_GOING_DOWN 2
#define ST_PICKING_UP 3
#define ST_PICKING_DOWN 4
#define ST_DOOR_OPEN 5
static char *g_state_str[] = {
"idle",
"up",
"down",
"pick +",
"pick -",
"idle"
};
typedef struct elev_s {
int id;
int capacity; // total capacity
int persons; // total persons in the elevator
int goal[MAX_TOP]; // goal floor queue
int num; // num of goal in the queue
int flags; // bit mask flags
int state; // current state
int floor; // current floor
int pick_floor; // pickup floor
} elev_t;
typedef struct evc_s {
int total; // total number of elevators
int top; // top floor of the elevators can get to
int head; // head position of request queue
int num; // number of requests in the queue
int request[QUEUE_SZ]; // request queue of floor
int dir[QUEUE_SZ]; // request queue of direction
elev_t elev[MAX_ELEVATORS]; // elevators
} evc_t;
typedef struct cmd_s {
int op; // operation
int arg1; // elevator id
int arg2; // direction
int arg3; // start floor
} cmd_t;
#define OP_EXIT 0
#define OP_STATUS 1
#define OP_UPDATE 2
#define OP_PICKUP 3
#define OP_STEP 4
#define OP_HELP 5
evc_t g_elevators = { 0 }; // single thread only
// parse a command parameter
char *parse_int(char *buff, int *arg) {
int neg = 0, k = 0;
*arg = 0;
if (*buff && *buff == '-') {
neg = 1;
buff ++;
}
while (*buff >= '0' && *buff <= '9') {
k = k * 10 + (*buff) - '0';
buff ++;
}
if (*buff == ' ') buff ++;
else if (*buff) return "";
*arg = neg ? 0 - k : k;
return buff;
}
// parse a command
int parse_cmd(char *buff, cmd_t *cmd) {
if (!strncmp(buff, "update ", 7)) {
cmd->op = OP_UPDATE;
buff += 7;
buff = parse_int(buff, &cmd->arg1);
buff = parse_int(buff, &cmd->arg2);
buff = parse_int(buff, &cmd->arg3);
} else
if (!strncmp(buff, "pickup ", 7)) {
cmd->op = OP_PICKUP;
buff += 7;
buff = parse_int(buff, &cmd->arg1);
buff = parse_int(buff, &cmd->arg2);
} else
if (!strcmp(buff, "status")) {
cmd->op = OP_STATUS;
} else
if (!strcmp(buff, "step")) {
cmd->op = OP_STEP;
} else
if (!strcmp(buff, "help")) {
cmd->op = OP_HELP;
} else
if (!strcmp(buff, "exit") ||
!strcmp(buff, "quit")) {
cmd->op = OP_EXIT;
} else {
return -1;
}
return 0;
}
// take user input for commands
void read_cmd(cmd_t *cmd, int n, int t) {
int i, c;
char buff[32];
again:
printf("> ");
i = 0;
do {
c = getchar();
if (i < 32 &&
(c != ' ' || (i > 0 && buff[i - 1] != ' '))) {
buff[i ++] = c;
}
} while (c != '\n');
if (i == 32) {
printf("Input too long, please try again!\n");
goto again;
}
while (i > 0 &&
(buff[i - 1] == '\n' ||
buff[i - 1] == ' ')) {
i --;
}
if (i == 0) goto again;
buff[i] = 0;
if (parse_cmd(&buff[0], cmd)) {
printf("Invalid input, enter help to show all commands!\n");
goto again;
}
}
void show_help() {
printf("status\t - show elevator status.\n");
printf("step\t - time stepping.\n");
printf("pickup <floor> <direction>\t - request pickup.\n");
printf("update <id> <floor> <goal>\t - update elevator when it ready for picking up.\n");
printf("exit\t - exit application.\n");
}
// print all elevator status and number of pending requests in the queue
void show_status() {
int i, j;
elev_t *p;
printf("ID\tFloor\tState\tFloors\n");
printf("====================================\n");
for (i = 0; i < g_elevators.total; i ++) {
p = &g_elevators.elev[i];
printf("%d:\t%d\t%s\t", p->id,
p->floor,
g_state_str[p->state]);
if ((p->state == ST_PICKING_UP ||
p->state == ST_PICKING_DOWN) &&
p->floor != p->pick_floor) {
printf("[%d] ", p->pick_floor);
} else {
for (j = 0; j < g_elevators.top; j ++) {
if (p->goal[j]) {
printf("%d ", j + 1);
}
}
}
printf("\n");
}
printf("Pending pickup requests: %d.\n", g_elevators.num);
}
// compare distance between elevator and pickup floor
int is_closer(elev_t *a, elev_t *b, int floor) {
int d1, d2;
d1 = a->floor - floor;
d1 = d1 > 0 ? d1 : 0 - d1;
d2 = b->floor - floor;
d2 = d2 > 0 ? d2 : 0 - d2;
return d1 < d2 ? 1 : 0;
}
// find an idle elevator and schedule it to run a pick up request
void schedule_run() {
int i, floor, dir;
elev_t *p, *idle_p;
start:
idle_p = NULL;
if (g_elevators.num == 0) return;
// take the first request from the queue
floor = g_elevators.request[g_elevators.head];
dir = g_elevators.dir[g_elevators.head];
for (i = 0; i < g_elevators.total; i ++) {
p = &g_elevators.elev[i];
#if 0
// pick up on the way
if (p->floor == floor) {
// pick up if there is just one on this floor
if ((p->state == ST_GOING_UP && dir > 0) ||
(p->state == ST_GOING_DOWN && dir < 0)) { // FIXME: check load is within capacity
p->persons ++; // increase load
g_elevators.head = (g_elevators.head + 1) % QUEUE_SZ;
g_elevators.num --;
goto starts;
}
}
#endif
// find the closest idle one
if (p->state == ST_IDLE) {
if (idle_p == NULL ||
is_closer(p, idle_p, floor)) {
idle_p = p;
}
}
}
// schedule the closest idle one to run
if (idle_p) {
idle_p->state = dir > 0 ? ST_PICKING_UP : ST_PICKING_DOWN;
idle_p->pick_floor = floor;
g_elevators.head = (g_elevators.head + 1) % QUEUE_SZ;
g_elevators.num --;
goto start;
}
}
// optimze running requests to shorten waiting time.
void optimize_requests() {
#if 0
int i, n;
int idle_buff[MAX_TOP][MAX_ELEVATORS + 1] = { 0 };
int pick_buff[MAX_TOP][MAX_ELEVATORS + 1] = { 0 };
elev_t *p;
// make elevator buffer on each floor
for (i = 0; i < g_elevators.total; i ++) {
p = &g_elevators.elev[i];
if (p->state == ST_IDLE) {
n = idle_buff[p->floor][0];
idle_buff[p->floor][n] = p->id;
idle_buff[p->floor][0] ++; // number of idle elevators on this floor
} else
if ((p->state == ST_PICKING_UP ||
p->state == ST_PICKING_DOWN) &&
p->pick_floor != p->floor) {
n = pick_buff[p->pick_floor][0];
pick_buff[p->pick_floor][n] = p->id;
pick_buff[p->pick_floor][0] ++; // number of elevators going to pick up on this floor
}
}
// FIXME: sort the buffers and optimize with matching by distance.
#endif
}
// schedule runs and optimize pickup requests
void schedule_and_optimize() {
schedule_run();
optimize_requests();
}
void move_up_down(elev_t *p, int step) {
p->floor += step;
// someone takes off
if (p->goal[p->floor - 1]) {
p->goal[p->floor - 1] = 0;
p->num --;
}
// all are off
if (p->num == 0) {
p->state = ST_DOOR_OPEN;
return;
}
}
void move_picking_up_down(elev_t *p, int up) {
if (p->floor == p->pick_floor) {
if (p->num > 0) {
// just switch state, not moving, seems like
// taking one step to close the door.
p->state = up ? ST_GOING_UP : ST_GOING_DOWN;
} else {
// FIXME: switch state to idle if waiting for
// update times out.
}
} else if (p->floor < p->pick_floor) {
p->floor ++;
} else {
p->floor --;
}
}
void time_stepping() {
int i, idle = 0;
elev_t *p;
// run elevator state machine
for (i = 0; i < g_elevators.total; i ++) {
p = &g_elevators.elev[i];
switch (p->state) {
case ST_IDLE:
idle = 1;
break;
case ST_GOING_UP:
move_up_down(p, 1);
break;
case ST_GOING_DOWN:
move_up_down(p, -1);
break;
case ST_PICKING_UP:
move_picking_up_down(p, 1);
break;
case ST_PICKING_DOWN:
move_picking_up_down(p, 0);
break;
case ST_DOOR_OPEN:
p->state = ST_IDLE;
idle = 1;
break;
default:
break;
}
}
if (idle) {
// trigger scheduling and optimization when idle elevator is available
schedule_and_optimize();
}
}
// enqueue a pickup request and trigger schedule
int schedule_pickup(int floor, int dir) {
int i, idle = 0;
elev_t *p;
// validate input
if (floor < 1 ||
floor > g_elevators.top ||
dir == 0 ||
(floor == 1 && dir < 0) ||
(floor == g_elevators.top && dir > 0)) {
printf("Invalid pickup request.\n");
return -1;
}
// if a pick up for this floor is on the way,
// don't schedule another elevator to run
for (i = 0; i < g_elevators.total; i ++) {
p = &g_elevators.elev[i];
if ((((p->state == ST_PICKING_UP) && dir > 0) ||
((p->state == ST_PICKING_DOWN) && dir < 0)) &&
p->pick_floor == floor) {
printf("Elevator %d is on the way!\n", p->id);
return -1;
}
if (p->state == ST_IDLE) {
idle = 1;
}
}
// ignore this request if queue is full
if (g_elevators.num == QUEUE_SZ) {
printf("Too many pickup requests, try later.\n");
return -1;
}
// enqueue the request
i = (g_elevators.head + g_elevators.num) % QUEUE_SZ;
g_elevators.request[i] = floor;
g_elevators.dir[i] = dir;
g_elevators.num ++;
if (idle) {
// trigger scheduling and optimization when idle elevator is available
schedule_and_optimize();
}
return 0;
}
// update elevator goal floor when it is ready to pickup
int update_floor(int eid, int floor, int goal) {
elev_t *p;
// validate request
if (eid < 1 || eid > g_elevators.total ||
floor < 1 || floor > g_elevators.top ||
goal < 1 || goal > g_elevators.top) {
printf("Invaid parameters %d, %d, %d.\n", eid, floor, goal);
return -1;
}
p = &g_elevators.elev[eid - 1];
if (p->state != ST_PICKING_UP &&
p->state != ST_PICKING_DOWN) {
printf("Elevator %d is not ready to take the update.\n", eid);
return -1;
}
if (p->pick_floor != floor) {
printf("Elevator %d not yet arrive at floor %d.\n", eid, floor);
return -1;
}
if ((p->state == ST_PICKING_UP && goal <= floor) ||
(p->state == ST_PICKING_DOWN && goal >= floor)) {
printf("Elevator %d is going to other direction.\n", eid);
return -1;
}
// set goal floors if it is not yet set.
if (p->goal[goal - 1] == 0) {
p->goal[goal - 1] = 1;
p->num ++;
}
return 0;
}
int main(int argc, char *argv[])
{
elev_t *p;
int i, n = MAX_ELEVATORS, c = MAX_CAPACITY, t = DEFAULT_TOP;
cmd_t cmd;
if (argc > 1) {
n = atoi(argv[1]); // total number of elevators being created
}
if (argc > 2) {
t = atoi(argv[2]); // top floor of all elevators can get to
}
if (argc > 3) {
c = atoi(argv[3]); // total capacity of every elevators
}
if (n < 1 || n > MAX_ELEVATORS) {
n = MAX_ELEVATORS;
printf("Total number of elevator is adjusted to default %d.\n", n);
}
if (c < 1 || c > MAX_CAPACITY) {
c = MAX_CAPACITY;
printf("Capacity of every elevator is adjusted to default %d.\n", c);
}
if (t < 2) {
t = DEFAULT_TOP;
printf("Top floor is adjusted to default %d.\n", t);
} else if (t > MAX_TOP) {
t = MAX_TOP;
printf("Top floor is adjusted to max %d.\n", t);
}
// initialization
g_elevators.total = n;
g_elevators.top = t;
for (i = 0; i < n; i ++) {
p = &g_elevators.elev[i];
p->id = i + 1;
p->capacity = c;
// spread out elevator to different floors
p->floor = 1 + (i * t) / n;
p->state = ST_IDLE;
}
printf("%d elevators are created and run between floor 1 - %d.\n", n, t);
printf("Enter help to see a list of commands.\n");
// main loop of command line interface
read_cmd(&cmd, n, t);
while (cmd.op != OP_EXIT) {
switch (cmd.op) {
case OP_STATUS:
show_status();
break;
case OP_UPDATE:
update_floor(cmd.arg1, cmd.arg2, cmd.arg3);
break;
case OP_PICKUP:
schedule_pickup(cmd.arg1, cmd.arg2);
break;
case OP_STEP:
time_stepping();
break;
case OP_HELP:
show_help();
break;
default:
break;
}
read_cmd(&cmd, n, t);
}
done:
return 0;
}