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)
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()
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 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)
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
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)
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)
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
], "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,
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
"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,