Ejemplo n.º 1
0
def run_prime_implicant_query(alpha, mgr, num_features, models_list):
    #print num_features, models_list
    for model_list in models_list:
        gamma, pmgr, pvtree2 = primes_given_term(alpha, model_list, mgr,
                                                 _primes_one_given_term)
        pvtree = sdd.sdd_manager_vtree(pmgr)
        pi_str = []

        gamma = sdd.sdd_global_minimize_cardinality(gamma, pmgr)
        for prime_model in models.models(gamma, pvtree):
            try:
                term = prime_to_dict(prime_model, num_features)
                term_str = " ".join([("*" if var not in term else
                                      "1" if term[var] == 1 else "0")
                                     for var in xrange(1, num_features + 1)])
                pi_str.append(term_str)
            except:
                pi_str = [
                    "Key error. Make sure instance is is a model of the SDD."
                ]
        pi_str.sort(key=lambda x: x.count('*'), reverse=True)

        print "Model: " + str(model_list) + ""
        print "PI explanations:"
        for pi in pi_str[:3]:
            print str(pi)

        sdd.sdd_vtree_free(pvtree2)
        sdd.sdd_manager_free(pmgr)
Ejemplo n.º 2
0
 def GetLocalConstraintsForRoot(self, file_prefix):
     then_vtree_filename = "%s/%s_then_vtree.vtree" % (file_prefix,
                                                       self.name)
     then_sdd_filename = "%s/%s_then_sdd.sdd" % (file_prefix, self.name)
     constraint = {}
     constraint["then_vtree"] = then_vtree_filename
     constraint["then"] = [then_sdd_filename]
     universe = []
     # internal edges
     for sub_region_edge_tup in self.sub_region_edges:
         universe.append(sub_region_edge_tup)
     GraphSet.set_universe(universe)
     universe = GraphSet.universe()
     paths = GraphSet()
     child_names = self.children.keys()
     for (i, j) in itertools.combinations(child_names, 2):
         paths = paths.union(GraphSet.paths(i, j))
     name_to_sdd_index = {}
     zdd_to_sdd_index = [None]  # for generating sdd from graphset
     sdd_index = 0
     for child in child_names:
         sdd_index += 1
         name_to_sdd_index["c%s" % child] = sdd_index
     for sub_region_edge in universe:
         corresponding_network_edges = self.sub_region_edges[
             sub_region_edge]
         coresponding_network_edges_sdd_index = []
         for single_edge in corresponding_network_edges:
             sdd_index += 1
             name_to_sdd_index[str(single_edge)] = sdd_index
             coresponding_network_edges_sdd_index.append(sdd_index)
         zdd_to_sdd_index.append(coresponding_network_edges_sdd_index)
     constraint["then_variable_mapping"] = name_to_sdd_index
     rl_vtree = sdd.sdd_vtree_new(sdd_index, "right")
     sdd_manager = sdd.sdd_manager_new(rl_vtree)
     sdd.sdd_vtree_free(rl_vtree)
     sdd.sdd_manager_auto_gc_and_minimize_off(sdd_manager)
     # Construct simple path constraint
     simple_path_constraint = generate_sdd_from_graphset(
         paths, sdd_manager, zdd_to_sdd_index)
     # non empty path in this region map
     none_of_child = sdd.util.sdd_negative_term(
         sdd_manager,
         [name_to_sdd_index["c%s" % child] for child in self.children])
     case_one = sdd.sdd_conjoin(none_of_child, simple_path_constraint,
                                sdd_manager)
     # empty path in this region map
     exactly_one_child = sdd.util.sdd_exactly_one(
         sdd_manager,
         [name_to_sdd_index["c%s" % child] for child in self.children])
     empty_path_constraint = sdd.util.sdd_negative_term(
         sdd_manager, sum(zdd_to_sdd_index[1:], []))
     case_two = sdd.sdd_conjoin(exactly_one_child, empty_path_constraint,
                                sdd_manager)
     total_constraint = sdd.sdd_disjoin(case_one, case_two, sdd_manager)
     sdd.sdd_save(then_sdd_filename, total_constraint)
     sdd.sdd_vtree_save(then_vtree_filename,
                        sdd.sdd_manager_vtree(sdd_manager))
     sdd.sdd_manager_free(sdd_manager)
     return constraint
