def test_ucs(self):
        """TTest and visualize uniform-cost search"""
        start = 'a'
        goal = 'u'

        node_positions = {
            n: self.romania.node[n]['pos']
            for n in self.romania.node.keys()
        }

        self.romania.reset_search()
        path = bidirectional_ucs(self.romania, start, goal)
        print(path)
        print(self.romania.explored_nodes)
        print(sum(1 for x in self.romania.explored_nodes.values() if x == 1))

        self.romania.reset_search()
        path2 = uniform_cost_search(self.romania, start, goal)
        print(path2)
        print(self.romania.explored_nodes)
        print(sum(1 for x in self.romania.explored_nodes.values() if x == 1))

        self.draw_graph(self.romania,
                        node_positions=node_positions,
                        start=start,
                        goal=goal,
                        path=path)
Example #2
0
 def test_bidirectional_ucs(self):
     """Test and generate GeoJSON for bidirectional UCS search"""
     path = bidirectional_ucs(self.atlanta, '69581003', '69581000')
     all_explored = self.atlanta.explored_nodes
     plot_search(self.atlanta, 'atlanta_search_bidir_ucs.json', path,
                 all_explored)
     print(path)
Example #3
0
    def test_grid_viz_bidirectional_search(self):
        """ Use this function to test out your code on a grid to visualize
        the paths and explored nodes for Bidirectional Search.
        This function will save the image files grid_expansion_bidirectional_search.png and
        grid_paths_bidirectional_search.png.
        """

        coordinates = [(0, 0), (6, 7)]
        path = bidirectional_ucs(self.grid, coordinates[0], coordinates[1])
        # path = bidirectional_a_star(self.grid, coordinates[0], coordinates[1], heuristic=custom_heuristic)
        explored = list(self.grid.explored_nodes.keys())
        """

        Color Map Code:
        * Nodes never explored : White
        * Nodes explored but not in path : Red
        * Nodes in path : Green

        """
        val_map = {
            0: {
                0: {
                    0: 'w',
                    1: 'w'
                },
                1: {
                    0: 'w',
                    1: 'w'
                },
            },
            1: {
                0: {
                    0: 'r',
                    1: 'r'
                },
                1: {
                    0: 'g',
                    1: 'b'
                }
            }
        }
        color_values = [
            val_map[node in explored][node in path][node in coordinates]
            for node in self.grid.nodes()
        ]
        save_graph(self.original_grid,
                   "grid_paths_bidirectional_search.png",
                   show_node_labels=True,
                   show_edge_labels=False,
                   color_values=color_values)

        expanded_nodes_dict = dict(self.grid.explored_nodes)
        # Color of nodes gets lighter as it gets explored more
        expansion_color_values = list(expanded_nodes_dict.values())
        save_graph(self.original_grid,
                   "grid_expansion_bidirectional_search.png",
                   show_node_labels=True,
                   show_edge_labels=False,
                   color_values=expansion_color_values)