示例#1
0
    def test_load_heuristics_from_disk_run_sstar(self):

        # change heuristic setting
        cfg.Algorithm.sstar_heuristic_type = "custom"    

        # bind custom_heuristics function
        Common.custom_heuristics = self.my_cust_h_func

        # create context handler and run S*-mm
        context = Context(graph, T)

        # profiler enable
        profiler.enable()

        # Now run
        context.run("S*-MM")

        # disable profiler
        profiler.disable()

        # store results
        results = context.return_solutions()   

        stats = pstats.Stats(profiler).sort_stats('cumtime')
        stats.print_stats()

        print(results)
示例#2
0
    def test_load_heuristics_from_disk_run_sstar(self):

        # load preprocessed heuristic from disk
        GenerateHeuristics.load_results(further_save_file)

        # change heuristic setting
        cfg.Algorithm.sstar_heuristic_type = "preprocess"

        # create context handler and run S*-mm
        context = Context(graph, T)

        # profiler enable
        profiler.enable()

        # Now run
        context.run("S*-MM")

        # disable profiler
        profiler.disable()

        # store results
        results = context.return_solutions()

        stats = pstats.Stats(profiler).sort_stats('cumtime')
        stats.print_stats()

        print(results)
示例#3
0
 def get_kruskal_info(self, terminal_instance):
     context = Context(self.graph, terminal_instance)
     # context.run('Kruskal', file_to_read=self.cache_filepath, heuristic_for_recon=self.heuristic_type, RECON_PATH=self.reconstruct_path_ornot)
     context.run('Kruskal',
                 file_to_read=self.cachefiles,
                 heuristic_for_recon=self.heuristic_type,
                 RECON_PATH=self.reconstruct_path_ornot)
     # lock.acquire()
     # kruskal_results_proxy.append(context.return_solutions())
     # lock.release()
     return context.return_solutions()
示例#4
0
    def run_individual_algs(self, inputs):
        job_id, data = inputs
        # print("starting job id: ", job_id)
        terminals, alg = data
        context = Context(self.graph, terminals)
        context.run(alg)
        # print("finished job id: ", job_id)

        # Try mutex locking to avoid race conditions
        # lock.acquire()
        # proxy_dict[alg].append(context.return_solutions())
        # lock.release()
        return alg, context.return_solutions()
示例#5
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)
示例#6
0
    def test_simple_parser_mapf(self):
        # Some online_data options
        # mapf

        # define filename to use
        # filename = "results/online_data/mapf/Berlin_1_256.map"
        # filename = "results/online_data/mapf/maze-32-32-2.map"
        filename = os.path.join(cfg.data_dir, "mapf", "maze-32-32-2.map")
        # Get package file location
        # cwd = os.path.dirname(os.path.realpath(steinerpy.__file__))+"/../"
        filepath = os.path.join(cwd, filename)

        # Return edge dictionary and terminal list
        # obs, height, width = DataParser.parse(filepath, dataset_type="mapf")
        graph = DataParser.parse(filepath, dataset_type="mapf")
        minX, maxX, minY, maxY = graph.grid_dim
        obs = graph.obstacles
        # # dims
        # minX, maxX = 0, width - 1
        # minY, maxY = 0, height - 1

        # 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(type_="SquareGrid", grid_dim=grid_dim, grid_size=grid_size, n_type= n_type, obstacles=obs)
        # graph.show_grid()

        # generate random terminals
        T = []
        while len(T) < 5:
            x = random.randint(minX, maxX)
            y = random.randint(minY, maxY)
            if (x, y) not in obs:
                T.append((x, y))

        # Create context, run and store results
        context = Context(graph, T)

        context.run('Kruskal')
        results1 = context.return_solutions()

        context.run('S*-HS')
        results2 = context.return_solutions()

        # assert results
        eps = 1e-6
        self.assertTrue(
            abs(sum(results1['dist']) - sum(results2['dist']) <= eps))
