示例#1
0
 def test_clone(self):
     """
     Should copy the population with a bias towards fitter individuals
     """
     # should select towards all ones
     f0 = np.arange(-3, 5)
     for i in range(5):
         fit0 = 1. / np.abs(f0 - 1.)
         rank = tools.linear_rank(fit0, sp=2)
         nchild = np.round(rank)
         f0 = tools.clone(f0, nchild)
     self.assertEqual(np.sum(f0), len(f0))
示例#2
0
    def test_linear_rank(self):

        # should handle dimensions correctly
        for sp in np.arange(1, 2, 0.1):
            fitness = np.random.rand(5) 
            rank = tools.linear_rank(fitness, sp=sp)
        
            # should return an array with the same length as fitness
            self.assertEqual(len(fitness), len(rank))
            self.assertEqual(rank.shape, (len(fitness),))

            # should always sum to the length of fitness
            self.assertAlmostEqual(sum(rank), len(fitness), 6)

            # should rank according to values if sp > 1.
            if sp > 1.:
                self.assertEqual(np.argmax(rank), np.argmax(fitness))