Example #1
0
def test_attractor():
    g = TransitionSystem()
    g.vars['x'] = 'bool'
    g.env_vars.add('x')
    g.add_path([0, 1, 2, 3])
    g.add_edge(4, 1, formula='x')
    aut = logicizer.graph_to_logic(g, 'loc', True)
    aut.plus_one = True
    aut.moore = True
    aut.build()
    target = aut.add_expr('loc = 2')
    u = fx.attractor(aut.action['env'], aut.action['sys'], target, aut)
    ok = {0: True, 1: True, 2: True, 3: False, 4: False}
    for q, value in ok.items():
        subs = dict(loc=q)
        v = aut.let(subs, u)
        assert (v == aut.true) == value, v
    inside = aut.add_expr('loc > 0')
    u = fx.attractor(aut.action['env'],
                     aut.action['sys'],
                     target,
                     aut,
                     inside=inside)
    ok = {0: False, 1: True, 2: True, 3: False, 4: False}
    for q, value in ok.items():
        subs = dict(loc=q)
        v = aut.let(subs, u)
        assert (v == aut.true) == value, v
Example #2
0
def counter_example():
    g = TransitionSystem()
    g.add_path([0, 1, 2, 3, 4])
    g.add_path([5, 6, 7, 0])
    g.add_edge(4, 5, formula="x")
    g.add_edge(4, 1, formula="!x")
    g.add_edge(1, 0)
    g.add_edge(3, 2)
    g.vars = dict(x='bool')
    g.env_vars.add('x')
    # g.dump('sys_graph_2.pdf')
    #
    aut_g = graph_to_logic(g, 'pc', ignore_initial=True)
    #
    aut = Automaton()
    aut.players = dict(alice=0, bob=1)
    aut.vars = dict(
        pc=dict(type='saturating', dom=(0, 7), owner='alice'),
        x=dict(type='bool', owner='bob'),
        _i=dict(type='saturating', dom=(0, 1), owner='alice'))
    aut.action['alice'] = [aut_g.action['sys'][0]]
    aut.win['alice: []<>'] = ['pc = 6']
    aut.win['bob: []<>'] = ['True']
    fill_blanks(aut)
    print(aut)
    aut = aut.build()
    make_assumptions(aut)
Example #3
0
def test_attractor():
    g = TransitionSystem()
    g.vars['x'] = 'bool'
    g.env_vars.add('x')
    g.add_path([0, 1, 2, 3])
    g.add_edge(4, 1, formula='x')
    aut = logicizer.graph_to_logic(g, 'loc', True)
    aut.plus_one = True
    aut.moore = True
    aut.build()
    target = aut.add_expr('loc = 2')
    u = fx.attractor(aut.action['env'], aut.action['sys'],
                     target, aut)
    ok = {0: True, 1: True, 2: True, 3: False, 4: False}
    for q, value in ok.items():
        subs = dict(loc=q)
        v = aut.let(subs, u)
        assert (v == aut.true) == value, v
    inside = aut.add_expr('loc > 0')
    u = fx.attractor(aut.action['env'], aut.action['sys'],
                     target, aut, inside=inside)
    ok = {0: False, 1: True, 2: True, 3: False, 4: False}
    for q, value in ok.items():
        subs = dict(loc=q)
        v = aut.let(subs, u)
        assert (v == aut.true) == value, v
def counter_example():
    g = TransitionSystem()
    g.add_path([0, 1, 2, 3, 4])
    g.add_path([5, 6, 7, 0])
    g.add_edge(4, 5, formula="x")
    g.add_edge(4, 1, formula="~ x")
    g.add_edge(1, 0)
    g.add_edge(3, 2)
    g.vars = dict(x='bool')
    g.env_vars.add('x')
    # g.dump('sys_graph_2.pdf')
    #
    aut_g = graph_to_logic(g, 'pc', ignore_initial=True)
    #
    aut = sym.Automaton()
    aut.players = dict(alice=0, bob=1)
    table = dict(pc=dict(type='saturating', dom=(0, 7), owner='alice'),
                 x=dict(type='bool', owner='bob'),
                 _i=dict(type='saturating', dom=(0, 1), owner=None))
    aut.add_vars(table, flexible=True)
    aut.varlist = dict(alice=['pc'], bob=['x'])
    aut.init_expr = dict(alice='TRUE', bob='TRUE')
    aut.action_expr = dict(alice=aut_g.action['sys'][0], bob='TRUE')
    aut.win_expr = dict(alice={'[]<>': ['pc = 6']}, bob={'[]<>': ['True']})
    # TODO: sym.fill_blanks(aut)
    print(aut)
    aut.build()
    make_assumptions(aut)
