Exemplo n.º 1
0
    def setUp(self):
        self.config = {
            "max_population": 10,

            "bitstring_generation": {
                "genome_length": 10
            },

            "codons": [
                "0000",
                "0001",
                "0010",
                "0011",
                "0100",
                "0101",
                "0110",
                "0111",
                "1000",
                "1001",
                "1011",
                "1111"
            ],

            "crossover": {
                "method": "ONE_POINT_CROSSOVER",
                "probability": 1.0
            }
        }
        generator = BitStringGenerator(self.config)
        self.bitstr_1 = generator.generate_random_bitstr()
        self.bitstr_2 = generator.generate_random_bitstr()
        self.crossover = BitStringCrossover(self.config)
Exemplo n.º 2
0
    def setUp(self):
        self.config = {
            "max_population": 10,

            "bitstring_generation": {
                "genome_length": 10
            },

            "codons": [
                "0000",
                "0001",
                "0010",
                "0011",
                "0100",
                "0101",
                "0110",
                "0111",
                "1000",
                "1001",
                "1011",
                "1111"
            ]
        }
        generator = BitStringGenerator(self.config)
        self.bitstr_1 = generator.generate_random_bitstr()
        self.bitstr_2 = generator.generate_random_bitstr()
Exemplo n.º 3
0
    def setUp(self):
        self.config = {
            "max_population": 10,


            "bitstring_generation": {
                "genome_length": 10
            },

            "codons": [
                "0000",
                "0001",
                "0010",
                "0011",
                "0100",
                "0101",
                "0110",
                "0111",
                "1000",
                "1001",
                "1011",
                "1111"
            ]
        }
        self.generator = BitStringGenerator(self.config)
Exemplo n.º 4
0
    def setUp(self):
        self.config = {
            "max_population": 10,

            "bitstring_generation": {
                "genome_length": 10
            },

            "codons": [
                "0000",
                "0001",
                "0010",
                "0011",
                "0100",
                "0101",
                "0110",
                "0111",
                "1000",
                "1001",
                "1011",
                "1111"
            ],

            "mutation": {
                "method": [
                    "POINT_MUTATION"
                ],
                "probability": 1.0
            }
        }
        generator = BitStringGenerator(self.config)
        self.bitstr = generator.generate_random_bitstr()
        self.mutation = BitStringMutation(self.config)
Exemplo n.º 5
0
    def __init__(self, config, **kwargs):
        self.config = config
        self.recorder = kwargs.get("recorder", None)
        self.generator = BitStringGenerator(self.config)

        # mutation stats
        self.method = None
        self.index = None
        self.mutation_probability = None
        self.random_probability = None
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None
Exemplo n.º 6
0
 def setUp(self):
     self.config = {
         "max_population":
         10,
         "bitstring_generation": {
             "genome_length": 10
         },
         "codons": [
             "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
             "1000", "1001", "1011", "1111"
         ]
     }
     generator = BitStringGenerator(self.config)
     self.bitstr_1 = generator.generate_random_bitstr()
     self.bitstr_2 = generator.generate_random_bitstr()
Exemplo n.º 7
0
class BitStrGeneratorTests(unittest.TestCase):
    def setUp(self):
        self.config = {
            "max_population": 10,


            "bitstring_generation": {
                "genome_length": 10
            },

            "codons": [
                "0000",
                "0001",
                "0010",
                "0011",
                "0100",
                "0101",
                "0110",
                "0111",
                "1000",
                "1001",
                "1011",
                "1111"
            ]
        }
        self.generator = BitStringGenerator(self.config)

    def test_generate_random_codon(self):
        codon = self.generator.generate_random_codon()

        self.assertTrue(len(codon) > 0)
        self.assertTrue(codon in self.config["codons"])

    def test_generate_random_bitstr(self):
        bitstr = self.generator.generate_random_bitstr()

        self.assertTrue(len(bitstr.genome) > 0)
        self.assertEquals(bitstr.length, 10)
        self.assertEquals(bitstr.score, None)

    def test_init(self):
        population = self.generator.init()

        self.assertEquals(len(population.individuals), 10)
Exemplo n.º 8
0
 def setUp(self):
     self.config = {
         "max_population":
         10,
         "bitstring_generation": {
             "genome_length": 10
         },
         "codons": [
             "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
             "1000", "1001", "1011", "1111"
         ],
         "mutation": {
             "method": ["POINT_MUTATION"],
             "probability": 1.0
         }
     }
     generator = BitStringGenerator(self.config)
     self.bitstr = generator.generate_random_bitstr()
     self.mutation = BitStringMutation(self.config)
Exemplo n.º 9
0
 def setUp(self):
     self.config = {
         "max_population":
         10,
         "bitstring_generation": {
             "genome_length": 10
         },
         "codons": [
             "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
             "1000", "1001", "1011", "1111"
         ],
         "crossover": {
             "method": "ONE_POINT_CROSSOVER",
             "probability": 1.0
         }
     }
     generator = BitStringGenerator(self.config)
     self.bitstr_1 = generator.generate_random_bitstr()
     self.bitstr_2 = generator.generate_random_bitstr()
     self.crossover = BitStringCrossover(self.config)
