Example #1
0
def test_orthotope_subseteq():
    fol, prm = setup_aut()
    varmap = prm._varmap
    r = lat.subseteq(varmap, fol)
    d = dict(a_x=1, b_x=2, a_y=0, b_y=3)
    r = fol.let(d, r)
    d = dict(u_x=1, v_x=1, u_y=2, v_y=3)
    r = fol.let(d, r)
    assert r == fol.false
    r = lat.subseteq(varmap, fol)
    d = dict(a_x=1, b_x=2, a_y=0, b_y=3)
    r = fol.let(d, r)
    d = dict(u_x=0, v_x=6, u_y=0, v_y=10)
    r = fol.let(d, r)
    assert r == fol.true
Example #2
0
def setup_orthotope_vars():
    fol = _fol.Context()
    fol.declare(
        x=(0, 5),
        y=(2, 14),
        px=(0, 5),
        qx=(0, 5),
        py=(2, 14),
        qy=(2, 14),
        ax=(0, 5),
        bx=(0, 5),
        ay=(2, 14),
        by=(2, 14),
    )
    xvars = ['x', 'y']
    px = dict(x=dict(a='px', b='qx'), y=dict(a='py', b='qy'))
    qx = dict(x=dict(a='ax', b='bx'), y=dict(a='ay', b='by'))
    prm = lat.Parameters()
    prm.x_vars = xvars
    prm._px = px
    prm._qx = qx
    prm.p_to_q = dict(px='ax', py='ay', qx='bx', qy='by')
    prm.p_vars = set(prm.p_to_q)
    prm.q_vars = set(prm.p_to_q.values())
    varmap = lat.parameter_varmap(px, qx)
    prm.p_leq_q = lat.subseteq(varmap, fol)
    prm.p_eq_q = lat.eq(varmap, fol)
    return fol, prm
Example #3
0
def test_orthotope_contains_x():
    aut, prm = setup_aut(15, 15)
    u = lat.x_in_implicant(prm, aut)
    values = dict(x=2, y=2, a_x=0, b_x=10, a_y=1, b_y=2)
    r = aut.let(values, u)
    assert r == aut.true, r
    d = prm._varmap
    u = lat.subseteq(d, aut)
    values = dict(a_x=0, b_x=3, a_y=1, b_y=2, u_x=0, v_x=5, u_y=0, v_y=2)
    r = aut.let(values, u)
    assert r == aut.true, r
    values = dict(a_x=0, b_x=3, a_y=1, b_y=2, u_x=0, v_x=2, u_y=0, v_y=2)
    r = aut.let(values, u)
    assert r == aut.false, r
Example #4
0
def test_covers():
    fol, prm = setup_aut()
    # set it here to avoid calling `subseteq` from
    # `setup_aut` because it will violate unit testing
    p_leq_q = lat.subseteq(prm._varmap, fol)
    prm.p_leq_q = p_leq_q
    cover = fol.add_expr('a_x = 0  /\  b_x = 5')
    # not covered
    f = fol.add_expr('1 <= x  /\  x <= 6')
    r = cov._covers(cover, f, prm, fol)
    assert r is False, r
    r_ = cov._covers_naive(cover, f, prm, fol)
    assert r == r_, (r, r_)
    # covered
    f = fol.add_expr('0 <= x  /\  x <= 4')
    r = cov._covers(cover, f, prm, fol)
    assert r is True, r
    r_ = cov._covers_naive(cover, f, prm, fol)
    assert r == r_, (r, r_)
Example #5
0
def test_orthotopes_using_robots_example():
    aut, prm = setup_aut(15, 15)
    p_vars = prm.p_vars
    # this predicate is constructed by `contract_maker`
    # for the robots example in ACC 2016
    f = robots_example(aut)
    prm.p_leq_q = lat.subseteq(prm._varmap, aut)
    prm.p_eq_q = lat.eq(prm._varmap, aut)
    u = lat.prime_implicants(f, prm, aut)
    support = aut.support(u)
    assert support == set(p_vars), (support, p_vars)
    n_primes = aut.count(u)
    k = aut.count(u)
    assert n_primes == k, (n_primes, k)
    log.info('{n} prime implicants'.format(n=n_primes))
    u = lat.essential_orthotopes(f, prm, aut)
    support = aut.support(u)
    assert support == set(p_vars), (support, p_vars)
    n_essential = aut.count(u)
    k = aut.count(u)
    assert n_essential == k, (n_essential, k)
    log.info('{n} essential prime implicants'.format(n=n_essential))
    assert n_essential == 7, n_essential
    # result: all primes are essential in this example
    #
    care = aut.true
    s = cov.dumps_cover(u, f, care, aut)
    log.info(s)
    log.info('BDD has {n} nodes'.format(n=len(aut.bdd)))
    # confirm that essential orthotopes cover exactly `f`
    c = lat.list_expr(u, prm, aut, simple=True)
    s = stx.disj(c)
    log.info(s)
    z = aut.add_expr(s)
    z = aut.exist(['a_x', 'b_x', 'a_y', 'b_y'], z)
    assert aut.support(z) == aut.support(f)
    assert z == f
    if plt is not None:
        _plot_orthotopes_for_robots_example(u, f, prm._px, xvars, aut)
    # pprint.pprint(aut.bdd.statistics())
    # pprint.pprint(aut.bdd.vars)
    log.info('{n} nodes in manager'.format(n=len(aut.bdd)))
