コード例 #1
0
def main():
    """Use this program to compare and contrast four graph searching algorithms."""
    map_40 = load_map_40()
    map_10 = load_map_10()
    map_simple = load_map_simple()

    algorithms = [a_star, uniform_cost, breath_first, depth_first]

    # The loop below will execute the same search using each of the four algorithms.
    # Experiment with different maps and searches to understand the attributes,
    # performance characteristics, and code of each algorithm.
    graph = map_40
    start_node = 5
    goal_node = 34

    print(f"Nodes in Map: {len(graph.intersections)} \n")

    for algo in algorithms:
        method = algo.SearchAlgo(graph, start_node, goal_node)
        method.search()
        print(f"Algorithm: {method.algorithm_name}")
        print(f"Path: {method.path}")
        print(f"Path length: {method.path_length}")
        print(f"Number of nodes visited: {len(method.visited)} ({len(method.visited) / len(method.map.intersections) * 100}%)")
        print(f"Nodes visited:", ', '.join(map(str, method.visited)))
        print("")
コード例 #2
0
def main():
    # interactive test
    map_10 = load_map_10()
    map_40 = load_map_40()

    print(f"Testing path planner...")
    start_node = 5
    goal_node = 34
    planner = PathPlanner(map_40, start_node, goal_node)
    path = planner.path

    print(f"Start Node: {start_node}")
    print(f"Goal Node: {goal_node}")
    print(f"Resulting path: {path}")
    if path == [5, 16, 37, 12, 34]:
        print(f"Great! Your code works for these inputs!\n")
    else:
        print("Something is off, your code produced the following:")
        print(path, "\n")

    # course test for validation of project
    from test import test
    print(f"Running project validation tests from Udacity...")
    test(PathPlanner)
コード例 #3
0
# In[1]:

# Run this cell first!

from helpers import Map, load_map_10, load_map_40, show_map
import math

get_ipython().run_line_magic('load_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')

# ### Map Basics

# In[3]:

map_10 = load_map_10()
show_map(map_10)

# The map above (run the code cell if you don't see it) shows a disconnected network of 10 intersections. The two intersections on the left are connected to each other but they are not connected to the rest of the road network. This map is quite literal in its expression of distance and connectivity. On the graph above, the edge between 2 nodes(intersections) represents a literal straight road not just an abstract connection of 2 cities.
#
# These `Map` objects have two properties you will want to use to implement A\* search: `intersections` and `roads`
#
# **Intersections**
#
# The `intersections` are represented as a dictionary.
#
# In this example, there are 10 intersections, each identified by an x,y coordinate. The coordinates are listed below. You can hover over each dot in the map above to see the intersection number.

# In[4]:

map_10.intersections