示例#1
0
 def test_add_improving(self):
     for k in range(5):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 100.0 - k
         yield self.h.add(i)
         self.assertEqual(len(self.h), k + 1)
     for k, values in enumerate(self.h):
         self.assertItemsEqual(values, [4 - k, 4 - k + 1, 4 - k + 2])
示例#2
0
 def test_value_vs_SSE_maxRatio(self):
     for k in range(10):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 10.0 + k
         yield self.h.add(i)
     XY = yield self.h.value_vs_SSE(['bar'], maxRatio=1.5)
     self.assertEqual(len(XY), 2)
     self.assertItemsEqual(XY[0], np.linspace(10.0, 15.0, 6))
     self.assertItemsEqual(XY[1], np.linspace(1.0, 6.0, 6))
示例#3
0
 def test_add_worsening(self):
     for k in range(5):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 100.0 + k
         yield self.h.add(i)
         self.assertEqual(len(self.h), k + 1)
         self.assertItemsEqual(self.h[k], [i.SSE] + i.values)
     for k, values in enumerate(self.h):
         self.assertItemsEqual(values, [k, k + 1, k + 2])
示例#4
0
 def doCrossovers(CR):
     N_allMutant = 0
     self.de.CR = CR
     for trial in range(N_runs):
         i1 = tb.MockIndividual(self.de.p, np.ones(Nd))
         self.de.crossover(i0, i1)
         self.assertNotEqual(i1, i0)
         if i1.equals(np.ones(Nd)):
             N_allMutant += 1
     return float(N_allMutant)/N_runs
示例#5
0
 def test_value_vs_SSE_notInPop(self):
     for k in range(10):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 10.0 + k
         kr = yield self.h.add(i)
         if k > 5: self.h.notInPop(kr)
     XY = yield self.h.value_vs_SSE(['bar'], notInPop=True)
     self.assertEqual(len(XY), 2)
     self.assertItemsEqual(XY[0], np.linspace(16.0, 19.0, 4))
     self.assertItemsEqual(XY[1], np.linspace(7.0, 10.0, 4))
示例#6
0
 def test_add_then_purge(self):
     for k in range(5):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 100.0 - k
         yield self.h.add(i)
         self.assertEqual(len(self.h), k + 1)
     self.assertEqual(len(self.h), 5)
     self.assertEqual(len(self.h.Kp), 5)
     self.h.purgePop()
     self.assertEqual(len(self.h), 0)
     self.assertEqual(len(self.h.Kp), 0)
示例#7
0
    def test_pickle(self):
        def values(k):
            return [k, np.exp(-0.1 * k), np.exp(-0.5 * k)]

        for k in range(10):
            i = tb.MockIndividual(values=values(k))
            i.SSE = 1000.0 + k
            yield self.h.add(i)
        s = pickle.dumps(self.h)
        h = pickle.loads(s)
        self.assertEqual(len(h), 10)
        for k, x in enumerate(h):
            sdiff = np.sum(np.square(x - values(k)))
            self.assertLess(sdiff, 1E-6)
示例#8
0
 def test_add_limitSize_improving_neverInPop(self):
     for k in range(15):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 1000.0 - k
         yield self.h.add(i, neverInPop=True)
     self.assertEqual(len(self.h), 10)
     self.assertEqual(len(self.h.Kp), 0)
     self.assertEqual(len(self.h.Kn), 10)
     valuesPrev = None
     for values in self.h:
         if valuesPrev is not None:
             for v, vp in zip(values, valuesPrev):
                 self.assertLess(v, vp)
         valuesPrev = values
示例#9
0
 def test_add_ignoreInfSSE(self):
     for k in range(5):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 100.0 + k if k < 3 else float('+inf')
         kr = yield self.h.add(i)
         if k < 3:
             self.assertLess(kr, 10)
             self.assertEqual(len(self.h), k + 1)
             self.assertItemsEqual(self.h[k], [i.SSE] + i.values)
         else:
             self.assertIs(kr, None)
             self.assertEqual(len(self.h), 3)
     for k, values in enumerate(self.h):
         self.assertItemsEqual(values, [k, k + 1, k + 2])
示例#10
0
 def test_add_limitSize_improving(self):
     krPopped = set()
     for k in range(15):
         i = tb.MockIndividual(values=[k, k + 1, k + 2])
         i.SSE = 1000.0 - k
         yield self.h.add(i)
         if len(self.h.kr) == 10:
             iHash, kr = self.popitem_predictably(self.h.kr)
             yield self.h.notInPop(kr)
             krPopped.add(kr)
     self.assertEqual(len(self.h), 10)
     valuesPrev = None
     for values in self.h:
         if valuesPrev is not None:
             for v, vp in zip(values, valuesPrev):
                 self.assertLess(v, vp)
         valuesPrev = values
示例#11
0
    def test_crossover(self):
        def doCrossovers(CR):
            N_allMutant = 0
            self.de.CR = CR
            for trial in range(N_runs):
                i1 = tb.MockIndividual(self.de.p, np.ones(Nd))
                self.de.crossover(i0, i1)
                self.assertNotEqual(i1, i0)
                if i1.equals(np.ones(Nd)):
                    N_allMutant += 1
            return float(N_allMutant)/N_runs

        N_runs = 1000
        Np = 100; Nd = 4
        yield self.makeDE(Np, Nd)
        i0 = tb.MockIndividual(self.p, np.zeros(Nd))
        self.assertEqual(doCrossovers(0), 0.0)
        self.assertEqual(doCrossovers(1.0), 1.0)
        # 1    2    3    4  
        # 1.0  0.5  0.5  0.5  x = 0.125
        self.assertAlmostEqual(doCrossovers(0.5), 0.125, 1)
        # 1    2    3    4  
        # 1.0  0.8  0.8  0.8  x = 0.729
        self.assertAlmostEqual(doCrossovers(0.9), 0.729, 1)