def test_io(self): gs = GraphSet() st = gs.dumps() self.assertEqual(st, "B\n.\n") gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet()) gs = GraphSet([g0]) st = gs.dumps() self.assertEqual(st, "T\n.\n") gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet([g0])) v = [g0, g1, g12, g123, g1234, g134, g14, g4] gs = GraphSet(v) st = gs.dumps() gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet(v)) # skip this test, becasue string is treated as an element # gs = GraphSet(st) # self.assertEqual(gs, GraphSet(v)) with tempfile.TemporaryFile() as f: gs.dump(f) f.seek(0) gs = GraphSet.load(f) self.assertEqual(gs, GraphSet(v))
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)
def test_io(self): gs = GraphSet() st = gs.dumps() self.assertEqual(st, "B\n.\n") gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet()) gs = GraphSet([g0]) st = gs.dumps() self.assertEqual(st, "T\n.\n") gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet([g0])) v = [g0, g1, g12, g123, g1234, g134, g14, g4] gs = GraphSet(v) st = gs.dumps() gs = GraphSet.loads(st) self.assertEqual(gs, GraphSet(v)) # skip this test, becasue string is treated as an element # gs = GraphSet(st) # self.assertEqual(gs, GraphSet(v)) f = tempfile.TemporaryFile() gs.dump(f) f.seek(0) gs = GraphSet.load(f) self.assertEqual(gs, GraphSet(v))
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)