Esempio n. 1
0
def _primes_two(alpha, variables, cache1, cache2, pmgr, mgr):
    if len(variables) == 0:
        if sdd.sdd_node_is_false(alpha): return sdd.sdd_manager_false(pmgr)
        if sdd.sdd_node_is_true(alpha): return sdd.sdd_manager_true(pmgr)

    key = (len(variables), sdd.sdd_id(alpha))
    if key in cache1:
        global cache_hits
        cache_hits += 1
        if cache_hits % 1000 == 0: print "cache-hits-update:", cache_hits
        return cache1[key]

    var, remaining = variables[0], variables[1:]
    alpha0 = sdd.sdd_condition(-var, alpha, mgr)
    alpha1 = sdd.sdd_condition(var, alpha, mgr)
    primes0 = _primes_two(alpha0, remaining, cache1, cache2, pmgr, mgr)
    primes1 = _primes_two(alpha1, remaining, cache1, cache2, pmgr, mgr)
    qrimes0 = _keep_imp(primes0, alpha1, remaining, cache1, cache2, pmgr, mgr)
    qrimes1 = _keep_imp(primes1, alpha0, remaining, cache1, cache2, pmgr, mgr)

    gamma = sdd.sdd_disjoin(qrimes0, qrimes1, pmgr)
    gamma = sdd.sdd_conjoin(_sdd_unused(var, pmgr), gamma, pmgr)
    kappa = sdd.sdd_conjoin(primes0, sdd.sdd_negate(qrimes0, pmgr), pmgr)
    kappa = sdd.sdd_conjoin(kappa, _sdd_used_neg(var, pmgr), pmgr)
    gamma = sdd.sdd_disjoin(gamma, kappa, pmgr)
    kappa = sdd.sdd_conjoin(primes1, sdd.sdd_negate(qrimes1, pmgr), pmgr)
    kappa = sdd.sdd_conjoin(kappa, _sdd_used_pos(var, pmgr), pmgr)
    gamma = sdd.sdd_disjoin(gamma, kappa, pmgr)

    cache1[key] = gamma
    return gamma
Esempio n. 2
0
def is_monotonic(alpha, mgr, num_features, constraint_sdd):

    counterexample = [[None, None] for _ in xrange(num_features)]
    for i in xrange(num_features):
        beta1 = sdd.sdd_condition(i + 1, alpha, mgr)
        beta2 = sdd.sdd_condition(-(i + 1), alpha, mgr)
        beta3 = sdd.sdd_conjoin(beta1, beta2, mgr)

        # check if f|x does not entail f|!x
        gamma = sdd.sdd_conjoin(
            sdd.sdd_conjoin(sdd.sdd_negate(beta2, mgr), beta1, mgr),
            constraint_sdd, mgr)
        model = next(models.models(gamma, sdd.sdd_manager_vtree(mgr)))
        counterexample[i][0] = [v for _, v in model.items()]
        if counterexample[i][0]:
            counterexample[i][0][i] = 1

        # check if f|!x does not entail f|x
        gamma = sdd.sdd_conjoin(
            sdd.sdd_conjoin(sdd.sdd_negate(beta1, mgr), beta2, mgr),
            constraint_sdd, mgr)
        model = next(models.models(gamma, sdd.sdd_manager_vtree(mgr)))
        counterexample[i][1] = [v for _, v in model.items()]
        if counterexample[i][1]:
            counterexample[i][1][i] = 0

    for c in counterexample:
        if c[0] and c[1]:
            return False, counterexample
    return True, counterexample
