def test_enumerate_mincovers_below(): """Test the function `enumerate_mincovers_below`.""" fol = _fol.Context() fol.declare(x=(0, 5)) u = fol.add_expr(r' x \in 0..1 ') care = fol.true prm = lat.setup_aux_vars(u, care, fol) lat.setup_lattice(prm, fol) x = fol.add_expr(r' a_x = 0 /\ b_x = 2 ') y = fol.add_expr(r''' \/ (a_x = 0 /\ b_x = 1) \/ (a_x = 0 /\ b_x = 3) \/ (a_x = 0 /\ b_x = 4) ''') cover_from_max = fol.add_expr(r''' a_x = 0 /\ b_x = 4 ''') mincovers_below = cov_enum._enumerate_mincovers_below( cover_from_max, x, y, prm, fol) mincovers_below_ = cov_enum._enumerate_mincovers_below_set_based( cover_from_max, x, y, prm, fol) assert mincovers_below == mincovers_below_, mincovers_below r = fol.add_expr(r''' \/ (a_x = 0 /\ b_x = 3) \/ (a_x = 0 /\ b_x = 4) ''') mincovers_below_ = set(fol.assign_from(d) for d in fol.pick_iter(r)) assert mincovers_below == mincovers_below_, (mincovers_below, mincovers_below_)
def cyclic_core(f, care, fol): """Shallow minimal cover, only up to cyclic core.""" log.info('cyclic core computation') t0 = time.time() # assert assert f in fol.bdd, f assert care in fol.bdd, care assert care != fol.false, 'empty care set' assert f != fol.false, 'nothing to cover' assert f != fol.true or care != fol.true, ( 'no variables involved in problem') prm = lat.setup_aux_vars(f, care, fol) lat.setup_lattice(prm, fol) fcare = ~care | f bab = _BranchAndBound(prm, fol) # covering problem x = lat.embed_as_implicants(f, prm, fol) y = lat.prime_implicants(fcare, prm, fol) # assert problem is feasible assert x != fol.false assert y != fol.false assert _covers(y, f, prm, fol) xcore, ycore, essential = _cyclic_core_fixpoint(x, y, bab, fol) if xcore == fol.false: assert _covers(essential, f, prm, fol) _print_cyclic_core(x, y, xcore, ycore, essential, t0, bab.prm, fol) return xcore, ycore, essential
def test_cyclic_core_recursion(): """One cyclic core.""" fol = _fol.Context() fol.declare(x=(0, 1), y=(0, 1), z=(0, 1)) s = r''' ( \/ (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 = fol.add_expr(s) care = fol.true # setup variables and lattice prm = lat.setup_aux_vars(f, care, fol) lat.setup_lattice(prm, fol) # covering problem fcare = f | ~care x = lat.embed_as_implicants(f, prm, fol) y = lat.prime_implicants(fcare, prm, fol) # enumerative check enumerated_covers(x, y, prm, fol) # symbolically minimize mincovers = cov_enum.minimize(f, care, fol) n = len(mincovers) assert n == 2, (n, mincovers) for cover in mincovers: n = fol.count(cover) primes = list(fol.pick_iter(cover)) assert n == 3, (n, primes)
def cyclic_core(f, care, fol): """Shallow minimal cover, only up to cyclic core.""" log.info('cyclic core computation') t0 = time.time() # assert assert f in fol.bdd, f assert care in fol.bdd, care assert care != fol.false, 'empty care set' assert f != fol.false, 'nothing to cover' assert f != fol.true or care != fol.true, ( 'no variables involved in problem') prm = lat.setup_aux_vars(f, care, fol) lat.setup_lattice(prm, fol) fcare = ~ care | f bab = _BranchAndBound(prm, fol) # covering problem x = lat.embed_as_implicants(f, prm, fol) y = lat.prime_implicants(fcare, prm, fol) # assert problem is feasible assert x != fol.false assert y != fol.false assert _covers(y, f, prm, fol) xcore, ycore, essential = _cyclic_core_fixpoint( x, y, bab, fol) if xcore == fol.false: assert _covers(essential, f, prm, fol) _print_cyclic_core( x, y, xcore, ycore, essential, t0, bab.prm, fol) return xcore, ycore, essential
def minimize(f, care, fol): """Compute minimal DNF of predicate `f` over integers. @param f: predicate over integer-valued variables @param care: care set as predicate over same variables @type f, care: BDD node @type fol: `omega.symbolic.fol.Context` @return: minimal cover as BDD over parameters @rtype: BDD node """ # reasons for permisiveness here: # # - enable inspecting env violations of assumption # - make errors visible # - use entire instantiation domain # - permit computing DNF for care set using same `fol.vars` # - tests if not _care_implies_type_hints(f, care, fol): log.warning('care set should imply type hints') # could let # f &= care # but explicit is better. # Also, this permits working outside type hints. if not _f_implies_care(f, care, fol): log.warning('f should imply care set') if (f | ~ care) == fol.true: log.warning('f covers care set, so trivial cover') log.info('---- branching ----') path_cost = 0.0 prm = lat.setup_aux_vars(f, care, fol) lat.setup_lattice(prm, fol) # covering problem fcare = f | ~ care # the slack is introduced by having more primes # (those for `fcare`) to cover the same minterms (`f`) x = lat.embed_as_implicants(f, prm, fol) y = lat.prime_implicants(fcare, prm, fol) bab = _BranchAndBound(prm, fol) # initialize upper bound bab.upper_bound = _upper_bound( x, y, prm.p_leq_q, prm.p_to_q, fol) # assert covers(bab.best_cover, f, prm, fol) cover, _ = _traverse(x, y, path_cost, bab, fol) if cover is None: cover, _ = _some_cover(x, y, prm.p_leq_q, p_to_q, fol) assert cover is not None cover = unfloors(cover, y, fol, bab) assert_is_a_cover_from_y( cover, y, f, prm, fol) low = care & ~ f assert _none_covered(cover, low, prm, fol) log.info('==== branching ==== ') return cover
def minimize(f, care, fol): """Compute minimal DNF of predicate `f` over integers. @param f: predicate over integer-valued variables @param care: care set as predicate over same variables @type f, care: BDD node @type fol: `omega.symbolic.fol.Context` @return: minimal covers as BDDs over parameters @rtype: set of BDD nodes """ # reasons for permisiveness here: # # - enable inspecting env violations of assumption # - make errors visible # - use entire instantiation domain # - permit computing DNF for care set using same `fol.vars` # - tests if not cov._care_implies_type_hints(f, care, fol): log.warning('care set should imply type hints') # could let # f &= care # but explicit is better. # Also, this permits working outside type hints. if not cov._f_implies_care(f, care, fol): log.warning('f should imply care set') if (f | ~care) == fol.true: log.warning('f covers care set, so trivial cover') log.info('---- branch and bound search ----') prm = lat.setup_aux_vars(f, care, fol) lat.setup_lattice(prm, fol) # covering problem fcare = f | ~care x = lat.embed_as_implicants(f, prm, fol) y = lat.prime_implicants(fcare, prm, fol) bab = cov._BranchAndBound(prm, fol) # initialize upper bound bab.upper_bound = cov._upper_bound(x, y, prm.p_leq_q, prm.p_to_q, fol) path_cost = 0.0 mincovers = _cyclic_core_fixpoint_recursive(x, y, path_cost, bab, fol) # assert assert mincovers for cover in mincovers: cov.assert_is_a_cover_from_y(cover, y, f, prm, fol) low = care & ~f assert cov._none_covered(cover, low, prm, fol) log.info('==== branch and bound search ==== ') return mincovers
def _minimize_two_managers(f, care, fol): """Optimized version of `minimize` for large problems.""" if not _care_implies_type_hints(f, care, fol): log.warning('care set should imply type hints') if not _f_implies_care(f, care, fol): log.warning('f should imply care set') if (f | ~ care) == fol.true: log.warning('f covers care set, so trivial cover') log.info('---- branching ----') path_cost = 0.0 # x_vars, px, qx, p_to_q prm = lat.setup_aux_vars(f, care, fol) # manager where optimization happens fol_2 = type(fol)() fol_2.add_vars(fol.vars) # x (to be covered) log.info('embed implicants') x = lat.embed_as_implicants(f, prm, fol) x = fol.copy(x, fol_2) # covering problem fcare = f | ~ care lat.setup_lattice(prm, fol_2) # y (to use in cover) log.info('primes') fcare_2 = fol.copy(fcare, fol_2) y = lat.prime_implicants(fcare_2, prm, fol_2) del fcare_2 bab = _BranchAndBound(prm, fol_2) # initialize upper bound bab.upper_bound = _upper_bound( x, y, prm.p_leq_q, prm.p_to_q, fol_2) # assert _covers(bab.best_cover, f, prm, fol_2) log.info('traverse') cover, _ = _traverse(x, y, path_cost, bab, fol_2) if cover is None: cover, _ = _some_cover(x, y, prm.p_leq_q, prm.p_to_q, fol_2) assert cover is not None cover = unfloors(cover, y, fol_2, bab) log.info('==== branching ==== ') del fcare, prm, bab cover = fol_2.copy(cover, fol) return cover