def idastar_total_time():
    global idastar_times
    idastar_times = []
    # Remove down.
    problems_filen = "db/problems.csv"
    fp = open(problems_filen, 'r')
    content = fp.readlines()
    fp.close()
    content = [x.strip() for x in content]
    w = open("results/IDAStarRuns.txt", 'w')
    for i in content:
        v = i.split(',')
        start = int(v[0])
        end = int(v[1])
        # Calculate the time for algorithm
        start_timer = time.time()
        last_node = idastar.find_idastar_route(start, end)
        end_timer = time.time()
        idastar_times.append(end_timer - start_timer)
        all_path = idastar.create_path(start, end)
        plot_path(idastar.roads, all_path)
        print(str(idastar.find_definite_time(all_path)) + ' ' + str(
            idastar.find_heuristic_time(all_path)) + '\n')
        w.write(str(idastar.find_definite_time(all_path)) + ' ' + str(
            idastar.find_heuristic_time(all_path)) + '\n')
    w.close()
    print("The time is:")
    print(idastar_times)
Exemplo n.º 2
0
def generate_tests(count, finder, h):
    paths = []
    times = []
    roadMap = load_map_from_csv()
    total_junctions = len(roadMap)
    f = open('results/AStarRuns_timed.txt', 'w')
    for i in range(0, count):
        result = []
        while not result:
            time = random.randint(1, 24*60)
            init_state_idx = random.randint(0, total_junctions-1)
            final_state_idx = random.randint(0, total_junctions-1)
            f.write('start: ' + str(init_state_idx) + ' end: ' + str(final_state_idx) + ' time: ' + str(time) + '\n')
            #print('start node: ' + str(roadMap[init_state_idx]))
            #print('end node: ' + str(roadMap[final_state_idx]))
            result = finder(roadMap, init_state_idx, final_state_idx, time)
        f.write('path: ' + str(result[1]) + '\n')
        f.write('actual time: ' + str(result[0]) + '\n')
        f.write('heuristic time: ' + str(h(roadMap[result[1][0]], roadMap[result[1][-1]])) + '\n\n')
        paths.append(result[1])
        times.append(result[0])
        plot_path(roadMap, paths[-1])

    g = open('results/AStarRuns_timed_matlab.txt', 'w')
    for time in times:
        g.write(str(time) + ', ')

    g.write('\n')
    for path in paths:
        heuristic_time = h(roadMap[path[0]], roadMap[path[-1]])
        g.write(str(heuristic_time) + ', ')

    plt.show()
    return paths
def ucs_total_time():
    global ucs_times
    ucs_times = []
    # Remove down
    problems_filen = "db/problems.csv"
    fp = open(problems_filen, 'r')
    content = fp.readlines()
    fp.close()
    content = [x.strip() for x in content]
    w = open("results/UCSRuns.txt", 'w')
    for i in content:
        v = i.split(',')
        start = int(v[0])
        end = int(v[1])
        # Create a list of run times in seconds to then find the standard deviation.
        start_timer = time.time()
        # Find the path using UCS for each of the problems in the file.
        nodes = ucs.find_ucs_route(start, end)
        end_timer = time.time()
        ucs_times.append(end_timer - start_timer)
        all_nodes = []
        # Get the junction indexes.
        for j in nodes:
            all_nodes.append(j[0])
        real_path = ucs.create_path(all_nodes[0], all_nodes[len(all_nodes) - 1])
        nodes = ucs.convert_to_junc(real_path)
        plot_path(ucs.roads, real_path)
        print(str(ucs.find_time(nodes, real_path)) + '\n')
        w.write(str(ucs.find_time(nodes, real_path)) + '\n')
    w.close()
    # Print the list of times for each problem.
    print("The times are:")
    print(ucs_times)
def astar_total_time():
    global astar_times
    astar_times = []
    # Remove down
    problems_filen = "db/problems.csv"
    fp = open(problems_filen, 'r')
    content = fp.readlines()
    fp.close()
    content = [x.strip() for x in content]
    w = open("results/AStarRuns.txt", 'w')
    for i in content:
        v = i.split(',')
        start = int(v[0])
        end = int(v[1])
        # Calculate time for each run.
        start_timer = time.time()
        nodes = astar.find_astar_route(start, end)
        end_timer = time.time()
        astar_times.append(end_timer - start_timer)
        plot_path(astar.roads, nodes)
        print(str(astar.calculate_definite_time(nodes)) + ' ' + str(
            astar.calculate_heuristic_time(nodes)) + '\n')
        w.write(str(astar.calculate_definite_time(nodes)) + ' ' + str(
            astar.calculate_heuristic_time(nodes)) + '\n')
    w.close()
    print("The times are:")
    print(astar_times)
