Пример #1
0
    def test_dijkstra_parallel_user_specifications(self):
        # Spec out our squareGrid
        minX = 0  # [m]
        maxX = 5
        minY = 0
        maxY = 5
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type)

        # Either sample a percentage of the configuration space or get a fixed number of samples
        fl_dij = AllPairsShortestPath.dijkstra_in_parallel(graph)
        #user specify sampling percentage
        fl_dij = AllPairsShortestPath.dijkstra_in_parallel(
            graph, random_sampling_percentage=10)

        # user specify nodes to run
        nodes = [(1, 1), (3, 0), (2, 4), (2, 2)]
        fl_dij = AllPairsShortestPath.dijkstra_in_parallel(graph, nodes=nodes)
        #
        # user specify sampling limit
        fl_dij = AllPairsShortestPath.dijkstra_in_parallel(
            graph, random_sampling_limit=10)
        pass
Пример #2
0
    def test_returned_tree_values(self):
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # DEPOTS MUST BE A SUBSET OF TERMINALS!
        depots = [(-8, -4), (12, -3), (-11, 12), (1, -11), (-4, 6)]

        terminals = [(-8, -4), (0, -13), (1, -5), (-15, 2),
                     (-1, -7), (-11, 12), (11, 5), (-10, -5), (7, 6), (0, 4),
                     (-8, 11), (12, -10), (1, 1), (-6, 1), (-1, 11), (-3, 1),
                     (-12, -13), (-14, -1), (-13, -12), (14, 2), (15, -10),
                     (2, 11), (5, -8), (12, 8), (15, -8), (13, 13), (0, 14),
                     (3, 11), (-12, 0), (8, 9), (-4, 6), (1, -11), (-1, 1),
                     (0, -12), (-1, -2), (12, -3), (-6, 13)]
        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGridDepot",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type,
                                          depots=depots)

        #Store Tree values
        dist = []

        # # Test Dijkstra
        # ao = SstarDijkstra(graph, terminals)
        # # test comps type
        # self.assertIsInstance(ao.comps, dict)
        # # Run algorithm, does it return true?
        # self.assertTrue(ao.run_algorithm())
        # # Save dist
        # dist.append(sum(ao.return_solutions()['dist']))

        # Create Astar object
        ao = SstarHS(graph, terminals)
        # test comps type
        self.assertIsInstance(ao.comps, dict)
        # run algorithm
        self.assertTrue(ao.run_algorithm())
        dist.append(sum(ao.return_solutions()['dist']))

        # Test Primal Dual
        # Create Astar object
        ao = SstarBS(graph, terminals)
        # test comps type
        self.assertIsInstance(ao.comps, dict)
        # run algorithm
        self.assertTrue(ao.run_algorithm())
        dist.append(sum(ao.return_solutions()['dist']))

        # Test equivalence
        self.assertTrue(abs(max(dist) - min(dist)) <= 1e-9)
Пример #3
0
    def test_create_square_grid(self):
        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        sq = GraphFactory.create_graph("SquareGrid",
                                       grid=grid,
                                       grid_dim=grid_dim,
                                       grid_size=grid_size,
                                       n_type=n_type)

        # test to see if sq is an instance of SquareGrid
        from steinerpy.library.graphs.graph import SquareGrid
        self.assertTrue(isinstance(sq, SquareGrid))

        # test if instance values are correct
        self.assertEqual(sq.grid_size, grid_size)
        self.assertEqual(sq.grid_dim, grid_dim)
        self.assertEqual(sq.neighbor_type, n_type)

        # Define obstacles (physical coordinates, origin is lower-left)
        obstacles = [(x, 0) for x in range(-10, 10, 1)]
        obstacles.extend([(0, y) for y in range(-10, 10, 1)])

        # We can either add obstacles using 'set_obstacle' method, or do it with GraphFactory
        sq.set_obstacles(obstacles)

        # Show image (comment this out if it blocks)
        sq.show_grid()
        # print("")

        # get all graphs nodes
        nodes = sq.get_nodes()
        # print("")

        # get all edges
        edges = sq.get_edges()
        # print(sq.edge_count(), len(edges))

        # adjacency matrix
        adj = sq.get_adjacency_matrix()
