Пример #1
0
def main(runs, max_agents):
    # Generate problems
    problems = []
    while len(problems) < runs:
        for i in range(2, max_agents + 1):
            problems.append(util.generate_problem(i, 16, 16, OBSTACLES))
            if len(problems) == runs:
                break

    instance = 0
    line = []
    f = open(f'results/cache-{datetime.now()}.csv', 'w')
    writer = csv.writer(f)
    result = [
        'instance', 'num agents', 'optimal length', 'algorithm', 'cache',
        'time', 'length'
    ]
    writer.writerow(result)

    global_start_time = timeit.default_timer()
    for problem in problems:
        print(f'Problem has {len(problem[1])} agents')
        optimal_length = 0
        try:
            for i in range(len(problem[1])):
                dist = rra_star.RRAstar(problem[0], problem[1][i],
                                        problem[2][i])
                optimal_length += dist.dist(problem[1][i])
        except rra_star.NoValidPathExists:
            optimal_length = 'NA'

        for algorithm in ALGORITHMS:
            for cache in [True, False]:
                result = [
                    instance,
                    len(problem[1]), optimal_length, algorithm.name, cache
                ]
                start_time = timeit.default_timer()
                try:
                    data = algorithm.entry(*problem,
                                           start_time=start_time,
                                           max_time=MAX_TIME,
                                           cache=cache,
                                           **algorithm.kwargs)
                    paths = data['paths']
                    length = sum(len(p) for p in paths)
                    end_time = timeit.default_timer()
                    result += [end_time - start_time, length]
                except (odid.TimeExceeded, rra_star.NoValidPathExists,
                        util.NoPathsFoundException,
                        version1b.ConflictNotSolved):
                    result += ['NA', 'NA']

                writer.writerow(result)
                f.flush()
        instance += 1
    global_end_time = timeit.default_timer()
    print(f'Total time {(global_end_time - global_start_time) * 1000:5.3f}ms')
    f.close()
Пример #2
0
def main(num_agents):
    world, starts, goals = util.generate_problem(num_agents, 16, 16, 0.2)

    # Create agents
    agents = [Agent(world, starts[i], goals[i]) for i in range(num_agents)]
    start_time = timeit.default_timer()
    paths = dimpp(agents, None, None)
    end_time = timeit.default_timer()
    print(f'elapsed time: {(end_time - start_time) * 1000:5.3f}ms')

    print('Making visualisation')
    vis = visualisation.Visualisation(world, num_agents, scale=20)
    vis.draw_paths('midpp.mkv', paths)
Пример #3
0
def main(agents):
    world, starts, goals = util.generate_problem(agents, 16, 16, 0.2)
    print(starts, goals)

    start_time = timeit.default_timer()
    paths = standard_algorithm(agents, world, starts, goals,
                               start_time=start_time, max_time=15)
    end_time = timeit.default_timer()
    print(f'elapsed time: {(end_time - start_time) * 1000:5.3f}ms')

    print('Writing visualisations')
    vis = visualisation.Visualisation(world, agents, scale=20)
    frames = vis.draw_paths('sa.mkv', paths)
Пример #4
0
def main(agents):
    world, starts, goals = util.generate_problem(agents, 16, 16, 0.2)
    print('starts:', starts)
    print('goals: ', goals)

    start_time = timeit.default_timer()
    paths = odid2(agents,
                  world,
                  starts,
                  goals,
                  start_time=start_time,
                  max_time=5)

    print('Writing visualisations')
    vis = visualisation.Visualisation(world, agents, scale=20)
    frames = vis.draw_paths('odid.mkv', paths)
Пример #5
0
def main(num_agents):
    world, starts, goals = util.generate_problem(num_agents, 16, 16, 0.2)

    # Create agents
    agents = [Agent(world, starts[i], goals[i]) for i in range(num_agents)]
    start_time = timeit.default_timer()
    if '--simple' in sys.argv or '-s' in sys.argv:
        paths = simple_poc(agents)
    else:
        paths = poc(agents, start_time=start_time, max_time=5)
    end_time = timeit.default_timer()
    print(f'elapsed time: {(end_time - start_time) * 1000:5.3f}ms')

    print('Making visualisation')
    vis = visualisation.Visualisation(world, num_agents, scale=20)
    vis.draw_paths('poc.mkv', paths)
Пример #6
0
def main(window):
    world, *problem = util.generate_problem(1, 16, 16, 0.2)
    start = problem[0][0]
    goal = problem[1][0]
    print('Navigating from', start, 'to', goal)
    actual_path = []

    while start != goal:
        path = astar(world, start, goal, window_size)
        try:
            start = path[int(window / 2)]
        except IndexError:
            start = path[-1]
        actual_path += path[:int(window / 2) + 1]
        print('Partial path', path)
        print('Path up to now', actual_path)
        print('next start', start, ', going to', goal)
        input()
