def ucs_runtime():
    arbitrary_solvable_problems = get_100_solvable_search_problems()
    for problem in arbitrary_solvable_problems:
        source = problem[0]
        target = problem[1]
        problem = RoutingProblem(source, target, MapData.get_instance())
        best_first_graph_search(problem, cost_function, f=g)
def idastar_runtime():
    arbitrary_solvable_problems = get_100_solvable_search_problems()
    for i in range(0, 5):
        source = arbitrary_solvable_problems[i][0]
        target = arbitrary_solvable_problems[i][1]
        problem = RoutingProblem(source, target, MapData.get_instance())
        idastar(problem)
def find_astar_route(source, target):
    problem = RoutingProblem(source, target, MapData.get_instance())
    # Use best first graph search algorithm when function f defined as g (the path cost) + h (the heuristic function)
    # and return the solution path.
    return best_first_graph_search(
        problem,
        cost_function,
        f=lambda n: g(n) + heuristic_function(n, Node(problem.goal)))[0]
def astar_runtime():
    arbitrary_solvable_problems = get_100_solvable_search_problems()
    for problem in arbitrary_solvable_problems:
        source = problem[0]
        target = problem[1]
        problem = RoutingProblem(source, target, MapData.get_instance())
        best_first_graph_search(
            problem,
            cost_function,
            f=lambda n: g(n) + heuristic_function(n, Node(problem.goal)))
def ucs_solution_for_100_problems():
    ucs_results_file = open('results/UCSRuns.txt', "w")
    problems_list = load_data()
    for problem in problems_list:
        source = problem[0]
        target = problem[1]
        problem = RoutingProblem(int(source), int(target),
                                 MapData.get_instance())
        ucs_results_file.write(
            str(best_first_graph_search(problem, cost_function, f=g)[1]) +
            "\n")
    ucs_results_file.close()
def idastar_solution_for_5_problems():
    astar_results_file = open('results/IDAStarRuns.txt', "w")
    problems_list = load_data()
    for line in range(0, 5):
        source = problems_list[line][0]
        target = problems_list[line][1]
        problem = RoutingProblem(int(source), int(target),
                                 MapData.get_instance())
        path = idastar(problem)
        cost = 0
        for i in range(len(path) - 1):
            links = MapData.get_instance().get(path[i]).links
            for link in links:
                if link.target == path[i + 1]:
                    cost += cost_function(link)
                    break
        astar_results_file.write("heuristic cost: " + str(
            heuristic_function(Node(problem.start_state), Node(problem.goal)))
                                 + ", actual cost: " + str(cost) + "\n")
    astar_results_file.close()
def astar_solution_for_100_problems():
    astar_results_file = open('results/AStarRuns.txt', "w")
    problems_list = load_data()
    counter = 0
    for problem in problems_list:
        source = problem[0]
        target = problem[1]
        problem = RoutingProblem(int(source), int(target),
                                 MapData.get_instance())
        path, cost = best_first_graph_search(
            problem,
            cost_function,
            f=lambda n: g(n) + heuristic_function(n, Node(problem.goal)))
        astar_results_file.write("heuristic cost: " + str(
            heuristic_function(Node(problem.start_state), Node(problem.goal)))
                                 + ", actual cost: " + str(cost) + "\n")
        # We want to draw solution maps only for 10 problems as required.
        if counter < 10:
            draw_solution_map(path)
        counter += 1
    astar_results_file.close()
def find_ucs_route(source, target):
    problem = RoutingProblem(source, target, MapData.get_instance())
    # Use best first graph search algorithm when function f defined as g (the path cost), and return the solution path.
    return best_first_graph_search(problem, cost_function, f=g)[0]
def find_idastar_route(source, target):
    problem = RoutingProblem(source, target, MapData.get_instance())
    return idastar(problem)