-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from jakobj/maint/ordered-primitives
Replace dictionary in Primitives with tuple and turn Primitives into frozen dataclass
- Loading branch information
Showing
13 changed files
with
127 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,76 @@ | ||
from dataclasses import dataclass, field | ||
import numpy as np | ||
|
||
from typing import Iterator, List, Tuple, Type | ||
from typing import Iterator, Tuple, Type | ||
|
||
from .node import Node | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Primitives: | ||
"""Class collecting primitives of the Cartesian Genetic Programming framework. | ||
""" | ||
|
||
_max_arity = 0 | ||
_primitives: dict = {} | ||
|
||
def __init__(self, primitives: List[Type[Node]]) -> None: | ||
"""Init function. | ||
"""Convenience class to manage primitives, i.e., Node classes. | ||
Parameters | ||
---------- | ||
primitives : List[Type[Node]] | ||
List of primitives. | ||
""" | ||
for i in range(len(primitives)): | ||
if not isinstance(primitives[i], type): | ||
raise TypeError(f"expected class but received {type(primitives[i])}") | ||
if not issubclass(primitives[i], Node): | ||
raise TypeError(f"expected subclass of Node but received {primitives[i].__name__}") | ||
""" | ||
|
||
self._primitives = {} | ||
for i in range(len(primitives)): | ||
self._primitives[i] = primitives[i] | ||
_primitives: Tuple[Type[Node], ...] | ||
_max_arity: int = field(init=False) | ||
|
||
# hide primitives dict behind MappingProxyType to make sure it | ||
# is not changed after construction | ||
# unfortunately not supported by pickle, necessary for | ||
# multiprocessing; another way to implement this? | ||
# self._primitives = types.MappingProxyType(self._primitives) | ||
def __post_init__(self): | ||
self._check_types() | ||
self.__dict__[ | ||
"_max_arity" | ||
] = self._determine_max_arity() # avoid using use __setattr_ since dataclass is frozen | ||
|
||
self._determine_max_arity() | ||
def _check_types(self): | ||
if not isinstance(self._primitives, tuple): | ||
raise TypeError(f"expected tuple but received {type(self._primitives)}") | ||
|
||
def __iter__(self) -> Iterator: | ||
return iter([self[i] for i in range(len(self._primitives))]) | ||
for i in range(len(self._primitives)): | ||
if not isinstance(self._primitives[i], type): | ||
raise TypeError( | ||
f"expected class but received instance of {type(self._primitives[i])}" | ||
) | ||
if not issubclass(self._primitives[i], Node): | ||
raise TypeError( | ||
f"expected subclass of Node but received class {self._primitives[i].__name__}" | ||
) | ||
|
||
def _determine_max_arity(self) -> None: | ||
def _determine_max_arity(self) -> int: | ||
|
||
arity = 1 # minimal possible arity (output nodes need one input) | ||
|
||
for idx, p in self._primitives.items(): | ||
for p in self._primitives: | ||
if arity < p._arity: | ||
arity = p._arity | ||
|
||
self._max_arity = arity | ||
return arity | ||
|
||
def __iter__(self) -> Iterator[Type[Node]]: | ||
return iter(self._primitives) | ||
|
||
def sample(self, rng: np.random.RandomState) -> int: | ||
"""Sample a random primitive. | ||
def sample_allele(self, rng: np.random.RandomState) -> int: | ||
"""Sample a random primitive index. | ||
Parameters | ||
---------- | ||
rng : numpy.RandomState | ||
Random number generator instance to use for crossover. | ||
Random number generator instance. | ||
Returns | ||
------- | ||
int | ||
Index of the sample primitive | ||
Index of the sampled primitive. | ||
""" | ||
return rng.choice(self.alleles) | ||
return rng.randint(len(self._primitives)) | ||
|
||
def __getitem__(self, key: int) -> Type[Node]: | ||
if key < 0 or key >= len(self._primitives): | ||
raise IndexError("primitive index out of bounds") | ||
return self._primitives[key] | ||
|
||
@property | ||
def max_arity(self) -> int: | ||
return self._max_arity | ||
|
||
@property | ||
def alleles(self) -> Tuple: | ||
return tuple(self._primitives.keys()) | ||
|
||
def __len__(self): | ||
return len(self._primitives) | ||
def is_valid_allele(self, allele: int) -> bool: | ||
return (allele >= 0) and (allele < len(self._primitives)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.