|
| 1 | +import numpy as np |
| 2 | +import time |
| 3 | + |
| 4 | + |
| 5 | +class MACHINE_SIMULATOR: |
| 6 | + |
| 7 | + def __init__(self): |
| 8 | + self.state = 'IDLE' |
| 9 | + self.prev_state = 'WORK' |
| 10 | + self.ttf = 0 |
| 11 | + self.tts = 0 |
| 12 | + self.ttr = 0 |
| 13 | + self.mtta = 0 |
| 14 | + |
| 15 | + def schedule(self, event_name, event_value, params_data, lim_anom, params_mtbf, params_mtts, params_mttr, delay): |
| 16 | + if event_name == 'INIT': |
| 17 | + # init the states |
| 18 | + self.state = 'IDLE' |
| 19 | + self.prev_state = 'WORK' |
| 20 | + # computes (gets from the distribution) the ttf |
| 21 | + self.ttf = self.generate_time(params_mtbf) |
| 22 | + self.mtta = self.ttf*lim_anom |
| 23 | + return [event_value, None, '', self.state] |
| 24 | + |
| 25 | + elif event_name == 'READ': |
| 26 | + # checks the state |
| 27 | + if self.state == 'IDLE': |
| 28 | + time.sleep(delay) |
| 29 | + return [None, None, '', self.state] |
| 30 | + |
| 31 | + elif self.state == 'WORK': |
| 32 | + # wait some time (performs the work) |
| 33 | + time.sleep(delay) |
| 34 | + # computes (decreases) the current TTF |
| 35 | + self.ttf -= delay |
| 36 | + # checks if is 0 -> failure |
| 37 | + if self.ttf <= 0: |
| 38 | + # computes (gets from the distribution) the tts (time to start the repair) |
| 39 | + self.tts = self.generate_time(params_mtts) |
| 40 | + self.state = 'BREAK' |
| 41 | + return [None, event_value, '', self.state] |
| 42 | + # normal operation |
| 43 | + else: |
| 44 | + # generates data |
| 45 | + anom = 1 if self.ttf <= self.mtta else 0 |
| 46 | + data_str = self.generate_data(params_data, anom) |
| 47 | + return [None, event_value, data_str, self.state] |
| 48 | + |
| 49 | + elif self.state == 'BREAK': |
| 50 | + # simulates the waiting time to repair |
| 51 | + time.sleep(delay) |
| 52 | + # computes (decreases) the current TTF |
| 53 | + self.tts -= delay |
| 54 | + # checks if is 0 -> starts repairing |
| 55 | + if self.tts <= 0: |
| 56 | + # computes (gets from the distribution) the ttr (time to repair) |
| 57 | + self.ttr = self.generate_time(params_mttr) |
| 58 | + self.state = 'REPAIR' |
| 59 | + return [None, event_value, '', self.state] |
| 60 | + # break state |
| 61 | + else: |
| 62 | + return [None, event_value, '', self.state] |
| 63 | + |
| 64 | + elif self.state == 'REPAIR': |
| 65 | + # simulates the waiting time to repair |
| 66 | + time.sleep(delay) |
| 67 | + # computes (decreases) the current TTF |
| 68 | + self.ttr -= delay |
| 69 | + # checks if is 0 -> starts repairing |
| 70 | + if self.ttr <= 0: |
| 71 | + # computes (gets from the distribution) the ttr (time to repair) |
| 72 | + self.ttf = self.generate_time(params_mtbf) |
| 73 | + self.mtta = self.ttf * lim_anom |
| 74 | + self.state = 'WORK' |
| 75 | + return [None, event_value, '', self.state] |
| 76 | + # break state |
| 77 | + else: |
| 78 | + return [None, event_value, '', self.state] |
| 79 | + |
| 80 | + elif event_name == 'ON-OFF': |
| 81 | + # switches the button |
| 82 | + if self.state == 'IDLE': |
| 83 | + # updates the data |
| 84 | + self.state = self.prev_state |
| 85 | + return [None, event_value, '', self.state] |
| 86 | + else: |
| 87 | + self.prev_state = self.state |
| 88 | + self.state = 'IDLE' |
| 89 | + return [None, None, '', self.state] |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def generate_data(params_str, anom): |
| 93 | + params = list(eval(params_str)) |
| 94 | + # generates the values |
| 95 | + sample = [] |
| 96 | + for norm_param, anom_param in params: |
| 97 | + # checks if an anomaly to generate |
| 98 | + mu, std = norm_param if anom == 0 else anom_param |
| 99 | + # generates the value |
| 100 | + value = round(np.random.normal(mu, std), 3) |
| 101 | + # stores the value |
| 102 | + sample.append(value) |
| 103 | + # appends the anom at the end and converts it to string |
| 104 | + sample.append(anom) |
| 105 | + data_str = ','.join(['{0}'.format(x) for x in sample]) |
| 106 | + return data_str |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def generate_time(params_str): |
| 110 | + params = list(eval(params_str)) |
| 111 | + t = round(np.random.normal(params[0], params[1])) |
| 112 | + return t |
0 commit comments