def get_rout(start, end, map):
    nodes = map.junctions()
    start_node = nodes[start]
    end_node = nodes[end]
    frontier = PriorityQueue()
    frontier.put((H(start_node, end_node), start_node))
    closed_list = set()
    came_from = {}
    F = {}
    F[start] = H(start_node, end_node)
    while not frontier.empty():
        node_cost, node = frontier.get()
        # G = F-H
        node_cost = node_cost - H(node, end_node)
        if node == end_node:
            return reconstruct_rout(came_from, end_node, start_node, nodes)
        closed_list.add(node)
        for link in node.links:
            child_idx = link.target
            child = nodes[child_idx]
            #f = g + h
            child_f = link_cost(link.highway_type,
                                link.distance) + node_cost + H(
                                    child, end_node)
            if child not in closed_list and not in_frontier(frontier, child):
                F[child.index] = child_f
                frontier.put((F[child.index], child))
                came_from[child.index] = node.index
            elif child_f < F[child.index]:
                F[child.index] = child_f
                del_from_frontier(frontier, child)
                frontier.put((F[child.index], child))
                came_from[child.index] = node.index
    return None
Exemple #2
0
def write_results_to_file(results_file, problems_file, algorithem, map):
    nodes = map.junctions()
    w = open(results_file, "w")
    r = open(problems_file, "r")
    read_lines = r.readlines()
    lines = [x.strip() for x in read_lines]
    for line in lines:
        split = line.split(",")
        source = int(split[0])
        target = int(split[1])
        if algorithem == "ucs":
            rout = ucs_get_rout.get_rout(source, target, map)
            time = calculate_time_of_rout(rout)
            w.write(str(time) + "\n")
        elif algorithem == "astar":
            rout = astar_get_rout.get_rout(source, target, map)
            time = calculate_time_of_rout(rout)
            h_time = H(nodes[source], nodes[target])
            w.write(str(h_time) + "," + str(time) + "\n")
        else:
            rout = idastar_get_rout.get_rout(source, target, map)
            time = calculate_time_of_rout(rout)
            h_time = H(nodes[source], nodes[target])
            w.write(str(h_time) + "," + str(time) + "\n")
    w.close()
    r.close()
Exemple #3
0
def testpaths(map):
    juncs = map.junctions()
    #problems.create_problems("db/problems.csv")
    # problems.write_results_to_file("results/UCSRuns.txt", "db/problems.csv",
    #                                "ucs", map)
    # problems.write_results_to_file("results/AStarRuns.txt", "db/problems.csv",
    #                                "astar", map)
    # problems.write_results_to_file("results/IDAStarRuns.txt", "db/problems.csv",
    #                                "idstar", map)

    # ucs_time = problems.get_run_times(map, "db/problems.csv", "ucs")
    # print(ucs_time)
    # astar_time = problems.get_run_times(map, "db/problems.csv", "astar")
    # print(astar_time)
    # ida_time = problems.get_run_times(map, "db/problems.csv", "ida_time")
    # print(ida_time)
    #problems.draw_times_graph("results/AStarRuns.txt")

    with open("db/problems.csv") as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    for c in content:
        c.split(",")
        v = c.split(',')
        start = int(v[0])
        end = int(v[1])
        print(" ")
        print(str(start) + "," + str(end))
        path = astar_get_rout.get_rout(start, end, map)
        cost = calculate_time_of_rout(path)
        h_cost = H(juncs[start], juncs[end])
        print(cost)
        print(h_cost)
        if (cost < h_cost):
            print("problem")

        # pp = []
        # for p in path:
        #     pp.append(p.index)
        # cost = find_time(map.junctions(),pp)

        # path = ucs_rout.get_rout_with_map_dict(start, end, map)
        if path_is_illegal(path, map, end):
            print("Bye")
            break
        index_path = []
        for node in path:
            index_path.append(node.index)

        plot_path(
            juncs,
            index_path,
        )

        print(' '.join(str(j) for j in index_path))
        print(len(path))
def get_rout(start, end, map):
    nodes = map.junctions()
    start_node = nodes[start]
    end_node = nodes[end]
    parent = {}
    current_limit = H(start_node, end_node)
    rout = []
    rout.append(start_node)
    cost = 0
    while current_limit != end_node:
        current_limit = search_rout(rout, cost, current_limit, parent, nodes,
                                    end_node)
    return reconstruct_rout(parent, end_node, start_node, nodes)
def search_rout(rout, current_cost, f_limit, parent, nodes, end):
    current_node = rout[-1]
    current_f = current_cost + H(current_node, end)
    if current_f > f_limit:
        return current_f
    if current_node == end:
        return current_node
    new_limit = 999999999999999
    for link in current_node.links:
        child = nodes[link.target]
        if child not in rout:
            parent[child.index] = current_node.index
            rout.append(child)
            cost = current_cost + link_cost(link.highway_type, link.distance)
            current_limit = search_rout(rout, cost, f_limit, parent, nodes,
                                        end)
            if current_limit == end:
                return current_limit
            if current_limit < new_limit:
                new_limit = current_limit
            rout.pop()
    return new_limit