Пример #4
0
    def test_kruskal_with_depots(self):

        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type
        # neighbor type

        # Define some depots
        # depots = [(-8,-4), (1,-11), (12,-10)]
        # depots = [(-15,-15), (-15,15)]
        # depots = [(27, 11), (45, 22), (62, 31), (13, 26), (35, 41)]
        depots = [(-8, -4), (12, -3), (-11, 12), (1, -11), (-4, 6)]

        # Create a squareGridDepot using GraphFactory
        graph = GraphFactory.create_graph("SquareGridDepot",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type,
                                          depots=depots)

        # Define some terminals
        # terminals = [(-10, -12), (-3, 10), (10, -7), (13, 6), (0, 3)]
        # terminals = [(-15,-15), (-15,15), (15,15), (15,-15)]
        terminals = [(-8, -4), (0, -13), (1, -5), (-15, 2),
                     (-1, -7), (-11, 12), (11, 5), (-10, -5), (7, 6), (0, 4),
                     (-8, 11), (12, -10), (1, 1), (-6, 1), (-1, 11), (-3, 1),
                     (-12, -13), (-14, -1), (-13, -12), (14, 2), (15, -10),
                     (2, 11), (5, -8), (12, 8), (15, -8), (13, 13), (0, 14),
                     (3, 11), (-12, 0), (8, 9), (-4, 6), (1, -11), (-1, 1),
                     (0, -12), (-1, -2), (12, -3), (-6, 13)]
        # terminals = [(62, 15), (63, 14), (3, 52), (35, 41), (59, 36), (7, 42), (28, 26), (45, 22), (62, 31), (28, 46), (23, 44), (5, 60), (10, 35), (61, 60), (36, 50), (57, 59), (19, 60), (59, 46), (53, 23), (63, 44), (44, 36), (59, 4), (33, 9), (44, 55), (63, 10), (45, 54), (49, 51), (13, 26), (43, 29), (0, 49), (35, 51), (27, 11), (25, 47), (38, 44), (49, 36), (23, 63), (55, 49), (11, 31), (28, 42), (50, 46), (46, 13), (55, 34), (34, 26), (19, 17), (44, 57), (17, 1), (31, 17), (4, 58), (31, 14), (21, 55), (7, 4), (39, 51), (4, 44), (62, 25), (2, 29), (59, 18), (11, 14), (55, 13), (29, 39), (7, 33), (23, 17), (6, 62), (23, 15), (27, 32), (13, 28), (25, 52), (53, 12), (60, 31), (43, 49), (35, 2), (60, 10), (7, 13), (49, 18), (38, 36), (57, 10), (27, 52), (14, 30), (28, 55), (35, 18), (17, 3), (38, 13), (9, 37), (41, 19), (43, 37), (34, 1), (30, 28), (53, 63), (60, 1), (18, 60), (54, 35), (52, 26), (61, 51), (34, 40), (7, 45), (26, 63), (45, 29), (50, 31), (35, 42), (60, 34), (54, 29)]

        # Use context to run kruskal
        context = Context(graph, terminals)

        self.assertTrue(context.run('Kruskal'))
        results = context.return_solutions()

        # test comps type
        self.assertIsInstance(results, dict)
Пример #5
0
    def test_sstar_astar_with_depots(self):

        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Define some depots
        # depots = [(-10,-12), (0,3)]
        # depots = [(-15,-15), (-15,15)]
        # depots = [(-8,-4), (1,-11), (12,-10), (-15, 2), (12,-3), (-11,12)]
        depots = [(-8, -4), (12, -3), (-11, 12), (1, -11), (-4, 6)]
        # depots = [(-8,-4), (-11,12), (12,-10)]

        # Create a squareGridDepot using GraphFactory
        graph = GraphFactory.create_graph("SquareGridDepot",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type,
                                          depots=depots)

        # Define some terminals
        # terminals = [(-10, -12), (-3, 10), (10, -7), (13, 6), (0, 3)]
        # terminals = [(-15,-15), (-15,15), (15,15), (15,-15)]
        terminals = [(-8, -4), (0, -13), (1, -5), (-15, 2),
                     (-1, -7), (-11, 12), (11, 5), (-10, -5), (7, 6), (0, 4),
                     (-8, 11), (12, -10), (1, 1), (-6, 1), (-1, 11), (-3, 1),
                     (-12, -13), (-14, -1), (-13, -12), (14, 2), (15, -10),
                     (2, 11), (5, -8), (12, 8), (15, -8), (13, 13), (0, 14),
                     (3, 11), (-12, 0), (8, 9), (-4, 6), (1, -11), (-1, 1),
                     (0, -12), (-1, -2), (12, -3), (-6, 13)]
        # terminals = [(-8, -4), (0, -13), (1, -5), (-15, 2), (-1, -7), (-11, 12)]

        # Create Astar object
        ao = SstarHS(graph, terminals)

        # test comps type
        self.assertIsInstance(ao.comps, dict)

        # run algorithm
        self.assertTrue(ao.run_algorithm())
