evokit.core package
Subpackages
Submodules
evokit.core.algorithm module
- class evokit.core.algorithm.Algorithm[source]
Bases:
ABCBase class for all evolutionary algorithms.
Derive this class to create custom algorithms.
Tutorial: Making a Custom Algorithm.
- static __new__(cls: Type[Self], *_: Any, **__: Any) Self[source]
Machinery.
Implement managed attributes.
- abstract __init__(*args: Any, **kwargs: Any) None[source]
Subclasses should override this method.
Initialise the state of an algorithm, including operators, the initial population(s), truncation strategy, and other parameters associated with the learning process as a whole.
- events: list[str] = []
- automatic_events: list[str] = ['STEP_BEGIN', 'STEP_END']
- abstract step(*args: Any, **kwargs: Any) None[source]
Advance the population by one generation.
Subclasses should override this method. Use operators to update the population (or populations). Call
update()to fire events for data collection mechanisms such asaccountant.Accountant.Note
The
generationattribute increments by 1 _after_step()is called. Do not manually incrementgeneration. This property is automatically managed.Calling
step()automatically fires two events viaupdate(): “STEP_BEGIN” before and “STEP_END” after. This behaviour cannot be suppressed. For more on events, seeaccountant.Accountant. Be advised that these automatic events are fired just like any other event – nothing prevents you from firing them insidestep(). Theautomatic_eventsclass attribute reports these events, like howeventsreports regular events.
- register(accountant: Accountant[Any, Any]) None[source]
Attach an
Accountantto this algorithm.- Parameters:
accountant – The accountant to attach.
- update(event: str) None[source]
Report an event to all attached
Accountantobjects.If the event is not in
events, raise an exception.- Parameters:
event – The event to report.
- Raises:
ValueError – if an reported event is not declared in
eventsand is not an automatically reported event inautomatic_events.
evokit.core.evaluator module
- class evokit.core.evaluator.Evaluator[source]
Bases:
ABC,Generic[D]Base class for all evaluators.
Derive this class to create custom evaluators.
Tutorial: Getting Started with OneMax.
- static __new__(cls, *args: Any, **kwargs: Any) Self[source]
Machinery.
Implement managed attributes.
- __init__(*args, processes: int | ProcessPoolExecutor | None = None, share_self: bool = False, **kwargs) None[source]
See
Variatorfor parametersprocessesandshare_self.
- retain_fitness: bool
If this evaluator should re-evaluate an
Individualwhosefitnessis already set.
- abstract evaluate(individual: D, *args: Any, **kwargs: Any) tuple[float, ...][source]
Evaluation strategy. Return the fitness of an individual.
Subclasses should override this method.
Note
“Better” individuals should have higher fitness.
Selectorshould prefer individuals with higher fitness.- Parameters:
individual – individual to evaluate
- evaluate_population(pop: Population[D], *args: Any, **kwargs: Any) None[source]
Context of
evaluate().Iterate individuals in a population. For each individual, compute its fitness with
evaluate(), then assign that value to itsIndividual.fitness.A subclass may override this method to implement behaviours that require access to the entire population.
- Effect:
For each item in
pop, set itsIndividual.fitness.
Note
This method must never return a value. It must assign to
fitnessfor eachIndividualin thePopulation.
evokit.core.population module
- class evokit.core.population.Individual[source]
Bases:
ABC,Generic[R]Base class for all individuals.
Derive this class to create custom representations.
The individual stores the encoding (
genome) and fitness (fitness) of a representation.- The individual can information outside of the genotype, such as a
.fitness, a reference to the parent, and strategy parameter(s).
Note
Implementation should store the genotype in
genome.Tutorial: Getting Started with OneMax.
- static __new__(cls: Type[Self], *args: Any, **kwargs: Any) Self[source]
Machinery.
Implement managed attributes.
- genome: R
Genotype of the individual.
- property fitness: tuple[float, ...]
Fitness of an individual.
Writing to this property changes the fitness of the individual. If this individual has yet to be assigned a fitness, reading from this property raises an exception.
To determine if the individual has a fitness, call
has_fitness().- Returns:
Fitness of the individual
Warning
If the current fitness is
None, return(nan,). This may happen when, for example, an offspring has just been produced.
- reset_fitness() None[source]
Reset the fitness of the individual.
- Effect:
The
fitnessof this individual becomesNone.
- abstract copy() Self[source]
Return an identical copy of the individual.
Subclasses should override this method.
Operations on in this individual should not affect the new individual. In addition to duplicating
genome, the implementation should decide whether to retain other fields such asfitness.Note
Ensure that changes made to the returned value do not affect the original value.
- class evokit.core.population.Population[source]
Bases:
UserList[D],Generic[D]A flat collection of individuals.
- __init__(initlist: Sequence[D] | None | Iterable[D] = None)[source]
- Parameters:
args – Initial items in the population
- copy() Self[source]
Return an independent population.
Changes made to items in the new population should not affect items in this population. This behaviour depends on correct implementation of
Individual.copy()in each item.Call
Individual.copy()for eachIndividualin this population. Collect the results, then create a new population with these values.
evokit.core.selector module
- class evokit.core.selector.Selector[source]
Bases:
ABC,Generic[D]Base class for all selectors.
Derive this class to create custom selectors.
Tutorial: Making a Custom Selector.
- __init__(budget: int, *args: Any, **kwargs: Any)[source]
- Parameters:
budget – Number of individuals to select.
Note
Implementations that select a variable number of individuals may ignore
budget.
- budget
Declared size of the output population
- select_population(from_population: Population[D], *args: Any, **kwargs: Any) Population[D][source]
Select from a population to a population.
The default implementation calls
select()onfrom_populationand collects the results.All subclasses should override either this method or
select(). Consider overriding this method if selection requires information about the whole population. Example: fitness proportionate selection.- Parameters:
from_population – population to select from.
- select(from_pool: Sequence[D], *args: Any, **kwargs: Any) tuple[D, ...][source]
Select individuals from a sequence of individuals.
All subclasses should override either this method or
select_population().- Parameters:
from_pool – tuple of individuals to select from.
Note
Each item in the returned tuple must be in
from_pool.The selector should treat higher fitness as “better”.
Evaluatorshould assign higher fitness to“better” individuals.
- Raises:
NotImplementedError – If the subclass does not override this method.
evokit.core.variator module
- class evokit.core.variator.Variator[source]
Bases:
ABC,Generic[D]Base class for all selectors.
Derive this class to create custom selectors.
Tutorial: Getting Started with OneMax.
- __init__(*args: Any, processes: int | ProcessPoolExecutor | None = None, share_self: bool = False, **kwargs: Any) None[source]
See
Variatorfor parametersprocessesandshare_self.
- processes
If attributes of this object will be shared when multiprocessing. See
__init__().
- abstract vary(parents: Sequence[D], *args: Any, **kwargs: Any) tuple[D, ...][source]
Apply the variator to a tuple of parents
Produce a tuple of individuals from a sequence of individuals.
The length of
.parentsis at mostarity.
- vary_population(population: Population[D], *args: Any, **kwargs: Any) Population[D][source]
Vary the population.
The default implementation separates
populationinto groups of size .arity, call .vary with each group as argument, then collect and returns the result.- Parameters:
population – Population to vary.
Note
The default implementation calls
Individual.reset_fitness()on each offspring to clear its fitness. Any implementation that overrides this method should do the same.
Module contents
Export modules from core.