Exemplo n.º 1
0
def setup_aut(xmax=15, ymax=15):
    fol = _fol.Context()
    fol.bdd.configure(reordering=True)
    # CAUTION: remember that type hints (safety)
    # needs to be added as care set
    fol.declare(x=(0, xmax), y=(0, ymax))
    x_vars = ['x', 'y']
    p_table = lat._parameter_table(
        x_vars, fol.vars, a_name='a', b_name='b')
    q_table = lat._parameter_table(
        x_vars, fol.vars, a_name='u', b_name='v')
    fol.declare(**p_table)
    fol.declare(**q_table)
    px = lat._map_vars_to_parameters(
        x_vars, a_name='a', b_name='b')
    qx = lat._map_vars_to_parameters(
        x_vars, a_name='u', b_name='v')
    varmap = lat.parameter_varmap(px, qx)
    p_to_q = lat._renaming_between_parameters(px, qx)
    prm = lat.Parameters()
    prm.x_vars = x_vars
    prm.p_vars = set(p_table)
    prm.q_vars = set(q_table)
    prm.p_to_q = p_to_q
    prm._px = px
    prm._qx = qx
    prm._varmap = varmap
    return fol, prm
Exemplo n.º 2
0
def test_transpose():
    fol = _fol.Context()
    dvars = {'p': (0, 4), 'q': (0, 4), "p_cp": (0, 4)}
    fol.declare(**dvars)
    s = '(p = 1) \/ (p = 2) \/ (p = 4)'
    p_is_prime = fol.add_expr(s)
    s = '(p = 1) \/ (p = 3)'
    p_is_signature = fol.add_expr(s)
    p_to_q = {'p': 'q'}
    p_leq_q = fol.add_expr("p <= q")
    u_leq_p = fol.add_expr("p_cp <= p")
    p_leq_u = fol.add_expr("p <= p_cp")
    prm = lat.Parameters()
    prm.u_leq_p = u_leq_p
    prm.p_leq_u = p_leq_u
    prm.p_leq_q = p_leq_q
    prm.p_to_q = p_to_q
    prm.p_vars = {'p'}
    prm.q_vars = {'q'}
    prm.u_vars = {'p_cp'}
    prm.p_to_u = {'p': 'p_cp'}
    bab = cov._BranchAndBound(prm, fol)
    tau = cov._floor(
        p_is_signature, p_is_prime, bab, fol)
    s = 'p = 1 \/ p = 3'
    tau_ = fol.add_expr(s)
    assert tau == tau_
Exemplo n.º 3
0
def test_max_transpose():
    fol = _fol.Context()
    # `p'` serves as `u`
    dvars = {'p': (0, 4), 'q': (0, 4), "p_cp": (0, 4)}
    fol.declare(**dvars)
    s = '(p = 2) \/ (p = 4)'
    p_is_prime = fol.add_expr(s)
    s = '(p = 1) \/ (p = 3)'
    p_is_signature = fol.add_expr(s)
    p_to_q = {'p': 'q'}
    # we use intervals `0..p` as literals
    px = dict(p=dict(a='0', b='p'))
    qx = dict(p=dict(a='0', b='q'))
    u_leq_p = fol.add_expr("p_cp <= p")
    p_leq_u = fol.add_expr("p <= p_cp")
    p_leq_q = fol.add_expr("p <= q")
    p_eq_q = fol.add_expr("p = q")  # /\ (0 = 0)
    prm = lat.Parameters()
    prm._px = px
    prm._qx = qx
    prm.u_leq_p = u_leq_p
    prm.p_leq_u = p_leq_u
    prm.p_leq_q = p_leq_q
    prm.p_eq_q = p_eq_q
    prm.p_to_q = p_to_q
    prm.p_vars = {'p'}
    prm.q_vars = {'q'}
    prm.u_vars = {'p_cp'}
    prm.p_to_u = {'p': 'p_cp'}
    bab = cov._BranchAndBound(prm, fol)
    max_tau_x = cov._max_transpose(
        p_is_signature, p_is_prime, bab, fol)
    s = 'p = 3'
    max_tau_x_ = fol.add_expr(s)
    assert max_tau_x == max_tau_x_