示例#7
0
    def run_func(self):
        """ Queries Kruskal's algorithm for each run instance for baseline results

        Results are saved to file

        Raises:
            FileExistsError: If output (baseline) file already exists
        
        Todo:
            * Consider using timeit for more accurate results
                https://stackoverflow.com/questions/7370801/how-to-measure-elapsed-time-in-python

        """
        #start time
        t0 = timer()

        # Get path and check for existance. Don't overwrite files!
        # directory = os.path.dirname(os.path.realpath(__file__))
        if os.path.exists(os.path.join(self.save_directory, self.filename)):
            raise FileExistsError('{} already exists!'.format(self.filename))

        # Create n-terminals for m-instances
        if self._terminals is None:
            # self._terminals = [[list(i) for i in self.create_terminals()][0] for j in range(self.m_instances)]
            self._terminals = self.create_terminals()
        obstacles = self.graph.obstacles

        # Run Kruskals on each and save results
        solution = []
        for ndx, t in enumerate(self._terminals):
            MyLogger.add_message("Running terminals: {}".format(t), __name__,
                                 "INFO")

            # ko = Kruskal(self.graph, t)
            # ko.run_algorithm()
            # solution.append(ko.return_solutions())
            # Ensure every terminal is reachable!
            keepRunning = True
            while keepRunning:
                try:
                    context = Context(self.graph, t)
                    context.run('Kruskal')
                    solution.append(context.return_solutions())
                    keepRunning = False
                except:
                    MyLogger.add_message(
                        "one or more terminals is landlocked! In ndx {}, with terminals: {}"
                        .format(ndx, t), __name__, "CRITICAL")
                    # Generate new terminals and replace the ones we have right now
                    t = [list(i) for i in self.create_terminals()][0]
                    self._terminals[ndx] = t

            print(ndx, '\n')

        # end time
        t1 = timer() - t0

        # dump instances and solution to file
        # directory = os.getcwd()
        if self.save_to_file:
            with open(os.path.join(self.save_directory, self.filename),
                      'wb') as f:
                pickle.dump(
                    {
                        'terminals': self._terminals,
                        'solution': solution,
                        'obstacles': obstacles
                    }, f)

        print("Finished! Wrote baseline file! Now generate results! {}".format(
            t1))
        if cfg.Misc.sound_alert == True:
            os.system(
                'spd-say "Finished! Wrote baseline file! Now generate results!"'
            )

        return {
            'terminals': self._terminals,
            'solution': solution,
            'obstacles': obstacles
        }
    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)
示例#9
0
from steinerpy.library.graphs.graph import GraphFactory
from steinerpy.context import Context

# Spec out our `SquareGrid` type graph object using `GraphFactory`
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)  

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

# Create context
context = Context(graph, terminals)

# run and store results for astar
context.run('S*-HS')
results = context.return_solutions()


示例#10
0
    def run_func(self, algorithms_to_run=None):
        """Run each of our algorithms using a context class

        The context class takes a 'graph' and an instance of 't' terminals, 
        then executes each algorithm on a given instance.
        Results are appended to the appropriate location in the 'results' attribute.
        Finally, output files have a  'results_' string prepended in the current directory
        for processing

        Returns:
            results (dict): Contains results for the algorithms ran

        """
        t0 = timer()
        # Use contextualizer to run algorithms (make sure debug visualizer is off for speed)
        if algorithms_to_run is None:
            results = {'S*-HS':[], 'S*-BS': [], 'S*-unmerged':[],  \
                    'S*-MM':[]}
            algorithms_to_run = list(results)
        else:
            results = {alg: [] for alg in algorithms_to_run}

        # results = {'Astar':[], 'SstarAstar':[], 'SstarDijkstra': [], 'SstarPrimalDual': []}

        for ndx, t in enumerate(self.data['terminals']):

            if self.run_instance is not None and ndx != self.run_instance:
                # Skip everything except user-specified run
                continue

            # Add obstacles to graph?
            if 'obstacles' in self.data and self.data[
                    'obstacles'] and self.graph.obstacles is not None:
                # self.graph.set_obstacles(self.data['obstacles'][ndx])
                self.graph.set_obstacles(self.data['obstacles'])

            # Log terminals
            MyLogger.add_message("terminals: {}".format(t), __name__, "INFO")

            # Create context
            context = Context(self.graph, t)

            # context.run('Astar')
            # results['Astar'].append(context.return_solutions())

            for alg in algorithms_to_run:
                context.run(alg)
                results[alg].append(context.return_solutions())
            # context.run('SstarMM')
            # results['SstarMM'].append(context.return_solutions())

            # context.run('SstarAstar')
            # results['SstarAstar'].append(context.return_solutions())

            # context.run('SstarPrimalDual')
            # results['SstarPrimalDual'].append(context.return_solutions())

            #We already have kruskals as base line
            print('finished with instance {} \n'.format(ndx))

        t1 = timer() - t0

        # Save results!
        if self.save_to_file:
            # directory = os.path.dirname(os.path.realpath(__file__))
            with open(os.path.join(self.save_directory, self.results_filename),
                      'wb') as f:
                pickle.dump(results, f)

            print(
                "Wrote results to file! Now process them! {} secs".format(t1))
            if cfg.Misc.sound_alert is True:
                os.system(
                    'spd-say "Finished! Wrote results to file! Now process them!"'
                )

        return results