Skip to content

Commit

Permalink
Create PopulationIsNotEvaluatedException and use is_evaluated property (
Browse files Browse the repository at this point in the history
#146)

* Create PopulationIsNotEvaluatedException and use is_evaluated property

* PEP8
  • Loading branch information
rogiervandergeer authored and koaning committed Aug 30, 2019
1 parent dffc2e6 commit c59da19
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions evol/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class PopulationIsNotEvaluatedException(RuntimeError):
pass


class StopEvolution(Exception):
Expand Down
3 changes: 2 additions & 1 deletion evol/helpers/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List

from evol import Individual
from evol.exceptions import PopulationIsNotEvaluatedException

"""
Below are functions that allocate individuals to the
Expand Down Expand Up @@ -66,4 +67,4 @@ def _ensure_evaluated(individuals: List[Individual]):
"""
for individual in individuals:
if individual.fitness is None:
raise RuntimeError(f'Individual {individual} has not been evaluated.')
raise PopulationIsNotEvaluatedException('Population must be evaluated.')
10 changes: 6 additions & 4 deletions evol/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import sys
import uuid

from evol.exceptions import PopulationIsNotEvaluatedException
from evol.population import BasePopulation


class BaseLogger:
"""
Expand Down Expand Up @@ -40,10 +43,9 @@ def __init__(self, target=None, stdout=False, fmt='%(asctime)s,%(message)s'):
self.logger.setLevel(level=logging.INFO)

@staticmethod
def check_population(population):
if any([i.fitness is None for i in population]):
# TODO: add a custom error type for PopulationNotEvaluated
raise RuntimeError("Population is not evaluated before logging!")
def check_population(population: BasePopulation) -> None:
if not population.is_evaluated:
raise PopulationIsNotEvaluatedException('Population must be evaluated when logging.')

def log(self, population, **kwargs):
"""
Expand Down

0 comments on commit c59da19

Please sign in to comment.