Exemplo n.º 5
0
def draw_path():
    random_problems = random.sample(range(1, 100), 10)
    dict = problems_to_dict()
    for problem in random_problems:
        source = list(dict)[problem]
        target = dict[source]
        path = find_astar_route(int(source), int(target), g, h)
        junctions = get_indexes(path)
        plot_path(roads, junctions, 'g')
Exemplo n.º 6
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))
Exemplo n.º 7
0
def saveWay():
    counter = 1
    with open("problems.csv", 'r') as f:
        filereader = csv.reader(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for row in filereader:
            if counter > 10:
                break;
            source = int(row[0])
            target = int(row[1])
            path = main.find_astar_route(source, target)
            print(' '.join(str(j.state.index) for j in path))

            draw.plot_path(road, path)
            fileName = 'solutions_img/' + str(source) + '_' + str(target) + '.png'
            plt.savefig(fileName)

            plt.show()
            counter = counter + 1
Exemplo n.º 8
0
def create_map(roads):
    count = 0
    # open the problem file and chose 10 problem in random
    file_open = open('problems.csv', 'r')
    read = file_open.readlines()
    while count != 10:
        line = random.choice(read)
        split = line.split(',')
        start_index = int(split[0])
        split = split[1].split()
        goal_index = int(split[0])
        # calculate by astar
        result = find_astar_route(start_index, goal_index, roads)
        path = list(result[2])
        # create the graph and show
        draw.plot_path(roads,path)
        plot.title("A_star-source: " + str(start_index) + "goal: " + str(goal_index))
        plot.show()
        count = count + 1
Exemplo n.º 9
0
def solve_problems(roads, algo_name):
    # define algorithms dictionary
    algorithms = {
        'UCS': (algos.find_ucs_route, None),
        'AStar': (algos.find_astar_route, functions.astar_heuristic),
        'IDAStar': (algos.find_idastar_route, functions.astar_heuristic)
    }
    algorithm, heuristic = algorithms[algo_name]

    running_time = []
    with open('db/problems.csv',
              'r') as problems_file, open('results/' + algo_name + 'Runs.txt',
                                          'w') as solutions_file:
        for line in problems_file:
            s, t = int(line.split(',')[0]), int(line.split(',')[1])

            # activate algorithm to find path, and messure it running time on input.
            time0 = time.time()
            path = algorithm(roads, s, t)
            delta_time = time.time() - time0
            running_time.append(delta_time)

            # plot the path and save it as file.
            plotter.plot_path(roads, path)
            plotter.plt.savefig('solutions_img/' + algo_name + '_' + str(s) +
                                '_' + str(t) + '.png')
            plotter.plt.cla()

            # calculate time of path, and write it to solutions file:
            time_of_path = functions.calculate_total_time(roads, path)
            if not heuristic:
                solutions_file.write(str(time_of_path) + '\n')
            else:
                estimated_time = heuristic(roads, s, t)
                solutions_file.write(
                    str(estimated_time) + ',' + str(time_of_path) + '\n')

    return running_time
Exemplo n.º 10
0
def build_path(lines, roads):
    for line in lines:
        line = line.strip("\n")
        line = list(line.split(","))
        line = [int(i) for i in line]
        plot_path(roads, line)
Exemplo n.º 11
0
import csv

from astar import Node, PriorityQueue, find_astar_rout_search
from ways import draw, graph
import matplotlib.pyplot as plt

roads = graph.load_map_from_csv()  # count=10001)

from ways.draw import set_no_axis, draw_links, plot_path


def read_csv():
    with open('problems.csv', mode='r') as csv_file:
        problems = list()
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            problems.append((row[0], row[1]))
    return problems


j=0
for i in read_csv():
    arr = find_astar_rout_search(int(i[0]), int(i[1]))
    plt.figure(num=1, figsize=(6, 15))
    'set_no_axis()'
    plot_path(roads,arr[0], 'c')
    plt.show()
    plt.clf()