Ejemplo n.º 3
0
 def GetLocalConstraintsForInternalClusters(self, file_prefix):
     if_vtree_filename = "%s/%s_if_vtree.vtree" % (file_prefix, self.name)
     if_sdd_filename_prefix = "%s/%s_if_sdd" % (file_prefix, self.name)
     then_vtree_filename = "%s/%s_then_vtree.vtree" % (file_prefix,
                                                       self.name)
     then_sdd_filename_prefix = "%s/%s_then_sdd" % (file_prefix, self.name)
     ifs = []
     thens = []
     if_variable_mapping = {}
     if_sdd_index = 0
     if_sdd_index += 1
     if_variable_mapping[
         "c%s" %
         self.name] = if_sdd_index  # cluster indicator for current cluster
     for external_edge in self.external_edges:
         if_sdd_index += 1
         if_variable_mapping[str(external_edge)] = if_sdd_index
     then_variable_mapping = {}
     # variables for the child clusters
     then_sdd_index = 0
     zdd_to_sdd_index = [None]
     for child in self.children:
         then_sdd_index += 1
         then_variable_mapping["c%s" % child] = then_sdd_index
     universe = self.sub_region_edges.keys()
     GraphSet.set_universe(universe)
     universe = GraphSet.universe()
     for node_pair in universe:
         correponding_sdd_indexes = []
         for internal_edge in self.sub_region_edges[node_pair]:
             then_sdd_index += 1
             then_variable_mapping[str(internal_edge)] = then_sdd_index
             correponding_sdd_indexes.append(then_sdd_index)
         zdd_to_sdd_index.append(correponding_sdd_indexes)
     if_vtree, then_vtree = sdd.sdd_vtree_new(if_sdd_index,
                                              "right"), sdd.sdd_vtree_new(
                                                  then_sdd_index, "right")
     if_manager, then_manager = sdd.sdd_manager_new(
         if_vtree), sdd.sdd_manager_new(then_vtree)
     sdd.sdd_manager_auto_gc_and_minimize_off(if_manager)
     sdd.sdd_manager_auto_gc_and_minimize_off(then_manager)
     sdd.sdd_vtree_free(if_vtree)
     sdd.sdd_vtree_free(then_vtree)
     #none of the external edges are used and cluster indicator is off
     case_index = 0
     case_one_if = sdd.util.sdd_negative_term(if_manager,
                                              range(1, if_sdd_index + 1))
     case_one_then = sdd.util.sdd_negative_term(
         then_manager, range(1, then_sdd_index + 1))
     sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                  case_one_if)
     sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                  case_one_then)
     ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
     thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     #none of the external edges are used and cluster indicator is on
     case_index += 1
     case_two_if = sdd.util.sdd_exactly_one_among(
         if_manager, [if_variable_mapping["c%s" % self.name]],
         range(1, if_sdd_index + 1))
     #***Non empty path in this region map
     none_of_child = sdd.util.sdd_negative_term(
         then_manager,
         [then_variable_mapping["c%s" % child] for child in self.children])
     paths = GraphSet()
     child_names = self.children.keys()
     for c1, c2 in itertools.combinations(child_names, 2):
         paths = paths.union(GraphSet.paths(c1, c2))
     simple_path_constraint = generate_sdd_from_graphset(
         paths, then_manager, zdd_to_sdd_index)
     case_one = sdd.sdd_conjoin(simple_path_constraint, none_of_child,
                                then_manager)
     #***Empty path in the region map
     exactly_one_chlid = sdd.util.sdd_exactly_one(
         then_manager,
         [then_variable_mapping["c%s" % child] for child in self.children])
     empty_path_constraint = sdd.util.sdd_negative_term(
         then_manager, sum(zdd_to_sdd_index[1:], []))
     case_two = sdd.sdd_conjoin(empty_path_constraint, exactly_one_chlid,
                                then_manager)
     case_two_then = sdd.sdd_disjoin(case_one, case_two, then_manager)
     sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                  case_two_if)
     sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                  case_two_then)
     ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
     thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     #Exactly one of the external edge is used and cluster_indicator is off
     aggregated_cases = {}
     for external_edge in self.external_edges:
         aggregated_cases.setdefault(self.external_edges[external_edge],
                                     []).append(external_edge)
     for entering_node in aggregated_cases:
         case_index += 1
         cur_case_if = sdd.util.sdd_exactly_one_among(
             if_manager, [
                 if_variable_mapping[str(e)]
                 for e in aggregated_cases[entering_node]
             ], range(1, if_sdd_index + 1))
         paths = GraphSet()
         for child in self.children:
             if child == entering_node:
                 continue
             paths = paths.union(GraphSet.paths(entering_node, child))
         cur_case_then = generate_sdd_from_graphset(paths, then_manager,
                                                    zdd_to_sdd_index)
         cur_case_then = sdd.sdd_disjoin(
             cur_case_then,
             sdd.util.sdd_negative_term(then_manager, [
                 then_variable_mapping[str(e)] for e in self.internal_edges
             ]), then_manager)
         #conjoin that all the child indicator is off
         cur_case_then = sdd.sdd_conjoin(
             cur_case_then,
             sdd.util.sdd_negative_term(then_manager, [
                 then_variable_mapping["c%s" % child]
                 for child in self.children
             ]), then_manager)
         sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                      cur_case_if)
         sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                      cur_case_then)
         ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
         thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     #Exactly two of the external edge is used and cluster_indicator is off
     aggregated_cases = {}
     for (i, j) in itertools.combinations(self.external_edges.keys(), 2):
         entering_points = (self.external_edges[i], self.external_edges[j])
         entering_points = (max(entering_points), min(entering_points))
         aggregated_cases.setdefault(entering_points, []).append((i, j))
     for entering_points in aggregated_cases:
         case_index += 1
         entering_edges = aggregated_cases[entering_points]
         cur_case_if = generate_exactly_two_from_tuples(
             if_manager,
             [(if_variable_mapping[str(e1)], if_variable_mapping[str(e2)])
              for (e1, e2) in entering_edges], range(1, if_sdd_index + 1))
         if entering_points[0] == entering_points[1]:
             cur_case_then = sdd.util.sdd_negative_term(
                 then_manager, range(1, then_sdd_index + 1))
         else:
             paths = GraphSet.paths(entering_points[0], entering_points[1])
             cur_case_then = generate_sdd_from_graphset(
                 paths, then_manager, zdd_to_sdd_index)
             cur_case_then = sdd.sdd_conjoin(
                 cur_case_then,
                 sdd.util.sdd_negative_term(then_manager, [
                     then_variable_mapping["c%s" % child]
                     for child in self.children
                 ]), then_manager)
         sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                      cur_case_if)
         sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                      cur_case_then)
         ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
         thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     sdd.sdd_vtree_save(if_vtree_filename,
                        sdd.sdd_manager_vtree(if_manager))
     sdd.sdd_vtree_save(then_vtree_filename,
                        sdd.sdd_manager_vtree(then_manager))
     sdd.sdd_manager_free(if_manager)
     sdd.sdd_manager_free(then_manager)
     constraint = {}
     constraint["if_vtree"] = if_vtree_filename
     constraint["if"] = ifs
     constraint["if_variable_mapping"] = if_variable_mapping
     constraint["then_vtree"] = then_vtree_filename
     constraint["then"] = thens
     constraint["then_variable_mapping"] = then_variable_mapping
     return constraint
