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, }
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)
def run(): world = World(ally_conf_path=args.conf_path) while world.time < args.iterations: try: world.update() except KeyboardInterrupt: break
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)
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)
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, }
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, }
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)
c = 0 move = None for agent in self.world.agents: if self == agent: continue dist = self.distance(agent) if dist < 10: c += 1 if dist < 5 and dist > 1 and not move: move = [x / 15 for x in self.vector_to(agent)] if self.cooldown == 0: if c > 2 and c < 20 and len(world.agents) < 1000: world.add(RandomMoving(color='FF0000FF', world=self.world)) self.cooldown = 10 else: self.cooldown -= 1 if move: self.move(*move) else: self.move((random() - 0.5) / 2, (random() - 0.5) / 2, (random() - 0.5) / 2) if __name__ == '__main__': world = World() [world.add(RandomMoving(world=world)) for x in range(10)] print '\n'.join(map(str, world.agents)) + '\ndone' for i in range(1000): world.genNext() print '\n'.join(map(str, world.agents)) + '\ndone'
class Spherical(Agent): def init(self, world): self.rounds = 0.0 self.world = world def act(self): for agent in self.world.agents: if self == agent: continue dist = self.distance(agent) if dist > 5: v = [x / 50 for x in self.vector_to(agent)] self.move(*v) return elif dist < 5: v = [x / 50 for x in self.vector_to(agent, 1)] self.move(*v) return else: return if __name__ == '__main__': world = World() [world.add(Spherical(world=world)) for x in range(1000)] print world for i in range(1000): print world.genNext()
#!/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()
self.move((random() - 0.5) / 2, (random() - 0.5) / 2, (random() - 0.5) / 2) class Predator(Agent): def init(self, world): self.world = world self.color = '00FFFFFF' def act(self): shuffle(self.world.Victim) for agent in self.world.Victim: if self == agent: continue dist = self.distance(agent) if dist < 18 and dist > 1: v = [x / 25 for x in self.vector_to(agent)] self.move(*v) return self.move((random() - 0.5) / 2, (random() - 0.5) / 2, (random() - 0.5) / 2) if __name__ == '__main__': world = World() [world.add(Victim(world=world)) for x in range(1000)] [world.add(Predator(world=world)) for x in range(100)] print world for i in range(1000): print world.genNext()
#!/usr/bin/env python from math import sin from swarm import Agent, World class SinAgent(Agent): def init(self): self.rounds = 0.0 def act(self): self.x += sin(self.rounds+int(self.name.split('_')[1])) self.y -= sin(self.rounds) self.rounds += .1 if __name__ == '__main__': world = World() [world.add(SinAgent()) for x in range(1000)] print world for i in range(1000): print world.genNext()
from random import random, shuffle class RandomFollow(Agent): def init(self, world): self.rounds = 0.0 self.world = world def act(self): shuffle(self.world.agents) for agent in self.world.agents: if self == agent: continue dist = self.distance(agent) if dist < 5 and dist > 1: v = [x/15 for x in self.vector_to(agent)] self.move(*v) return self.move((random()-0.5)/2, (random()-0.5)/2, (random()-0.5)/2) if __name__ == '__main__': world = World() [world.add(RandomFollow(world=world)) for x in range(100)] print world for i in range(1000): print world.genNext()
move = None for agent in self.world.agents: if self == agent: continue dist = self.distance(agent) if dist < 10: c += 1 if dist < 5 and dist > 1 and not move: move = [x/15 for x in self.vector_to(agent)] if self.cooldown == 0: if c > 2 and c < 20 and len(world.agents) < 1000: world.add(RandomMoving(color='FF0000FF', world=self.world)) self.cooldown = 10 else: self.cooldown -= 1 if move: self.move(*move) else: self.move((random()-0.5)/2, (random()-0.5)/2, (random()-0.5)/2) if __name__ == '__main__': world = World() [world.add(RandomMoving(world=world)) for x in range(10)] print '\n'.join(map(str, world.agents))+'\ndone' for i in range(1000): world.genNext() print '\n'.join(map(str, world.agents))+'\ndone'
def init(self, world): self.rounds = 0.0 self.world = world def act(self): for agent in self.world.agents: if self == agent: continue dist = self.distance(agent) if dist > 5: v = [x/50 for x in self.vector_to(agent)] self.move(*v) return elif dist < 5: v = [x/50 for x in self.vector_to(agent, 1)] self.move(*v) return else: return if __name__ == '__main__': world = World() [world.add(Spherical(world=world)) for x in range(1000)] print world for i in range(1000): print world.genNext()
class Predator(Agent): def init(self, world): self.world = world self.color = '00FFFFFF' def act(self): shuffle(self.world.Victim) for agent in self.world.Victim: if self == agent: continue dist = self.distance(agent) if dist < 18 and dist > 1: v = [x/25 for x in self.vector_to(agent)] self.move(*v) return self.move((random()-0.5)/2, (random()-0.5)/2, (random()-0.5)/2) if __name__ == '__main__': world = World() [world.add(Victim(world=world)) for x in range(1000)] [world.add(Predator(world=world)) 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()
#!/usr/bin/env python from math import sin from random import randint from swarm import Agent, World class SinAgent(Agent): def init(self, world): self.rounds = 0.0 self.max_age = randint(1, 100) self.world = world self.color = 'FF00FFFF' def act(self): self.x += sin(self.rounds + int(self.name.split('_')[1])) self.y -= sin(self.rounds) self.rounds += .1 if self.rounds > self.max_age: self.world.remove(self) if __name__ == '__main__': world = World() [world.add(SinAgent(world=world)) for x in range(1000)] print world for i in range(1000): print world.genNext()