Пример #6
0
    def test_all_pairs_shortest_path_methods(self):
        # Spec out our squareGrid
        minX = 0  # [m]
        maxX = 5
        minY = 0
        maxY = 5
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type)

        tstart = timer()
        fl_dij = AllPairsShortestPath.dijkstra_in_parallel(
            graph, flatten_results_into_pairs=True)
        fl_dij_time = timer() - tstart

        #try running floyd_warshall
        # c = cProfile.Profile()
        # c.enable()
        tstart = timer()
        fl = AllPairsShortestPath.floyd_warshall_simple_slow_parallel(
            graph, processes=4)
        fl_time = timer() - tstart
        # c.disable()
        # c.print_stats()

        tstart = timer()
        fl_np = AllPairsShortestPath.floyd_warshall_simple_slow(graph)
        fl_np_time = timer() - tstart

        print("parallel dij: ", fl_dij_time)
        print("parallel fw: ", fl_time)
        print("sequential fw: ", fl_np_time)
        # compare results
        for x in fl_np:
            if round(fl_np[x]) != round(fl[x]) or round(fl_dij[x]) != round(
                    fl_np[x]):
                print(x, fl_np[x], fl[x], fl_dij[x])
                raise ValueError
Пример #7
0
    def test_generate_landmarks(self):
        # Spec out our squareGrid
        minX = 0			# [m]
        maxX = 15      
        minY = 0
        maxY = 15
        grid = None         # pre-existing 2d numpy array?
        grid_size = 1       # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8           # neighbor type

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid", grid=grid, grid_dim=grid_dim, grid_size=grid_size, n_type= n_type)      

        gh = GenerateHeuristics.get_heuristics(graph, processes=cpu_count)
        
        # try retreiving some lower_bound values
        val = GenerateHeuristics.retrieve_heuristic_value(gh, (0,0), (4,5))
        
        print("\n",val)
Пример #8
0
    def test_floyd_warshall_simple_slow(self):
        # Spec out our squareGrid
        minX = 0  # [m]
        maxX = 7
        minY = 0
        maxY = 7
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type)

        #try running floyd_warshall
        fl = AllPairsShortestPath.floyd_warshall_simple_slow(graph)
        print("")
Пример #9
0
    def test_create_generic(self):
        from steinerpy.library.graphs.graph import MyGraph

        # Define some edges
        edgeDict = {
            ('v1', 'v2'): 1,
            ('v2', 'v3'): 1,
            ('v3', 'v4'): 1,
            ('v4', 'v5'): 1,
            ('v5', 'v6'): 1,
            ('v6', 'v7'): 1,
            ('v7', 'v8'): 1,
            ('v8', 'v5'): 1
        }

        # Create a generic graph using factory method
        genG = GraphFactory.create_graph("Generic",
                                         edge_dict=edgeDict,
                                         graph_type="undirected",
                                         visualize=False)

        # test to see if genG is an instance of MyGraph
        self.assertTrue(isinstance(genG, MyGraph))
Пример #10
0
    def test_kruskal(self):

        # Spec out our squareGrid
        minX = -15			# [m]
        maxX = 15           
        minY = -15
        maxY = 15
        grid = None         # pre-existing 2d numpy array?
        grid_size = 1       # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8           # neighbor type

        # obstacles?
        # obstacles = [(0,0), (8,9), (5, 5), (11, 9), (2, 0), (4,0), (1,3)]
        obstacles = [(2,y) for y in range(-15,11)]
        obstacles.extend([(x,11) for x in range(-5,5)])
        # obstacles = []

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid", grid=grid, grid_dim=grid_dim, grid_size=grid_size, n_type= n_type, obstacles=obstacles)      

        # Define terminals
        terminals = [(-10, -12), (-3, 10), (10, -7), (13, 6), (0, 3)]

        # Create Astar object
        ao = Kruskal(graph, terminals)

        # run algorithm
        self.assertTrue(ao.run_algorithm())

        # Get solution
        self.assertTrue(isinstance(ao.return_solutions(), dict))

        # make sure solution is non-empty
        self.assertTrue(len(ao.return_solutions()['sol'])>0)
        self.assertTrue(len(ao.return_solutions()['path'])>0)
        self.assertTrue(len(ao.return_solutions()['dist'])>0)
