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 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()
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()
#!/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()
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()
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 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 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()
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()
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()
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'