Esempio n. 3
0
def _primes_one_given_term(alpha, variables, inst, cache, cache_dummy, pmgr,
                           mgr):
    if len(variables) == 0:
        if sdd.sdd_node_is_true(alpha): return sdd.sdd_manager_true(pmgr)
        if sdd.sdd_node_is_false(alpha): return sdd.sdd_manager_false(pmgr)
    #add cases for true/false

    key = (len(variables), sdd.sdd_id(alpha))
    if key in cache:
        return cache[key]

    var, remaining = variables[0], variables[1:]
    val, remaining_val = inst[0], inst[1:]
    beta2 = sdd.sdd_forall(var, alpha, mgr)

    gamma2 = _primes_one_given_term(beta2, remaining, remaining_val, cache,
                                    cache_dummy, pmgr, mgr)
    gamma9 = gamma2
    pvar = 3 * (var - 1) + 1
    kappa2 = sdd.sdd_manager_literal(-pvar, pmgr)
    gamma2 = sdd.sdd_conjoin(gamma2, kappa2, pmgr)

    if val == 0:
        beta0 = sdd.sdd_condition(-var, alpha, mgr)
        gamma0 = _primes_one_given_term(beta0, remaining, remaining_val, cache,
                                        cache_dummy, pmgr, mgr)
        gamma0 = sdd.sdd_conjoin(gamma0, sdd.sdd_negate(gamma9, pmgr), pmgr)
        kappa0 = sdd.sdd_conjoin(sdd.sdd_manager_literal(-(pvar + 1), pmgr),
                                 sdd.sdd_manager_literal((pvar + 2), pmgr),
                                 pmgr)
        kappa0 = sdd.sdd_conjoin(kappa0, sdd.sdd_manager_literal(pvar, pmgr),
                                 pmgr)
        gamma0 = sdd.sdd_conjoin(gamma0, kappa0, pmgr)
        #gamma0 = sdd.sdd_conjoin(gamma0,sdd.sdd_negate(gamma9,pmgr),pmgr)

    if val == 1:
        beta1 = sdd.sdd_condition(var, alpha, mgr)
        gamma1 = _primes_one_given_term(beta1, remaining, remaining_val, cache,
                                        cache_dummy, pmgr, mgr)
        gamma1 = sdd.sdd_conjoin(gamma1, sdd.sdd_negate(gamma9, pmgr), pmgr)
        kappa1 = sdd.sdd_conjoin(sdd.sdd_manager_literal((pvar + 1), pmgr),
                                 sdd.sdd_manager_literal(-(pvar + 2), pmgr),
                                 pmgr)
        kappa1 = sdd.sdd_conjoin(kappa1, sdd.sdd_manager_literal(pvar, pmgr),
                                 pmgr)
        gamma1 = sdd.sdd_conjoin(gamma1, kappa1, pmgr)
        #gamma1 = sdd.sdd_conjoin(gamma1,sdd.sdd_negate(gamma9,pmgr),pmgr)

    if val == 0:
        gamma = sdd.sdd_disjoin(gamma0, gamma2, pmgr)
    if val == 1:
        gamma = sdd.sdd_disjoin(gamma1, gamma2, pmgr)
    #gamma = sdd.sdd_disjoin(sdd.sdd_disjoin(gamma0, gamma1, pmgr), gamma2, pmgr)

    #if len(variables) > 60:
    #  print len(variables), sdd.sdd_manager_count(mgr)
    cache[key] = gamma
    return gamma
Esempio n. 4
0
def enumerate_primes(primes, pmgr, var_count):
    pvtree = sdd.sdd_manager_vtree(pmgr)
    while not sdd.sdd_node_is_false(primes):
        mincard = sdd.sdd_global_minimize_cardinality(primes, pmgr)
        for model in models.models(mincard, pvtree):
            term = prime_to_dict(model, var_count)
            yield term
        primes = sdd.sdd_conjoin(primes, sdd.sdd_negate(mincard, pmgr), pmgr)
Esempio n. 5
0
def _remove_dummies(alpha, var_count, pmgr):
    for var in xrange(1, var_count + 1):
        var = 3 * (var - 1) + 1
        beta = sdd.sdd_manager_literal(-var, pmgr)
        gamma = sdd.sdd_disjoin(sdd.sdd_manager_literal(var + 1, pmgr),
                                sdd.sdd_manager_literal(var + 2, pmgr), pmgr)
        beta = sdd.sdd_conjoin(beta, gamma, pmgr)
        alpha = sdd.sdd_conjoin(alpha, sdd.sdd_negate(beta, pmgr), pmgr)
    return alpha
