コード例 #1
0
def dijkstra_shortest_path(start_node, dest_node):
    """
            Test Dijkstra's shortest path in comparison to networkx's shortest_path.

            Parameters:
            -----------
            start_node: networkx Node
                The starting node.
            dest_node: networkx Node
                The ending node.

            Passing Criteria:
            --------
            Both path lengths must be the same.

        """
    context = Context(strategies.StrategyDijkstra(graph, 0, 'vanilla'))
    path = context.run_strategy_route(start_node, dest_node)
    regular_path_length = graph_utils.get_path_length(graph, path)
    print("Dijkstra shortest path", path)
    nx_shortest_path = shortest_path(start_node, dest_node)
    nx_shortest_path_length = graph_utils.get_path_length(
        graph, nx_shortest_path)
    if nx_shortest_path_length == regular_path_length:
        print("Test Passed")
    else:
        print("Test Failed")
    print("\n")
コード例 #2
0
def dijkstra_max_elevation_grade(start_node, dest_node):
    """
                    Test Dijkstra's max elevation path using grade as the weight.

                    Parameters:
                    -----------
                    start_node: networkx Node
                        The starting node.
                    dest_node: networkx Node
                        The ending node.

                    Passing Criteria:
                    --------
                    max_elevation path must have the smaller path length than the limit
                     and grade is greater than regular greater.

                """
    print("Dijkstra max elevation using grade as weight")
    context = Context(strategies.StrategyDijkstra(graph, limit, 'max grade'))
    path = context.run_strategy_route(start_node, dest_node)
    print("Elevation path", path)
    grade = graph_utils.get_average_grade(graph, path)
    max_path_length = graph_utils.get_path_length(graph, path)

    context = Context(strategies.StrategyDijkstra(graph, 0, 'vanilla'))
    path = context.run_strategy_route(start_node, dest_node)
    print("Vanilla path", path)
    regular_grade = graph_utils.get_average_grade(graph, path)
    regular_path_length = graph_utils.get_path_length(graph, path)
    max_length = regular_path_length * (1 + limit)
    print("Vanilla path length:", regular_path_length,
          "\nMax Possible Path length:", max_length,
          "\nLength of elevation path:", max_path_length)
    print("Vanilla path Grade:", round(regular_grade, 2), "\nNew Grade:",
          round(grade, 2))
    if max_path_length <= max_length:
        if grade >= regular_grade:
            print("Test Passed")
        else:
            print(
                "Test Passed: Path length. Dijkstra's wasn't able to maximize grade"
            )
    else:
        print("Test Failed")
    print("\n")
コード例 #3
0
def dijkstra_min_elevation(start_node, dest_node):
    """
                Test Dijkstra's min elevation path using elevation as the weight.

                Parameters:
                -----------
                start_node: networkx Node
                    The starting node.
                dest_node: networkx Node
                    The ending node.

                Passing Criteria:
                --------
                min_elevation path must have the smaller path length than the limit
                 and min_elevation is less than regular elevation.

    """
    print("Dijkstra min elevation using elevation change as weight")
    context = Context(
        strategies.StrategyDijkstra(graph, limit, 'min elevation_change'))
    path = context.run_strategy_route(start_node, dest_node)
    print("Elevation path", path)
    elevation = graph_utils.get_path_elevation(graph, path)
    max_path_length = graph_utils.get_path_length(graph, path)

    context = Context(strategies.StrategyDijkstra(graph, 0, 'vanilla'))
    path = context.run_strategy_route(start_node, dest_node)
    print("Vanilla path", path)
    regular_elevation = graph_utils.get_path_elevation(graph, path)
    regular_path_length = graph_utils.get_path_length(graph, path)
    max_length = regular_path_length * (1 + limit)
    print("Vanilla path length:", regular_path_length,
          "\nMax Possible Path length:", max_length,
          "\nLength of elevation path:", max_path_length)
    print("Vanilla path Elevation:", regular_elevation, "\nNew Elevation:",
          elevation)
    if max_path_length <= max_length and elevation <= regular_elevation:
        print("Test Passed")
    else:
        print("Test Failed")
    print("\n")
コード例 #4
0
def all_shortest_path(start, dest):
    """
                Run all the shortest path algorithms: BFS, Dijkstra, A*

                Parameters:
                -----------
                start: String
                    The starting address.
                dest: String
                    The ending address.

                Returns:
                --------
                times: An array of the time (as float values) it took each algorithm to run.

                """
    try:
        start_node = get_node_from_address(graph, start)
        dest_node = get_node_from_address(graph, dest)
    except Exception as e:
        print(e)
        print("Could not map address to node")
    times = []
    print("Running Dijkstra")
    context = Context(strategies.StrategyDijkstra(graph, 0, 'vanilla'))
    start_time = time()
    path = context.run_strategy_route(start_node, dest_node)
    end_time = time()
    times.append(end_time - start_time)
    print("Running BFS")
    context = Context(strategies.StrategyBFS(graph, 0, 'vanilla'))
    start_time = time()
    path = context.run_strategy_route(start_node, dest_node)
    end_time = time()
    times.append(end_time - start_time)
    print("Running A*")
    context = Context(strategies.StrategyAStar(graph, 0, 'vanilla'))
    start_time = time()
    path = context.run_strategy_route(start_node, dest_node)
    end_time = time()
    times.append(end_time - start_time)
    return times
コード例 #5
0
def max_elevation_path_performance(start, dest, method):
    """
                        Run a performance test on the maximum elevation path of Dijkstra and A*.

                        Parameters:
                        -----------
                        start: String
                            The starting address.
                        dest: String
                            The ending address.
                        method: String
                            The method to use: Maximize Elevation Gain or Maximize Steepness.

                        Returns:
                        --------
                        times: An array of the time (as float values) it took each algorithm to run.

                        """
    try:
        start_node = get_node_from_address(graph, start)
        dest_node = get_node_from_address(graph, dest)
    except Exception as e:
        print(e)
        print("Could not map address to node")
    times = []
    print("Running Dijkstra")
    context = Context(strategies.StrategyDijkstra(graph, 0, method))
    start_time = time()
    path = context.run_strategy_route(start_node, dest_node)
    end_time = time()
    times.append(end_time - start_time)
    print("Running A*")
    context = Context(strategies.StrategyAStar(graph, 0, method))
    start_time = time()
    path = context.run_strategy_route(start_node, dest_node)
    end_time = time()
    times.append(end_time - start_time)
    return times