Exemplo n.º 1
0
def test_closed_system():
    c1 = aiger_bv.atom(1, 'c1', signed=False)
    a = aiger_bv.atom(1, 'a', signed=False)

    dyn = circ2mdp(chain(n=4, state_name='s', action='a'))
    dyn <<= (c1 & a).with_output('a').aigbv
    c1_coin = coin((1, 8), name='c1')
    dyn <<= c1_coin

    assert dyn.inputs == {'a'}
    assert dyn.outputs == {'s'}
    start = {'s_prev': (True, False, False, False, False)}
    end = {'s_prev': (False, True, False, False, False)}
    assert dyn.prob(start, {'a': (0, )}, end) == 0
    assert dyn.prob(start, {'a': (1, )}, end) == c1_coin.prob() == 1 / 8

    c2 = aiger_bv.atom(1, 'c2', signed=False)
    const_false = aiger_bv.atom(1, 0, signed=False)
    state = aiger_bv.atom(5, 's', signed=False)
    clip = state == 0b00001

    policy = circ2mdp(aiger_bv.ite(clip, const_false, c2).with_output('a'))
    policy <<= coin((1, 8), name='c2')

    sys = (policy >> dyn).feedback(inputs=['s'],
                                   outputs=['s'],
                                   latches=['s_prev2'],
                                   keep_outputs=True)
    assert sys.inputs == set()
    assert sys.outputs == {'s'}
Exemplo n.º 2
0
def test_mdp_readme():
    from aiger_bv import atom
    from aiger_coins import circ2mdp

    x = atom(3, 'x', signed=False)
    y = atom(3, 'y', signed=False)
    expr = (x & y).with_output('x&y')

    mdp1 = circ2mdp(expr)
    dist = aiger_coins.dist((0, 1, 2), name='y')

    mdp2 = dist >> mdp1

    assert mdp1.inputs == {'x', 'y'}
    assert mdp2.inputs == {'x'}

    mdp3 = mdp2 | circ2mdp(aiger_bv.identity_gate(3, 'z'))
    assert mdp3.inputs == {'x', 'z'}
    assert mdp3.outputs == {'x&y', 'z'}

    mdp4 = mdp3.feedback(inputs=['z'], outputs=['x&y'], keep_outputs=True)
    assert mdp4.inputs == {'x'}
    assert mdp4.outputs == {'x&y', 'z'}

    action = atom(1, 'action', signed=False)
    x_prev = atom(1, 'x_prev', signed=False)
    c = atom(1, 'c', signed=False)

    x_next = (x_prev & c & action).with_output('x_next')

    sys = circ2mdp(x_next).feedback(
        keep_outputs=True,
        inputs=['x_prev'],
        outputs=['x_next'],
        initials=[(True, )],
    )
    sys <<= coin((1, 2), name='c')
    assert sys.inputs == {'action'}
    assert sys.outputs == {'x_next'}

    sys_actions = 3 * [{'action': (True, )}]
    states = 3 * [{'x_next': (True, )}]

    actions = sys.encode_trc(sys_actions, states)
    assert not any(v['c'][0] for v in actions)

    sys_actions2, states2 = sys.decode_trc(actions)
    assert sys_actions2 == sys_actions
    assert states2 == states
Exemplo n.º 3
0
def test_mdp_smoke():
    x = aiger_bv.identity_gate(2, 'x')
    y = aiger_bv.identity_gate(3, 'y')

    circ = x | y
    dist = aiger_coins.dist(((0, 3), (1, 3), (2, 3)), name='y')
    assert dist.expr.output == 'y'

    dyn = circ2mdp(circ, {'y': dist})
    assert dyn.inputs == {'x'}

    dyn2 = dist >> circ2mdp(circ)
    assert dyn2.inputs == {'x'}

    assert dyn2.aigbv.inputs == {'x'} | dist.inputs
    assert dyn2.aigbv.outputs == dyn2.outputs | {'##valid'}

    assert '##valid[0]' in dyn2.aig.outputs

    x = aiger_bv.atom(3, 'x', signed=False)
    y = aiger_bv.atom(3, 'y', signed=False)
    mdp = dist >> circ2mdp(x < y)
    assert mdp.inputs == {'x'}
    assert len(mdp.outputs) == 1
Exemplo n.º 4
0
def test_find_env_input():
    x, c = map(aiger_ptltl.atom, ('x', 'c'))

    sys = (x & c).historically().aig
    sys = circ2mdp(aiger_bv.aig2aigbv(sys))
    sys <<= coin((1, 2), name='c')

    assert sys.inputs == {'x'}
    assert len(sys.outputs) == 1

    out, *_ = sys.outputs

    start = end = sys.aigbv.latch2init
    action = {'x': (True, )}
    coins = sys.find_env_input(start, action, end)

    _, lmap = sys.aigbv(inputs={**action, **coins})
    assert lmap == end
Exemplo n.º 5
0
def test_find_coin_flips():
    x, c = map(aiger_ptltl.atom, ('x', 'c'))

    sys = (x & c).historically().aig
    sys = circ2mdp(aiger_bv.aig2aigbv(sys))
    sys <<= coin((1, 2), name='c')

    assert sys.inputs == {'x'}
    assert len(sys.outputs) == 1

    out, *_ = sys.outputs
    sys_actions = 3 * [{'x': (True, )}]
    states = 3 * [{out: (True, )}]

    actions = sys.encode_trc(sys_actions, states)
    assert not any(v['c'][0] for v in actions)

    sys_actions2, states2 = sys.decode_trc(actions)
    assert sys_actions2 == sys_actions
    assert states2 == states