Esempio n. 6
0
def _primes_one(alpha, variables, cache, cache_dummy, pmgr, mgr):
    if len(variables) == 0:
        if sdd.sdd_node_is_true(alpha): return sdd.sdd_manager_true(pmgr)
        if sdd.sdd_node_is_false(alpha): return sdd.sdd_manager_false(pmgr)
    #add cases for true/false

    key = (len(variables), sdd.sdd_id(alpha))
    if key in cache:
        global cache_hits
        cache_hits += 1
        #if cache_hits % 1000 == 0: print "cache-hits-update:", cache_hits
        return cache[key]

    var, remaining = variables[0], variables[1:]
    beta2 = sdd.sdd_forall(var, alpha, mgr)
    gamma2 = _primes_one(beta2, remaining, cache, cache_dummy, pmgr, mgr)
    gamma9 = gamma2
    pvar = 3 * (var - 1) + 1
    kappa2 = sdd.sdd_manager_literal(-pvar, pmgr)
    gamma2 = sdd.sdd_conjoin(gamma2, kappa2, pmgr)

    beta0 = sdd.sdd_condition(-var, alpha, mgr)
    gamma0 = _primes_one(beta0, remaining, cache, cache_dummy, pmgr, mgr)
    gamma0 = sdd.sdd_conjoin(gamma0, sdd.sdd_negate(gamma9, pmgr), pmgr)
    kappa0 = sdd.sdd_conjoin(sdd.sdd_manager_literal(-(pvar + 1), pmgr),
                             sdd.sdd_manager_literal((pvar + 2), pmgr), pmgr)
    kappa0 = sdd.sdd_conjoin(kappa0, sdd.sdd_manager_literal(pvar, pmgr), pmgr)
    gamma0 = sdd.sdd_conjoin(gamma0, kappa0, pmgr)
    #gamma0 = sdd.sdd_conjoin(gamma0,sdd.sdd_negate(gamma9,pmgr),pmgr)

    beta1 = sdd.sdd_condition(var, alpha, mgr)
    gamma1 = _primes_one(beta1, remaining, cache, cache_dummy, pmgr, mgr)
    gamma1 = sdd.sdd_conjoin(gamma1, sdd.sdd_negate(gamma9, pmgr), pmgr)
    kappa1 = sdd.sdd_conjoin(sdd.sdd_manager_literal((pvar + 1), pmgr),
                             sdd.sdd_manager_literal(-(pvar + 2), pmgr), pmgr)
    kappa1 = sdd.sdd_conjoin(kappa1, sdd.sdd_manager_literal(pvar, pmgr), pmgr)
    gamma1 = sdd.sdd_conjoin(gamma1, kappa1, pmgr)
    #gamma1 = sdd.sdd_conjoin(gamma1,sdd.sdd_negate(gamma9,pmgr),pmgr)

    gamma = sdd.sdd_disjoin(gamma0, gamma1, pmgr)
    gamma = sdd.sdd_disjoin(gamma, gamma2, pmgr)

    cache[key] = gamma
    return gamma
Esempio n. 7
0
def test_admission():
    var_count = 4
    vtree = sdd.sdd_vtree_new(var_count, "balanced")
    mgr = sdd.sdd_manager_new(vtree)

    # WFEG
    # ( w ^ g )
    alpha = sdd.sdd_conjoin(sdd.sdd_manager_literal(1, mgr),
                            sdd.sdd_manager_literal(4, mgr), mgr)
    # ( w ^ f ^ e )
    beta = sdd.sdd_conjoin(sdd.sdd_manager_literal(1, mgr),
                           sdd.sdd_manager_literal(2, mgr), mgr)
    beta = sdd.sdd_conjoin(beta, sdd.sdd_manager_literal(3, mgr), mgr)
    # ( f ^ e ^ g )
    gamma = sdd.sdd_conjoin(sdd.sdd_manager_literal(2, mgr),
                            sdd.sdd_manager_literal(3, mgr), mgr)
    gamma = sdd.sdd_conjoin(gamma, sdd.sdd_manager_literal(4, mgr), mgr)
    alpha = sdd.sdd_disjoin(alpha, beta, mgr)
    alpha = sdd.sdd_disjoin(alpha, gamma, mgr)

    alpha = sdd.sdd_negate(alpha, mgr)
    beta, pmgr = primes(alpha, mgr)
    _sanity_check(alpha, mgr, beta, pmgr)
    vtree = sdd.sdd_manager_vtree(mgr)
    pvtree = sdd.sdd_manager_vtree(pmgr)

    import models
    for model in models.models(alpha, vtree):
        print models.str_model(model)

    for model in models.models(beta, pvtree):
        print models.str_model(model)

    for model in models.models(alpha, vtree):
        print "==", models.str_model(model)
        model_list = [model[var] for var in sorted(model.keys())]
        gamma, pmgr = compatible_primes(alpha,
                                        model_list,
                                        mgr,
                                        primes_mgr=(beta, pmgr))
        pvtree = sdd.sdd_manager_vtree(pmgr)
        for prime_model in models.models(gamma, pvtree):
            print models.str_model(prime_model)
            term = prime_to_dict(prime_model, var_count)
            print " ".join([
                ("*" if var not in term else "+" if term[var] == 1 else "-")
                for var in xrange(1, var_count + 1)
            ])

    print "dead-nodes:", sdd.sdd_manager_dead_count(mgr)
    print "dead-nodes:", sdd.sdd_manager_dead_count(pmgr)
Esempio n. 8
0
 def negate(self, node):
     new_sdd = sdd.sdd_negate(node, self.__manager)
     self.ref(new_sdd)
     return new_sdd
Esempio n. 9
0
 def negate(self, node):
     assert node is not None
     new_sdd = sdd.sdd_negate(node, self.__manager)
     self.ref(new_sdd)
     return new_sdd