Exemple #1
0
def compute_success_rate():
    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution'
    ]
    print('Files Found: {}'.format(files))
    success_count = {}
    for window_ratio in [1, 2, 3, 4, 5]:
        success_count[window_ratio] = 10
        for f in files:
            cost_matrix = load_cost_matrix(f)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector, window_ratio)

            max_cost = get_starting_cost(cost_matrix, task_windows)
            try:
                plan, visit_times, profit, cost, solver_stats = get_solution(
                    score_vector, cost_matrix, task_windows, max_cost)
            except ValueError:
                print('Failed with cost {}'.format(max_cost))
                success_count[window_ratio] -= 1
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            print(solver_stats.solve_time)
            print(solver_stats.setup_time)
            msg = 'Time taken: {} seconds'
            time = solver_stats.solve_time + solver_stats.setup_time
            print(msg.format(time))
        success_count[window_ratio] /= 10.0

    print('Success probabilities across constraint ratios')
    print(success_count)
    return success_count
Exemple #2
0
def compute_solve_times(files):
    problem_sizes = [3, 4, 5, 6, 7, 8, 9, 10]
    small_map_solve_times = {p: [] for p in problem_sizes}
    large_map_solve_times = {p: [] for p in problem_sizes}
    for n in problem_sizes:
        for f in files:
            cost_matrix = load_cost_matrix(f)
            # cost_matrix = cost_matrix[0:n, 0:n]
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)

            max_cost = get_starting_cost(cost_matrix, task_windows)
            try:
                plan, visit_times, profit, cost, solver_stats = get_solution(
                    score_vector, cost_matrix, task_windows, max_cost)
            except ValueError:
                print('Failed with cost {}'.format(max_cost))
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            msg = 'Time taken: {} seconds'
            time = solver_stats.solve_time + solver_stats.setup_time
            print(msg.format(time))
            # display_results(g, plan, task_windows, visit_times, wait_times, cost)
            if f[-12:-7] == '20x20':
                small_map_solve_times[n].append(time)
            elif f[-12:-7] == '50x50':
                large_map_solve_times[n].append(time)

    return (small_map_solve_times, large_map_solve_times)
Exemple #3
0
def get_time_data():
    np.random.seed(1)

    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution'
    ]
    maps20x20 = [f for f in files if '20x20' in f]
    maps50x50 = [f for f in files if '20x20' in f]
    big_maps = [f for f in files if '100_POI' in f]

    runtimes = {}
    for n in [4, 6, 8, 10, 12, 14]:
        print(n)
        runtimes[n] = []
        for f in big_maps:
            cost_matrix = load_cost_matrix(f)
            # high diagonal costs cause numerical errors
            # use constraints to prevent travel to self
            # diagonal cost must be > 0 for this to work
            # but should be low
            np.fill_diagonal(cost_matrix, 1)
            cost_matrix = cost_matrix[0:n, 0:n]
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)
            # print('----- Task Windows -----')
            # print(np.around(task_windows, 2).astype('float'))
            max_cost = get_starting_cost(cost_matrix, task_windows)

            plan, visit_times, profit, cost, solve_time = get_solution(
                score_vector, cost_matrix, task_windows, max_cost)
            runtimes[n].append(solve_time)

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            print('----- Visited -----')
            print(np.sum(plan, axis=1))
            print('----- Plan -----')
            print(np.around(plan, 2).astype('int32'))
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            print('----- Wait Times -----')
            print(np.around(wait_times, 2))

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            msg = 'Time taken: {} seconds'
            print(msg.format(solve_time))
            # display_results(g, plan, task_windows, visit_times, wait_times, cost)

    with open('results_tw.txt', 'w') as f:
        f.write(str(runtimes))
Exemple #4
0
    np.random.seed(1)
    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution' and 'Q' not in f
    ]
    maps20x20 = [f for f in files if '20x20' in f]

    rewards = {}
    times = {}
    # for budget in [50, 100, 150, 200, 250, 300]:
    for budget in [600, 500, 400, 300, 200]:
        print('Budget: {}'.format(budget))
        rewards[budget] = []
        times[budget] = []
        for f in maps20x20:
            cost_matrix = load_cost_matrix(f)
            np.fill_diagonal(cost_matrix, 1)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)

            try:
                plan, visit_times, profit, cost, solve_time = get_solution(
                    score_vector, cost_matrix, task_windows, budget)
            except ValueError:
                print('No solution for {}'.format(f))
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)
def get_time_data():
    np.random.seed(1)
    print(c.installed_solvers())
    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution'
    ]
    maps20x20 = [f for f in files if '20x20' in f]
    maps50x50 = [f for f in files if '20x20' in f]
    big_maps = [f for f in files if '100_POI' in f]
    print(big_maps)
    # print('20x20 maps:')
    # print(maps20x20)
    # print('50x50 maps:')
    # print(maps50x50)

    runtimes = {}
    # scores = {}
    # for n in [4, 6, 8, 10, 12, 14, 16]:
    # for n in [4, 6, 8, 10, 12, 14]:
    # for n in [14]:
    for n in [4, 6, 8, 10, 12, 14]:
        runtimes[n] = []
        for f in big_maps:
            # print(f)
            cost_matrix = load_cost_matrix(f)
            # high diagonal costs cause numerical errors
            # use constraints to prevent travel to self
            # diagonal cost must be > 0 for this to work
            # but should be low
            np.fill_diagonal(cost_matrix, 1)
            cost_matrix = cost_matrix[0:n, 0:n]
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)

            task_windows = setup_task_windows(score_vector)

            budget = 350  # works

            plan, reward, cost, solve_time = get_solution(
                score_vector, cost_matrix, budget)
            runtimes[n].append(solve_time)

            print('----- Plan -----')
            print(plan)
            print('----- Edge Costs -----')
            print(cost_matrix)
            print('----- Scores -----')
            print(score_vector)

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            msg = 'The maximum reward tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)
            a_times = get_arrival_times(plan, cost_matrix)
            print(a_times)
            print(get_plan_score(task_windows, plan, a_times))

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(reward, cost, verified_cost))
            print('Time taken: {:.2f} seconds'.format(solve_time))

            # display_results(g, tour, cost_matrix)

    # print(runtimes)
    with open('results_op.txt', 'w') as f:
        f.write(str(runtimes))