Example #5
0
def test_logicizer_env():
    g = TransitionSystem()
    g.vars['x'] = 'bool'
    g.owner = 'env'
    g.add_edge(0, 1, x=True)
    g.add_edge(1, 2, formula="x'")
    g.add_edge(2, 1)
    (env_init, env_act, sys_init,
     sys_act) = logicizer._graph_to_formulas(g,
                                             nodevar='k',
                                             ignore_initial=True,
                                             self_loops=True)
    s = stx.conj(env_act)
    e1 = "(((((k = 0)) => (((x <=> True)) /\ ((k' = 1)))) \n"
    e2 = "(((((k = 0)) => (((k' = 1)) /\ ((x <=> True)))) \n"
    assert s.startswith(e1) or s.startswith(e2), s
    e3 = ("/\ (((k = 1)) => ((x') /\ ((k' = 2))))) \n"
          "/\ (((k = 2)) => ((k' = 1)))) \/ (k' = k)")
    assert s.endswith(e3), s
    # test automaton
    aut = logicizer.graph_to_logic(g, nodevar='k', ignore_initial=True)
    assert 'x' in aut.vars, aut.vars
    assert 'k' in aut.vars, aut.vars
    xtype = aut.vars['x']['type']
    ktype = aut.vars['k']['type']
    assert xtype == 'bool', xtype
    assert ktype == 'int', ktype
Example #6
0
def test_descendants():
    g = TransitionSystem()
    g.add_path([0, 1, 2])
    aut = logicizer.graph_to_logic(g, 'pc', True)
    source = aut.add_expr('pc = 0')
    constrain = aut.true
    v = fx.descendants(source, constrain, aut)
    assert v == aut.add_expr('pc_0 <=> ~ pc_1'), _to_expr(v, aut)
Example #7
0
def test_descendants():
    g = TransitionSystem()
    g.add_path([0, 1, 2])
    aut = logicizer.graph_to_logic(g, 'pc', True)
    source = aut.add_expr('pc = 0')
    constrain = aut.true
    v = fx.descendants(source, constrain, aut)
    assert v == aut.add_expr('pc_0 <=> ~ pc_1'), _to_expr(v, aut)
Example #8
0
def test_ee_image_only_sys():
    g = TransitionSystem()
    g.add_path([0, 1, 2])
    aut = logicizer.graph_to_logic(g, 'pc', True)
    u = aut.add_expr('pc = 0')
    v = fx.ee_image(u, aut)
    v_ = aut.add_expr('pc = 1')
    assert v == v_, _to_expr(v, aut)
    v = fx.ee_image(v, aut)
    v_ = aut.add_expr('pc = 2')
    assert v == v_, _to_expr(v, aut)
Example #9
0
def test_ee_image_only_sys():
    g = TransitionSystem()
    g.add_path([0, 1, 2])
    aut = logicizer.graph_to_logic(g, 'pc', True)
    u = aut.add_expr('pc = 0')
    v = fx.ee_image(u, aut)
    v_ = aut.add_expr('pc = 1')
    assert v == v_, _to_expr(v, aut)
    v = fx.ee_image(v, aut)
    v_ = aut.add_expr('pc = 2')
    assert v == v_, _to_expr(v, aut)
Example #10
0
def test_ee_image():
    g = TransitionSystem()
    g.vars = dict(x='bool')
    g.env_vars = {'x'}
    g.add_edge(0, 1, formula='x')
    g.add_edge(0, 1, formula=' ~ x')
    g.add_edge(0, 2, formula='x')
    aut = logicizer.graph_to_logic(g, 'pc', True)
    source = aut.add_expr('pc = 0')
    u = fx.ee_image(source, aut)
    u_ = aut.add_expr('(pc = 1) \/ (pc = 2)')
    assert u == u_, _to_expr(u, aut)
Example #11
0
def test_ee_image():
    g = TransitionSystem()
    g.vars = dict(x='bool')
    g.env_vars = {'x'}
    g.add_edge(0, 1, formula='x')
    g.add_edge(0, 1, formula=' ~ x')
    g.add_edge(0, 2, formula='x')
    aut = logicizer.graph_to_logic(g, 'pc', True)
    source = aut.add_expr('pc = 0')
    u = fx.ee_image(source, aut)
    u_ = aut.add_expr('(pc = 1) \/ (pc = 2)')
    assert u == u_, _to_expr(u, aut)
