Skip to content

Commit

Permalink
Merge branch 'dev' into origin/main
Browse files Browse the repository at this point in the history
  • Loading branch information
nmichlo committed Sep 25, 2021
2 parents 585afe9 + 48014e0 commit fea9468
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
44 changes: 29 additions & 15 deletions ruck/_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

import re
import warnings
from typing import Generic
from typing import List
from typing import Optional
from typing import Tuple
from typing import TypeVar

import numpy as np
from typing import Union


# ========================================================================= #
# Members #
# ========================================================================= #
import numpy as np


class MemberIsNotEvaluatedError(Exception):
Expand All @@ -44,14 +47,15 @@ class MemberAlreadyEvaluatedError(Exception):


T = TypeVar('T')
Fitness = Union[float, Tuple[float, ...]]


_RE_WHITESPACE = re.compile(r'\s\s+')


class Member(Generic[T]):

def __init__(self, value: T, fitness: float = None):
def __init__(self, value: T, fitness: Fitness = None):
self._value = value
self._fitness = None
# set fitness
Expand All @@ -63,26 +67,36 @@ def value(self) -> T:
return self._value

@property
def fitness_unsafe(self) -> Optional[float]:
def fitness_unsafe(self) -> Optional[Fitness]:
return self._fitness

@fitness_unsafe.setter
def fitness_unsafe(self, fitness: Fitness):
if self.is_evaluated:
raise MemberAlreadyEvaluatedError('The member has already been evaluated, the fitness can only ever be set once. Create a new member instead!')
if fitness is None:
raise ValueError('cannot set the fitness value to None')
# set the value
self._fitness = fitness

@property
def fitness(self) -> float:
def fitness(self) -> Fitness:
if not self.is_evaluated:
raise MemberIsNotEvaluatedError('The member has not been evaluated, the fitness has not yet been set.')
return self._fitness

@fitness.setter
def fitness(self, fitness: float):
if self.is_evaluated:
raise MemberAlreadyEvaluatedError('The member has already been evaluated, the fitness can only ever be set once. Create a new member instead!')
if np.isnan(fitness):
raise ValueError('fitness values cannot be NaN, this is an error!')
self._fitness = float(fitness)

def set_fitness(self, fitness: float) -> 'Member[T]':
self.fitness = fitness
return self
def fitness(self, fitness: Fitness):
# check values
if isinstance(fitness, (float, int)):
pass
elif isinstance(fitness, tuple):
if not all(isinstance(f, (float, int)) for f in fitness):
warnings.warn('multivariate fitness value does not consist of floats, this is probably an error!')
else:
warnings.warn(f'fitness value is not a float or tuple of floats, this is probably an error! Got type: {type(fitness)}')
# set the value
self.fitness_unsafe = fitness

@property
def is_evaluated(self) -> bool:
Expand Down
8 changes: 7 additions & 1 deletion ruck/_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ class EaModule(Generic[T], HParamsMixin):
def get_stats_groups(self) -> Dict[str, StatsGroup[T, Any]]:
# default stats groups
return {
'fit': StatsGroup(lambda pop: [m.fitness for m in pop], min=np.min, max=np.max, mean=np.mean)
'fit': StatsGroup(
lambda pop: [m.fitness for m in pop],
min =lambda fitnesses: np.min(fitnesses, axis=0).tolist(),
max =lambda fitnesses: np.max(fitnesses, axis=0).tolist(),
mean=lambda fitnesses: np.mean(fitnesses, axis=0, dtype='float64').tolist(),
std =lambda fitnesses: np.std(fitnesses, axis=0, dtype='float64').tolist(),
)
}

def get_progress_stats(self) -> Sequence[str]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
author="Nathan Juraj Michlo",
author_email="[email protected]",

version="0.2.0",
version="0.2.1",
python_requires=">=3.6",
packages=setuptools.find_packages(),

Expand Down

0 comments on commit fea9468

Please sign in to comment.