Beispiel #1
1
    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
Beispiel #2
0
    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
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)
Beispiel #4
0
def countGridGraphSet(num):
    universe = tl.grid(num, num)
    GraphSet.set_universe(universe)
    start = 1
    goal = (num + 1)**2
    paths = GraphSet.paths(start, goal)
    return len(paths)
Beispiel #5
0
    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
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
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)
# -*- coding: utf-8 -*-


from graphillion import GraphSet
import graphillion.tutorial as tl
n=15
m=10
nm = n+m
goal = (n+1)*(m+1)

universe=tl.grid(n,m)
GraphSet.set_universe(universe)

Gt=GraphSet.paths(1,goal)
Gs=Gt.graph_size(nm)

Guniv = GraphSet({})
Gss = Guniv.graph_size(nm)
Gs2 = Gss.paths(1,goal)

len(Gs)
len(Gs2)

# 3266598486981642
# 41044208702632496804
# 1568758030464750013214100
# 182413291514248049241470885236 
if __name__ == '__main__':
    if len(sys.argv) != 4:
        print "usage: %s [GRID-M] [GRID-N] [MIDPOINT]" % sys.argv[0]
        exit(1)
    dim = (int(sys.argv[1]),int(sys.argv[2]))
    dimension = (dim[0]-1,dim[1]-1)
    midpoint = int(sys.argv[3])
    #dimension = (1,1)

    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))
Beispiel #11
0
from graphillion import GraphSet
import graphillion.tutorial as tl

n = 8

universe = tl.grid(n, n)
GraphSet.set_universe(universe)
tl.draw(universe)

start = 1
goal = (n + 1)**2

paths = GraphSet.paths(start, goal)
print(len(paths))
Beispiel #12
0
import graphillion.tutorial as t1
universe = t1.grid(2,2)
GraphSet.set_universe(universe)

print universe

t1.draw(universe)
Beispiel #13
0
from graphillion import GraphSet
import graphillion.tutorial as tl

universe = tl.grid(2, 2)
GraphSet.set_universe(universe)
lines = GraphSet({'include': [(8, 9), (5, 8), (4, 5)], 'exclude': [(6, 9)]})
trees = GraphSet.trees(is_spanning=True)
common = trees & lines

for path in common:
    tl.draw(path)
Beispiel #14
0
'''









#チュートリアルの写経
from graphillion import GraphSet
import graphillion.tutorial as tl  # チュートリアルのためのヘルパー・モジュール


universe = tl.grid(8, 8)
GraphSet.set_universe(universe)
filepath = "test.txt"

print("good so far")

def cal(start, goal):
    paths = GraphSet.paths(start, goal)
    return len(paths)  
    # 結果が大規模のときは paths.len() を使う

n = cal(1, 81)
print(n)

with open(filepath, mode='w') as f:
    f.write(str(n))
from graphillion import GraphSet  # graphillionのクラスGraphSetをインポート
# graphillionのモジュールtutorialをtlという名前でインポート
import graphillion.tutorial as tl

universe = tl.grid(2, 2)  # 2x2のグリッドを生成
GraphSet.set_universe(universe)
# 必ず存在する辺と必ず存在しない辺を定義
lines = GraphSet({'include': [(8, 9), (5, 8), (4, 5)], 'exclude': [(6, 9)]})
# 全域木を生成
trees = GraphSet.trees(is_spanning=True)
common = trees & lines
# 結果の描画
for path in common:
    tl.draw(path)