Exemple #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
Exemple #2
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
Exemple #3
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)')
Exemple #4
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)')
Exemple #5
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)))
Exemple #6
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)))
Exemple #7
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)
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)
Exemple #9
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
Exemple #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)
Exemple #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)