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

Refactor the max_time_limit on 'target'

parent f3e02080
No related branches found
No related tags found
No related merge requests found
...@@ -47,7 +47,7 @@ class Chromosome: ...@@ -47,7 +47,7 @@ class Chromosome:
class GeneticAlgorithm(BaseAlgorithm): class GeneticAlgorithm(BaseAlgorithm):
""" Finds nulls by running a genetic algorithm on all possible """ Finds nulls by running a genetic algorithm on all possible
discrete values. discrete values.
""" """
...@@ -69,6 +69,7 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -69,6 +69,7 @@ class GeneticAlgorithm(BaseAlgorithm):
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
self.max_time_limit = 20 * 1000
self.generations = 0 self.generations = 0
self.chromosomes = [] self.chromosomes = []
...@@ -95,7 +96,11 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -95,7 +96,11 @@ class GeneticAlgorithm(BaseAlgorithm):
self.organize_sample() self.organize_sample()
solve_function = getattr(self, "solve_" + self.stop_criterion) solve_function = getattr(self, "solve_" + self.stop_criterion)
solve_function() solve_function()
return (self.make_weights(self.chromosomes[0]), self.chromosomes[0].get_score(), self.generations) return (
self.make_weights(self.chromosomes[0]),
self.chromosomes[0].get_score(),
self.generations
)
def solve_time(self): def solve_time(self):
start_time = time_ns() start_time = time_ns()
...@@ -104,7 +109,7 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -104,7 +109,7 @@ class GeneticAlgorithm(BaseAlgorithm):
def solve_target(self): def solve_target(self):
start_time = time_ns() start_time = time_ns()
while (time_ns() - start_time) // 10**6 <= 10 * 1000: while (time_ns() - start_time) // 10**6 <= self.max_time_limit:
self.step() self.step()
def solve_iter(self): def solve_iter(self):
...@@ -115,6 +120,7 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -115,6 +120,7 @@ class GeneticAlgorithm(BaseAlgorithm):
self.create_children() self.create_children()
self.mutate_sample() self.mutate_sample()
self.organize_sample() self.organize_sample()
self.generations += 1
def create_children(self): def create_children(self):
"""Using the better half of the population, creates children overwriting the bottom half by doing crossovers. """Using the better half of the population, creates children overwriting the bottom half by doing crossovers.
...@@ -137,8 +143,6 @@ class GeneticAlgorithm(BaseAlgorithm): ...@@ -137,8 +143,6 @@ class GeneticAlgorithm(BaseAlgorithm):
) if len(self.buckets[bucket_opp]) > 0 else choice(self.chromosomes) ) if len(self.buckets[bucket_opp]) > 0 else choice(self.chromosomes)
self.crossover_bucket(p1, p2, child, child + 1) self.crossover_bucket(p1, p2, child, child + 1)
self.generations += 1
def organize_sample(self): def organize_sample(self):
"""Reorganizes the sample by updating pattern for all chromosomes and sorting them by their scores. """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.""" Optionally, if use_buckets is True, allocates each chromosome to its respective bucket."""
......
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