Пример #1
0
 def testDieStarveSync(self):
     env = environment.environment(10,'sync')
     # Create a starving fox
     rabbit= agents.rabbit(age=2,food=-1,pos=[5.5,5.5],speed=2,last_breed=7)
     # Ensure that it knows that its time to die
     rabbit.die(env)
     self.failUnless(rabbit.messages.dead,'Starved rabbit did not die')
Пример #2
0
 def testDieOldSync(self):
     env = environment.environment(10,'sync')
     # Create an old rabbit
     rabbit= agents.rabbit(age=1000,food=10,pos=[5.5,5.5],speed=2,last_breed=7)
     # Ensure that it knows that its time to die
     rabbit.die(env)
     self.failUnless(rabbit.messages.dead,'Ancient rabbit did not die')
Пример #3
0
 def testDefault(self):
     self.rabbit = agents.rabbit()
     self.assertEqual(self.rabbit.age,[])
     self.assertEqual(self.rabbit.food,[])
     self.assertEqual(self.rabbit.pos,[])
     self.assertEqual(self.rabbit.speed,[])
     self.assertEqual(self.rabbit.last_breed,[])
Пример #4
0
 def testBreed1(self):
     #Esnure that a rabbit that can breed, does breed
     env = environment.environment(5)
     #Create a rabbit that's ready to breed
     rabbit = agents.rabbit(age=30,food=100,pos=[5.5,5.5],speed=2,last_breed=21)
     new_rabbit = rabbit.breed(env)
     self.failUnless(isinstance(new_rabbit,agents.rabbit))
Пример #5
0
    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)
Пример #6
0
 def testMigrateWhenFoodAvailable(self):
     #Does a rabbit at (5.5,5.5) migrate to the same position as the MATLAB version given that food is available
     self.env= environment.environment(10,mode='sync')
     numpy.random.seed(1) #MMigrate uses random numbers so need to seed to get reproducable results
     
     rabbit= agents.rabbit(age=2,food=10,pos=[5.5,5.5],speed=2,last_breed=7)
     rabbit.migrate(self.env)
     self.failUnlessAlmostEqual(rabbit.pos[0],4.917022,places=4,msg='Migrating rabbit has given different results from MATLAB')
     self.failUnlessAlmostEqual(rabbit.pos[1],5.22032449,places=4,msg='Migrating rabbit has given different results from MATLAB')
Пример #7
0
    def testExtractLocalFoodTopLeftCorner(self):
        self.env = environment.environment(10)
        rabbit = agents.rabbit(age=2,food=10,pos=[1.1,1.1],speed=2,last_breed=7) #Rabbit in corner of simulation

        (local_food,xmin,ymin)  = rabbit.extract_local_food(self.env)

        self.failUnlessEqual(numpy.shape(local_food),(3,3),'Incorrect local food size for rabbit in top left corner')

        self.failUnless(numpy.all(local_food==50),'Incorrect local food values for rabbit in top left')
Пример #8
0
    def testEatInDesert(self):
        self.env = environment.environment(10)
        self.rabbit = agents.rabbit(age=2,food=10,pos=[5.5,5.5],speed=2,last_breed=7) #In a food desert
        #The rabbit is in a food desert. Does it eat correctly?
        starting_food = self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]]
        self.failUnlessEqual(starting_food,0,'This rabbit was supposed to be in a desert')
        eaten = self.rabbit.eat(self.env)

        self.failUnlessEqual(self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]],0,'Starting food was 0. Rabbit incorrectly changed that!')
        self.failIfEqual(eaten,1,'Rabbit reports that it has eaten when it can''t have')
Пример #9
0
    def testExtractLocalFoodBottomRightCorner(self):
        self.env = environment.environment(10)
        rabbit = agents.rabbit(age=2,food=10,pos=[9.5,9.5],speed=2,last_breed=7) #Rabbit in corner of simulation
        
        (local_food,xmin,ymin)  = rabbit.extract_local_food(self.env)

        self.failUnlessEqual(numpy.shape(local_food),(3,3),'Incorrect local food size for rabbit in bottom right corner')

        correct = numpy.array([[0,50,50],[50,50,50],[50,50,50]])
        self.failUnless(numpy.array_equal(local_food,correct),'Incorrect local food values for rabbit in bottom right corner')
Пример #10
0
 def testMigrateWhenFoodNotAvailable(self):
     #Does a rabbit at (9.8,9.8) migrate to the same posiiton as the MATLAV version given that no food is available
     numpy.random.seed(1) #MMigrate uses random numbers so need to seed to get reproducable results
     
     self.env= environment.environment(10,mode='sync')
     self.env.food= numpy.zeros((10,10)) #Eat all the food
     rabbit= agents.rabbit(age=2,food=10,pos=[9.8,9.8],speed=2,last_breed=7)
     rabbit.migrate(self.env)
     
     self.failUnlessAlmostEqual(rabbit.pos[0],7.86930887,places=4,msg='Migrating rabbit has given different results from MATLAB')
     self.failUnlessAlmostEqual(rabbit.pos[1],9.27805005,places=4,msg='Migrating rabbit has given different results from MATLAB')
Пример #11
0
    def testExtractLocalFoodCentral(self):
        correct = [[50,50,50,50,50], \
               [50,50,50,50,50], \
               [50,50,50,50,50], \
               [50,50,50,0,0], \
               [50,50,50,0,0]]

        self.env = environment.environment(10)
        rabbit = agents.rabbit(age=2,food=10,pos=[5,5],speed=2,last_breed=7) 
        (local_food,xmin,ymin) = rabbit.extract_local_food(self.env)

        self.failUnless(numpy.array_equal(local_food,correct),'Incorrect local food values for rabbit in center')
Пример #12
0
 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)
Пример #13
0
    def testEatWhenHasFood(self):
         #The rabbit has some food.  Does it eat correctly?
        self.env = environment.environment(10)
        self.rabbit = agents.rabbit(age=2,food=10,pos=[1.2,1.2],speed=2,last_breed=7) #Food initially=50
        
        starting_food = self.env.food[(self.rabbit.cpos[0],self.rabbit.cpos[1])]
        self.failIfEqual(starting_food,0,'This rabbit was supposed to have access to food')
        
        eaten = self.rabbit.eat(self.env)

        self.failUnlessEqual(self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]],starting_food-1,'Rabbit has incorrectly changed food quanity after eating.')

        self.failIfEqual(eaten,0,'Rabbit reports that it hasn''t eaten when it has')
Пример #14
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')
Пример #15
0
 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')
Пример #16
0
    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)
Пример #17
0
 def testCpos(self):
     # Ensures cpos is calculated correctly
     self.rabbit1 = agents.rabbit(age=2,food=10,pos=[5.5,5.5],speed=2,last_breed=7) #In a food desert
     self.failUnless(all(self.rabbit1.cpos==5))