Пример #7
0
def main(runs, max_agents):
    # Generate problems
    problems = []
    while len(problems) < runs:
        for i in range(2, max_agents + 1):
            problems.append(util.generate_problem(i, 16, 16, OBSTACLES))
            if len(problems) == runs:
                break

    instance = 0
    line = []
    f = open(f'results/quality-{datetime.now()}.csv', 'w')
    writer = csv.writer(f)
    result = ['instance', 'num agents', 'algorithm', 'length', 'loops']
    writer.writerow(result)

    global_start_time = timeit.default_timer()
    for problem in problems:
        num_agents = len(problem[1])
        optimal_length = 0
        print(f"Problem has {num_agents} agents")

        for algorithm in ALGORITHMS:
            result = [instance, num_agents, algorithm.name]
            start_time = timeit.default_timer()
            try:
                data = algorithm.entry(*problem,
                                       start_time=start_time,
                                       max_time=MAX_TIME,
                                       **algorithm.kwargs)
                paths = data['paths']
                length = sum(len(p) for p in paths)
                end_time = timeit.default_timer()
                loops = calculate_loops(paths)
                result += [length, loops]
            except (odid.TimeExceeded, rra_star.NoValidPathExists,
                    util.NoPathsFoundException, version1b.ConflictNotSolved):
                result += ['NA', 'NA']
            writer.writerow(result)
            f.flush()
        instance += 1
    global_end_time = timeit.default_timer()
    print(f'Total time: {(global_end_time - global_start_time) * 1000:5.3f}ms')
    f.close()
Пример #8
0
def eval_weights(algorithm, weights, num_problems, max_agents):
    # Generate problems
    problems = []
    while len(problems) < num_problems:
        for i in range(2, max_agents):
            problems.append(util.generate_problem(i, 16, 16, OBSTACLES))
            if len(problems) == num_problems:
                break

    actual = []
    optimal = []
    for world, starts, goals in problems:
        start_time = timeit.default_timer()
        optimal.append(0)
        agents = [
            version1.Agent(world, starts[i], goals[i], weights=weights)
            for i in range(len(starts))
        ]
        # Calculate the sum length of the paths
        try:
            for agent in agents:
                agent.plan(start_time=start_time, max_time=MAX_TIME)
                optimal[-1] += len(agent.path)
        except (odid.TimeExceeded, rra_star.NoValidPathExists,
                util.NoPathsFoundException, version1b.ConflictNotSolved):
            actual.append(float('inf'))
            continue
        # Do the actual cooperative planning
        try:
            res = algorithm.entry(world, starts, goals, start_time, MAX_TIME,
                                  **algorithm.kwargs)
            actual.append(sum(len(path) for path in res['paths']))
        except (odid.TimeExceeded, rra_star.NoValidPathExists,
                util.NoPathsFoundException, version1b.ConflictNotSolved):
            actual.append(float('inf'))

    diff = [
        res[1] - res[0] for res in zip(optimal, actual)
        if res[1] != float('inf')
    ]
    print(diff)
    if len(diff) == 0:
        return float('inf')
    return sum(diff) / len(diff)
Пример #9
0
def main(runs, max_agents):
    problems = []
    while len(problems) < runs:
        for i in range(2, max_agents + 1):
            problems.append(util.generate_problem(i, 16, 16, OBSTACLES))
            if len(problems) == runs:
                break

    instance = 0
    line = []
    f = open(f'results/conflictsize-{datetime.now()}.csv', 'w')
    writer = csv.writer(f)
    result = ['instance', 'num.agents'] + list(range(2, 6))
    writer.writerow(result)

    global_start_time = timeit.default_timer()
    for problem in problems:
        num_agents = len(problem[1])
        results = [instance, num_agents] + [0] * 4
        print(f"Problem has {num_agents} agents")
        start_time = timeit.default_timer()
        agents = [
            version1b.Agent(problem[0],
                            problem[1][i],
                            problem[2][i],
                            weights=Weights(0.3129151, 5.569737, 2.677335))
            for i in range(len(problem[1]))
        ]
        try:
            ret = version1b.version1(agents, start_time, MAX_TIME, False)
            for conflict in ret['sizes']:
                results[conflict] += 1
        except (odid.TimeExceeded, rra_star.NoValidPathExists,
                util.NoPathsFoundException, version1b.ConflictNotSolved):
            result = [instance, num_agents] + ['NA'] * 4
        writer.writerow(results)
        f.flush()
        instance += 1
