forked from myrlund/salabim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Machine shop animated new.py
271 lines (216 loc) · 8.01 KB
/
Machine shop 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
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
"""
Machine shop example
Covers:
- Interrupts
- Resources: PreemptiveResource
Scenario:
A workshop has *n* identical machines. A stream of jobs (enough to
keep the machines busy) arrives. Each machine breaks down
periodically. Repairs are carried out by one repairman. The repairman
has other, less important tasks to perform, too. Broken machines
preempt theses tasks. The repairman continues them when he is done
with the machine repair. The workshop works continuously.
"""
import random
import salabim as sim
RANDOM_SEED = 42
PT_MEAN = 10.0 # Avg. processing time in minutes
PT_SIGMA = 2.0 # Sigma of processing time
MTTF = 300.0 # Mean time to failure in minutes
BREAK_MEAN = 1 / MTTF # Param. for expovariate distribution
REPAIR_TIME = 30.0 # Time it takes to repair a machine in minutes
JOB_DURATION = 30.0 # Duration of other jobs in minutes
NUM_MACHINES = 10 # Number of machines in the machine shop
WEEKS = 4 # Simulation time in weeks
SIM_TIME = WEEKS * 7 * 24 * 60 # Simulation time in minutes
SCALE = 10
class MachineBarAnimate(sim.Animate):
def __init__(self, machine):
self.machine = machine
sim.Animate.__init__(self, rectangle0=(0, 0, 0, 0), linewidth0=0)
def rectangle(self, t):
if self.machine.scheduled_time() == sim.inf:
d = self.machine.left_to_do
else:
d = self.machine.scheduled_time() - t
return(
100, 100 + self.machine.n * 30,
100 + d * SCALE, 100 + self.machine.n * 30 + 20)
def fillcolor(self, t):
if self.machine.mode() == 'work':
return 'green'
if self.machine.mode() == 'wait':
return 'red'
if self.machine.mode() == 'repair':
return 'orange'
return ''
class MachineTextAnimate(sim.Animate):
def __init__(self, machine):
self.machine = machine
sim.Animate.__init__(self,
x0=10, y0=100 + self.machine.n * 30, text='', anchor='sw', font='narrow', fontsize0=15)
def text(self, t):
return '{} {:4d}'.format(self.machine.ident, self.machine.parts_made)
class MachineBarJobAnimate(sim.Animate):
def __init__(self, machine):
self.machine = machine
sim.Animate.__init__(self, rectangle0=(0, 0, 0, 0), linewidth0=0)
def rectangle(self, t):
d = self.machine.job_time
return(
100, 100 + self.machine.n * 30,
100 + d * SCALE, 100 + self.machine.n * 30 + 20)
def fillcolor(self, t):
if self.machine.mode() == 'work':
return ('green', 25)
if self.machine.mode() == 'wait':
return ('red', 25)
if self.machine.mode() == 'repair':
return ('orange', 25)
return ''
class RepairBlockAnimate(sim.Animate):
def __init__(self, i):
self.i = i
sim.Animate.__init__(self, y0=10, rectangle0=(0, 0, 20, 20), linecolor0='white')
def x(self, t):
return xrepairman(self.i, t)
def rectangle(self, t):
if self.i == -1:
if repairman.claimers()[0] is None:
d = 0
else:
d = repairman.claimers()[0].scheduled_time() - t
else:
if repairman.requesters()[self.i] is None:
d = 0
else:
if repairman.requesters()[self.i] == other:
d = repairman.requesters()[self.i].left_to_do
else:
d = repairman.requesters()[self.i].repair_time
return (0, 0, d * SCALE, 20)
def fillcolor(self, t):
if self.i == -1:
if repairman.claimers()[0] is None:
return ''
else:
return 'orange'
else:
if repairman.requesters()[self.i] is None:
return ''
else:
return 'red'
class RepairTextAnimate(sim.Animate):
def __init__(self, i):
self.i = i
sim.Animate.__init__(self, y0=10 + 3, text='',
textcolor0='white', font='narrow', fontsize0=15, anchor='sw')
def x(self, t):
return xrepairman(self.i, t) + 2
def text(self, t):
if self.i == -1:
if repairman.claimers()[0] is None:
return ''
else:
return repairman.claimers()[0].ident
else:
if repairman.requesters()[self.i] is None:
return ''
else:
return repairman.requesters()[self.i].ident
def xrepairman(i, t):
start = 0
if i != -1:
start += (repairman.claimers()[0].scheduled_time() - t)
for j in range(i):
if repairman.requesters()[j] is not None:
if repairman.requesters()[j] != other:
start += repairman.requesters()[j].repair_time
return 10 + start * SCALE
def do_animation():
env.animation_parameters(modelname='Machine shop', speed=4)
for machine in machines:
MachineBarAnimate(machine)
MachineTextAnimate(machine)
MachineBarJobAnimate(machine)
MachineBarAnimate(other)
MachineTextAnimate(other)
MachineBarJobAnimate(other)
for i in range(-1, NUM_MACHINES):
RepairBlockAnimate(i)
RepairTextAnimate(i)
def time_per_part():
"""Return actual processing time for a concrete part."""
return random.normalvariate(PT_MEAN, PT_SIGMA)
def time_to_failure():
"""Return time until next failure for a machine."""
return random.expovariate(BREAK_MEAN)
class Machine(sim.Component):
"""A machine produces parts and my get broken every now and then.
If it breaks, it requests a *repairman* and continues the production
after the it is repaired.
A machine has a *name* and a numberof *parts_made* thus far.
"""
def setup(self, n):
self.n = n
self.ident = str(n)
self.parts_made = 0
self.broken = False
self.disturber = Disturber(machine=self)
def process(self):
while True:
self.job_time = time_per_part()
self.left_to_do = self.job_time
while True:
yield self.hold(self.left_to_do, mode='work') # might be updated by disturber
if self.broken:
if repairman.claimers()[0] == other:
other.release()
other.activate()
self.repair_time = REPAIR_TIME
yield self.request((repairman, 1, 0), mode='wait')
yield self.hold(self.repair_time, mode='repair')
self.release()
self.broken = False
else:
break
self.parts_made += 1
class Disturber(sim.Component):
def setup(self, machine):
self.machine = machine
def process(self):
while True:
yield self.hold(time_to_failure())
if not self.machine.broken:
self.machine.broken = True
self.machine.left_to_do = self.machine.remaining_duration()
self.machine.activate()
class Other(sim.Component):
def setup(self):
self.n = -1
self.ident = 'X'
self.parts_made = 0
def process(self):
while True:
self.job_time = JOB_DURATION
self.left_to_do = self.job_time
while self.left_to_do > 1e-8:
yield self.request((repairman, 1, 1), mode='wait')
yield self.hold(self.left_to_do, mode='work')
other.release()
self.parts_made += 1
# Setup and start the simulation
print('Machine shop')
env = sim.Environment()
random.seed(RANDOM_SEED) # This helps reproducing the results
repairman = sim.Resource('repairman')
machines = [Machine(n=i) for i in range(NUM_MACHINES)]
other = Other(name='$')
# Execute!
do_animation()
env.run(till=SIM_TIME)
# Analyis/results
print('Machine shop results after %s weeks' % WEEKS)
for machine in machines:
print('%s made %d parts.' % (machine.name(), machine.parts_made))
repairman.print_statistics()