-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevator.py
439 lines (371 loc) · 13.8 KB
/
elevator.py
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
from __future__ import print_function # compatibility with Python 2.x
from __future__ import division # compatibility with Python 2.x
import random
import gym
from gym.spaces import Discrete, MultiDiscrete, Box
import numpy as np
import salabim as sim
def create_env(capacity=4, num_lifts=3, topfloor=15):
def do_animation():
global xvisitor_dim
global yvisitor_dim
global xlift
global capacity_last, num_lifts_last, topfloor_last
env.modelname("Elevator")
env.speed(32)
env.background_color("20%gray")
if make_video:
env.video("Elevator.mp4")
xvisitor_dim = 30
yvisitor_dim = xvisitor_dim
yfloor0 = 20
# x positions dict for all lifts
xlift = {}
xled = {}
x = env.width()
for lift in lifts:
x -= (capacity + 1) * xvisitor_dim
xlift[lift] = x
x -= xvisitor_dim
xsign = x
x -= xvisitor_dim / 2
for direction in (up, down):
x -= xvisitor_dim / 2
xled[direction] = x
x -= xvisitor_dim
xwait = x
for floor in floors:
y = yfloor0 + floor.n * yvisitor_dim
floor.y = y
for direction in (up, down):
if (direction == up and floor.n < topfloor) or (direction == down and floor.n > 0):
b = xvisitor_dim / 4
# print triangle for direction in front of each floor
animate_led = sim.AnimatePolygon(
spec=(-b, -b, b, -b, 0, b),
x=xled[direction],
y=y + 2 * b,
angle=0 if direction == up else 180,
fillcolor=direction_color(direction),
visible=lambda arg, t: (arg.floor_direction) in requests,
)
animate_led.floor_direction = (floor, direction)
# floor separation line
sim.AnimateLine(x=0, y=y, spec=(0, 0, xwait, 0))
# print floor number in front of each floor
sim.AnimateText(x=xsign, y=y + yvisitor_dim / 2, text=str(floor.n), fontsize=xvisitor_dim / 2)
# print the waiting line in front of each floor
sim.AnimateQueue(queue=floor.visitors, x=xwait - xvisitor_dim, y=floor.y, direction="w", title="")
for lift in lifts:
x = xlift[lift]
# draw the lift
lift.pic = sim.AnimateRectangle(
x=x, y=lift.y, spec=(0, 0, capacity * xvisitor_dim, yvisitor_dim), fillcolor="lightblue", linewidth=0
)
# print visitors in this lift
sim.AnimateQueue(queue=lift.visitors, x=xlift[lift], y=lift.y, direction="e", title=lift.name(), arg=lift)
num_lifts_last = num_lifts
sim.AnimateSlider(
x=510,
y=0,
width=90,
height=20,
vmin=1,
vmax=5,
resolution=1,
v=num_lifts,
label="#elevators",
action=set_num_lifts,
xy_anchor="nw",
)
topfloor_last = topfloor
sim.AnimateSlider(
x=610,
y=0,
width=90,
height=20,
vmin=5,
vmax=20,
resolution=1,
v=topfloor,
label="top floor",
action=set_topfloor,
xy_anchor="nw",
)
capacity_last = capacity
sim.AnimateSlider(
x=710,
y=0,
width=90,
height=20,
vmin=2,
vmax=6,
resolution=1,
v=capacity,
label="capacity",
action=set_capacity,
xy_anchor="nw",
)
sim.AnimateSlider(
x=510,
y=-50,
width=90,
height=25,
vmin=0,
vmax=400,
resolution=25,
v=load_0_n,
label="Load 0->n",
action=set_load_0_n,
xy_anchor="nw",
)
sim.AnimateSlider(
x=610,
y=-50,
width=90,
height=25,
vmin=0,
vmax=400,
resolution=25,
v=load_n_n,
label="Load n->n",
action=set_load_n_n,
xy_anchor="nw",
)
sim.AnimateSlider(
x=710,
y=-50,
width=90,
height=25,
vmin=0,
vmax=400,
resolution=25,
v=load_n_0,
label="Load n->0",
action=set_load_n_0,
xy_anchor="nw",
)
env.animate(True)
def set_load_0_n(val):
global load_0_n
load_0_n = float(val)
if vg_0_n.ispassive():
vg_0_n.activate()
def set_load_n_n(val):
global load_n_n
load_n_n = float(val)
if vg_n_n.ispassive():
vg_n_n.activate()
def set_load_n_0(val):
global load_n_0
load_n_0 = float(val)
if vg_n_0.ispassive():
vg_n_0.activate()
def set_capacity(val):
global capacity
global capacity_last
capacity = int(val)
if capacity != capacity_last:
capacity_last = capacity
env.main().activate()
def set_num_lifts(val):
global num_lifts
global num_lifts_last
num_lifts = int(val)
if num_lifts != num_lifts_last:
num_lifts_last = num_lifts
env.main().activate()
def set_topfloor(val):
global topfloor
global topfloor_last
topfloor = int(val)
if topfloor != topfloor_last:
topfloor_last = topfloor
env.main().activate()
def direction_color(direction):
if direction == 1:
return "red"
if direction == -1:
return "green"
return "yellow"
class VisitorGenerator(sim.Component):
def setup(self, from_, to, id):
self.from_ = from_
self.to = to
self.id = id
def process(self):
while True:
from_ = random.randint(self.from_[0], self.from_[1])
# while is just to avoid having similar from and to
while True:
to = random.randint(self.to[0], self.to[1])
if from_ != to:
break
Visitor(from_=from_, to=to)
if self.id == "0_n":
load = load_0_n
elif self.id == "n_0":
load = load_n_0
else:
load = load_n_n
if load == 0:
yield self.passivate()
else:
iat = 3600 / load
r = random.uniform(0.5, 1.5)
yield self.hold(r * iat)
class Visitor(sim.Component):
def setup(self, from_, to):
self.fromfloor = floors[from_]
self.tofloor = floors[to]
self.direction = getdirection(self.fromfloor, self.tofloor)
def animation_objects(self, q):
size_x = xvisitor_dim
size_y = yvisitor_dim
b = 0.1 * xvisitor_dim
an0 = sim.AnimateRectangle(
spec=(b, 2, xvisitor_dim - b, yvisitor_dim - b),
linewidth=0,
fillcolor=direction_color(self.direction),
text=str(self.tofloor.n),
fontsize=xvisitor_dim * 0.7,
textcolor="white",
)
return size_x, size_y, an0
def process(self):
self.enter(self.fromfloor.visitors)
if not (self.fromfloor, self.direction) in requests:
requests[self.fromfloor, self.direction] = self.env.now()
for car in lifts:
if car.ispassive():
car.activate()
yield self.passivate()
class VisitorsInCar(sim.Queue):
pass
class Lift(sim.Component):
def setup(self):
self.capacity = capacity
self.direction = still
self.floor = floors[0]
self.dooropen = False
self.visitors = VisitorsInCar()
self.neural_control = True
def y(self, t):
if self.mode() == "Move":
y = sim.interpolate(t, self.mode_time(), self.scheduled_time(), self.floor.y, self.nextfloor.y)
else:
y = self.floor.y
return y
def process(self):
self.floor = floors[0]
self.direction = still
self.dooropen = False
while True:
if self.direction == still:
if not requests:
yield self.passivate(mode="Idle")
# unloading the visitors who reached the desired floor
if self.count_to_floor(self.floor) > 0:
yield self.hold(dooropen_time, mode="Door open")
self.dooropen = True
for visitor in self.visitors:
if visitor.tofloor == self.floor:
visitor.leave(self.visitors) # the visitor leaves the lift visitors queue
visitor.activate()
yield self.hold(exit_time, mode="Let exit")
if self.direction == still:
self.direction = up # just random
# loading the lift
for self.direction in (self.direction, -self.direction):
# we delete the call of the lift because it has arrived
if (self.floor, self.direction) in requests:
del requests[self.floor, self.direction]
if not self.dooropen:
yield self.hold(dooropen_time, mode="Door open")
self.dooropen = True
for visitor in self.floor.visitors:
# only pickup the visitors going the same way as the lift
if visitor.direction == self.direction:
# we still have some room in the lift
if len(self.visitors) < self.capacity:
# the visitor leaves the floor queue
visitor.leave(self.floor.visitors)
# to join the lift
visitor.enter(self.visitors)
yield self.hold(enter_time, mode="Let in")
if self.floor.count_in_direction(self.direction) > 0:
if not (self.floor, self.direction) in requests:
requests[self.floor, self.direction] = self.env.now()
if self.neural_control is False:
if self.visitors:
break
else:
# we select the earliest request to serve it first by defining self.direction accordingly
if requests:
earliest = sim.inf
for (floor, direction) in requests:
if requests[floor, direction] < earliest:
self.direction = getdirection(self.floor, floor)
earliest = requests[floor, direction]
else:
# or keep the lift still if no requests
self.direction = still
if self.dooropen:
yield self.hold(doorclose_time, mode="Door close")
self.dooropen = False
if self.direction != still:
# define the next target floor
print(f'go to the {self.floor.n + self.direction}th floor')
if len(floors) >= (self.floor.n + self.direction) >= 0:
self.nextfloor = floors[self.floor.n + self.direction]
yield self.hold(move_time, mode="Move")
self.floor = self.nextfloor
else:
print('out of range')
def count_to_floor(self, tofloor):
n = 0
for visitor in self.visitors:
if visitor.tofloor == tofloor:
n += 1
return n
class Visitors(sim.Queue):
pass
class Floor:
def __init__(self):
self.visitors = Visitors()
self.n = self.visitors.sequence_number()
def count_in_direction(self, dir):
n = 0
for visitor in self.visitors:
if visitor.direction == dir:
n += 1
return n
def getdirection(fromfloor, tofloor):
if fromfloor.n < tofloor.n:
return +1
if fromfloor.n > tofloor.n:
return -1
return 0
up = 1
still = 0
down = -1
move_time = 10
dooropen_time = 3
doorclose_time = 3
enter_time = 3
exit_time = 3
load_0_n = 50
load_n_n = 100
load_n_0 = 100
env = sim.Environment()
vg_0_n = VisitorGenerator(from_=(0, 0), to=(1, topfloor), id="0_n", name="vg_0_n")
vg_n_0 = VisitorGenerator(from_=(1, topfloor), to=(0, 0), id="n_0", name="vg_n_0")
vg_n_n = VisitorGenerator(from_=(1, topfloor), to=(1, topfloor), id="n_n", name="vg_n_n")
requests = {}
floors = [Floor() for ifloor in range(topfloor+1)]
env.define_floors(floors)
lifts = [Lift() for ilift in range(num_lifts)]
env.define_lifts(lifts)
make_video = False
do_animation()
return env