Ejemplo n.º 1
0
    def setUp(self):
        self.alphabet = TestAlphabet()
        genome_1 = MutableSeq("11111", self.alphabet)
        self.org_1 = Organism(genome_1, test_fitness)

        genome_2 = MutableSeq("22222", self.alphabet)
        self.org_2 = Organism(genome_2, test_fitness)

        self.sym_crossover = GeneralPointCrossover(3, 1.0)
        self.asym_crossover = GeneralPointCrossover(4, 1.0)
    def setUp(self):
        self.alphabet = TestAlphabet()
        genome_1 = MutableSeq("11111", self.alphabet)
        self.org_1 = Organism(genome_1, test_fitness)

        genome_2 = MutableSeq("22222", self.alphabet)
        self.org_2 = Organism(genome_2, test_fitness)

        self.sym_crossover = GeneralPointCrossover(3, 1.0)
        self.asym_crossover = GeneralPointCrossover(4, 1.0)
Ejemplo n.º 3
0
class FourPointTest(unittest.TestCase):
    """Test 'simple' 4-point crossovers.
    """
    def setUp(self):
        self.alphabet = TestAlphabet()
        genome_1 = MutableSeq("11111", self.alphabet)
        self.org_1 = Organism(genome_1, test_fitness)

        genome_2 = MutableSeq("22222", self.alphabet)
        self.org_2 = Organism(genome_2, test_fitness)

        self.sym_crossover = GeneralPointCrossover(3, 1.0)
        self.asym_crossover = GeneralPointCrossover(4, 1.0)

    def test_basic_crossover(self):
        """Test basic 4-point crossover functionality.
        """
        start_genome_1 = self.org_1.genome[:]
        start_genome_2 = self.org_2.genome[:]

        new_org_1, new_org_2 = self.sym_crossover.do_crossover(self.org_1,
                                                               self.org_2)

        self.assertNotEqual(str(new_org_1.genome), str(start_genome_1),
                            "Did not perform a crossover when expected.")
        self.assertNotEqual(str(new_org_2.genome), str(start_genome_2),
                            "Did not perform a crossover when expected.")

        self.assertNotEqual(str(new_org_1), str(self.org_1),
                            "Returned an exact copy of the original organism.")
        self.assertNotEqual(str(new_org_2), str(self.org_2),
                            "Returned an exact copy of the original organism.")

    def test_prop_sym_crossover(self):
        """Test properties of symmetric 4-point crossover.
        """
        new_org_1, new_org_2 = self.sym_crossover.do_crossover(self.org_1,
                                                               self.org_2)

        self.assertEqual(len(new_org_1.genome), len(new_org_2.genome),
                         "Strings are of different sizes after symmetric crossover.")

        self.assertEqual(str(new_org_1.genome).count("1"),
                         str(new_org_2.genome).count("2"),
                         "There should be equal, inverse distributions")
        self.assertEqual(str(new_org_1.genome).count("2"),
                         str(new_org_2.genome).count("1"),
                         "There should be equal, inverse distributions")

    def test_basic_asym_crossover(self):
        """Test basic asymmetric 2-point crossover functionality.
        """
        start_genome_1 = self.org_1.genome[:]
        start_genome_2 = self.org_2.genome[:]

        new_org_1, new_org_2 = self.asym_crossover.do_crossover(self.org_1,
                                                                self.org_2)

        self.assertNotEqual(str(new_org_1.genome), str(start_genome_1),
                            "Did not perform a crossover when expected.")
        self.assertNotEqual(str(new_org_2.genome), str(start_genome_2),
                            "Did not perform a crossover when expected.")

        self.assertNotEqual(str(new_org_1), str(self.org_1),
                            "Returned an exact copy of the original organism.")
        self.assertNotEqual(str(new_org_2), str(self.org_2),
                            "Returned an exact copy of the original organism.")
class FourPointTest(unittest.TestCase):
    """Test 'simple' 4-point crossovers.
    """
    def setUp(self):
        self.alphabet = TestAlphabet()
        genome_1 = MutableSeq("11111", self.alphabet)
        self.org_1 = Organism(genome_1, test_fitness)

        genome_2 = MutableSeq("22222", self.alphabet)
        self.org_2 = Organism(genome_2, test_fitness)

        self.sym_crossover = GeneralPointCrossover(3, 1.0)
        self.asym_crossover = GeneralPointCrossover(4, 1.0)

    def test_basic_crossover(self):
        """Test basic 4-point crossover functionality.
        """
        start_genome_1 = self.org_1.genome[:]
        start_genome_2 = self.org_2.genome[:]

        new_org_1, new_org_2 = self.sym_crossover.do_crossover(
            self.org_1, self.org_2)

        self.assertNotEqual(str(new_org_1.genome), str(start_genome_1),
                            "Did not perform a crossover when expected.")
        self.assertNotEqual(str(new_org_2.genome), str(start_genome_2),
                            "Did not perform a crossover when expected.")

        self.assertNotEqual(
            str(new_org_1), str(self.org_1),
            "Returned an exact copy of the original organism.")
        self.assertNotEqual(
            str(new_org_2), str(self.org_2),
            "Returned an exact copy of the original organism.")

    def test_prop_sym_crossover(self):
        """Test properties of symmetric 4-point crossover.
        """
        new_org_1, new_org_2 = self.sym_crossover.do_crossover(
            self.org_1, self.org_2)

        self.assertEqual(
            len(new_org_1.genome), len(new_org_2.genome),
            "Strings are of different sizes after symmetric crossover.")

        self.assertEqual(
            str(new_org_1.genome).count("1"),
            str(new_org_2.genome).count("2"),
            "There should be equal, inverse distributions")
        self.assertEqual(
            str(new_org_1.genome).count("2"),
            str(new_org_2.genome).count("1"),
            "There should be equal, inverse distributions")

    def test_basic_asym_crossover(self):
        """Test basic asymmetric 2-point crossover functionality.
        """
        start_genome_1 = self.org_1.genome[:]
        start_genome_2 = self.org_2.genome[:]

        new_org_1, new_org_2 = self.asym_crossover.do_crossover(
            self.org_1, self.org_2)

        self.assertNotEqual(str(new_org_1.genome), str(start_genome_1),
                            "Did not perform a crossover when expected.")
        self.assertNotEqual(str(new_org_2.genome), str(start_genome_2),
                            "Did not perform a crossover when expected.")

        self.assertNotEqual(
            str(new_org_1), str(self.org_1),
            "Returned an exact copy of the original organism.")
        self.assertNotEqual(
            str(new_org_2), str(self.org_2),
            "Returned an exact copy of the original organism.")