def test_keep_higher(self):
        """Make sure we always keep higher fitness when specified.
        """
        crossover = SafeFitnessCrossover(self.test_crossover)

        self.test_crossover.type = "same"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertEqual(str(new_org_1), str(self.org_1),
                         "Did not retain organism for same fitness.")
        self.assertEqual(str(new_org_2), str(self.org_2),
                         "Did not retain organism for same fitness.")

        self.test_crossover.type = "lower"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertEqual(
            str(new_org_1), str(self.org_1),
            "Did not retain organism when crossover had lower fitness.")
        self.assertEqual(
            str(new_org_2), str(self.org_2),
            "Did not retain organism when crossover had lower fitness.")

        self.test_crossover.type = "higher"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertTrue(
            new_org_1.fitness > self.org_1.fitness
            and new_org_2.fitness > self.org_2.fitness,
            "Did not get new organism when it had higher fitness.")
    def test_keep_lower(self):
        """Make sure we do normal crossover functionality when specified.
        """
        crossover = SafeFitnessCrossover(self.test_crossover, 1.0)

        self.test_crossover.type = "same"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertEqual(str(new_org_1), str(self.org_1),
                         "Did not retain organism for same fitness.")
        self.assertEqual(str(new_org_2), str(self.org_2),
                         "Did not retain organism for same fitness.")

        self.test_crossover.type = "lower"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertNotEqual(str(new_org_1), str(self.org_1),
                            "Retained lower fitness organism in crossover.")
        self.assertNotEqual(str(new_org_2), str(self.org_2),
                            "Retained lower fitness organism in crossover.")

        self.test_crossover.type = "higher"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        self.assertTrue(
            new_org_1.fitness > self.org_1.fitness
            and new_org_2.fitness > self.org_2.fitness,
            "Did not get new organism under higher fitness conditions.")
示例#3
0
    def test_keep_lower(self):
        """Make sure we do normal crossover functionality when specified.
        """
        crossover = SafeFitnessCrossover(self.test_crossover, 1.0)

        self.test_crossover.type = "same"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        assert (new_org_1 == self.org_1 and new_org_2 == self.org_2), \
               "Did not retain organism for same fitness."

        self.test_crossover.type = "lower"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        assert (new_org_1 != self.org_1 and new_org_2 != self.org_2), \
               "Did not retain lower fitness organism in crossover."

        self.test_crossover.type = "higher"
        new_org_1, new_org_2 = crossover.do_crossover(self.org_1, self.org_2)

        assert (new_org_1.fitness > self.org_1.fitness and
                new_org_2.fitness > self.org_2.fitness), \
                "Did not get new organism under higher fitness conditions."