Пример #10
0
def main(runs, max_agents):
    # Generate problems
    problems = []
    while len(problems) < runs:
        for i in range(2, max_agents + 1):
            problems.append(util.generate_problem(i, 16, 16, OBSTACLES))
            if len(problems) == runs:
                break

    instance = 0
    line = []
    f = open(f'results/benchmark-{datetime.now()}.csv', 'w')
    writer = csv.writer(f)
    result = [
        'instance', 'num agents', 'algorithm', 'time', 'length',
        'initial conflicts', 'solved conflicts', 'extra length'
    ]
    writer.writerow(result)

    global_start_time = timeit.default_timer()
    for problem in problems:
        print(f"Problem has {len(problem[1])} agents")
        optimal_length = 0
        num_agents = len(problem[1])
        try:
            for i in range(len(problem[1])):
                dist = rra_star.RRAstar(problem[0], problem[1][i],
                                        problem[2][i])
                optimal_length += dist.dist(problem[1][i])
        except rra_star.NoValidPathExists:
            optimal_length = 'NA'
        result = [
            instance, num_agents, 'optimal', 'NA', optimal_length, 'NA', 'NA',
            'NA'
        ]
        writer.writerow(result)

        for algorithm in ALGORITHMS:
            result = [instance, num_agents, algorithm.name]
            start_time = timeit.default_timer()
            try:
                data = algorithm.entry(*problem,
                                       start_time=start_time,
                                       max_time=MAX_TIME,
                                       **algorithm.kwargs)
                paths = data['paths']
                init = data['initial']
                final = data['solved']
                length = sum(len(p) for p in paths)
                end_time = timeit.default_timer()
                result += [
                    end_time - start_time, length, init, final,
                    length - optimal_length
                ]
            except odid.TimeExceeded:
                print('Time exceeded')
                result += ['NA', 'NA', 'NA', 'NA']
            except (rra_star.NoValidPathExists, util.NoPathsFoundException):
                print('No valid path exists')
                result += ['NA', 'NA', 'NA', 'NA']
            except version1b.ConflictNotSolved:
                print('Could not find a solution to a conflict')
                result += ['NA', 'NA', 'NA', 'NA']
            finally:
                end_time = timeit.default_timer()
                print(algorithm.name, 'time:',
                      f'{(end_time - start_time) * 1000:5.3f}ms')
            writer.writerow(result)
            f.flush()
        instance += 1
    global_end_time = timeit.default_timer()
    print(f'Total time: {(global_end_time - global_start_time) * 1000:5.3f}ms')
    f.close()
Пример #11
0
def main(runs, max_agents):
    # Generate problems
    problems = []
    while len(problems) < runs:
        for i in range(2, max_agents):
            problems.append(util.generate_problem(i, 16, 16, 0.2))
            if len(problems) == runs:
                break

    f = open('poc_bench.csv', 'w')
    i = 0
    f.write('num,od,poc,version1\n')

    global_start_time = timeit.default_timer()
    for problem in problems:
        f.write(f'{i},')
        i += 1
        # Run OD+ID
        start_time = timeit.default_timer()
        try:
            odid.odid2(len(problem[1]),
                       *problem,
                       start_time=start_time,
                       max_time=MAX_TIME)
            end_time = timeit.default_timer()
            f.write(f'{end_time-start_time},')
        except odid.TimeExceeded:
            end_time = timeit.default_timer()
            print('OD+ID failed')
            f.write('NA,')
        except (rra_star.NoValidPathExists, util.NoPathsFoundException):
            print('No valid path exists')
            f.write('NA,')
        finally:
            print(f'OD  time: {(end_time - start_time) * 1000:5.3f}ms')

        # Run proof of concept
        start_time = timeit.default_timer()
        try:
            agents = [
                poc.Agent(problem[0], problem[1][i], problem[2][i])
                for i in range(len(problem[1]))
            ]
            poc.poc(agents, start_time=start_time, max_time=MAX_TIME)
            end_time = timeit.default_timer()
            f.write(f'{end_time-start_time},')
        except odid.TimeExceeded:
            end_time = timeit.default_timer()
            print('POC failed')
            f.write('NA,')
        except (rra_star.NoValidPathExists, util.NoPathsFoundException):
            print('No valid path exists')
            f.write('NA,')
        finally:
            print(f'POC time: {(end_time - start_time) * 1000:5.3f}ms')

        # Run Version 1
        start_time = timeit.default_timer()
        try:
            agents = [
                version1.Agent(problem[0], problem[1][i], problem[2][i])
                for i in range(len(problem[1]))
            ]
            version1.version1(agents, start_time, MAX_TIME, False)
            end_time = timeit.default_timer()
            f.write(f'{end_time-start_time}\n')
        except odid.TimeExceeded:
            end_time = timeit.default_timer()
            print('Version1 failed')
            f.write('NA\n')
        except (rra_star.NoValidPathExists, util.NoPathsFoundException):
            print('No valid path exists')
            f.write('NA\n')
        finally:
            print(f'Version1 time: {(end_time - start_time) * 1000:5.3f}ms')

        # Write latest results to file
        f.flush()
    global_end_time = timeit.default_timer()

    print(f'Final time: {(global_end_time - global_start_time) * 1000:5.3f}ms')
    f.close()