forked from myrlund/salabim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossing.py
51 lines (39 loc) · 1.31 KB
/
crossing.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
import salabim as sim
class Car(sim.Component):
def setup(self, dir):
self.name('Car ' + dir + '.')
self.direction = dir
def process(self):
yield self.wait((light[self.direction], '"$" in ("green","yellow")'))
class CarGenerator(sim.Component):
def process(self):
while True:
yield self.hold(sim.Uniform(2, 4).sample())
dir = sim.Pdf(directions, 1).sample()
Car(dir=dir)
class TrafficLight(sim.Component):
def setup(self):
light['west'].set('red')
light['east'].set('red')
light['north'].set('green')w
light['south'].set('green')
def process(self):
while True:
yield self.hold(55)
for direction in directions:
if light[direction].get() == 'green':
light[direction].set('yellow')
yield self.hold(5)
for direction in directions:
if light[direction].get() == 'yellow':
light[direction].set('red')
else:
light[direction].set('green')
env = sim.Environment(trace=True)
directions = ('west', 'east', 'north', 'south')
light = {}
for direction in directions:
light[direction] = sim.State('light_' + direction)
TrafficLight()
CarGenerator()
env.run(500)