forked from salabim/salabim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLock animated new.py
208 lines (159 loc) · 6.42 KB
/
Lock animated new.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
import salabim as sim
left = -1
right = +1
def sidename(side):
return 'l' if side == left else 'r'
def shortname(ship):
s = ''
for c in ship.name():
if c != '.':
s = s + c
return s
def shipcolor(side):
if side == left:
return 'blue'
else:
return 'red'
def ship_polygon(ship):
return (ship.side * (ship.length - 2), 0, ship.side * 3, 0,
ship.side * 2, 3, ship.side * (ship.length - 2), 3)
def lock_water_rectangle(t):
if lock.mode() == 'Switch':
y = sim.interpolate(t, lock.mode_time(), lock.scheduled_time(
), ylevel[lock.side], ylevel[-lock.side])
else:
y = ylevel[lock.side]
return (xdoor[left], -waterdepth, xdoor[right], y)
def lock_door_left_rectangle(t):
if lock.mode() == 'Switch' or lock.side == right:
y = ylevel[right] + 2
else:
y = ylevel[left] - waterdepth
return (xdoor[left] - 1, -waterdepth, xdoor[left] + 1, y)
def lock_door_right_rectangle(t):
if lock.mode() == 'Switch' or lock.side == left:
y = ylevel[right] + 2
else:
y = ylevel[right] - waterdepth
return (xdoor[right] - 1, -waterdepth, xdoor[right] + 1, y)
def animation_pre_tick(self, t):
if lock.mode() == 'Switch':
y = sim.interpolate(t, lock.mode_time(), lock.scheduled_time(),
ylevel[lock.side], ylevel[-lock.side])
else:
y = ylevel[lock.side]
lockqueue.animate(x=xdoor[-lock.side], y=y, direction='w' if lock.side == left else 'e')
def do_animation():
global ylevel, xdoor, waterdepth
lockheight = 5
waterdepth = 2
ylevel = {left: 0, right: lockheight}
xdoor = {left: -0.5 * locklength, right: 0.5 * locklength}
xbound = {left: -1.2 * locklength, right: 1.2 * locklength}
sim.Environment.animation_pre_tick = animation_pre_tick
env.animation_parameters(
x0=xbound[left], y0=-waterdepth, x1=xbound[right], modelname='Lock', speed=8, fps=30)
for side in [left, right]:
wait[side].animate(x=xdoor[side], y=10 + ylevel[side], direction='n')
sim.Animate(rectangle0=(xbound[left], ylevel[left] - waterdepth,
xdoor[left], ylevel[left]), fillcolor0='aqua', linewidth0=0)
sim.Animate(rectangle0=(xdoor[right], ylevel[right] - waterdepth,
xbound[right], ylevel[right]), fillcolor0='aqua', linewidth0=0)
a = sim.Animate(rectangle0=(0, 0, 0, 0), fillcolor0='aqua', linewidth0=0)
a.rectangle = lock_water_rectangle
a = sim.Animate(rectangle0=(0, 0, 0, 0), fillcolor0='black', linewidth0=0)
a.rectangle = lock_door_left_rectangle
a = sim.Animate(rectangle0=(0, 0, 0, 0), fillcolor0='black', linewidth0=0)
a.rectangle = lock_door_right_rectangle
a = sim.Animate(text='', x0=10, y0=650, screen_coordinates=True,
fontsize0=15, font='narrow', anchor='w')
a.text = lambda t: 'mean waiting left : {:5.1f} (n={})'.\
format(wait[left].length_of_stay.mean(),
wait[left].length_of_stay.number_of_entries())
a = sim.Animate(text='', x0=10, y0=630, screen_coordinates=True,
fontsize0=15, font='narrow', anchor='w')
a.text = lambda t: 'mean waiting right: {:5.1f} (n={})'.\
format(wait[right].length_of_stay.mean(),
wait[right].length_of_stay.number_of_entries())
a = sim.Animate(text='xx=12.34', x0=10, y0=610, screen_coordinates=True,
fontsize0=15, font='narrow', anchor='w')
a.text = lambda t: ' nr waiting left : {:3d}'.format(wait[left].length())
a = sim.Animate(text='xx=12.34', x0=10, y0=590, screen_coordinates=True,
fontsize0=15, font='narrow', anchor='w')
a.text = lambda t: ' nr waiting right: {:3d}'.format(wait[right].length())
sim.AnimateSlider(x=520, y=env.height, width=100, height=20,
vmin=16, vmax=60, resolution=4, v=iat, label='iat', action=set_iat)
sim.AnimateSlider(x=660, y=env.height, width=100, height=20,
vmin=10, vmax=60, resolution=5, v=meanlength, label='mean length', action=set_meanlength)
def set_iat(val):
global iat
iat = float(val)
def set_meanlength(val):
global meanlength
meanlength = float(val)
class Shipgenerator(sim.Component):
def process(self):
while True:
yield self.hold(sim.Exponential(iat).sample())
ship = Ship(name=sidename(self.side) + 'ship.')
ship.side = self.side
ship.length = meanlength * sim.Uniform(2. / 3, 4. / 3).sample()
if lock.mode() == 'Idle':
lock.activate()
class Ship(sim.Component):
def animation_objects(self, q):
size_x = self.length
size_y = 5
if self.side == left:
anchor = 'se'
else:
anchor = 'sw'
an1 = sim.Animate(polygon0=ship_polygon(self),
fillcolor0=shipcolor(self.side), anchor=anchor, linewidth0=0)
an2 = sim.Animate(text=shortname(self), textcolor0='white',
anchor=anchor, fontsize0=2.5, offsetx0=self.side * 5)
return (size_x, size_y, an1, an2)
def process(self):
self.enter(wait[self.side])
yield self.passivate(mode='Wait')
yield self.hold(intime, mode='Sail in')
self.leave(wait[self.side])
self.enter(lockqueue)
lock.activate()
yield self.passivate(mode='In lock')
self.leave(lockqueue)
yield self.hold(outtime, mode='Sail out')
lock.activate()
class Lock(sim.Component):
def process(self):
while True:
if len(wait[left]) + len(wait[right]) == 0:
yield self.passivate(mode='Idle')
usedlength = 0
for ship in wait[self.side]:
if usedlength + ship.length <= locklength:
usedlength += ship.length
ship.activate()
yield self.passivate('Wait for sail in')
yield self.hold(switchtime, mode='Switch')
self.side = -self.side
for ship in lockqueue:
ship.activate()
yield self.passivate('Wait for sail out')
env = sim.Environment()
locklength = 60
switchtime = 10
intime = 2
outtime = 2
meanlength = 30
iat = 20
lockqueue = sim.Queue('lockqueue')
wait = {}
for side in (left, right):
wait[side] = sim.Queue(name=sidename(side) + 'Wait')
shipgenerator = Shipgenerator(name=sidename(side) + 'Shipgenerator')
shipgenerator.side = side
lock = Lock('Lock')
lock.side = left
do_animation()
env.run()