def testDieOldSync(self): env = environment.environment(5,'sync') #Create an old fox fox= agents.fox(age=1000, food=10, pos=[5.5,5.5], speed=2, last_breed=7) fox.die(env) self.failUnless(fox.messages.dead)
def testDieStarveSync(self): env = environment.environment(5,'sync') #Create a starving fox fox= agents.fox(age=2, food=-1, pos=[5.5,5.5], speed=2, last_breed=7) #Ensure that it knows that its time to die fox.die(env) self.failUnless(fox.messages.dead)
def testBreed4(self): #Esnure that a fox that is not ready to breed, doesn't env = environment.environment(5) #Create a fox that's not ready to breed doesn't fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=10) new_fox = fox.breed(env) self.failUnless(new_fox is None)
def testBreed3(self): #Esnure that a breeding fox correctly zeros last_breed env = environment.environment(5) #Create a fox that's ready to breed fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=21) new_fox = fox.breed(env) self.failUnless(new_fox.last_breed == 0 and new_fox.last_breed == 0)
def testBreed1(self): #Esnure that a fox that can breed, does breed env = environment.environment(5) #Create a fox that's ready to breed fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=21) new_fox = fox.breed(env) self.failUnless(isinstance(new_fox,agents.fox))
def create_agents(self, nr, nf, list_type='joined'): """ environment.create_agents(nr, nf) - Creates a list of agents within the environment Input Parameters ---------------- nr : integer Number of rabbits nf : integer Number of foxes list_type : string (default='joined') Determines how the agents are stored. 'joined' - maintains a single list containing both rabbits and foxes. 'seprate' - maintains separate lists of rabbits and foxes (THIS WONT WORK WITH THE REST OF ECOLAB YET) Output: ------- agent_list : list List(s) of generated agents """ # We create the transpose here in order to generate matrices # that are identical to MATLAB's # generate random initial positions for rabbits and foxes rloc = np.transpose((self.bm_size-1)*rand(2, nr))+1 floc = np.transpose((self.bm_size-1)*rand(2, nf))+1 rabbits = [] foxes = [] for r in range(nr): pos = rloc[r, :] age = np.ceil(rand()*10) food = np.ceil(rand()*20)+20 lbreed = np.round(rand()*rabbit.brdfq) rabbits.append(rabbit(age, food, pos, rabbit.speed, lbreed)) for f in range(nf): pos = floc[f, :] age = np.ceil(rand()*10) food = np.ceil(rand()*20)+20 lbreed = np.round(rand()*fox.brdfq) foxes.append(fox(age, food, pos, fox.speed, lbreed)) if list_type == 'separate': self.foxes = foxes self.rabbits = rabbits elif list_type == 'joined': self.agents = rabbits self.agents.extend(foxes)
def testEat1Async(self): env = environment.environment(5,'async') # Create a very fast fox fox = agents.fox(age=2, food=10, pos=[1.5,1.5], speed=10, last_breed=7) # create a rabbit very close to it env.agents = [agents.rabbit(age=2, food=10, pos=[1.52,1.52], speed=10, last_breed=7)] #Set the previous position of rabbit to be [1.52,1.52] env.agents[0].messages.pos = [1.52, 1.52] # Its almost certain that this fox ate this rabbit fox.eat(env) # Doex rabbit know that its been eaten? self.failUnless(env.agents[0].has_been_eaten)
def testEat2(self): # Ensure that the same rabbit doesn't get eaten multiple times in async mode env = environment.environment(5,mode='async') env.agents = [] # Surround a rabbit by 4 foxes in eating range env.agents.append(agents.rabbit(20 ,30, [1.5, 1.5], 2, 1)) env.agents.append(agents.fox(20 ,30, [1.5, 1.25], 10, 10)) env.agents.append(agents.fox(20 ,30, [1.25, 1.5], 10, 10)) env.agents.append(agents.fox(20 ,30, [1.25, 1.25], 10, 10)) env.agents.append(agents.fox(20 ,30, [1.3, 1.25], 10, 10)) eaten1 = env.agents[1].eat(env) eaten2 = env.agents[2].eat(env) eaten3 = env.agents[3].eat(env) eaten4 = env.agents[4].eat(env) self.failUnless(eaten1, 'Fox 1 should have eaten but didn''t') self.failUnless(not eaten2, 'Fox 2 should not have eaten but did') self.failUnless(not eaten3, 'Fox 3 should not have eaten but did') self.failUnless(not eaten4, 'Fox 4 should not have eaten but did') self.failUnlessEqual(agents.rabbit.num_rabbits,0)
def testBreed2(self): #Ensure that an eaten rabbit doesn't breed in async mode env = environment.environment(5,mode='async') env.agents = [] # Put a hungry fox next to a rabbit env.agents.append(agents.fox(20,30,[1.5,1.5],5,10)) env.agents.append(agents.rabbit(20,30,[1.5,1.25],2,10)) eaten = env.agents[0].eat(env) self.failUnless(eaten,'Fox should have eaten rabbit') new = env.agents[1].breed(env) self.failUnless(new is None,'Dead rabbit bred')
def testDieDoubleCount(self): # Ensure that we don't decrement rabbit counter twice # If an eaten rabbit was also ready to die env = environment.environment(10,'async') env.agents = [] # Put an old rabbit next to a hungry fox env.agents.append(agents.rabbit(1000,30,[1.5,1.5],2,1)) env.agents.append(agents.fox(20,30,[1.5,1.25],10,10)) # Fox eats rabbit eaten = env.agents[1].eat(env) self.failUnless(eaten,'Fox didn''t eat rabbit') # Apply die rule to the dead rabbit env.agents[0].die(env) self.failUnlessEqual(agents.rabbit.num_rabbits,0,'Should be 0 rabbits')
def testFoxMigrate(self): numpy.random.seed(1) fox= agents.fox(age=2, food=10, pos=[5.5,5.5], speed=2, last_breed=7) fox.migrate(self.env) self.failUnlessAlmostEqual(fox.pos[0], 3.765721, places=4, msg='Migrating fox has given different results from MATLAB') self.failUnlessAlmostEqual(fox.pos[1], 6.496130, places=4, msg='Migrating fox has given different results from MATLAB')