evokit.evolvables package
Subpackages
- evokit.evolvables.lgp package
- Module contents
InstructionStructureTypeStructureScopeStructOverLinesStructUntilLabelStructNextLineLabelForWhileIfStateVectorTypecell()cells()OperationConditionRegisterStatesRegisterStates.__init__()RegisterStates.registersRegisterStates.constantsRegisterStates.verboseRegisterStates.set_registers()RegisterStates.set_constants()RegisterStates.get_cell_value()RegisterStates.get_state_vector()RegisterStates.run()RegisterStates.check_condition()RegisterStates.copy()
check_all()LGPFactoryLGPEvaluatorLinearGeneticProgramCrossover
- Module contents
- evokit.evolvables.otypes package
- evokit.evolvables.primitives package
Submodules
- class evokit.evolvables.algorithms.HomogeneousAlgorithm[source]
Bases:
Algorithm,ABC,Generic[T]An algorithm with one population. Exists primarily for typing purposes.
Algorithms that use one homogeneous population should derive this class.
- step()
- class evokit.evolvables.algorithms.SimpleLinearAlgorithm[source]
Bases:
HomogeneousAlgorithm[T]A very simple evolutionary algorithm.
An evolutionary algorithm that maintains one population and performs one round of selection in each step.
- Each step includes the following operations:
evaluate
populationevent:
POST_VARIATION- select from
population update
populationwith result
- select from
event:
POST_EVALUATION- vary
population update
populationwith result
- vary
event:
POST_SELECTION
- __init__(population: Population[T], evaluator: Evaluator[T], selector: Selector[T], variator: Variator[T]) None[source]
- events: list[str] = ['POST_VARIATION', 'POST_EVALUATION', 'POST_SELECTION']
Events that can be reported by this algorithm.
- class evokit.evolvables.algorithms.LinearAlgorithm[source]
Bases:
HomogeneousAlgorithm[T]A general evolutionary algorithm [SIMPLE_GA].
An evolutionary algorithm that maintains one population. Each step includes two rounds of selection.
- Each step includes the following operations:
evaluate
populationevent:
POST_PARENT_EVALUATION- select parents from
population update
populationwith result
- select parents from
event:
POST_PARENT_SELECTIONvary
populationevent:
POST_VARIATIONevaluate
populationevent:
POST_OFFSPRING_EVALUATION- select survivors from
population update
populationwith result
- select survivors from
event:
POST_OFFSPRING_SELECTION
[SIMPLE_GA]Introduction to Evolutionary Computing [2nd ed.], A. E. Eiben and J. E. Smith (2015), Fig 3.1
- __init__(population: Population[T], parent_evaluator: Evaluator[T], parent_selector: Selector[T], variator: Variator[T], survivor_evaluator: Evaluator[T], survivor_selector: Selector[T]) None[source]
- events: list[str] = ['POST_PARENT_EVALUATION', 'POST_PARENT_SELECTION', 'POST_VARIATION', 'POST_OFFSPRING_EVALUATION', 'POST_OFFSPRING_SELECTION']
Events that can be reported by this algorithm.
- class evokit.evolvables.algorithms.CanonicalGeneticAlgorithm[source]
Bases:
HomogeneousAlgorithm[T]The canonical genetic algorithm [CANON_GA].
An evolutionary algorithm that consecutively apply two variators. In Holland’s foundational algorithm, these are crossover followed by mutation.
- Each step includes the following operations:
vary
populationwithvariator1event:
POST_VARIATION_1vary
populationwithvariator2event:
POST_VARIATION_2evaluate
populationevent:
POST_EVALUATION- select survivors from
population update
populationwith result
- select survivors from
event:
POST_SELECTION
[CANON_GA]Adaptation in Natural and Artificial Systems, Holland (1975)
- __init__(population: Population[T], evaluator: Evaluator[T], selector: Selector[T], variator1: Variator[T], variator2: Variator[T]) None[source]
- events: list[str] = ['POST_VARIATION_1', 'POST_VARIATION_2', 'POST_EVALUATION', 'POST_SELECTION']
Events that can be reported by this algorithm.
- class evokit.evolvables.bitstring.BitString[source]
Bases:
Individual[int]A string of bits.
Tutorial: Getting Started with OneMax.
- __init__(value: int, size: int) None[source]
- Parameters:
value – Integer whose binary representation is used.
size – Length of the binary string
- static random(size: int) BitString[source]
Return a random binary string.
Each item in the returned value may be either 1 or 0 with equal probability.
- Parameters:
size – Size of the generated binary string.
- copy() Self[source]
Return a copy of this object.
Operations performed on the returned value do not affect this object.
- get(pos: int) Literal[1] | Literal[0][source]
Return the bit at position
pos.- Parameters:
pos – Position of the returned bit value.
- Raises:
IndexError – If
posis out of range.`
- set(pos: int) None[source]
Set the bit at position
posto 0.- Parameters:
pos – Position of the bit value to set.
- Effect:
Change
genome.
- Raises:
IndexError – If
posis out of range.`
- clear(pos: int) None[source]
Set the bit at position
posto 0.- Parameters:
pos – Position of the bit value to clear.
- Effect:
Change
genome.
- Raises:
IndexError – If
posis out of range.`
- flip(pos: int) None[source]
Flip the bit at position
pos.- Parameters:
pos – Position of the bit value to flip.
- Effect:
Change
genome.
- Raises:
IndexError – If
posis outside of range.
- classmethod from_bit_list(bit_list: list[int]) BitString[source]
Return a
BitStringwhosegenomeis the value of bit_list parsed as binary.- Parameters:
bit_list – A string of values
0or1.
Warning
For efficiency, this method does not check if each item in
bit_listis one of1and0.Effectively, even numbers will be treated as
1s whereas odd numbers will be treated as0s.
- class evokit.evolvables.bitstring.CountBits[source]
-
Count the number of
1s.Evaluator for
BitString. For each1in the binary string, incur a reward of 1.
- class evokit.evolvables.bitstring.MutateBits[source]
-
Randomly flip each bit in the parent.
1-to-1 variator for
BitString. At each bit in the parent, flip it with probabilitymutation_rate`.- ..note::
This operator can use Numpy (if installed) to speed up bit flips by orders of magnitude.
- class evokit.evolvables.bitstring.OnePointCrossover[source]
-
Split and recombine parents.
2-to-1 variator for
BitString. Split parents at position k, then interleave the segments.
- class evokit.evolvables.gp.Expression[source]
Bases:
Generic[T]A node in an expression tree.
Recursive data structure that implements program trees. An
Expressionis also aCallablethat only accepts arguments passed by position.The attribute
valueis the value of this node.- __init__(arity: int, value: T | Callable[..., T] | Symbol, children: list[Expression[T]], factory: ExpressionFactory[T] | None = None)[source]
- arity: int
Arity of the expression node.
- children
Children of the expression node.
- property factory: ExpressionFactory[T]
The
ExpressionFactory, if any, that built this object.The
ExpressionFactorymaintains hyperparameters that instruct the construction of this object.
- copy() Self[source]
Return a deep copy.
Call the
copy(self, ...)method onvalue, each item inchildren, andvalue(ifvalueimplements a method namedcopy). Use the results to create a newExpression
- nodes() tuple[Expression[T], ...][source]
Return a flat list view of all nodes and subnodes.
Note that operations performed on items in the returned list affect the original objects.
- class evokit.evolvables.gp.Symbol[source]
Bases:
objectDummy object used by
ExpressionFactory. This object represents a positional argument, as opposed to, for example, a constant value or an expression that evaluates to a value.- pos: int
- class evokit.evolvables.gp.ExpressionFactory[source]
Bases:
Generic[T]Factory class for
Expression.Build
Expressioninstances with supplied hyperparameters. Register the factory itself with each expression built by settingExpression.factory.Please see
evolvables.funcsfor a set of primitives, or define custom functions.Note
If
arity = 0, thenprimitivesmust include at least one literal. Otherwise, the tree cannot be built, as no terminal node can be drawn.- __init__(primitives: tuple[T | Callable[..., T], ...], arity: int)[source]
- Parameters:
primitives – instructions and terminals that occupy nodes of the expression tree. Listing a primitive more than once increases its chance of being selected.
arity – Arity of constructed
Expressioninstances.
- Raises:
ValueError if arity=0 and primitives does not contain –
nullary values. The tree cannot be built without terminals. –
- build(node_budget: int, layer_budget: int, nullary_ratio: float | None = None) Expression[T][source]
Build an expression tree to specifications.
The parameters
node_budgetandlayer_budgetare no constraints. Rather, if the tree exceeds these budgets, then only nullary values can be drawn. This ensures that the tree does not exceed these budgets by too much.Costs are incurred after a batch nodes are drawn.
- Parameters:
node_budget – Total number of nodes in the tree.
layer_budget – Depth of the tree.
nullary_ratio – Probability of drawing a nullary node.
- Raises:
ValueError` if nullary_ratio is not in range [0...1] –
- draw_primitive(nullary_ratio: float | None = None, free_draw: bool = False) T | Callable[..., T] | Symbol[source]
Return an item from
primitive_pool- Parameters:
nullary_ratio – Probability of drawing terminals. If set, non-terminals are drawn with probability (
1-nullary_ratio).free_draw – if
True, then the call does not affect or respect constraints on node counts. For example, it can still draw non-terminal nodes, even while exceeding node count and depth constraints.
- class evokit.evolvables.gp.Program[source]
Bases:
Individual[Expression[T]]A tree-based genetic program.
Tutorial: Symbolic Regression with Genetic Programming.
- __init__(expr: Expression[T])[source]
- class evokit.evolvables.gp.ProgramFactory[source]
Bases:
Generic[T]Convenience factory class for
Program.Contain an
ExpressionFactoryinstance. Delete storage of hyperparameters andProgramFactory.build()to the internalExpressionFactory.
- class evokit.evolvables.gp.CrossoverSubtree[source]
-
Crossover operator that randomly exchange subtrees of parents.
Select an internal node from each parent. Then, select one child of each internal node and exchange these child nodes. Doing so also exchanges subtrees that begin at these child nodes.
- class evokit.evolvables.gp.MutateNode[source]
-
Mutator that changes the primitive in a uniformly random node to a uniformly selected primitive of the same arity.
- class evokit.evolvables.gp.MutateSubtree[source]
-
Mutation operator that randomly mutates subtrees.
Uniformly select an internal node, then uniformly select a child of that node. Replace that child with a subtree, constructed by calling
ExpressionFactory.build()of the associatedExpressionFactoryfound inExpression.factory.
- class evokit.evolvables.gp.SymbolicEvaluator[source]
Bases:
Evaluator[Program[float]]Evaluator for symbolic regression.
Compare the output of a program tree against a given objective function, over a fixed set of points. Assign higher fitness to programs whose output is closer to that of the objective function.
- __init__(objective: Callable[..., float], support: tuple[tuple[float, ...], ...])[source]
- Parameters:
objective – Function to compare against.
support – Collection of points on which the program is compared against
objective.
- Raises:
TypeError – if the first item in
supportdoes not match the arity ofobjective.
- class evokit.evolvables.gp.PenaliseNodeCount[source]
Bases:
Evaluator[Program[float]]Evaluator that favours smaller program trees.
For each node in the given program tree, incur a penalty of
coefficient.
- evokit.evolvables.gp_visualiser.ident = 0
Global counter of the number of dispatched identifiers.
- evokit.evolvables.gp_visualiser.p2dot(gp: ~evokit.evolvables.gp.Program[~typing.Any], dispatcher: ~typing.Callable[[], str] = <function _dispatch_ident>) Digraph[source]
Visualise a tree-based genetic program.
Return a
graphviz.Digraphthat represents the given tree-based genetic program.- Parameters:
gp – Genetic program to visualise.
dispatcher –
Callablethat should return a unique identifier when called.
- evokit.evolvables.prefabs.make_onemax(pop_size: int, ind_size: int, mutate_p: float, variator_processes: int | ProcessPoolExecutor | None = None, evaluator_processes: int | ProcessPoolExecutor | None = None, max_parents=5) SimpleLinearAlgorithm[BitString][source]
Create a simple elitist onemax algorithm that tracks 5 generations of parents.
Useful for playing around with features.
- class evokit.evolvables.selectors.NullSelector[source]
Bases:
Selector[D]Selector that does nothing.
- select_population(from_population: Population[D]) Population[D][source]
Return all items in
from_populationin new population.
- class evokit.evolvables.selectors.TruncationSelector[source]
Bases:
Selector[D]Simple selector that select individuals with highest fitness.
- select_population(from_population: Population[D]) Population[D][source]
- class evokit.evolvables.selectors.TournamentSelector[source]
Bases:
Selector[D]Tournament selector:
From the population, select uniform sample of size
bracket_size.Iterate through the sample, stop when a selection is made. At index
i, select that item with probability(where
is
p). If no selection is made when reaching the end of the sample, select the last item.Repeat until
budgetitems are selected.
- bracket_size: int
Size of a tournament bracket.
- p: float
Selection probability.
- evokit.evolvables.selectors.Elitist(sel: Selector[D]) Selector[D][source]
Decorator that adds elitism to a selector.
Wrap sel.select_population, so that the selector becomes elitist.
An elitist selector retains (and updates) the highest-fitness individual encountered so far, and always deposits that individual to the selected pool.
- Parameters:
sel – A selector.