Exemplo n.º 1
0
def main():
    directory, make_plot, force_yes = parse_args()
    results = {
        'a-star': [],
        'bf': [],
        'greedy': [],
    }

    files = glob(f'{directory}/*.in')
    files.sort()
    experiment_start = process_time()
    tracemalloc.start()

    for i, file in enumerate(files):
        case_start = process_time()
        nodes = read_data(file)
        n = len(nodes)

        # Brute Force
        solver = BruteForce(nodes)
        path, cost, time = solver.run()
        results['bf'].append({'n': n, 'time': time})

        # A*
        solver = AStarAlgorithm(nodes)
        path, cost, time = solver.run()
        results['a-star'].append({'n': n, 'time': time})

        # Greedy
        solver = GreedyAlgorithm(nodes)
        path, cost, time = solver.run()
        results['greedy'].append({'n': n, 'time': time})

        case_end = process_time()
        case_time = case_end - case_start

        print(
            f'Case {i + 1}/{len(files)}: Finished {n} nodes in {case_time:.4f} seconds'
        )
        if not force_yes and case_time > 60 and i + 1 < len(files):
            response = input('Continue? [Y/n]: ')
            if response == 'n':
                break

    experiment_stop = process_time()
    experiment_time = experiment_stop - experiment_start
    print(f'Finished experiments in {experiment_time:.2f} seconds')
    _, peak = tracemalloc.get_traced_memory()
    print(f'Peak memory usage: {(peak / 10**6):.1f}MB')

    for algo in results.keys():
        with open(f'results_{algo}.csv', 'w') as file:
            writer = DictWriter(file, ['n', 'time'])

            writer.writeheader()
            writer.writerows(results[algo])
def test_algs(towns_matrix):
    print("\nAnts colony algorithm started...")
    start = time.monotonic()
    ac = AntsColony(towns_matrix=towns_matrix,
                    ants_count=5,
                    interations_count=10000,
                    vaporization_coef=0.95,
                    power_coef=2)
    shortest_way = ac.run()
    print("Shortest way:", shortest_way, "Time: ", time.monotonic() - start)

    print("\nBrute Force algorithm started...")
    start = time.monotonic()
    bf = BruteForce(towns_matrix=towns_matrix)
    shortest_way = bf.run()
    print("Shortest way:", shortest_way, "Time: ", time.monotonic() - start)
def single_experiment(input_file, algo):
    tracemalloc.start()

    nodes = read_data(input_file)
    n = len(nodes)

    if algo == 'bf':
        solver = BruteForce(nodes)
    elif algo == 'a-star':
        solver = AStarAlgorithm(nodes)
    elif algo == 'greedy':
        solver = GreedyAlgorithm(nodes)
    else:
        raise Exception(f'Algorithm {algo} not implemented!')

    _, cost, time = solver.run()

    _, peak = tracemalloc.get_traced_memory()

    return n, time, cost, peak
def main():
    file, algorithm, text_only = parse_args()
    nodes = read_data(file)

    if algorithm == 'bf':
        solver = BruteForce(nodes)
    elif algorithm == 'a-star':
        solver = AStarAlgorithm(nodes)
    elif algorithm == 'greedy':
        solver = GreedyAlgorithm(nodes)
    else:
        raise Exception(f'Algorithm {algorithm} not implemented!')

    path, cost, time = solver.run()

    labels = [node.label for node in path]
    print(' '.join(labels))
    print(cost)
    print(time)

    if not text_only:
        plot_path(path)