Skip to content

Commit

Permalink
Merge pull request #157 from alcides/streaming_fitnesses
Browse files Browse the repository at this point in the history
Created an async version of evaluate
  • Loading branch information
alcides authored Mar 2, 2024
2 parents 2fd24be + fde4628 commit dbc60ec
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions geneticengine/algorithms/gp/operators/elitism.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def iterate(
generation: int,
) -> list[Individual]:
evaluator.evaluate(problem, population)
# TODO: We do not need to sort here.
new_population = sort_population(population, problem)
return new_population[:target_size]
1 change: 1 addition & 0 deletions geneticengine/algorithms/gp/operators/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ def iterate(
generation: int,
) -> list[Individual]:
evaluator.evaluate(problem, population)
# TODO: We do not need to sort here.
new_population = sort_population(population, problem)
return new_population
5 changes: 4 additions & 1 deletion geneticengine/evaluation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ def __init__(self):
self.count = 0

@abstractmethod
def evaluate_async(self, problem: Problem, indivs: list[Individual[Any, Any]]): ...

def evaluate(self, problem: Problem, indivs: list[Individual[Any, Any]]):
...
for _ in self.evaluate_async(problem, indivs):
pass

def register_evaluation(self):
self.count += 1
Expand Down
3 changes: 2 additions & 1 deletion geneticengine/evaluation/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def save_abc(pickler, obj):
class ParallelEvaluator(Evaluator):
"""Evaluates individuals in parallel, each time they are needed."""

def evaluate(self, problem: Problem, indivs: list[Individual[Any, Any]]):
def evaluate_async(self, problem: Problem, indivs: list[Individual[Any, Any]]):
def mapper(ind: Individual) -> Fitness:
return self.eval_single(problem, ind)

Expand All @@ -25,3 +25,4 @@ def mapper(ind: Individual) -> Fitness:
for i, f in zip(indivs, fitnesses):
i.set_fitness(problem, f)
self.register_evaluation()
yield i
5 changes: 3 additions & 2 deletions geneticengine/evaluation/sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
class SequentialEvaluator(Evaluator):
"""Default evaluator for individuals, executes sequentially."""

def evaluate(self, problem: Problem, indivs: list[Individual[Any, Any]]):
def evaluate_async(self, problem: Problem, indivs: list[Individual[Any, Any]]):
for individual in indivs:
if not individual.has_fitness(problem):
f = self.eval_single(problem, individual)
self.count += 1
self.register_evaluation()
individual.set_fitness(problem, f)
yield individual
6 changes: 2 additions & 4 deletions geneticengine/evaluation/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def __init__(

def evaluate(self, individuals: list[Individual]):
problem = self.problem
self.evaluator.evaluate(problem, individuals)
for ind in individuals:
for ind in self.evaluator.evaluate_async(problem, individuals):
is_best = False
if self.best_individual is None:
self.best_individual = ind
Expand Down Expand Up @@ -91,8 +90,7 @@ def is_dominated(self, current: Individual, others: list[Individual]):

def evaluate(self, individuals: list[Individual]):
problem = self.problem
self.evaluator.evaluate(problem, individuals)
for ind in individuals:
for ind in self.evaluator.evaluate_async(problem, individuals):
not_dominated = len(self.pareto_front) == 0 or not self.is_dominated(ind, self.pareto_front)
if not_dominated:
new_pareto_front = [ind]
Expand Down

0 comments on commit dbc60ec

Please sign in to comment.