Ejemplo n.º 4
0
 def GetLocalConstraintsForLeaveClusters(self, file_prefix):
     if_vtree_filename = "%s/%s_if_vtree.vtree" % (file_prefix, self.name)
     if_sdd_filename_prefix = "%s/%s_if_sdd" % (file_prefix, self.name)
     then_vtree_filename = "%s/%s_then_vtree.vtree" % (file_prefix,
                                                       self.name)
     then_sdd_filename_prefix = "%s/%s_then_sdd" % (file_prefix, self.name)
     ifs = []
     thens = []
     if_variable_mapping = {}
     if_sdd_index = 0
     if_sdd_index += 1
     if_variable_mapping[
         "c%s" %
         self.name] = if_sdd_index  # cluster indicator for current cluster
     for external_edge in self.external_edges:
         if_sdd_index += 1
         if_variable_mapping[str(external_edge)] = if_sdd_index
     then_variable_mapping = {}
     zdd_to_sdd_index = [None]
     universe = []
     node_pair_to_edges = {}
     for internal_edge in self.internal_edges:
         if (internal_edge.x, internal_edge.y) not in node_pair_to_edges:
             universe.append((internal_edge.x, internal_edge.y))
         node_pair_to_edges.setdefault((internal_edge.x, internal_edge.y),
                                       []).append(internal_edge)
     GraphSet.set_universe(universe)
     universe = GraphSet.universe()
     then_sdd_index = 0
     for node_pair in universe:
         correponding_sdd_indexes = []
         for internal_edge in node_pair_to_edges[node_pair]:
             then_sdd_index += 1
             then_variable_mapping[str(internal_edge)] = then_sdd_index
             correponding_sdd_indexes.append(then_sdd_index)
         zdd_to_sdd_index.append(correponding_sdd_indexes)
     if_vtree, then_vtree = sdd.sdd_vtree_new(if_sdd_index,
                                              "right"), sdd.sdd_vtree_new(
                                                  then_sdd_index, "right")
     if_manager, then_manager = sdd.sdd_manager_new(
         if_vtree), sdd.sdd_manager_new(then_vtree)
     sdd.sdd_manager_auto_gc_and_minimize_off(if_manager)
     sdd.sdd_manager_auto_gc_and_minimize_off(then_manager)
     sdd.sdd_vtree_free(if_vtree)
     sdd.sdd_vtree_free(then_vtree)
     #none of the external edges are used and cluster indicator is off
     case_index = 0
     case_one_if = sdd.util.sdd_negative_term(if_manager,
                                              range(1, if_sdd_index + 1))
     case_one_then = sdd.util.sdd_negative_term(
         then_manager, range(1, then_sdd_index + 1))
     sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                  case_one_if)
     sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                  case_one_then)
     ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
     thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     #none of the external edges are used and cluster indicator is on
     case_index += 1
     case_two_if = sdd.util.sdd_exactly_one_among(
         if_manager, [if_variable_mapping["c%s" % self.name]],
         range(1, if_sdd_index + 1))
     paths = GraphSet()
     for (i, j) in itertools.combinations(self.nodes, 2):
         paths = paths.union(GraphSet.paths(i, j))
     case_two_then = generate_sdd_from_graphset(paths, then_manager,
                                                zdd_to_sdd_index)
     sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                  case_two_if)
     sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                  case_two_then)
     ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
     thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     #exactly one of the external edge is used and cluster indicator is off
     aggregated_cases = {}
     for external_edge in self.external_edges:
         aggregated_cases.setdefault(self.external_edges[external_edge],
                                     []).append(external_edge)
     for entering_node in aggregated_cases:
         case_index += 1
         cur_case_if = sdd.util.sdd_exactly_one_among(
             if_manager, [
                 if_variable_mapping[str(e)]
                 for e in aggregated_cases[entering_node]
             ], range(1, if_sdd_index + 1))
         paths = GraphSet()
         for node in self.nodes:
             if node == entering_node:
                 continue
             paths = paths.union(GraphSet.paths(entering_node, node))
         cur_case_then = generate_sdd_from_graphset(paths, then_manager,
                                                    zdd_to_sdd_index)
         # disjoin the empty path
         cur_case_then = sdd.sdd_disjoin(
             cur_case_then,
             sdd.util.sdd_negative_term(then_manager,
                                        range(1, then_sdd_index + 1)),
             then_manager)
         sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                      cur_case_if)
         sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                      cur_case_then)
         ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
         thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     # exactly two of the external edge is used and cluster_indicator is off
     aggregated_cases = {}
     for (i, j) in itertools.combinations(self.external_edges.keys(), 2):
         entering_points = (self.external_edges[i], self.external_edges[j])
         entering_points = (max(entering_points), min(entering_points))
         aggregated_cases.setdefault(entering_points, []).append((i, j))
     for entering_points in aggregated_cases:
         case_index += 1
         entering_edges = aggregated_cases[entering_points]
         cur_case_if = generate_exactly_two_from_tuples(
             if_manager,
             [(if_variable_mapping[str(e1)], if_variable_mapping[str(e2)])
              for (e1, e2) in entering_edges], range(1, if_sdd_index + 1))
         if entering_points[0] == entering_points[1]:
             cur_case_then = sdd.util.sdd_negative_term(
                 then_manager, range(1, then_sdd_index + 1))
         else:
             paths = GraphSet.paths(entering_points[0], entering_points[1])
             cur_case_then = generate_sdd_from_graphset(
                 paths, then_manager, zdd_to_sdd_index)
         sdd.sdd_save("%s_%s" % (if_sdd_filename_prefix, case_index),
                      cur_case_if)
         sdd.sdd_save("%s_%s" % (then_sdd_filename_prefix, case_index),
                      cur_case_then)
         ifs.append("%s_%s" % (if_sdd_filename_prefix, case_index))
         thens.append("%s_%s" % (then_sdd_filename_prefix, case_index))
     sdd.sdd_vtree_save(if_vtree_filename,
                        sdd.sdd_manager_vtree(if_manager))
     sdd.sdd_vtree_save(then_vtree_filename,
                        sdd.sdd_manager_vtree(then_manager))
     sdd.sdd_manager_free(if_manager)
     sdd.sdd_manager_free(then_manager)
     constraint = {}
     constraint["if_vtree"] = if_vtree_filename
     constraint["if"] = ifs
     constraint["if_variable_mapping"] = if_variable_mapping
     constraint["then_vtree"] = then_vtree_filename
     constraint["then"] = thens
     constraint["then_variable_mapping"] = then_variable_mapping
     return constraint