Exemplo n.º 10
0
    def __init__(self, config, **kwargs):
        self.config = config
        self.recorder = kwargs.get("recorder", None)
        self.generator = BitStringGenerator(self.config)

        # mutation stats
        self.method = None
        self.index = None
        self.mutation_probability = None
        self.random_probability = None
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None
Exemplo n.º 11
0
class BitStringMutation(object):
    def __init__(self, config, **kwargs):
        self.config = config
        self.recorder = kwargs.get("recorder", None)
        self.generator = BitStringGenerator(self.config)

        # mutation stats
        self.method = None
        self.index = None
        self.mutation_probability = None
        self.random_probability = None
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None

    def point_mutation(self, bitstr, index=None):
        # mutate node
        if index is None:
            index = randint(0, len(bitstr.genome) - 1)
            self.index = index
        else:
            self.index = index

        new_codon = self.generator.generate_random_codon()
        bitstr.genome[index] = new_codon
        self.mutated = True

    def mutate(self, bitstr):
        self.method = sample(self.config["mutation"]["method"], 1)[0]
        self.mutation_probability = self.config["mutation"]["probability"]
        self.random_probability = random()
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None

        # record before mutation
        self.before_mutation = "".join(bitstr.genome)

        # mutate
        if self.mutation_probability >= self.random_probability:
            if self.method == "POINT_MUTATION":
                self.point_mutation(bitstr)
            else:
                raise RuntimeError("Undefined mutation method [{0}]".format(
                    self.method))

        # record after mutation
        self.after_mutation = "".join(bitstr.genome)

        # record
        if self.recorder is not None:
            self.recorder.record(RecordType.MUTATION, self)

    def to_dict(self):
        self_dict = {
            "method": self.method,
            "mutation_probability": self.mutation_probability,
            "random_probability": self.random_probability,
            "mutated": self.mutated,
            "before_mutation": self.before_mutation,
            "after_mutation": self.after_mutation
        }

        return self_dict
Exemplo n.º 12
0
            ],
            "selection": {
                "method": "TOURNAMENT_SELECTION",
                "tournament_size": 2
            },
            "crossover": {
                "method": "ONE_POINT_CROSSOVER",
                "probability": 0.8
            },
            "mutation": {
                "method": ["POINT_MUTATION"],
                "probability": 0.2
            }
        }

        generator = BitStringGenerator(config)

        # genetic operators
        selection = Selection(config)
        crossover = BitStringCrossover(config)
        mutation = BitStringMutation(config)

        # run GA
        population = generator.init()

        details = play.play_details(
            population=population,
            evaluate=evaluate,
            functions=None,
            selection=selection,
            crossover=crossover,
Exemplo n.º 13
0
class BitStringMutation(object):
    def __init__(self, config, **kwargs):
        self.config = config
        self.recorder = kwargs.get("recorder", None)
        self.generator = BitStringGenerator(self.config)

        # mutation stats
        self.method = None
        self.index = None
        self.mutation_probability = None
        self.random_probability = None
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None

    def point_mutation(self, bitstr, index=None):
        # mutate node
        if index is None:
            index = randint(0, len(bitstr.genome) - 1)
            self.index = index
        else:
            self.index = index

        new_codon = self.generator.generate_random_codon()
        bitstr.genome[index] = new_codon
        self.mutated = True

    def mutate(self, bitstr):
        self.method = sample(self.config["mutation"]["method"], 1)[0]
        self.mutation_probability = self.config["mutation"]["probability"]
        self.random_probability = random()
        self.mutated = False
        self.before_mutation = None
        self.after_mutation = None

        # record before mutation
        self.before_mutation = "".join(bitstr.genome)

        # mutate
        if self.mutation_probability >= self.random_probability:
            if self.method == "POINT_MUTATION":
                self.point_mutation(bitstr)
            else:
                raise RuntimeError(
                    "Undefined mutation method [{0}]".format(self.method)
                )

        # record after mutation
        self.after_mutation = "".join(bitstr.genome)

        # record
        if self.recorder is not None:
            self.recorder.record(RecordType.MUTATION, self)

    def to_dict(self):
        self_dict = {
            "method": self.method,
            "mutation_probability": self.mutation_probability,
            "random_probability": self.random_probability,
            "mutated": self.mutated,
            "before_mutation": self.before_mutation,
            "after_mutation": self.after_mutation
        }

        return self_dict
Exemplo n.º 14
0
                "method": "TOURNAMENT_SELECTION",
                "tournament_size": 2
            },

            "crossover": {
                "method": "ONE_POINT_CROSSOVER",
                "probability": 0.8
            },

            "mutation": {
                "method": ["POINT_MUTATION"],
                "probability": 0.2
            }
        }

        generator = BitStringGenerator(config)

        # genetic operators
        selection = Selection(config)
        crossover = BitStringCrossover(config)
        mutation = BitStringMutation(config)

        # run GA
        population = generator.init()

        details = play.play_details(
            population=population,
            evaluate=evaluate,
            functions=None,
            selection=selection,
            crossover=crossover,