Ejemplo n.º 1
0
def test_never_false():
    spec = LTL.atom('x').historically()
    monitor = BV.aig2aigbv(spec.aig)
    dyn = C.pcirc(BV.identity_gate(1, 'x'))

    horizon = 3
    model = from_pcirc(dyn, monitor, steps=horizon)
    graph = model.graph()
    assert len(graph.nodes) == horizon + 2
    assert len(graph.edges) == 2 * horizon
Ejemplo 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
Ejemplo 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
Ejemplo n.º 4
0
def dist2mdp(dist):
    circ = identity_gate(dist.size, dist.output)
    return circ2mdp(circ=circ, input2dist={dist.output: dist})
Ejemplo n.º 5
0
 def coins(self) -> UnsignedBVExpr:
     coins = (identity_gate(self.aigbv.imap[i].size, i)
              for i in self.inputs)
     coins = reduce(lambda x, y: x | y, coins)
     return UnsignedBVExpr(coins)
Ejemplo n.º 6
0
def test_never_false1():
    spec = LTL.atom('x').historically()
    monitor = BV.aig2aigbv(spec.aig)
    dyn = C.pcirc(BV.identity_gate(1, 'x'))

    horizon = 4
    model = from_pcirc(dyn, monitor, steps=horizon)

    coeff = np.log(2)
    actor = model.improviser(rationality=coeff)

    expected = [
        0,
        coeff,
        np.log(1 + 2),
        np.log(3 + 2),
        np.log(7 + 2),
        np.log(15 + 2),
    ]
    # LSE visits powers of 2 for the special coeff.
    expected = fn.lmap(pytest.approx, expected)

    vals = sorted(list(actor.node2val.values()))
    assert all(x == y for x, y in zip(expected, vals))

    expected = sorted([
        np.log(9) - np.log(17),
        np.log(8) - np.log(17),
    ])
    expected = fn.lmap(pytest.approx, expected)

    def lprob(elems):
        return actor.prob(elems, log=True)

    assert lprob([]) == 0
    assert lprob([1]) == pytest.approx(np.log(9) - np.log(17))
    assert lprob([1, 1]) == pytest.approx(np.log(5) - np.log(17))
    assert lprob([1, 1, 1]) == pytest.approx(np.log(3) - np.log(17))
    assert lprob([1, 1, 1, 1]) == pytest.approx(coeff - np.log(17))

    # ----------- Fail on first state ---------------------
    base = np.log(8) - np.log(17)
    assert lprob([0]) == pytest.approx(base)
    # Uniform after failing.
    assert lprob([0, 1]) == pytest.approx(base - np.log(2))
    assert lprob([0, 0]) == pytest.approx(base - np.log(2))
    assert lprob([0, 0, 0]) == pytest.approx(base - np.log(4))
    assert lprob([0, 0, 1]) == pytest.approx(base - np.log(4))

    # ----------- Fail on second state ---------------------
    base = np.log(4) - np.log(17)
    assert lprob([1, 0]) == pytest.approx(base)
    assert lprob([1, 0, 0]) == pytest.approx(base - np.log(2))
    assert lprob([1, 0, 1]) == pytest.approx(base - np.log(2))
    assert lprob([1, 0, 0, 0]) == pytest.approx(base - np.log(4))
    assert lprob([1, 1, 0, 1]) == pytest.approx(base - np.log(4))

    with pytest.raises(ValueError):
        lprob([1, 1, 1, 1, 1, 1])

    example = list(actor.sample())
    assert -float('inf') < lprob(example)

    ctrl = actor.policy()
    example = []
    for env in [None, 0, 0, 0]:
        example.append(ctrl.send(env))
    assert -float('inf') < lprob(example)

    actor = model.improviser(psat=0.7)
    assert actor.sat_prob() == pytest.approx(0.7)
Ejemplo n.º 7
0
def sys1():
    return C.circ2mdp(BV.identity_gate(1, 'a'))