Exemplo n.º 1
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
Exemplo n.º 2
0
def all_paths(dimension,dim):
    from graphillion import GraphSet
    import graphillion.tutorial as tl
    start,goal = 1,(dimension[0]+1)*(dimension[1]+1)
    
    universe = tl.grid(*dimension)
    GraphSet.set_universe(universe)
    paths = GraphSet()
    for i in range(start,goal):
        for j in range(i+1,goal+1):
            paths = GraphSet.union(paths,GraphSet.paths(i,j))

    f = open("graphs/general_ends-%d-%d.zdd" % (dim[0],dim[1]),"w")
    paths.dump(f)
    f.close()

    nodes = [None] + [ (x,y) for x in xrange(dim[0]) for y in xrange(dim[1]) ]
    from collections import defaultdict
    graph = defaultdict(list)
    for index,edge in enumerate(paths.universe()):
        x,y = edge
        x,y = nodes[x],nodes[y]
        graph[x].append( (index+1,y) )
        graph[y].append( (index+1,x) )
    graph_filename = "graphs/general_ends-%d-%d.graph.pickle" % (dim[0],dim[1])

    with open(graph_filename,'wb') as output:
        pickle.dump(graph,output)
Exemplo n.º 3
0
def main():
    """Create a structure that represents all paths going from a group of startpoints to a group of endpoints.

    The start point given by the user is the NE point of a group of 4 points
    The end point given by the user is the NE point of a group of 4 points
        The other 3 points are the ones that are one step W, one step S, and two steps SW.

    """


    if len(sys.argv) != 5:
        print "usage: %s [GRID-M] [GRID-N] [STARTPOINT] [ENDPOINT]" % sys.argv[0]
        exit(1)
    dim = (int(sys.argv[1]),int(sys.argv[2]))
    rows = dim[0]
    cols = dim[1]
    dimension = (dim[0]-1,dim[1]-1)
    startpoint = int(sys.argv[3])
    endpoint = int(sys.argv[4])

    starts = neighbors(startpoint, cols)
    ends = neighbors(endpoint, cols)

    from graphillion import GraphSet
    import graphillion.tutorial as tl

    universe = tl.grid(*dimension)
    GraphSet.set_universe(universe)

    paths = GraphSet()

    for start in starts:
        for end in ends:
            paths = GraphSet.union(paths,GraphSet.paths(start,end))

    print "number of paths: " + str(paths.len())
    
    """ AC: SAVE ZDD TO FILE """
    f = open("graphs/fixed_ends-%d-%d-%d-%d.zdd" % (dim[0],dim[1],startpoint,endpoint),"w")
    paths.dump(f)
    f.close()

    
    """ AC: SAVE GRAPH """
    nodes = [None] + [ (x,y) for x in xrange(dim[0]) for y in xrange(dim[1]) ]
    from collections import defaultdict
    graph = defaultdict(list)
    for index,edge in enumerate(paths.universe()):
        x,y = edge
        x,y = nodes[x],nodes[y]
        graph[x].append( (index+1,y) )
        graph[y].append( (index+1,x) )
    graph_filename = "graphs/fixed_ends-%d-%d-%d-%d.graph.pickle" % (dim[0],dim[1],startpoint,endpoint)

    # save to file
    import pickle
    with open(graph_filename,'wb') as output:
        pickle.dump(graph,output)
     problem_spec = json.load(fp)
 edge_list = problem_spec["graph"]
 terminal_nodes = problem_spec["terminal_nodes"]
 non_terminal_nodes = problem_spec["non_terminal_nodes"]
 universe = ConvertEdgesToUniverse(edge_list)
 GraphSet.set_universe(universe)
 universe = GraphSet.universe()
 nodes = NodesInEdges(edge_list)
 terminal_path = {}
 non_terminal_path = {}
 path_cache = {}
 for i, j in itertools.combinations(nodes, 2):
     path_cache[(min(i, j), max(i, j))] = GraphSet.paths(i, j)
 internal_path = GraphSet()
 for path in path_cache.values():
     internal_path = internal_path.union(path)
 for node in terminal_nodes:
     cur_path = GraphSet()
     for other_node in nodes:
         if other_node == node:
             continue
         node_key = (min(node, other_node), max(node, other_node))
         cur_path = cur_path.union(path_cache[node_key])
     terminal_path[node] = cur_path
 result = {}
 result["internal_path"] = DumpToStr(internal_path)
 for node in terminal_nodes:
     result["terminal_%s" % node] = DumpToStr(terminal_path[node])
 for node_pair in non_terminal_nodes:
     min_node = min(node_pair[0], node_pair[1])
     max_node = max(node_pair[0], node_pair[1])
Exemplo n.º 5
0
    from graphillion import GraphSet
    import graphillion.tutorial as tl
    universe = tl.grid(*dimension)
    GraphSet.set_universe(universe)

    start,goal = 1,(dimension[0]+1)*(dimension[1]+1)
    #create an empty GraphSet
    paths = GraphSet()
    paths_no_mp = GraphSet()
    for i in range(start,goal):
        print i
        for j in range(i+1,goal+1):
            #paths = GraphSet.union(paths,GraphSet.paths(i,j))
            """Exclude midpoint"""
            if i != midpoint and j != midpoint:
                paths_no_mp = GraphSet.union(paths_no_mp,GraphSet.paths(i,j))
            paths = GraphSet.union(paths,GraphSet.paths(i,j))

    pathsThruMidpoint = paths.including(midpoint)
    pathsNoMidpoint = paths_no_mp.including(midpoint)

    #tl.draw(pathsThruMidpoint.choice())
    print "number of paths through midpoint: " + str(pathsThruMidpoint.len())
    print "number of paths without stopping at midpoint: " + str(pathsNoMidpoint.len())
    print_edge_numbering(paths.universe(),dim[0],dim[1])
    #print paths.universe()
    #for p in pathsThruMidpoint:
    #    print p

    #dim = (dimension[0]+1,dimension[1]+1)
    #""" AC: SAVE ZDD TO FILE
Exemplo n.º 6
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
Exemplo n.º 7
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