Example #6
0
def test_orthotope_contains_x():
    aut, prm = setup_aut(15, 15)
    u = lat.x_in_implicant(prm, aut)
    values = dict(x=2, y=2,
                  a_x=0, b_x=10,
                  a_y=1, b_y=2)
    r = aut.let(values, u)
    assert r == aut.true, r
    d = prm._varmap
    u = lat.subseteq(d, aut)
    values = dict(
        a_x=0, b_x=3, a_y=1, b_y=2,
        u_x=0, v_x=5, u_y=0, v_y=2)
    r = aut.let(values, u)
    assert r == aut.true, r
    values = dict(
        a_x=0, b_x=3, a_y=1, b_y=2,
        u_x=0, v_x=2, u_y=0, v_y=2)
    r = aut.let(values, u)
    assert r == aut.false, r
Example #7
0
def test_scaling_equality():
    aut = _fol.Context()
    x_vars = dict(x=(0, 10), y=(0, 15), z=(0, 15))
    aut.declare(**x_vars)
    params = dict(pa='a', pb='b', qa='u', qb='v')
    p_dom = lat._parameter_table(x_vars,
                                 aut.vars,
                                 a_name=params['pa'],
                                 b_name=params['pb'])
    q_dom = lat._parameter_table(x_vars,
                                 aut.vars,
                                 a_name=params['qa'],
                                 b_name=params['qb'])
    aut.declare(**p_dom)
    aut.declare(**q_dom)
    px = lat._map_vars_to_parameters(x_vars,
                                     a_name=params['pa'],
                                     b_name=params['pb'])
    qx = lat._map_vars_to_parameters(x_vars,
                                     a_name=params['qa'],
                                     b_name=params['qb'])
    p_to_q = lat._renaming_between_parameters(px, qx)
    x_as_x = {xj: dict(a=xj, b=xj) for xj in px}
    varmap = lat.parameter_varmap(px, x_as_x)
    log.info('Number of variables: {n}'.format(n=len(varmap)))
    u = lat.subseteq(varmap, aut)
    #
    s = ('( '
         '(z = 1  /\  y <= 0)  \/ '
         '(x = 0  /\  z = 1)  \/ '
         '(y >= 1  /\  x <= 0)  \/ '
         '(y >= 1  /\  z <= 0)  \/ '
         '(x >= 1  /\  z <= 0)  \/ '
         '(x >= 1  /\  y <= 0) '
         ') ')
    f = aut.add_expr(s)
    prm = lat.Parameters()
    prm._px = px
    lat.embed_as_implicants(f, prm, aut)
Example #8
0
def setup_orthotope_vars():
    fol = _fol.Context()
    fol.declare(
        x=(0, 5), y=(2, 14),
        px=(0, 5), qx=(0, 5),
        py=(2, 14), qy=(2, 14),
        ax=(0, 5), bx=(0, 5),
        ay=(2, 14), by=(2, 14),)
    xvars = ['x', 'y']
    px = dict(x=dict(a='px', b='qx'),
               y=dict(a='py', b='qy'))
    qx = dict(x=dict(a='ax', b='bx'),
              y=dict(a='ay', b='by'))
    prm = lat.Parameters()
    prm.x_vars = xvars
    prm._px = px
    prm._qx = qx
    prm.p_to_q = dict(px='ax', py='ay', qx='bx', qy='by')
    prm.p_vars = set(prm.p_to_q)
    prm.q_vars = set(prm.p_to_q.values())
    varmap = lat.parameter_varmap(px, qx)
    prm.p_leq_q = lat.subseteq(varmap, fol)
    prm.p_eq_q = lat.eq(varmap, fol)
    return fol, prm
Example #9
0
def test_scaling_equality():
    aut = _fol.Context()
    x_vars = dict(x=(0, 10), y=(0, 15), z=(0, 15))
    aut.declare(**x_vars)
    params = dict(pa='a', pb='b', qa='u', qb='v')
    p_dom = lat._parameter_table(
        x_vars, aut.vars,
        a_name=params['pa'], b_name=params['pb'])
    q_dom = lat._parameter_table(
        x_vars, aut.vars,
        a_name=params['qa'], b_name=params['qb'])
    aut.declare(**p_dom)
    aut.declare(**q_dom)
    px = lat._map_vars_to_parameters(
        x_vars, a_name=params['pa'], b_name=params['pb'])
    qx = lat._map_vars_to_parameters(
        x_vars, a_name=params['qa'], b_name=params['qb'])
    p_to_q = lat._renaming_between_parameters(px, qx)
    x_as_x = {xj: dict(a=xj, b=xj) for xj in px}
    varmap = lat.parameter_varmap(px, x_as_x)
    log.info('Number of variables: {n}'.format(n=len(varmap)))
    u = lat.subseteq(varmap, aut)
    #
    s = (
        '( '
        '(z = 1  /\  y <= 0)  \/ '
        '(x = 0  /\  z = 1)  \/ '
        '(y >= 1  /\  x <= 0)  \/ '
        '(y >= 1  /\  z <= 0)  \/ '
        '(x >= 1  /\  z <= 0)  \/ '
        '(x >= 1  /\  y <= 0) '
        ') ')
    f = aut.add_expr(s)
    prm = lat.Parameters()
    prm._px = px
    lat.embed_as_implicants(f, prm, aut)