Ejemplo n.º 5
0

def str_model(model, var_count=None):
    """Convert model to string."""
    if var_count is None:
        var_count = len(model)
    return " ".join(str(model[var]) for var in xrange(1, var_count + 1))


if __name__ == '__main__':
    var_count = 10
    vtree = sdd.sdd_vtree_new(var_count, "balanced")
    manager = sdd.sdd_manager_new(vtree)

    alpha = sdd.sdd_manager_false(manager)
    for var in xrange(1, var_count + 1):
        lit = sdd.sdd_manager_literal(-var, manager)
        alpha = sdd.sdd_disjoin(alpha, lit, manager)

    vt = sdd.sdd_manager_vtree(manager)
    model_count = 0
    for model in models(alpha, vt):
        model_count += 1
        print str_model(model, var_count=var_count)

    #lib_mc = sdd.sdd_model_count(alpha,manager)
    print "model count: %d" % model_count

    sdd.sdd_manager_free(manager)
    sdd.sdd_vtree_free(vtree)
Ejemplo n.º 6
0

def str_model(model, var_count=None):
    """Convert model to string."""
    if var_count is None:
        var_count = len(model)
    return " ".join(str(model[var]) for var in xrange(1, var_count + 1))


if __name__ == "__main__":
    var_count = 10
    vtree = sdd.sdd_vtree_new(var_count, "balanced")
    manager = sdd.sdd_manager_new(vtree)

    alpha = sdd.sdd_manager_false(manager)
    for var in xrange(1, var_count + 1):
        lit = sdd.sdd_manager_literal(-var, manager)
        alpha = sdd.sdd_disjoin(alpha, lit, manager)

    vt = sdd.sdd_manager_vtree(manager)
    model_count = 0
    for model in models(alpha, vt):
        model_count += 1
        print str_model(model, var_count=var_count)

    # lib_mc = sdd.sdd_model_count(alpha,manager)
    print "model count: %d" % model_count

    sdd.sdd_manager_free(manager)
    sdd.sdd_vtree_free(vtree)