Beispiel #1
0
    def test_cross_operator(self):
        first = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen()
        ])

        second = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen(),
            DenseGen(),
            ActivationGen(),
            DenseGen(),
            ActivationGen()
        ])

        first_child, second_child = first.cross(second)

        self.assertEqual(11, len(first_child))
        self.assertEqual(5, len(second_child))

        self.assertTrue(
            first_child.contains_type(GenType.Flatten),
            "Chromosomes should contain Flatten gen.")

        self.assertListEqual(
            map(lambda gen: gen.type, first_child),
            [
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Flatten,
                GenType.Dense,
                GenType.Activation,
                GenType.Dense,
                GenType.Activation,
                GenType.Dense,
                GenType.Activation
            ])

        self.assertListEqual(
            map(lambda gen: gen.type, second_child),
            [
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Flatten,
                GenType.Dense,
                GenType.Activation
            ])
Beispiel #2
0
    def test_mutation_operator(self):
        origin = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen()
        ])

        mutated = origin.mutate()

        self.assertNotEqual(origin, mutated)
Beispiel #3
0
    def test_check_to_be_mutated_case(self):
        chromosome = Chromosome([
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen()
        ])

        to_be_mutated = MutationStrategy1d().check(chromosome)

        self.assertTrue(to_be_mutated)
Beispiel #4
0
    def evaluate(self, chromosome):
        target_gens = list(chromosome.gens)

        start_index = 1
        end_index = lambda: chromosome.index_of(GenType.Flatten)

        if (end_index() - start_index) <= 3:
            if target_gens[end_index()].type == GenType.Activation:
                target_gens.insert(end_index(), Convolution2DGen())
                target_gens.insert(end_index(), ActivationGen())
            else:
                target_gens.insert(end_index(), ActivationGen())
                target_gens.insert(end_index(), Convolution2DGen())
        elif (end_index() - start_index) <= 5:
            target_gens.pop(end_index())

        return Chromosome(target_gens)
Beispiel #5
0
    def evaluate(self, chromosome):
        target_gens = list(chromosome.gens)

        start_index = chromosome.index_of(GenType.Flatten) + 1
        end_index = lambda: len(target_gens) - 2

        if (end_index() - start_index) <= 3:
            print("Index1: {0}".format(end_index()))
            if target_gens[end_index()].type == GenType.Activation:
                target_gens.insert(end_index(), ActivationGen())
                target_gens.insert(end_index(), DenseGen())
            else:
                target_gens.insert(end_index(), DenseGen())
                target_gens.insert(end_index(), ActivationGen())
        elif (end_index() - start_index) <= 5:
            print("Index2: {0}".format(end_index()))
            target_gens.pop(end_index())

        return Chromosome(target_gens)
Beispiel #6
0
    def test_mutation_by_insertion(self):
        chromosome = Chromosome([
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen()
        ])

        mutated = MutationStrategy1d(threshold=2).evaluate(chromosome)

        self.assertListEqual(
            map(lambda g: g.type, mutated),
            [
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Flatten,
                GenType.Dense,
                GenType.Activation,
                GenType.Dense
            ])
Beispiel #7
0
 def setUp(self):
     self.chromosome = Chromosome()
Beispiel #8
0
class ChromosomeTest(unittest.TestCase):

    def setUp(self):
        self.chromosome = Chromosome()

    def test_immutability(self):
        new_chromosome = self.chromosome.attach(Convolution2DGen())
        self.assertNotEqual(new_chromosome, self.chromosome)

    def test_chromosome_length(self):
        new_chromosome = self.chromosome\
            .attach(Convolution2DGen())\
            .attach(ActivationGen())\
            .attach(Convolution2DGen())

        self.assertEqual(3, len(new_chromosome))

    def test_mutation_operator(self):
        origin = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen()
        ])

        mutated = origin.mutate()

        self.assertNotEqual(origin, mutated)

    def test_cross_operator(self):
        first = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen()
        ])

        second = Chromosome(initial_gens=[
            Convolution2DGen(),
            ActivationGen(),
            FlattenGen(),
            DenseGen(),
            ActivationGen(),
            DenseGen(),
            ActivationGen(),
            DenseGen(),
            ActivationGen()
        ])

        first_child, second_child = first.cross(second)

        self.assertEqual(11, len(first_child))
        self.assertEqual(5, len(second_child))

        self.assertTrue(
            first_child.contains_type(GenType.Flatten),
            "Chromosomes should contain Flatten gen.")

        self.assertListEqual(
            map(lambda gen: gen.type, first_child),
            [
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Flatten,
                GenType.Dense,
                GenType.Activation,
                GenType.Dense,
                GenType.Activation,
                GenType.Dense,
                GenType.Activation
            ])

        self.assertListEqual(
            map(lambda gen: gen.type, second_child),
            [
                GenType.Convolution2d,
                GenType.Activation,
                GenType.Flatten,
                GenType.Dense,
                GenType.Activation
            ])