Skip to content
Snippets Groups Projects
Commit 115f82be authored by Sepehr Madani's avatar Sepehr Madani
Browse files

Refactor GeneticAlgorithm for readability

parent b8ca4403
No related branches found
No related tags found
No related merge requests found
...@@ -65,7 +65,7 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -65,7 +65,7 @@ class GeneticAlgorithm(BaseAlgorithm):
Chromosome.init_consts(self.N, self.bit_count, self.mutation_factor) Chromosome.init_consts(self.N, self.bit_count, self.mutation_factor)
self.stop_criterion = options.stop_criterion # time, target, iter self.stop_criterion = options.stop_criterion # time, target, iter
self.gen_to_repeat = options.gen_to_repeat self.gen_to_repeat = options.gen_to_repeat
self.time_limit = options.time_limit self.time_limit = options.time_limit
self.stop_after_score = options.stop_after_score self.stop_after_score = options.stop_after_score
...@@ -192,15 +192,15 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -192,15 +192,15 @@ class GeneticAlgorithm(BaseAlgorithm):
def make_weights(self, chromosome): def make_weights(self, chromosome):
"""Returns e^{iθ} value for a chromosome's θs""" """Returns e^{iθ} value for a chromosome's θs"""
angles = [(bits - (2**self.bit_count - 1) / 2) * 2*pi / (2**self.bit_resolution) for bits in chromosome.gene] angles = [(bits - (2**self.bit_count-1)/2) * (2*pi) / (2**self.bit_resolution) for bits in chromosome.gene]
weights = [complex(cos(theta), sin(theta)) for theta in angles] weights = [complex(cos(theta), sin(theta)) for theta in angles]
return weights return weights
def crossover(self, p1, p2, c1, c2): def crossover(self, p1, p2, c1, c2):
"""Merges two parents' genes to create two children""" """Merges two parents' genes to create two children"""
self.chromosomes[c1] = deepcopy(self.chromosomes[p1]) self.chromosomes[c1].gene = self.chromosomes[p1].gene.copy()
self.chromosomes[c2] = deepcopy(self.chromosomes[p2]) self.chromosomes[c2].gene = self.chromosomes[p2].gene.copy()
for i in range(self.N): for i in range(self.N):
if random() >= 0.5: if random() >= 0.5:
self.chromosomes[c1].gene[i], self.chromosomes[c2].gene[i] = ( self.chromosomes[c1].gene[i], self.chromosomes[c2].gene[i] = (
...@@ -212,10 +212,8 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -212,10 +212,8 @@ class GeneticAlgorithm(BaseAlgorithm):
"""Creates two children from parents' genes using the AM-GM approximation""" """Creates two children from parents' genes using the AM-GM approximation"""
for ii in range(self.N): for ii in range(self.N):
g1 = p1.gene[ii] self.chromosomes[c1].gene[ii] = (p1.gene[ii] + p2.gene[ii]) // 2
g2 = p2.gene[ii] self.chromosomes[c2].gene[ii] = (p1.gene[ii] + p2.gene[ii] + 1) // 2
self.chromosomes[c1].gene[ii] = (g1 + g2) // 2
self.chromosomes[c2].gene[ii] = (g1 + g2 + 1) // 2
def initialize_sample(self): def initialize_sample(self):
"""Destroys all chromosomes and creates a new random population""" """Destroys all chromosomes and creates a new random population"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment