-
Notifications
You must be signed in to change notification settings - Fork 0
/
Systems.py
488 lines (413 loc) · 15.3 KB
/
Systems.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import random
from collections import defaultdict
class DifferentialDriveRobot:
def __init__(self, lower_speed_limit=0, upper_speed_limit=10, stochastic_fault_prob=None):
self.lower_speed_limit = lower_speed_limit
self.upper_speed_limit = upper_speed_limit
self.left_speed = 0
self.right_speed = 0
self.left_fault = None
self.right_fault = None
self.faults = ['left_faster', 'left_slower', 'left_stuck', 'right_faster', 'right_slower', 'right_stuck']
def reset_faults(self):
self.left_fault = None
self.right_fault = None
def change_speed(self, left_change=0, right_change=0):
left_change, right_change = self.speed_after_faults(left_change, right_change)
self.left_speed += left_change
self.right_speed += right_change
self.left_speed = max(self.lower_speed_limit, min(self.left_speed, self.upper_speed_limit))
self.right_speed = max(self.lower_speed_limit, min(self.right_speed, self.upper_speed_limit))
def get_heading_direction(self):
if self.left_speed > self.right_speed:
return 'RIGHT'
elif self.right_speed > self.left_speed:
return 'LEFT'
elif self.left_speed == 0 and self.right_speed == 0:
return 'STILL'
else:
return 'STRAIGHT'
def inject_fault(self, fault):
assert fault in self.faults
if 'left' in fault and self.left_fault is None:
self.left_fault = fault
return 'FaultInjected'
elif self.right_fault is None:
self.right_fault = fault
return 'FaultInjected'
return "-"
def speed_after_faults(self, left_speed, right_speed):
if 'left_faster' == self.left_speed:
left_speed += 2
if 'left_slower' == self.left_speed:
left_speed -= 2
if 'left_stuck' == self.left_speed:
left_speed = -self.left_speed
if 'right_faster' == self.right_fault:
right_speed += 2
if 'right_slower' == self.right_fault:
right_speed -= 2
if 'right_stuck' == self.right_fault:
right_speed = -self.right_speed
return left_speed, right_speed
class WindTurbine:
def __init__(self, spin_speed_limit=6):
self.state = "INIT"
self.spin_speed_limit = spin_speed_limit
self.spin_speed = 0
self.speed_increment = 1
self.faults = ['unexpected_speed_increase', 'unexpected_slow_down']
self.active_fault = False
def reset(self):
self.state = "INIT"
self.spin_speed = 0
self.speed_increment = 2
self.active_fault = False
def update_spin_speed(self):
self.spin_speed = min(self.spin_speed + self.speed_increment, self.spin_speed_limit)
def stop_turbine(self):
self.spin_speed = 0
def get_turbine_speed(self):
return self.spin_speed
def inject_fault(self, fault):
assert fault in self.faults
if self.active_fault:
return "-"
self.active_fault = True
if fault == 'unexpected_speed_increase':
self.speed_increment = min(self.speed_increment + 1, self.spin_speed_limit)
if self.spin_speed > 0:
self.update_spin_speed()
if fault == 'unexpected_slow_down':
self.speed_increment = max(self.speed_increment - 1, 0)
if self.spin_speed > 0:
self.update_spin_speed()
return self.get_turbine_speed()
class LightSwitch:
def __init__(self, goal=5):
self.timer = 0
self.delay = 0
self.time_goal = goal
def reset(self):
self.timer = 0
self.delay = 0
def press_switch(self):
if self.timer == self.time_goal:
return
self.timer += 1 - self.delay
self.timer = max(min(self.timer, self.time_goal), 0)
def increase_delay(self):
self.delay = 1
def fix_delay(self):
self.delay = 0
def get_status(self):
return "SHINING" if self.timer == self.time_goal else self.timer
class GearBox:
def __init__(self, num_gears=5):
self.gear = 1
self.num_gears = num_gears
self.faults = []
self.reverse_fault_counter = 0
self.clutch_pressed = False
self.gear_changed = False
def reset(self):
self.gear = 1
self.faults = []
self.reverse_fault_counter = 0
self.clutch_pressed = False
self.gear_changed = False
def press_clutch(self):
if not self.clutch_pressed:
self.clutch_pressed = True
return 'CLUTCH_PRESSED'
return 'NO_EFFECT'
def release_clutch(self):
if self.clutch_pressed:
self.clutch_pressed = False
self.gear_changed = False
return 'CLUTCH_RELEASED'
return 'NO_EFFECT'
def put_in_reverse(self):
if self.clutch_pressed and not self.gear_changed:
if self.gear == 1:
self.gear = -1
self.gear_changed = True
return self.gear
else:
self.reverse_fault_counter += 1
if self.reverse_fault_counter >= 2:
return "BROKEN"
return self.gear
return 'NO_EFFECT'
def increase_gear(self):
if self.clutch_pressed and not self.gear_changed:
self.gear = min(max(self.gear + 1, 1), self.num_gears)
self.gear_changed = True
return self.gear
return 'NO_EFFECT'
def decrease_gear(self):
if self.clutch_pressed and not self.gear_changed:
self.gear = max(self.gear - 1, 1)
self.gear_changed = True
return self.gear
return 'NO_EFFECT'
class VendingMachine:
def __init__(self):
self.money_counter = 0
self.coin_counter = defaultdict(int)
self.coins = {0.2, 0.5, 1}
self.products = {'coke', 'water', 'peanuts'}
def reset(self):
self.money_counter = 0
self.coin_counter.clear()
def add_coin(self, coin):
assert coin in self.coins
self.money_counter += coin
self.coin_counter[coin] += 1
if self.coin_counter[0.2] == 5:
self.money_counter = 2
return 'MAX_COINS_REACHED'
if self.money_counter > 2:
self.money_counter = 2
return 'MAX_COINS_REACHED'
return f'COIN_ADDED_{coin}'
def get_product(self, product):
assert product in self.products
if product == 'coke' and self.money_counter >= 1.5:
self.money_counter -= 1.5
return 'DROP_COKE'
elif product == 'water' and self.money_counter >= 0.5:
self.money_counter -= 0.5
return 'DROP_WATER'
elif product == 'peanuts' and self.money_counter >= 1:
self.money_counter -= 1
return 'DROP PEANUTS'
return 'INSUFFICIENT_COINS'
class Crossroad:
def __init__(self, max_car_limit=5):
self.directions = {'NS', 'EW'}
self.curr_dir = 'NS'
self.cars_ns = 0 # cars seen by traffic light
self.cars_ew = 0 # cars seen by traffic light
self.real_cars_ns = 0
self.real_cars_ew = 0
self.time_to_change = 0
self.scheduled_change = None
self.NS_traffic_sensor = False
self.NS_pedestrian = False
self.EW_traffic_sensor = False
self.EW_pedestrian = False
self.faulty_button = False
self.faulty_sensor_NS = False
self.faulty_sensor_EW = False
self.observable_cars = True
self.max_car_limit = max_car_limit
def pedestrian_button(self, direction):
assert direction in self.directions
if direction == 'NS' and not self.faulty_button:
self.NS_pedestrian = True
elif direction == 'EW' and not self.faulty_button:
self.EW_pedestrian = True
return self.change_traffic_lights()
def car_arriving(self, direction):
assert direction in self.directions
if direction == 'NS':
if not self.faulty_sensor_NS:
self.NS_traffic_sensor = True
self.cars_ns += 1 # sensor detecting arriving cars is faulty
self.real_cars_ns += 1
elif direction == 'EW':
if not self.faulty_sensor_EW:
self.EW_traffic_sensor = True
self.cars_ew += 1
self.real_cars_ew += 1
return self.change_traffic_lights()
def waiting(self):
self.update()
if self.cars_ew == 0 and self.cars_ns > 0 and not self.scheduled_change:
# self.scheduled_change = 'NS'
# self.time_to_change = 2
self.curr_dir = 'NS'
elif self.cars_ns == 0 and self.cars_ew > 0 and not self.scheduled_change:
# self.scheduled_change = 'EW'
# self.time_to_change = 2
self.curr_dir = 'EW'
return self.curr_dir + self.traffic_state()
def change_traffic_lights(self):
# IN case that both NS and EW are active, NS has advantage
if self.NS_pedestrian:
self.NS_pedestrian = False
if self.curr_dir == 'NS':
pass
elif not self.scheduled_change:
self.scheduled_change = 'NS'
self.time_to_change = 2
elif self.EW_pedestrian:
self.EW_pedestrian = False
if self.curr_dir == 'EW':
pass
elif not self.scheduled_change:
self.scheduled_change = 'EW'
self.time_to_change = 2
elif self.NS_traffic_sensor:
self.NS_traffic_sensor = False
if self.curr_dir == 'NS':
pass
elif not self.scheduled_change and not self.cars_ew > self.cars_ns:
self.scheduled_change = 'NS'
self.time_to_change = 3
elif self.EW_traffic_sensor:
self.EW_traffic_sensor = False
if self.curr_dir == 'EW':
pass
elif not self.scheduled_change and not self.cars_ns > self.cars_ew:
self.scheduled_change = 'EW'
self.time_to_change = 3
self.update()
return self.curr_dir + self.traffic_state()
def traffic_state(self):
# nr. of should never be > 5, so we signal a jam if nr. of cars > 6
if self.observable_cars:
state = "jam_ns" if self.real_cars_ns > 5 else str(self.real_cars_ns)
state += ":"
state += "jam_ew" if self.real_cars_ew > 5 else str(self.real_cars_ew)
else:
state = "jam_ns" if self.real_cars_ns > 5 else ""
state += ":" if self.real_cars_ns > 5 and self.real_cars_ew > 5 else ""
state += "jam_ew" if self.real_cars_ew > 5 else ""
return state
def update(self):
if self.curr_dir == 'EW':
self.cars_ew = max(0, self.cars_ew - 1)
self.real_cars_ew = max(0, self.real_cars_ew - 1)
else:
self.cars_ns = max(0, self.cars_ns - 1)
self.real_cars_ns = max(0, self.real_cars_ns - 1)
if self.scheduled_change:
self.time_to_change -= 1
if self.time_to_change <= 0:
self.curr_dir = self.scheduled_change
self.scheduled_change = None
self.time_to_change = 0
def inject_fault_in_sensor_ns(self):
if not self.faulty_sensor_EW and not self.faulty_sensor_NS:
self.faulty_sensor_NS = True
return "None"
def inject_fault_in_sensor_ew(self):
if not self.faulty_sensor_EW and not self.faulty_sensor_NS:
self.faulty_sensor_EW = True
return "None"
def inject_fault_in_button(self):
self.faulty_button = True
return "None"
class StochasticLightSwitch:
def __init__(self):
self.counter = 0
self.fault_activated = False
def reset(self):
self.counter = 0
self.fault_activated = False
def release(self):
if self.fault_activated:
return 'SHINING'
else:
self.counter = 0
return 'OFF'
def press(self):
self.counter = min(self.counter + 1, 5)
if self.counter == 2:
if random.random() <= 0.2:
self.fault_activated = True
return 'SHINING'
if self.fault_activated:
return 'SHINING'
if self.counter == 5:
if random.random() >= 0.1:
return 'OFF'
return 'SHINING'
return 'OFF'
class DeterministicCoffeeMachine:
def __init__(self, inject_fault=True):
self.counter = 0
self.inject_fault = inject_fault
def add_coin(self):
if self.counter == 3:
return 'CoinsFull'
self.counter = min(self.counter + 1, 3)
return 'CoinAdded'
def button(self):
output = 'Coffee' if self.counter >= 2 else 'NoAction'
if self.counter == 3:
if self.inject_fault:
self.counter -= 1 if self.counter == 3 else 2
elif self.counter == 2:
self.counter -= 2
return output
class DeterministicCoffeeMachineDFA:
def __init__(self):
self.counter = 0
self.correct_counter = 0
def add_coin(self):
if self.counter == 3:
return False
self.counter = min(self.counter + 1, 3)
self.correct_counter = min(self.correct_counter + 1, 3)
return False
def button(self):
property_violation = self.counter >= 2 and self.correct_counter < 2
if self.counter == 3:
self.counter -= 1
self.correct_counter = max(self.correct_counter - 2, 0)
elif self.correct_counter == 2:
self.counter -= 2
self.correct_counter = max(self.correct_counter - 2, 0)
return property_violation
class StochasticCoffeeMachine:
def __init__(self):
self.counter = 0
def add_coin(self):
if self.counter == 3:
if random.random() >= 0.2:
return 'CoinsFull'
else:
self.counter = 0
return 'ReturnCoins'
self.counter = min(self.counter + 1, 3)
return 'CoinAdded'
def button(self):
if self.counter >= 2:
self.counter -= 2
return 'Coffee'
else:
if random.random() <= 0.02:
self.counter = 0
return 'Coffee'
return 'NoAction'
class DeterministicFaultInjectedCoffeeMachine:
def __init__(self):
self.counter = 0
self.fault = None
self.possible_faults = ['coin_double_value', 'button_no_effect']
def inject_fault(self, fault):
assert fault in self.possible_faults
if self.fault:
return 'False'
else:
self.fault = fault
return 'True'
def add_coin(self):
if self.counter == 3:
return 'CoinsFull'
if self.fault == 'coin_double_value':
self.counter = min(self.counter + 2, 3)
else:
self.counter = min(self.counter + 1, 3)
return 'CoinAdded'
def button(self):
if self.fault == 'button_no_effect':
return 'NoAction'
if self.counter >= 2:
self.counter -= 2
return 'Coffee'
else:
return 'NoAction'