Example #12
0
def grid_world_example():
    # robot a
    a = TransitionSystem()
    a.add_path([0, 1, 2, 3, 4, 5, 0])
    a.initial_nodes.add(4)
    n_a = len(a) - 1
    aut_a = graph_to_logic(a, 'a', ignore_initial=False, self_loops=True)
    # robot b
    b = TransitionSystem()
    b.add_path([5, 4, 3, 2, 1, 0, 5])
    b.add_path([2, 6, 2])
    b.add_path([0, 7, 0])
    b.initial_nodes.add(0)
    n_b = len(b) - 1
    aut_b = graph_to_logic(b, 'b', ignore_initial=False, self_loops=True)
    # interleaving repr
    aut = Automaton()
    aut.players = dict(car_a=0, car_b=1)
    n = len(aut.players)
    aut.vars = dict(
        a=dict(type='saturating', dom=(0, n_a), owner='car_a'),
        b=dict(type='saturating', dom=(0, n_b), owner='car_b'),
        _i=dict(type='saturating', dom=(0, n - 1), owner=None))
    aut.init['car_a'] = [aut_a.init['sys'][0]]
    aut.init['car_b'] = [aut_b.init['sys'][0]]
    aut.action['car_a'] = [aut_a.action['sys'][0]]
    aut.action['car_b'] = [aut_b.action['sys'][0]]
    # avoid collisions
    s = "(a != b) & (a' != b)"
    # & ((a = 4) -> (a' != 4))
    aut.action['car_a'].append(s)
    s = "(a != b) & (a != b')"
    # & ((a = 4) -> (b = 1))
    aut.action['car_b'].append(s)
    aut.win['car_a: []<>'] = ['(a = 4)', '(b = 1)']
    aut.win['car_b: []<>'] = ['True']
    fill_blanks(aut)
    print(aut)
    aut = aut.build()
    make_assumptions(aut)
Example #13
0
def test_ue_image_no_constrain():
    g = TransitionSystem()
    g.vars = dict(x='bool')
    g.env_vars = {'x'}
    g.add_edge(0, 1, formula='x')
    g.add_edge(0, 2, formula=' ~ x')
    aut = logicizer.graph_to_logic(g, 'pc', True)
    # source constrained to x
    source = aut.add_expr('x /\ (pc = 0)')
    u = fx.ee_image(source, aut)
    assert u == aut.add_expr('pc = 1')
    # source contains both x and ~ x
    source = aut.add_expr('pc = 0')
    u = fx.ee_image(source, aut)
    assert u == aut.add_expr('(pc = 1) \/ (pc = 2)')
Example #14
0
def test_ue_image_no_constrain():
    g = TransitionSystem()
    g.vars = dict(x='bool')
    g.env_vars = {'x'}
    g.add_edge(0, 1, formula='x')
    g.add_edge(0, 2, formula=' ~ x')
    aut = logicizer.graph_to_logic(g, 'pc', True)
    # source constrained to x
    source = aut.add_expr('x /\ (pc = 0)')
    u = fx.ee_image(source, aut)
    assert u == aut.add_expr('pc = 1')
    # source contains both x and ~ x
    source = aut.add_expr('pc = 0')
    u = fx.ee_image(source, aut)
    assert u == aut.add_expr('(pc = 1) \/ (pc = 2)')
Example #15
0
def semi_symbolic():
    """Example using a semi-enumerated state machine.

    Instructive variants:

    - `formula = "x'"`
    - `self_loops = True`
    - `aut.moore = False`
    """
    g = TransitionSystem()
    g.owner = 'sys'
    g.vars = dict(x='bool')
    g.env_vars.add('x')
    g.add_path(range(11))
    g.add_edge(10, 10)
    g.add_edge(10, 0, formula="x")
    # symbolic
    aut = logicizer.graph_to_logic(
        g, 'nd', ignore_initial=True, self_loops=False)
    aut.init['env'] = 'nd = 1'
    aut.win['<>[]'] = aut.bdds_from(' ~ x')
    aut.win['[]<>'] = aut.bdds_from('nd = 0')
    aut.qinit = '\A \A'
    aut.moore = True
    aut.plus_one = True
    print(aut)
    # compile to BDD
    z, yij, xijk = gr1.solve_streett_game(aut)
    gr1.make_streett_transducer(z, yij, xijk, aut)
    # print t.bdd.to_expr(t.action['sys'][0])
    r = aut.action['sys']
    # aut.bdd.dump('bdd.pdf', roots=[r])
    g = enumerate_controller(aut)
    h, _ = sym_enum._format_nx(g)
    pd = nx.drawing.nx_pydot.to_pydot(h)
    pd.write_pdf('game_states.pdf')
    print('Enumerated strategy has {n} nodes.'.format(
        n=len(g)))
    print(('Winning set:', aut.bdd.to_expr(z)))
    print('{n} BDD nodes in total'.format(
        n=len(aut.bdd)))
Example #16
0
def semi_symbolic():
    """Example using a semi-enumerated state machine.

    Instructive variants:

    - `formula = "x'"`
    - `self_loops = True`
    - `aut.moore = False`
    """
    g = TransitionSystem()
    g.owner = 'sys'
    g.vars = dict(x='bool')
    g.env_vars.add('x')
    nx.add_path(g, range(11))
    g.add_edge(10, 10)
    g.add_edge(10, 0, formula="x")
    # symbolic
    aut = logicizer.graph_to_logic(g,
                                   'nd',
                                   ignore_initial=True,
                                   self_loops=False)
    aut.init['env'] = 'nd = 1'
    aut.win['<>[]'] = aut.bdds_from(' ~ x')
    aut.win['[]<>'] = aut.bdds_from('nd = 0')
    aut.qinit = '\A \A'
    aut.moore = True
    aut.plus_one = True
    print(aut)
    # compile to BDD
    z, yij, xijk = gr1.solve_streett_game(aut)
    gr1.make_streett_transducer(z, yij, xijk, aut)
    # print t.bdd.to_expr(t.action['sys'][0])
    r = aut.action['sys']
    # aut.bdd.dump('bdd.pdf', roots=[r])
    g = enumerate_controller(aut)
    h, _ = sym_enum._format_nx(g)
    pd = nx.drawing.nx_pydot.to_pydot(h)
    pd.write_pdf('game_states.pdf')
    print('Enumerated strategy has {n} nodes.'.format(n=len(g)))
    print(('Winning set:', aut.bdd.to_expr(z)))
    print('{n} BDD nodes in total'.format(n=len(aut.bdd)))
def gridworld_example():
    # car a
    a = TransitionSystem()
    a.add_path([0, 1, 2, 3, 4, 5, 0])
    a.initial_nodes.add(4)
    n_a = len(a) - 1
    aut_a = graph_to_logic(a, 'a', ignore_initial=False, self_loops=True)
    # car b
    b = TransitionSystem()
    b.add_path([5, 4, 3, 2, 1, 0, 5])
    b.add_path([2, 6, 2])
    b.add_path([0, 7, 0])
    b.initial_nodes.add(0)
    n_b = len(b) - 1
    aut_b = graph_to_logic(b, 'b', ignore_initial=False, self_loops=True)
    # interleaving repr
    aut = sym.Automaton()
    aut.players = dict(car_a=0, car_b=1)
    n = len(aut.players)
    table = dict(a=dict(type='int', dom=(0, n_a), owner='car_a'),
                 b=dict(type='int', dom=(0, n_b), owner='car_b'),
                 _i=dict(type='int', dom=(0, n - 1), owner='scheduler'))
    aut.add_vars(table, flexible=True)
    aut.varlist['car_a'] = ['a']
    aut.varlist['car_b'] = ['b']
    s = r'''
        InitA == ({init_a}) /\ (a \in 0 .. {n_a})
        InitB == ({init_b}) /\ (b \in 0 .. {n_b})

        NextA ==
            /\ ({action_a})
               # type invariance action
            /\ a \in 0 .. {n_a}
            /\ a' \in 0 .. {n_a}
               # avoid collisions
            /\ (a != b) /\ (a' != b)
            /\ ((a = 4) => (a' != 4))

        NextB ==
            /\ ({action_b})
               # type invariance action
            /\ b \in 0 .. {n_b}
            /\ b' \in 0 .. {n_b}
               # avoid collisions
            /\ (a != b) /\ (a != b')

        RecurA1 == (a = 4)
        RecurA2 == (b = 1)
        RecurB == True
        '''.format(n_a=n_a,
                   n_b=n_b,
                   init_a=aut_a.init['sys'][0],
                   init_b=aut_b.init['sys'][0],
                   action_a=aut_a.action['sys'][0],
                   action_b=aut_b.action['sys'][0])
    aut.define(s)
    aut.init_expr = dict(car_a='InitA', car_b='InitB')
    aut.action_expr = dict(car_a='NextA', car_b='NextB')
    aut.win_expr = dict(car_a={'[]<>': ['RecurA1', 'RecurA2']},
                        car_b={'[]<>': ['RecurB']})
    print(aut)
    aut.build()
    make_assumptions(aut)