예제 #1
0
 def test(self):
     g1 = [True, False, True]
     g2 = [False, False, False]
     uc = UniformCrossover()
     crossed = uc(Individual(g1, g1, 0.0), Individual(g2, g2, 0.0))
     for i in range(len(g1)):
         self.assertIn(crossed[i], [g1[i], g2[i]])
예제 #2
0
 def test(self):
     g1 = [True, False, True]
     g2 = [False, False, False]
     mpx = MultiPointCrossover(1)
     crossed = mpx(Individual(g1, g1, 0.0), Individual(g2, g2, 0.0))
     for i in range(len(g1)):
         self.assertIn(crossed[i], [g1[i], g2[i]])
예제 #3
0
 def test_make_move(self):
     sem12 = SwapElementsMove(1, 2)
     perm_gen = RandomPermutation(10)
     perm = perm_gen()
     ind = Individual(perm, perm, 0.0)
     perm_act = sem12.make_move(ind)
     ind2 = Individual(perm_act, perm_act, 0.0)
     perm_act_rev = sem12.reverse().make_move(ind2)
     self.assertListEqual(perm, perm_act_rev)
예제 #4
0
    def test(self):
        rpm = ReversePartMove(1, 2)
        rpm_r = rpm.reverse()
        self.assertEqual(rpm.i, rpm_r.i)
        self.assertEqual(rpm.j, rpm_r.j)

        self.assertTrue(rpm.conflicts(rpm))

        ind = Individual([0, 1, 2], [0, 1, 2], 0.0)
        ind_moved = rpm.make_move(ind)
        self.assertEqual(ind.g[0], ind_moved[0])
        self.assertEqual(ind.g[1], ind_moved[2])
        self.assertEqual(ind.g[2], ind_moved[1])
예제 #5
0
 def test(self):
     g1 = [True, False, True, False, True, True, False]
     for num_flips in range(4):
         for can_repeat in [True, False]:
             uso = BitFlips(num_flips, can_repeat=can_repeat)
             mutated = uso(Individual(g1, g1, 0.0))
             flipped = 0
             for i in range(len(g1)):
                 if mutated[i] != g1[i]:
                     flipped += 1
             if can_repeat:
                 self.assertTrue(flipped <= num_flips)
             else:
                 self.assertEqual(num_flips, flipped)
예제 #6
0
    def test(self):
        population = [
            Individual([0], [0], 0.0, fitness=0.0),
            Individual([1], [1], 0.0, fitness=0.0),
            Individual([2], [2], 0.0, fitness=0.0),
            Individual([3], [3], 0.0, fitness=0.0),
            Individual([4], [4], 0.0, fitness=0.0)
        ]
        universal_parent = Individual([-1], [-1], 0.0, fitness=0.0)
        for ind in population:
            ind.reproduction_auxiliary = universal_parent

        selectors = [
            RouletteWheel(2),
            TournamentSelection(2, 3),
            TruncationSelection(3),
            ParentReplacementSelection(),
            PopulationTracker(RouletteWheel(2))
        ]

        for s in selectors:
            selected = s(population)
            for ind in selected:
                self.assertIsInstance(ind, Individual)
예제 #7
0
 def test(self):
     ind = Individual([0, 1, 2], [0, 1, 2], 0.0)
     neighbourhood = tsp_swap_neighbourhood(ind)
     for neighbour in neighbourhood:
         self.assertNotEqual(neighbour.i, neighbour.j)
예제 #8
0
 def test(self):
     pss = PermutationSingleSwap(2)
     g = [1, 2]
     permuted = pss(Individual(g, g, 0.0))
     self.assertEqual(permuted, [2, 1])