Exemple #1
0
 def run():
     world = World(ally_conf_path=args.conf_path)
     while world.time < args.iterations:
         try:
             world.update()
         except KeyboardInterrupt:
             break
Exemple #2
0
def head2head(configuration, outpath, iterations=10000):
    """
    Run a head to head simulation using the configuration for the black
    team against the red team. Write detailed stats out to the outpath.
    Can also specificy the number of iterations to run the simulation for.
    """
    start = time.time()
    world = World(ally_conf_path=configuration, maximum_time=iterations)

    with open(outpath, 'w') as outfile:
        writer = csv.writer(outfile)
        header = (
            'black',
            'red',
        ) + ('deposit', ) * world.deposits
        writer.writerow(header)
        writer.writerow(world.status())

        for step in xrange(world.iterations):
            try:
                world.update()
                writer.writerow(world.status())
            except Exception as e:
                break

    finit = time.time()
    delta = finit - start

    return {
        'fitness': world.ally_home.stash,
        'run_time': delta,
        'iterations': world.time,
        'home_stash': world.ally_home.stash,
        'enemy_stash': world.enemy_home.stash,
    }
Exemple #3
0
def simulate(args):
    """
    Run a headless simulation with configuration file
    """
    start = time.time()
    world = World(ally_conf_path=args.conf_path)

    print "Starting headless simulation, use CTRL+C to quit."
    while world.time < world.iterations:
        try:
            world.update()
            if world.time % 1000 == 0:
                print "%ik iterations completed" % (world.time / 1000)
        except KeyboardInterrupt:
            print "Quitting Early!"
            break

    finit = time.time()
    delta = finit - start

    output = []
    output.append("Ran %i time steps in %0.3f seconds" % (world.time, delta))
    output.append("Agents successfully collected %i resources" %
                  world.ally_home.stash)
    return "\n".join(output)
Exemple #4
0
def head2head(args):
    """
    Run a head to head simulation, outputing the magnitude of the stash of
    each team in the simulation at every time step.
    """
    start = time.time()
    world = World(ally_conf_path=args.conf_path, maximum_time=args.iterations)

    print "Starting headless simulation, use CTRL+C to quit."
    writer = csv.writer(args.stream, delimiter='\t')
    writer.writerow(('black', 'red'))
    while world.time < world.iterations:
        try:
            world.update()
            writer.writerow(
                (str(world.ally_home.stash), str(world.enemy_home.stash)))
            if world.time % 1000 == 0:
                print "%ik iterations completed" % (world.time / 1000)
        except KeyboardInterrupt:
            print "Quitting Early!"
            break

    finit = time.time()
    delta = finit - start

    output = []
    output.append("Ran %i time steps in %0.3f seconds" % (world.time, delta))
    output.append("Agents successfully collected %i resources" %
                  world.ally_home.stash)
    return "\n".join(output)
Exemple #5
0
def visual(args):
    """
    Run the visual/PyGame version of the simulation
    """
    start = time.time()
    world = World(ally_conf_path=args.conf_path)
    size = args.screen_size
    fps = args.fps
    visualize(world, [size, size], fps)
    finit = time.time()
    delta = finit - start

    output = []
    output.append("Ran %i time steps in %0.3f seconds" % (world.time, delta))
    output.append("Agents successfully collected %i resources" %
                  world.ally_home.stash)
    return "\n".join(output)
Exemple #6
0
def runsim(configuration):
    """
    Run a simulation for the given number of timesteps and return fitness.
    """
    start = time.time()
    world = World(ally_conf_path=configuration)

    for step in xrange(world.iterations):
        try:
            world.update()
        except Exception as e:
            break

    finit = time.time()
    delta = finit - start

    return {
        'fitness': world.ally_home.stash,
        'run_time': delta,
        'iterations': world.time,
        'home_stash': world.ally_home.stash,
        'enemy_stash': world.enemy_home.stash,
    }
Exemple #7
0
#!/usr/bin/env python

from swarm import Agent, World

if __name__ == '__main__':
    world = World()
    [world.add(Agent()) for x in range(100)]
    print world
    for i in range(1000):
        print world.genNext()
    def act(self):
        nearbys = self.world.zones.get_nearby(self, 2)
        vectors = []
        for agent in nearbys:
            if self == agent:
                continue
            dist = self.distance(agent)
            if dist < 1:
                if agent.type == self.type:
                    vectors.append(self.vector_to(agent, 1))
            elif agent.type != self.type:
                vectors.append(self.vector_to(agent, -1))
        dist = self.distance([2.5, -0.5, 1.5])
        if len(vectors):
            self.move(*[x/15 for x in merge_vectors(vectors)])
        else:
            new_coords = [x/(dist*5) for x in self.vector_to([2.5, -0.5, 1.5])]
            self.move(*new_coords)


if __name__ == '__main__':
    world = World(zone_precisions=(-1,-1,-1))
    [world.add(Cubic(world=world)) for x in range(100)]
    [world.add(Spheric(world=world)) for x in range(100)]
    print world
    for i in range(1000):
        print world.genNext()