本文整理汇总了Python中graphillion.GraphSet类的典型用法代码示例。如果您正苦于以下问题:Python GraphSet类的具体用法?Python GraphSet怎么用?Python GraphSet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GraphSet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeZDD
def makeZDD(haps):
n = len(haps[1])
ng = len(haps)
universe = []
for i in range(n):
universe.append((i,i+1))
GraphSet.set_universe(universe)
g_univ = GraphSet({})
g1 = []
i = 1
for i in range(ng):
tmp = []
for j in range(n):
if haps[i][j] == 1:
tmp.append(universe[j])
if i == 0:
g1 = [tmp]
else :
g1.append(tmp)
G1 = GraphSet(g1)
return G1
示例2: test_forests
def test_forests(self):
try:
#universe = tl.grid(8, 8, 0.37)
universe = [(1, 2), (1, 10), (2, 3), (2, 11), (3, 12), (4, 5), (5, 6), (5, 14), (6, 15), (7, 8), (8, 9), (8, 17), (9, 18), (10, 11), (11, 12), (11, 20), (13, 22), (14, 15), (14, 23), (16, 25), (17, 26), (18, 27), (19, 20), (19, 28), (20, 21), (21, 22), (21, 30), (22, 23), (22, 31), (23, 24), (24, 25), (25, 26), (26, 27), (26, 35), (27, 36), (28, 29), (28, 37), (29, 30), (29, 38), (30, 31), (31, 40), (32, 33), (32, 41), (33, 42), (34, 35), (37, 38), (37, 46), (38, 39), (40, 41), (41, 42), (41, 50), (42, 51), (43, 52), (44, 45), (45, 54), (46, 55), (47, 48), (47, 56), (48, 49), (49, 58), (50, 59), (51, 52), (51, 60), (52, 53), (53, 54), (53, 62), (55, 56), (56, 65), (57, 58), (57, 66), (59, 60), (59, 68), (61, 62), (61, 70), (62, 63), (64, 65), (64, 73), (65, 74), (66, 75), (67, 76), (68, 69), (69, 70), (69, 78), (70, 71), (70, 79), (71, 80), (72, 81), (74, 75), (75, 76), (76, 77), (80, 81)]
GraphSet.set_universe(universe)
generators = [1, 9, 73, 81]
forests = GraphSet.forests(roots=generators, is_spanning=True)
self.assertEqual(len(forests), 54060425088)
too_large_trees = GraphSet()
for substation in generators:
too_large_trees |= GraphSet.trees(root=substation).larger(23)
safe_forests = forests.excluding(too_large_trees)
self.assertEqual(len(safe_forests), 294859080)
closed_switches = (forests - safe_forests).choice()
scores = {}
for switch in universe:
scores[switch] = 1 if switch in closed_switches else -1
failures = safe_forests.blocking().minimal()
self.assertEqual(len(failures), 1936)
failure = failures.choice()
for line in failure:
safe_forests = safe_forests.excluding(line)
self.assertEqual(len(safe_forests), 0)
except ImportError:
pass
示例3: test_paths
def test_paths(self):
try:
universe = tl.grid(8, 8)
GraphSet.set_universe(universe, traversal='bfs')
start = 1
goal = 81
paths = GraphSet.paths(start, goal)
if len(paths) == 980466698:
stderr.write("Warning: Graphillion requires 64-bit machines, though your machine might be 32-bit.\n")
self.assertEqual(len(paths), 3266598486981642)
key = 64
treasure = 18
paths_to_key = GraphSet.paths(start, key).excluding(treasure)
treasure_paths = paths.including(paths_to_key).including(treasure)
self.assertEqual(len(treasure_paths), 789438891932744)
self.assertTrue(treasure_paths < paths)
i = 0
data = []
for path in treasure_paths.rand_iter():
data.append(tl.how_many_turns(path))
if i == 100: break
i += 1
for path in treasure_paths.min_iter():
min_turns = tl.how_many_turns(path)
break
self.assertEqual(min_turns, 5)
except ImportError:
pass
示例4: test_forests
def test_forests(self):
try:
universe = tl.grid(8, 8, 0.37)
GraphSet.set_universe(universe)
generators = [1, 9, 73, 81]
forests = GraphSet.forests(roots=generators, is_spanning=True)
self.assertEqual(len(forests), 54060425088)
too_large_trees = GraphSet()
for substation in generators:
too_large_trees |= GraphSet.trees(root=substation).larger(23)
safe_forests = forests.excluding(too_large_trees)
self.assertEqual(len(safe_forests), 294859080)
closed_switches = (forests - safe_forests).choice()
scores = {}
for switch in universe:
scores[switch] = 1 if switch in closed_switches else -1
failures = safe_forests.blocking().minimal()
self.assertEqual(len(failures), 1936)
failure = failures.choice()
for line in failure:
safe_forests = safe_forests.excluding(line)
self.assertEqual(len(safe_forests), 0)
except ImportError:
pass
示例5: test_paths
def test_paths(self):
try:
universe = tl.grid(8, 8)
GraphSet.set_universe(universe)
start = 1
goal = 81
paths = GraphSet.paths(start, goal)
self.assertEqual(len(paths), 3266598486981642)
key = 64
treasure = 18
paths_to_key = GraphSet.paths(start, key).excluding(treasure)
treasure_paths = paths.including(paths_to_key).including(treasure)
self.assertEqual(len(treasure_paths), 789438891932744)
self.assertTrue(treasure_paths < paths)
i = 0
data = []
for path in treasure_paths.rand_iter():
data.append(tl.how_many_turns(path))
if i == 100: break
i += 1
for path in treasure_paths.min_iter():
min_turns = tl.how_many_turns(path)
break
self.assertEqual(min_turns, 5)
except ImportError:
pass
示例6: test_show_messages
def test_show_messages(self):
a = GraphSet.show_messages()
b = GraphSet.show_messages(True)
self.assertTrue(b)
c = GraphSet.show_messages(False)
self.assertTrue(c)
d = GraphSet.show_messages(a)
self.assertFalse(d)
示例7: capacity
def capacity(self):
gs = GraphSet()
self.assertFalse(gs)
gs = GraphSet([g0, g12, g13])
self.assertTrue(gs)
self.assertEqual(len(gs), 3)
self.assertEqual(gs.len(), 3)
示例8: main
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)
示例9: test_comparison
def test_comparison(self):
gs = GraphSet([g12])
self.assertEqual(gs, GraphSet([g12]))
self.assertNotEqual(gs, GraphSet([g13]))
# __nonzero__
self.assertTrue(gs)
self.assertFalse(GraphSet())
v = [g0, g12, g13]
gs = GraphSet(v)
self.assertTrue(gs.isdisjoint(GraphSet([g1, g123])))
self.assertFalse(gs.isdisjoint(GraphSet([g1, g12])))
self.assertTrue(gs.issubset(GraphSet(v)))
self.assertFalse(gs.issubset(GraphSet([g0, g12])))
self.assertTrue(gs <= GraphSet(v))
self.assertFalse(gs <= GraphSet([g0, g12]))
self.assertTrue(gs < GraphSet([g0, g1, g12, g13]))
self.assertFalse(gs < GraphSet(v))
self.assertTrue(gs.issuperset(GraphSet(v)))
self.assertFalse(gs.issuperset(GraphSet([g1, g12])))
self.assertTrue(gs >= GraphSet(v))
self.assertFalse(gs >= GraphSet([g1, g12]))
self.assertTrue(gs > GraphSet([[], g12]))
self.assertFalse(gs > GraphSet(v))
示例10: all_paths
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)
示例11: test_init
def test_init(self):
GraphSet.set_universe([('i', 'ii')])
self.assertEqual(GraphSet.universe(), [('i', 'ii')])
GraphSet.set_universe([e1 + (.3,), e2 + (-.2,), e3 + (-.2,), e4 + (.4,)])
self.assertEqual(GraphSet.universe(),
[e1 + (.3,), e2 + (-.2,), e3 + (-.2,), e4 + (.4,)])
GraphSet.set_universe([e1 + (.3,), e2 + (-.2,), e3 + (-.2,), e4 + (.4,)],
traversal='dfs', source=1)
self.assertEqual(GraphSet.universe(),
[e2 + (-.2,), e4 + (.4,), e1 + (.3,), e3 + (-.2,)])
self.assertRaises(KeyError, GraphSet.set_universe, [(1,2), (2,1)])
示例12: test_networkx
def test_networkx(self):
try:
import networkx as nx
except ImportError:
return
try:
if nx.__version__[0] == "1": # for NetworkX version 1.x
GraphSet.converters['to_graph'] = nx.Graph
GraphSet.converters['to_edges'] = nx.Graph.edges
else: # for NetworkX version 2.x
GraphSet.converters['to_graph'] = nx.from_edgelist
GraphSet.converters['to_edges'] = nx.to_edgelist
g = nx.grid_2d_graph(3, 3)
GraphSet.set_universe(g)
g = GraphSet.universe()
self.assertTrue(isinstance(g, nx.Graph))
self.assertEqual(len(g.edges()), 12)
v00, v01, v10 = (0,0), (0,1), (1,0)
e1, e2 = (v00, v01), (v00, v10)
gs = GraphSet([nx.Graph([e1])])
self.assertEqual(len(gs), 1)
g = gs.pop()
self.assertEqual(len(gs), 0)
self.assertTrue(isinstance(g, nx.Graph))
self.assertTrue(list(g.edges()) == [(v00, v01)] or list(g.edges()) == [(v01, v00)])
gs.add(nx.Graph([e2]))
self.assertEqual(len(gs), 1)
except:
raise
finally:
GraphSet.converters['to_graph'] = lambda edges: edges
GraphSet.converters['to_edges'] = lambda graph: graph
示例13: _enumerate_forests
def _enumerate_forests(self, suspicious_cut):
vg = [[r] for r in self.graph.roots]
dc = {}
l = len(self.graph.graph.nodes())
for v in self.graph.graph.nodes():
if v in suspicious_cut: dc[v] = 0
elif v in self.graph.roots: dc[v] = xrange(l)
else: dc[v] = xrange(1, l)
return GraphSet.graphs(vertex_groups=vg, degree_constraints=dc,
no_loop=True)
示例14: test_constructors
def test_constructors(self):
gs = GraphSet()
self.assertTrue(isinstance(gs, GraphSet))
self.assertEqual(len(gs), 0)
gs = GraphSet([])
self.assertEqual(len(gs), 0)
gs = GraphSet([g1, [(3,1)]])
self.assertEqual(len(gs), 2)
self.assertTrue(g1 in gs)
self.assertTrue(g2 in gs)
gs = GraphSet({})
self.assertEqual(len(gs), 2**4)
gs = GraphSet({'include': [e1, e2], 'exclude': [(4,3)]})
self.assertEqual(len(gs), 2)
self.assertTrue(g12 in gs)
self.assertTrue(g123 in gs)
self.assertRaises(KeyError, GraphSet, [(1,4)])
self.assertRaises(KeyError, GraphSet, [[(1,4)]])
self.assertRaises(KeyError, GraphSet, {'include': [(1,4)]})
# copy constructor
gs1 = GraphSet([g0, g12, g13])
gs2 = gs1.copy()
self.assertTrue(isinstance(gs2, GraphSet))
gs1.clear()
self.assertEqual(gs1, GraphSet())
self.assertEqual(gs2, GraphSet([g0, g12, g13]))
# repr
gs = GraphSet([g0, g12, g13])
self.assertEqual(
repr(gs),
"GraphSet([[], [(1, 2), (1, 3)], [(1, 2), (2, 4)]])")
gs = GraphSet({})
self.assertEqual(
repr(gs),
"GraphSet([[], [(1, 2)], [(1, 3)], [(2, 4)], [(3, 4)], [(1, 2), (1, 3)], [(1, ...")
示例15: _build_graph
def _build_graph(self):
graph = Graph()
sorted_sections = []
for s in self.switches:
ns = set()
for t in self._find_neighbors(s):
if t in self.sections:
ns.add(t)
neighbors = set()
is_root = False
for t in sorted(ns):
junctions = set([t])
for u in self._find_neighbors(t):
if u in self.sections:
junctions.add(u)
if u in self.sections and u < t:
t = u
neighbors.add(t)
if t not in sorted_sections:
sorted_sections.append(t)
v = sorted_sections.index(t) + 1
graph._section2vertex[t] = v
graph._vertex2sections[v] = tuple(junctions)
e = tuple([sorted_sections.index(t) + 1 for t in sorted(neighbors)])
assert len(e) == 2
graph.edges.append(e)
graph._switch2edge[s] = e
graph._edge2switch[e] = s
assert len(graph.edges) == len(self.switches)
for s in self.sections:
if self.sections[s]['substation']:
for t in self._find_neighbors(s):
if t < s:
s = t
graph.roots.add(sorted_sections.index(s) + 1)
assert len(graph.roots) == len(self._get_root_sections())
GraphSet.set_universe(graph.edges, traversal='as-is')
graph.graph = nx.Graph(graph.edges)
return graph