def markov_example(): """ Describe the system as a Markov Process. states explanations : 0 : A and B working 1 : A working and B not working 2 : A not working and B working 3 : neither A nor B working """ A = Component('A', 1e-4, 1.1e-3) B = Component('B', 4e-4, 1.4e-3) components = (A, B) initstates = {0: 0.8, 1: 0.1, 2: 0.1} process = Markovprocess(components, initstates) timerange = range(0, 5000, 10) states = {u'nominal' : lambda x: all(x), u'dégradé' : lambda x: not(all(x)) and any(x), #at least one but all u'défaillant' : lambda x: not(x[0] or x[1]), #none u'disponible' : lambda x: any(x), #at least one } for name, state in states.iteritems(): data = [process.value(t, statefunc=state) for t in timerange] p.plot(timerange, data, label=name) p.legend() p.show()
def setUp(self): #Let’s build some standard systems systems = { 'series-parallel': System(), 'parallel-series': System(), 'simple': System() } lambdas = {'alim': 1e-4, 'motor': 2e-5} mus = {'alim': 5e-4, 'motor': 2e-3} alim = [ Component('Alim_A', lambda_=lambdas['alim'], mu=mus['alim']), Component('Alim_B', lambda_=lambdas['alim'], mu=mus['alim']), ] motors = [ Component('Motor_A', lambda_=lambdas['motor'], mu=mus['motor']), Component('Motor_B', lambda_=lambdas['motor'], mu=mus['motor']), ] systems['simple']['E'] = alim[0] systems['simple'][alim[0]] = motors[0] systems['simple'][motors[0]] = 'S' systems['series-parallel']['E'] = [alim[0], alim[1]] systems['series-parallel'][alim[0]] = motors[0] systems['series-parallel'][alim[1]] = motors[1] systems['series-parallel'][motors[0]] = 'S' systems['series-parallel'][motors[1]] = 'S' systems['parallel-series']['E'] = [alim[0], alim[1]] systems['parallel-series'][alim[0]] = [motors[0], motors[1]] systems['parallel-series'][alim[1]] = [motors[0], motors[1]] systems['parallel-series'][motors[0]] = 'S' systems['parallel-series'][motors[1]] = 'S' #Let’s build the markov equivalent system self.components = (alim[0], alim[1], motors[0], motors[1]) self.process = Markovprocess(self.components, {0: 1}) #All the components work self.states = { 'series-parallel': lambda x: (x[0] * x[2]) + (x[1] * x[3]), 'parallel-series': lambda x: (x[0] + x[1]) * (x[2] + x[3]), 'simple': lambda x: x[0] * x[2], } self.systems = systems
S=(machineA, machineB, machineC) def normal(x) : return all(x) def available(x) : return ((x[0] and x[1]) or(x[2] and x[1]) or (x[0]and x[2])) def damaged(x) : return available(x) and not(normal(x)) def dead(x) : return not available(x) initstates = {0:1} process = Markovprocess(S, initstates) print(process.value(5000, available)) #0.957231110811 import pylab as p states = { u'available': available, } timerange = range(0, 5000, 10) for (name, func) in states.iteritems(): proba = [process.value(t, func) for t in timerange] p.plot(timerange, proba, label=name)
def available(x): return ((x[0] and x[1]) or (x[2] and x[1]) or (x[0] and x[2])) def damaged(x): return available(x) and not (normal(x)) def dead(x): return not available(x) initstates = {0: 1} process = Markovprocess(S, initstates) print(process.value(5000, available)) #0.957231110811 import pylab as p states = { u'available': available, } timerange = range(0, 5000, 10) for (name, func) in states.iteritems(): proba = [process.value(t, func) for t in timerange] p.plot(timerange, proba, label=name) p.legend() p.show()
import fiabilipy from fiabilipy import Component, Markovprocess A0, A1, A2 = [ Component('A{}'.format(i), 1e-4, 1.1e-3, 1.1e-3) for i in range(3) ] M0, M1, M2 = [ Component('M{}'.format(i), 1e-3, 1.2e-2, 1.2e-3) for i in range(3) ] compA = (A0, A1, A2) compM = (M0, M1, M2) components = (compA + compM) initstates = {0: 0.9, 1: 0.1} process = Markovprocess(components, initstates) def normal(x): return all(x) def available(x): return (x[0] or x[1] or x[2]) and (x[3] or x[4] or x[5]) def damaged(x): return available(x) and not (normal(x)) def faulty(x): return not available(x)
class TestMarkov(unittest2.TestCase): #The reliabily and the availability of systems is already tested. Let’s #assume they are correct for any systems. #The idea behind those tests is very simple. We build different systems twice : # - one with the “standard” way, by describing a system by its reliabily block # diagram. # - one with a Markov process, which is going to be tested #once that is done, the availabilities computed have just to be compared each #others. It a different result is found, then there is a bug (or more…) def setUp(self): #Let’s build some standard systems systems = { 'series-parallel': System(), 'parallel-series': System(), 'simple': System() } lambdas = {'alim': 1e-4, 'motor': 2e-5} mus = {'alim': 5e-4, 'motor': 2e-3} alim = [ Component('Alim_A', lambda_=lambdas['alim'], mu=mus['alim']), Component('Alim_B', lambda_=lambdas['alim'], mu=mus['alim']), ] motors = [ Component('Motor_A', lambda_=lambdas['motor'], mu=mus['motor']), Component('Motor_B', lambda_=lambdas['motor'], mu=mus['motor']), ] systems['simple']['E'] = alim[0] systems['simple'][alim[0]] = motors[0] systems['simple'][motors[0]] = 'S' systems['series-parallel']['E'] = [alim[0], alim[1]] systems['series-parallel'][alim[0]] = motors[0] systems['series-parallel'][alim[1]] = motors[1] systems['series-parallel'][motors[0]] = 'S' systems['series-parallel'][motors[1]] = 'S' systems['parallel-series']['E'] = [alim[0], alim[1]] systems['parallel-series'][alim[0]] = [motors[0], motors[1]] systems['parallel-series'][alim[1]] = [motors[0], motors[1]] systems['parallel-series'][motors[0]] = 'S' systems['parallel-series'][motors[1]] = 'S' #Let’s build the markov equivalent system self.components = (alim[0], alim[1], motors[0], motors[1]) self.process = Markovprocess(self.components, {0: 1}) #All the components work self.states = { 'series-parallel': lambda x: (x[0] * x[2]) + (x[1] * x[3]), 'parallel-series': lambda x: (x[0] + x[1]) * (x[2] + x[3]), 'simple': lambda x: x[0] * x[2], } self.systems = systems def test_availability(self): #Let’s do `maxiter` checks of availability values, for times randomly #picked between [0, `maxtime`) maxiter = 1000 maxtime = 420000 for _ in range(maxiter): t = random() * maxtime for name, states in self.states.items(): self.assertAlmostEqual(self.process.value(t, states), self.systems[name].availability(t))