forked from myrlund/salabim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elevator.py
189 lines (158 loc) · 5.76 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
import salabim as sim
class VisitorGenerator(sim.Component):
def setup(self, from_, to, id, *args, **kwargs):
self.from_ = from_
self.to = to
self.id = id
def process(self):
while True:
from_ = sim.random.randint(self.from_[0], self.from_[1])
while True:
to = sim.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_0_n
else:
load = load_n_n
if load == 0:
yield self.passivate()
else:
iat = 3600 / load
r = sim.random.uniform(0.5, 1.5)
yield self.hold(r * iat)
class Visitor(sim.Component):
def setup(self, from_, to, *args, **kwargs):
self.fromfloor = floors[from_]
self.tofloor = floors[to]
self.direction = getdirection(self.fromfloor, self.tofloor)
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 cars:
if car.ispassive():
car.activate()
yield self.passivate()
class Car(sim.Component):
def setup(self, capacity):
self.capacity = capacity
self.direction = still
self.floor = floors[0]
self.visitors = sim.Queue(name='visitors in car')
def process(self):
dooropen = False
self.floor = floors[0]
self.direction = still
dooropen = False
while True:
if self.direction == still:
if not requests:
yield self.passivate(mode='Idle')
if self.count_to_floor(self.floor) > 0:
yield self.hold(dooropen_time, mode='Door open')
dooropen = True
for visitor in self.visitors:
if visitor.tofloor == self.floor:
visitor.leave(self.visitors)
visitor.activate()
yield self.hold(exit_time, mode='Let exit')
if self.direction == still:
self.direction = up # just random
for self.direction in (self.direction, -self.direction):
if (self.floor, self.direction) in requests:
del requests[self.floor, self.direction]
if not dooropen:
yield self.hold(dooropen_time, mode='Door open')
dooropen = True
for visitor in self.floor.visitors:
if visitor.direction == self.direction:
if len(self.visitors) < self.capacity:
visitor.leave(self.floor.visitors)
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.visitors:
break
else:
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:
self.direction = still
if dooropen:
yield self.hold(doorclose_time, mode='Door close')
dooropen = False
if self.direction != still:
self.nextfloor = floors[self.floor.n + self.direction]
yield self.hold(move_time, mode='Move')
self.floor = self.nextfloor
def count_to_floor(self, tofloor):
n = 0
for visitor in self.visitors:
if visitor.tofloor == tofloor:
n += 1
return n
class Floor():
def __init__(self, n):
self.n = n
self.visitors = sim.Queue(name='visitors ' + str(n))
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
env = sim.Environment(random_seed=1234567)
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
capacity = 4
ncars = 3
topfloor = 15
VisitorGenerator(
from_=(0, 0), to=(1, topfloor), id='0_n', name='vg_0_n')
VisitorGenerator(
from_=(1, topfloor), to=(0, 0), id='n_0', name='vg_n_0')
VisitorGenerator(
from_=(1, topfloor), to=(1, topfloor), id='n_n', name='vg_n_n')
requests = {}
floors = {ifloor: Floor(ifloor) for ifloor in range(topfloor + 1)}
cars = [Car(capacity=capacity) for icar in range(ncars)]
env.trace(True)
env.run(1000)
env.trace(False)
for floor in floors.values():
floor.visitors.reset_monitors()
env.run(50000)
print('Floor n length length_of_stay')
for floor in floors.values():
print(
'{:5d}{:5d}{:15.3f}{:15.3f}'. format(
floor.n,
floor.visitors.length.number_of_entries(),
floor.visitors.length.mean(),
floor.visitors.length_of_stay.mean()))