Пример #11
0
    def test_astar(self):

        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        graph = GraphFactory.create_graph("SquareGrid",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type)

        # Define terminals
        terminals = [(-10, -12), (-3, 10), (10, -7), (13, 6), (0, 3)]
        # terminals = [(-15, -15), (15, 15)]

        # Create Astar object
        ao = Unmerged(graph, terminals)

        # test comps type
        self.assertIsInstance(ao.comps, dict)

        # run algorithm
        self.assertTrue(ao.run_algorithm())

        self.assertTrue(isinstance(ao.return_solutions(), dict))

        self.assertTrue(len(ao.return_solutions()['sol']) > 0)
        self.assertTrue(len(ao.return_solutions()['path']) > 0)
        self.assertTrue(len(ao.return_solutions()['dist']) > 0)
Пример #12
0
    def test_sstar_primaldual_with_depots(self):

        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Define some depots
        # depots = [(-10,-12), (0,3)]
        depots = [(-15, -15), (-15, 15)]

        # Create a squareGridDepot using GraphFactory
        graph = GraphFactory.create_graph("SquareGridDepot",
                                          grid=grid,
                                          grid_dim=grid_dim,
                                          grid_size=grid_size,
                                          n_type=n_type,
                                          depots=depots)

        # Define some terminals
        # terminals = [(-10, -12), (-3, 10), (10, -7), (13, 6), (0, 3)]
        terminals = [(-15, -15), (-15, 15), (15, 15), (15, -15)]

        # Create Astar object
        ao = SstarBS(graph, terminals)

        # test comps type
        self.assertIsInstance(ao.comps, dict)

        # run algorithm
        self.assertTrue(ao.run_algorithm())
Пример #13
0
if __name__ == "__main__":
    # Spec out our squareGrid
    minX = -15  # [m]
    maxX = 15
    minY = -15
    maxY = 15
    grid = None  # pre-existing 2d numpy array?
    grid_size = 1  # grid fineness[m]
    grid_dim = [minX, maxX, minY, maxY]
    n_type = 8  # neighbor type
    # Create a squareGrid using GraphFactory
    from steinerpy.library.graphs.graph import GraphFactory
    graph = GraphFactory.create_graph("SquareGrid",
                                      grid=grid,
                                      grid_dim=grid_dim,
                                      grid_size=grid_size,
                                      n_type=n_type)

    # generate m instances with N terminals
    N = 2
    instances = 10

    import steinerpy.config as cfg
    save_directory = cfg.results_dir + "/tests"
    # specify directory to write baseline file to
    # directory = os.path.dirname(os.path.realpath(__file__))+"/../"

    #create and run
    gb = GenerateBaseLine(graph, N, instances, save_directory,
                          'baseline_{}t-{}i.pkl'.format(N, instances))
    def test_alg_baseline(self):
        # Spec out our squareGrid
        minX = -15  # [m]
        maxX = 15
        minY = -15
        maxY = 15
        grid = None  # pre-existing 2d numpy array?
        grid_size = 1  # grid fineness[m]
        grid_dim = [minX, maxX, minY, maxY]
        n_type = 8  # neighbor type

        # Create a squareGrid using GraphFactory
        try:
            graph = GraphFactory.create_graph("SquareGrid",
                                              grid=grid,
                                              grid_dim=grid_dim,
                                              grid_size=grid_size,
                                              n_type=n_type)
            self.assertTrue(True)
        except Exception as _e:
            raise _e

        # Load Kruskal Baseline data
        directory = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(directory, 'baseline_test_single.pkl'),
                  'rb') as f:
            data = pickle.load(f)
        '''data={ 
            'solution': [{'sol':[], 'path':[], 'dist':[]}, {...}, ..., {}]
            'terminals':[ [(x,y),...,(xn,yn)],[...],[...],....,[] ]
            }
        '''

        # Use contextualizer to run algorithms (make sure debug visualizer is off)
        results = {
            'S*-unmerged': [],
            'S*-HS': [],
            'S*-HS0': [],
        }

        # Fill 'Kruskal' results from baseline file
        for ndx, t in enumerate(data['terminals']):
            # Create context
            context = Context(graph, t)

            #formerly dijkstra
            context.run('S*-HS0')
            results['S*-HS0'].append(context.return_solutions())

            # astar unmerged
            context.run('S*-unmerged')
            results['S*-unmerged'].append(context.return_solutions())

            # formerly bi-directional astar
            context.run('S*-HS')
            results['S*-HS'].append(context.return_solutions())
            #We already have kruskals as base line

        # Save results!
        directory = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(directory, 'results_test_single.pkl'),
                  'wb') as f:
            pickle.dump(results, f)