forked from myrlund/salabim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lock with resources.py
84 lines (64 loc) · 2.21 KB
/
Lock with resources.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
import salabim as sim
left = -1
right = +1
def sidename(side):
return 'l' if side == left else 'r'
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 process(self):
if lock.ispassive():
lock.activate()
yield self.request((lockmeters[self.side], self.length), key_in[self.side])
self.enter(lockqueue)
yield self.hold(intime)
self.release(key_in[self.side])
yield self.request(key_out)
yield self.hold(outtime)
self.leave(lockqueue)
self.release(key_out)
class Lock(sim.Component):
def process(self):
yield self.request(key_in[left])
yield self.request(key_in[right])
yield self.request(key_out)
while True:
if len(key_in[self.side].requesters()) == 0:
if len(key_in[-self.side].requesters()) == 0:
yield self.passivate()
self.release(key_in[self.side])
yield self.request((key_in[self.side], 1, 1000))
lockmeters[self.side].release()
yield self.hold(switchtime, mode='Switch')
self.side = -self.side
self.release(key_out)
yield self.request((key_out, 1, 1000), mode=None)
env = sim.Environment(trace=True)
locklength = 60
switchtime = 10
intime = 2
outtime = 2
meanlength = 30
iat = 30
lockmeters = {}
key_in = {}
lockqueue = sim.Queue('lockqueue')
key_out = sim.Resource(name='key_out')
for side in (left, right):
lockmeters[side] = sim.Resource(capacity=locklength, name=sidename(
side) + ' lock meters', anonymous=True)
key_in[side] = sim.Resource(name=sidename(side) + ' key in')
shipgenerator = Shipgenerator(name=sidename(side) + 'Shipgenerator')
shipgenerator.side = side
lock = Lock(name='lock')
lock.side = left
env.run(100)
for side in (left, right):
lockmeters[side].print_statistics()