Exemplo n.º 4
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
Exemplo n.º 5
0
def test_list_orthotope_expr():
    fol = _fol.Context()
    fol.declare(
        x=(-4, 5), a_x=(-4, 5), b_x=(-4, 5),
        y=(-7, 15), a_y=(-7, 15), b_y=(-7, 15))
    px, _ = dummy_parameters()
    prm = lat.Parameters()
    prm._px = px
    f = fol.add_expr('x = 1')
    care = fol.true
    s = (
        '(a_x = 2) /\ (2 <= b_x) /\ (b_x <= 3) '
        '/\ (a_y = 1 ) /\ (b_y = 5)')
    cover = fol.add_expr(s)
    r = lat.list_expr(cover, prm, fol)
    r = set(r)
    r_ = {'(x = 2) /\ (y \in 1 .. 5)',
          '(x \in 2 .. 3) /\ (y \in 1 .. 5)'}
    assert r == r_, (r, r_)
    # simple syntax, for parsing back
    # (needed until able to parse `x \in a .. b` expressions)
    r = lat.list_expr(cover, prm, fol, simple=True)
    r = set(r)
    r_ = {'(x = 2) /\ (1 <= y) /\ (y <= 5)',
          '(2 <= x) /\ (x <= 3) /\ (1 <= y) /\ (y <= 5)'}
    assert r == r_, (r, r_)
    # clipping on, but nothing to clip
    r = lat.list_expr(cover, prm, fol, use_dom=True)
    r = set(r)
    r_ = {'(x = 2) /\ (y \in 1 .. 5)',
          '(x \in 2 .. 3) /\ (y \in 1 .. 5)'}
    assert r == r_, (r, r_)
    # clip using type hints
    s = (
        '(a_x = -4) /\ (5 <= b_x)'
        '/\ (a_y = 1 ) /\ (b_y = 5)')
    cover = fol.add_expr(s)
    r = lat.list_expr(cover, prm, fol, use_dom=True)
    r = set(r)
    r_ = {'(y \in 1 .. 5)'}
    assert r == r_, (r, r_)
    # empty orthotopes
    s = (
        '(a_x = -4) /\ (5 <= b_x)'
        '/\ (a_y = 3 ) /\ (b_y = 1)')
    cover = fol.add_expr(s)
    with assert_raises(AssertionError):
        lat.list_expr(cover, prm, fol, use_dom=True)
    with assert_raises(AssertionError):
        lat.list_expr(cover, prm, fol)
Exemplo n.º 6
0
def test_branch_and_bound_class():
    fol = _fol.Context()
    prm = lat.Parameters()
    bab = cov._BranchAndBound(prm, fol)
    bab.lower_bound = 15
    with assert_raises(AssertionError):
        bab.upper_bound = 10
    bab.upper_bound = 20
    # only init for lower bound
    with assert_raises(AssertionError):
        bab.lower_bound = 17
    with assert_raises(AssertionError):
        bab.upper_bound = 10
    bab.upper_bound = 18
Exemplo n.º 7
0
def test_contains_covered():
    fol = _fol.Context()
    fol.declare(p_cp=(0, 4), p=(0, 4), q=(0, 4))
    s = 'p_cp = 1 \/ p_cp = 2 \/ p_cp = 5'
    u_is_signature = fol.add_expr(s)
    prm = lat.Parameters()
    prm.p_vars = {'p'}
    prm.q_vars = {'q'}
    prm.u_vars = {'p_cp'}
    prm.p_to_q = {'p': 'q'}
    prm.u_leq_p = fol.add_expr('p_cp <= p')
    prm.p_leq_q = fol.add_expr('p <= q')
    # prm.p_to_u = {'p': 'u'}
    p_like_q = cov._contains_covered(u_is_signature, prm.u_leq_p, prm, fol)
    supp = fol.support(p_like_q)
    # pprint.pprint(list(fol.pick_iter(p_like_q, care_vars=supp)))
    s = ('((q >= 1) => (p >= 1)) /\ '
         '((q >= 2) => (p >= 2)) /\ '
         '((q >= 5) => (p >= 5))')
    p_like_q_ = fol.add_expr(s)
    assert p_like_q == p_like_q_
Exemplo n.º 8
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)
Exemplo n.º 9
0
def test_maxima():
    prm = lat.Parameters()
    fol, _ = setup_aut(5, 5)
    s = 'x = 1 \/ x = 3 \/ x = 4'
    u = fol.add_expr(s)
    # x <= y
    s = '(x = 1 /\ y = 1) \/ (x = 1 /\ y = 3)'
    prm.p_leq_q = fol.add_expr(s)
    prm.p_vars = {'x'}
    prm.q_vars = {'y'}
    prm.p_to_q = {'x': 'y'}
    r = stx.conj('{pj} = {qj}'.format(pj=pj, qj=qj)
                      for pj, qj in prm.p_to_q.items())
    prm.p_eq_q = fol.add_expr(r)
    t0 = time.time()
    m = cov._maxima(u, prm, fol)
    t1 = time.time()
    dt = t1 - t0
    log.info('`maxima` time (sec): {dt:1.2f}'.format(dt=dt))
    # print result
    gen = fol.pick_iter(m, care_vars=['x'])
    c = list(gen)
    log.info(c)