evokit.evolvables package

Submodules

evokit.evolvables.binstring module

class evokit.evolvables.binstring.BinaryString[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) BinaryString[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 pos is out of range.`

set(pos: int) None[source]

Set the bit at position pos to 0.

Parameters:

pos – Position of the bit value to set.

Effect:

Change genome.

Raises:

IndexError – If pos is out of range.`

clear(pos: int) None[source]

Set the bit at position pos to 0.

Parameters:

pos – Position of the bit value to clear.

Effect:

Change genome.

Raises:

IndexError – If pos is 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 pos is outside of range.

class evokit.evolvables.binstring.CountBits[source]

Bases: Evaluator[BinaryString]

Count the number of 1 s.

Evaluator for BinaryString. For each 1 in the binary string, incur a reward of 1.

evaluate(s1: BinaryString) tuple[float][source]
class evokit.evolvables.binstring.MutateBits[source]

Bases: Variator[BinaryString]

Randomly flip each bit in the parent.

1-to-1 variator for BinaryString. At each bit in the parent, flip it with probability mutation_rate`.

__init__(mutation_rate: float)[source]
Parameters:

mutation_rate – Probability to flip each bit in the parent.

Raises:

ValueError – If mutation_rate is not in range [0,1].

vary(parents: Sequence[BinaryString]) Tuple[BinaryString, ...][source]
evokit.evolvables.binstring.trial_run() None[source]

evokit.evolvables.funcs module

evokit.evolvables.funcs.sin(x: float) float[source]
evokit.evolvables.funcs.cos(x: float) float[source]
evokit.evolvables.funcs.tan(x: float) float[source]
evokit.evolvables.funcs.add(x: float, y: float) float[source]
evokit.evolvables.funcs.sub(x: float, y: float) float[source]
evokit.evolvables.funcs.mul(x: float, y: float) float[source]
evokit.evolvables.funcs.div(x: float, y: float) float[source]
evokit.evolvables.funcs.avg(x: float, y: float) float[source]
evokit.evolvables.funcs.lim(x: float, max_val: float, min_val: float) float[source]

evokit.evolvables.gp module

class evokit.evolvables.gp.Expression[source]

Bases: Generic[T]

A node in an expression tree.

Recursive data structure that implements program trees. An Expression is also a Callable that only accepts arguments passed by position.

The attribute value

__init__(arity: int, value: T | Callable[..., T] | Symbol, children: List[Expression[T]], factory: ExpressionFactory | None = None)[source]
property factory: ExpressionFactory[T]

The ExpressionFactory, if any, that built this object.

The ExpressionFactory maintains hyperparameters that instruct the construction of this object.

__call__(*args: T) T[source]

Evaluate the expression tree with arguments.

Recursively evaluate expression nodes in children. Then, apply value to the results, in the same order as the children they are resolved from.

Parameters:

*args – Arguments to the program.

copy() Self[source]

Return a deep copy.

Call the python:copy(self, …): method on value, each item in children, and value (if value implements a method named copy). Use the results to create a new Expression

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: object

Dummy object used by ExpressionFactory.

See:

??? TODO

__init__(pos: int)[source]
pos: int
class evokit.evolvables.gp.ExpressionFactory[source]

Bases: Generic[T]

Factory class for Expression.

Build Expression instances with supplied hyperparameters. Register the factory itself with each expression built by setting Expression.factory.

Please see evolvables.funcs for a set of primitives, or define custom functions.

Note

If arity = 0, then primitives must include at least one literal. Otherwise, the tree cannot be built, as no terminal node can be drawn.

See:

Expression.factory

__init__(primitives: Tuple[T | Callable[..., T], ...], arity: int)[source]
Parameters:
  • primitives – instructions and terminals that occupy nodes of the

  • its (expression tree. Listing a primitive more than once increases)

  • selected. (chance of being)

  • arity – arity of constructed Expression instances.

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[source]

Build an expression tree to specifications.

The parameters node_budget and layer_budget are not 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 lies outside of 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

  • probability (are drawn with)

  • free_draw – if True, then the call does not affect or respect

  • example (constraints on node counts. For)

  • non-terminal (it can still draw)

  • nodes

  • constraints. (even while exceeding node count and depth)

primitive_by_arity(arity: int) T | Callable[..., T] | Symbol[source]

Draw a instruction or terminal of the given arity.

If no primitive of the given arity exists, return an empty list.

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]
copy() Self[source]
class evokit.evolvables.gp.ProgramFactory[source]

Bases: Generic[T]

Convenience factory class for Program.

Contain an :class`ExpressionFactory` instance. Delete storage of hyperparameters and ProgramFactory.build() to the internal :class`ExpressionFactory`.

__init__(primitives: Tuple[T | Callable[..., T], ...], arity: int)[source]
build(node_budget: int, layer_budget: int, nullary_ratio: float | None = None) Program[source]
class evokit.evolvables.gp.CrossoverSubtree[source]

Bases: Variator[Program[float]]

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.

__init__(shuffle: bool = False)[source]
Parameters:
  • shuffle – If True: collect all child nodes of both

  • list (shuffle that)

  • list

  • assign (then)

  • parents. (items back to respective)

vary(parents: Sequence[Program[float]]) Tuple[Program[float], ...][source]
class evokit.evolvables.gp.MutateNode[source]

Bases: Variator[Program]

Mutator that changes the primitive in a uniformly random node to a uniformly selected primitive of the same arity.

__init__() None[source]
vary(parents: Sequence[Program]) Tuple[Program, ...][source]
Parameters:

parents – collection where the 0th item is the parent.

:raises ValueError` if the parent’s Program.genome: :raises does not have :attr:`Expression.factory set.:

class evokit.evolvables.gp.MutateSubtree[source]

Bases: Variator[Program]

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 associated ExpressionFactory found in Expression.factory.

__init__(node_budget: int, layer_budget: int, nullary_ratio: float | None = None) None[source]
vary(parents: Sequence[Program]) Tuple[Program, ...][source]
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 that

  • support – collection of points on which the program

  • objective. (is compared against)

Raises:
  • TypeError if the first item in support does not

  • match the arity of objective.

evaluate(program: Program[float]) tuple[float][source]
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.

__init__(coefficient: float)[source]
Parameters:
  • coefficient – penalty coefficient for each node in the

  • tree. (program)

evaluate(program: Program[float]) tuple[float][source]

evokit.evolvables.gp_visualiser module

evokit.evolvables.gp_visualiser.ident = 0

Global counter of the number of dispatched identifiers.

evokit.evolvables.gp_visualiser.p2dot(gp: ~evokit.evolvables.gp.Program, dispatcher: ~typing.Callable[[], str] = <function _dispatch_ident>) Digraph[source]

Visualise a tree-based genetic program.

Return a graphviz.Digraph that represents the given tree-based genetic program.

Parameters:
  • gp – Genetic program to visualise.

  • dispatcherCallable that should return a unique

  • called. (identifier when)

Module contents