From 9c8e1be3b5acf5637f2e0216a5d46e2d8a0b7fcb Mon Sep 17 00:00:00 2001 From: Sepehr Madani <ssepehrmadani@gmail.com> Date: Thu, 30 Jul 2020 13:31:15 -0400 Subject: [PATCH] Fix syntax errors --- algorithms/genetic_algorithm.py | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/algorithms/genetic_algorithm.py b/algorithms/genetic_algorithm.py index 35a3c23..5990427 100644 --- a/algorithms/genetic_algorithm.py +++ b/algorithms/genetic_algorithm.py @@ -44,16 +44,16 @@ class GeneticAlgorithm(BaseAlgorithm): self.bit_resolution = options.bit_resolution self.mutation_factor = options.mutation_factor self.overwrite_mutations = options.overwrite_mutations - + self.stop_criterion = options.stop_criterion # time, target, iter self.gen_to_repeat = options.gen_to_repeat self.time_limit = options.time_limit self.stop_after_score = options.stop_after_score - + self.generations = 0 self.chromosomes = [] - + self.buckets = None if options.use_buckets: self.bucket_count = options.bucket_count @@ -67,15 +67,15 @@ class GeneticAlgorithm(BaseAlgorithm): if self.buckets is not None: assert len(self.null_degrees) == 1 assert self.bucket_count & 1 == 0 - assert self.stop_criterion is in ["time", "target", "iter"] + assert self.stop_criterion in ["time", "target", "iter"] def solve(self): self.initialize_sample() self.organize_sample() solve_function = getattr(self, "solve_" + self.stop_criterion) solve_function() - return (self.make_weights(self.chromosomes[0]), self.chromosomes[0].get_score(), generations) - + return (self.make_weights(self.chromosomes[0]), self.chromosomes[0].get_score(), self.generations) + def solve_time(self): start_time = time_ns() while (time_ns() - start_time) // 10**6 <= self.time_limit: @@ -83,13 +83,13 @@ class GeneticAlgorithm(BaseAlgorithm): def solve_target(self): start_time = time_ns() - while (time_ns() - start_time) // 10**6 <= 10 * 1000 + while (time_ns() - start_time) // 10**6 <= 10 * 1000: self.step() def solve_iter(self): for generation in range(self.gen_to_repeat): self.step() - + def step(self): self.create_children() self.mutate_sample() @@ -98,7 +98,7 @@ class GeneticAlgorithm(BaseAlgorithm): def create_children(self): """Using the better half of the population, creates children overwriting the bottom half by doing crossovers. If use_buckets is True, uses AM-GM–based crossover. Otherwise, it uses the basic merger crossover.""" - + for child in range(self.sample_size // 2, self.sample_size - 1, 2): if self.buckets is None: p1, p2 = sample(range(self.sample_size // 2), 2) @@ -115,13 +115,13 @@ class GeneticAlgorithm(BaseAlgorithm): key=lambda x: abs(x.pattern + p1.pattern) ) if len(self.buckets[bucket_opp]) > 0 else choice(self.chromosomes) self.crossover_bucket(p1, p2, child, child + 1) - + self.generations += 1 def organize_sample(self): """Reorganizes the sample by updating pattern for all chromosomes and sorting them by their scores. Optionally, if use_buckets is True, allocates each chromosome to its respective bucket.""" - + # Update pattern for chromosome in self.chromosomes: if chromosome.needs_update: @@ -149,7 +149,7 @@ class GeneticAlgorithm(BaseAlgorithm): def mutate_sample(self): """Mutates the sample excluding the best chromosome. Overwrites the previous chromosomes if overwrite_mutations is True.""" - + if self.overwrite_mutations: # For all except the best chromosome for chromosome in self.chromosomes[1:]: @@ -171,16 +171,16 @@ class GeneticAlgorithm(BaseAlgorithm): def make_weights(self, chromosome): """Returns e^{iθ} value for a chromosome's θs""" - + weights = [] for bits in chromosome.gene: angle = (bits - (2 ** self.bit_count - 1) / 2) * 2 * pi / (2 ** self.bit_resolution) - weights.append(exp(1j * angle)) + weights.append(complex(cos(angle), sin(angle))) return weights def crossover(self, p1, p2, c1, c2): """Merges two parents' genes to create two children""" - + self.chromosomes[c1] = deepcopy(self.chromosomes[p1]) self.chromosomes[c2] = deepcopy(self.chromosomes[p2]) for i in range(self.N): @@ -192,7 +192,7 @@ class GeneticAlgorithm(BaseAlgorithm): def crossover_bucket(self, p1, p2, c1, c2): """Creates two children from parents' genes using the AM-GM approximation""" - + for ii in range(self.N): g1 = p1.gene[ii] g2 = p2.gene[ii] @@ -201,7 +201,8 @@ class GeneticAlgorithm(BaseAlgorithm): def initialize_sample(self): """Destroys all chromosomes and creates a new random population""" - + + self.generations = 0 self.chromosomes.clear() if self.overwrite_mutations: self.chromosomes = [ -- GitLab