Beispiel #1
0
def test_action_to_steps():
    aut = symbolic.Automaton()
    aut.declare_variables(x='bool', y=(0, 17))
    aut.varlist['env'] = ['x']
    aut.varlist['sys'] = ['y']
    keys = ('x', 'y')
    aut.init['env'] = aut.add_expr('x /\ (y = 1)')
    aut.action['sys'] = aut.add_expr("y' /= y")
    aut.action['env'] = aut.true
    aut.qinit = '\A \A'
    aut.moore = True
    aut.build()
    g = enum.action_to_steps(aut, qinit=aut.qinit)
    # 36 states reachable, but should enumerate fewer
    assert len(g) == 4, g.nodes()
    # these are state projections (partial assignments)
    # a state assigns to all variable names
    # (infinitely many)
    states = {
        enum._node_tuple(d, keys)
        for u, d in g.nodes(data=True)}
    assert tuple([True, 1]) in states, states
    r = {p for p in states if p[0] is True}
    assert len(r) == 2
    r = {p for p in states if p[0] is False}
    assert len(r) == 2
Beispiel #2
0
def synthesize_enumerated_streett(spec, use_cudd=False):
    """Return transducer enumerated as a graph.

    @type spec: `tulip.spec.form.GRSpec`
    @param use_cudd: efficient BDD computations with `dd.cudd`
    @rtype: `networkx.DiGraph`
    """
    aut = _grspec_to_automaton(spec)
    sym.fill_blanks(aut)
    bdd = _init_bdd(use_cudd)
    aut.bdd = bdd
    a = aut.build()
    assert a.action['sys'][0] != bdd.false
    t0 = time.time()
    z, yij, xijk = gr1.solve_streett_game(a)
    t1 = time.time()
    # unrealizable ?
    if not gr1.is_realizable(z, a):
        print('WARNING: unrealizable')
        return None
    t = gr1.make_streett_transducer(z, yij, xijk, a)
    t2 = time.time()
    (u, ) = t.action['sys']
    assert u != bdd.false
    g = enum.action_to_steps(t, qinit=spec.qinit)
    h = _strategy_to_state_annotated(g, a)
    del u, yij, xijk
    t3 = time.time()
    log.info(('Winning set computed in {win} sec.\n'
              'Symbolic strategy computed in {sym} sec.\n'
              'Strategy enumerated in {enu} sec.').format(win=t1 - t0,
                                                          sym=t2 - t1,
                                                          enu=t3 - t2))
    return h
Beispiel #3
0
def synthesize_enumerated_streett(spec):
    """Return transducer enumerated as a graph.

    @type spec: `tulip.spec.form.GRSpec`
    @rtype: `networkx.DiGraph`
    """
    aut = _grspec_to_automaton(spec)
    assert aut.action['sys'] != aut.false
    t0 = time.time()
    z, yij, xijk = gr1.solve_streett_game(aut)
    t1 = time.time()
    # unrealizable ?
    if not gr1.is_realizable(z, aut):
        print('WARNING: unrealizable')
        return None
    gr1.make_streett_transducer(z, yij, xijk, aut)
    t2 = time.time()
    g = enum.action_to_steps(aut, 'env', 'impl', qinit=aut.qinit)
    h = _strategy_to_state_annotated(g, aut)
    del z, yij, xijk
    t3 = time.time()
    log.info(('Winning set computed in {win} sec.\n'
              'Symbolic strategy computed in {sym} sec.\n'
              'Strategy enumerated in {enu} sec.').format(win=t1 - t0,
                                                          sym=t2 - t1,
                                                          enu=t3 - t2))
    return h
def test_action_to_steps():
    aut = symbolic.Automaton()
    aut.declare_variables(x='bool', y=(0, 17))
    env = 'env_foo'
    sys = 'sys_bar'
    aut.varlist[env] = ['x']
    aut.varlist[sys] = ['y']
    keys = ('x', 'y')
    aut.init[env] = aut.add_expr('x /\ (y = 1)')
    aut.init[sys] = aut.true
    aut.action[env] = aut.true
    aut.action[sys] = aut.add_expr("y' /= y")
    aut.win['<>[]'] = aut.bdds_from('x')
    aut.win['[]<>'] = aut.bdds_from('y != 1')
    aut.qinit = '\A \A'
    aut.moore = True
    aut.prime_varlists()
    g = enum.action_to_steps(aut, env=env, sys=sys, qinit=aut.qinit)
    # 36 states reachable, but should enumerate fewer
    assert len(g) == 4, g.nodes()
    # these are state projections (partial assignments)
    # a state assigns to all variable names
    # (infinitely many)
    states = {enum._node_tuple(d, keys) for u, d in g.nodes(data=True)}
    assert tuple([True, 1]) in states, states
    r = {p for p in states if p[0] is True}
    assert len(r) == 2
    r = {p for p in states if p[0] is False}
    assert len(r) == 2
Beispiel #5
0
def enumerate_impl(spec):
    """Return enumerated controller."""
    # modifies env, sys (which serve as a reusable interface)
    spec.init['sys'] = spec.init['impl']
    spec.action['sys'] = spec.action['impl']
    graph = enum.action_to_steps(spec, qinit=spec.qinit)
    graph.inputs = spec.varlist['env']
    graph.outputs = spec.varlist['sys']
    # pprint.pprint(graph.nodes(data=True))
    return graph
Beispiel #6
0
def synthesize_some_controller(aut):
    """Return a controller that implements the spec.

    If no controller exists, then raise an `Exception`.
    The returned controller is represented as a `networkx` graph.
    """
    z, yij, xijk = gr1.solve_streett_game(aut)
    gr1.make_streett_transducer(z, yij, xijk, aut)
    g = enum.action_to_steps(
        aut, env='env', sys='impl', qinit=aut.qinit)
    return g
Beispiel #7
0
def synthesize_rabin_controller(b):
    """Return enumerated graph with steps as edges.
     after solving Rabin(1) game.

    @param b: game with <>[] & []<> winning
    @type b: `symbolic.Automaton`
    """
    qinit = b.qinit
    aut_rabin = b.build()
    zk, yki, xkijr = gr1.solve_rabin_game(aut_rabin)
    t= gr1.make_rabin_transducer(zk, yki, xkijr, aut_rabin)
    g = enum.action_to_steps(t, qinit=qinit)
    return g
Beispiel #8
0
def synthesize_some_controller(aut):
    """Return a controller that implements the spec.

    If no controller exists, then raise an `Exception`.
    The returned controller is represented as a `networkx` graph.
    """
    z, yij, xijk = gr1.solve_streett_game(aut)
    gr1.make_streett_transducer(z, yij, xijk, aut)
    aut.init['env'] = aut.init['impl_env']
    aut.init['sys'] = aut.init['impl_sys']
    aut.action['sys'] = aut.action['impl']
    g = enum.action_to_steps(aut, qinit=aut.qinit)
    return g
Beispiel #9
0
'''

aut.define(specs)
aut.init.update(env='envInit', sys='sysInit')
aut.action.update(env='envNext', sys='sysNext')

# aut.win['<>[]'] = aut.bdds_from('(sysX1 = 0 /\ sysY1 = 0)','(sysX1 = 2 /\ sysY1 = 0)', '(sysX1 = 0 /\ sysY1 = 2)')
# aut.win['[]<>'] = aut.bdds_from('(sysX1 = 0 /\ sysY1 = 0)','(sysX1 = 2 /\ sysY1 = 0)', '(sysX1 = 0 /\ sysY1 = 2)')

aut.win['<>[]'] = aut.bdds_from('TRUE')
aut.win['[]<>'] = aut.bdds_from('pos1 = 1', 'pos1 = 2')

aut.qinit = '\E \A'
aut.moore = True
aut.plus_one = True

# g = enum.action_to_steps(aut, 'env', 'sys', qinit=aut.qinit)
# h, _ = sym_enum._format_nx(g)
# pd = nx.drawing.nx_pydot.to_pydot(h)
# pd.write_pdf('outputs/single_gridworld_game_states_omega.pdf')

z, yij, xijk = gr1.solve_streett_game(aut)
gr1.make_streett_transducer(z, yij, xijk, aut)
aut.varlist['sys'].append('_goal')
aut.prime_varlists()
# enumerate
g = enum.action_to_steps(aut, 'env', 'impl', qinit=aut.qinit)
h, _ = sym_enum._format_nx(g)
pd = nx.drawing.nx_pydot.to_pydot(h)
pd.write_pdf('single_gridworld_game_states_omega.pdf')
Beispiel #10
0
def enumerate_controller(aut):
    aut.init['env'] = aut.init['impl_env']
    aut.init['sys'] = aut.init['impl_sys']
    aut.action['sys'] = aut.action['impl']
    g = enum.action_to_steps(aut, qinit=aut.qinit)
    return g
Beispiel #11
0
def enumerate_controller(aut):
    g = enum.action_to_steps(
        aut, env='env', sys='impl', qinit=aut.qinit)
    return g
Beispiel #12
0
def enumerate_controller(aut):
    aut.init['env'] = aut.init['impl_env']
    aut.init['sys'] = aut.init['impl_sys']
    aut.action['sys'] = aut.action['impl']
    g = enum.action_to_steps(aut, qinit=aut.qinit)
    return g
Beispiel #13
0
print(a.action['sys'])

assert a.action['sys'][0] != bdd.false
t0 = time.time()
z, yij, xijk = gr1.solve_streett_game(a)
t1 = time.time()
print(t1-t0)
# unrealizable ?
if not gr1.is_realizable(z, a):
    print('WARNING: unrealizable')

t = gr1.make_streett_transducer(z, yij, xijk, a)
t2 = time.time()
(u,) = t.action['sys']
assert u != bdd.false

g = enum.action_to_steps(t, qinit=spec.qinit)
h = omega_int._strategy_to_state_annotated(g, a)
del u, yij, xijk
t3 = time.time()
print((
    'Winning set computed in {win} sec.\n'
    'Symbolic strategy computed in {sym} sec.\n'
    'Strategy enumerated in {enu} sec.').format(
    win=t1 - t0,
    sym=t2 - t1,
    enu=t3 - t2))
bdd.dump('manager.p')
bdd2 = omega_int._init_bdd(use_cudd)
_bdd.copy_vars(t.bdd,bdd2)
n1=bdd.copy(z,bdd2)