Evolutionary computation
Evolutionary computation: genetic algorithms and evolving bit strings.
The bit strings evolve on fitness landscapes.
AgedStrategyElement(label, *, actions=0.0, score=0.0, age=0, size=None)
Bases: StrategyElement
Strategy of Robby with an age.
It dies of old age or starves in the evolution by circumstance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
actions
|
object
|
The initial action per situation, an array of 243 integers from 0 to 6. |
0.0
|
score
|
object
|
The initial score. |
0.0
|
age
|
Annotated[object, NN]
|
The initial age in generations. |
0
|
size
|
int | None
|
The number of strategies, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import AgedStrategyElement
>>> AgedStrategyElement('strategy', age=3).age
3
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
267 268 269 270 271 272 273 274 275 276 277 | |
AnnealedCandidateElement(label, x=0.0, y=0.0, space=None)
Bases: Element
Candidate solution of the plane.
Its coordinates are sampled one state at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the candidate or the group. |
required |
x
|
object
|
The first coordinate. |
0.0
|
y
|
object
|
The second coordinate. |
0.0
|
space
|
BaseSpace | None
|
The lattice whose sites hold one candidate each, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import AnnealedCandidateElement
>>> AnnealedCandidateElement('candidate', x=1.0).x
1.0
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
346 347 348 349 350 351 352 353 354 355 | |
AnnealingInteraction(label, *, target, state, energy, temperature=1.0, generations=100.0)
Bases: Interaction
Boltzmann weight of the energy of candidates at a temperature that falls geometrically.
The objective of every candidate is its energy over the temperature of the time t,
E / (T * 1e-3 ** (t / generations)), so the temperature falls to 1e-3 of its start over generations steps,
and the MetropolisSolver of the system moves the state, such as
MetropolisSolver(moves={'candidates.x': 'gaussian'}).
It anneals a route of a salesman, the placement of a circuit, or the weights of a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of candidates on the sites of a lattice. |
required |
state
|
str
|
The name of the state the moves change. |
required |
energy
|
Callable[[SimpleNamespace], Expression]
|
The function |
required |
temperature
|
Annotated[float, open(0, oo)]
|
The temperature at the start. |
1.0
|
generations
|
Annotated[float, open(0, oo)]
|
The number of steps over which the temperature falls to 1e-3 of its start. |
100.0
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import AnnealingInteraction
>>> energy = lambda s: s.target.x**2
>>> AnnealingInteraction('anneal', target='candidates', state='x', energy=energy, temperature=5.0).temperature
5.0
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
BitStringElement(label, *, genome=0.0, fitness=0.0, size=None)
Bases: Element
Agent whose genome is a string of bits, with the fitness of that genome.
The genome is stored as the integer it spells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
genome
|
Annotated[object, NN]
|
The initial genome, an integer whose binary digits are the bits. |
0.0
|
fitness
|
Annotated[object, closed_open(0, oo)]
|
The initial fitness. |
0.0
|
size
|
int | None
|
The number of agents, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import BitStringElement
>>> group = BitStringElement('genomes', genome=[0, 1, 2, 3], size=4)
>>> group.label, group.size
('genomes', 4)
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_landscapes.py
58 59 60 61 62 63 64 65 66 67 68 | |
BitStringEvolutionSystem(n_agents=100, *, n_bits=8, death=0.1, mutation=0.05, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A population of bit strings that evolves on a random additive fitness landscape.
Every step each agent dies with a constant probability, and the dead are replaced by copies of parents chosen in
proportion to their fitness, with one bit flipped with a small probability, so the mean fitness climbs. The system
has the group genomes of states genome and fitness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The size of the population, which stays constant. |
100
|
n_bits
|
int
|
The number of bits of a genome. |
8
|
death
|
float
|
The probability that an agent dies in a step. |
0.1
|
mutation
|
float
|
The probability that a newborn has one random bit flipped. |
0.05
|
random_state
|
int | Generator | None
|
The seed or generator of the landscape and of the random initial genomes. |
0
|
label
|
str
|
The label of the system, which names it when it is nested in another one. |
'system'
|
solver
|
BaseSolver | str
|
The solver of the problem, or |
'auto'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import BitStringEvolutionSystem
>>> BitStringEvolutionSystem(10, n_bits=4).prepare().system_.interactions[0].target
'genomes'
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_landscapes.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
CircumstanceInteraction(label, *, target, lifespan=7, starvation=0.3, mutation=0.005, switch=None)
Bases: Interaction
Evolution by circumstance of the strategies of Robby.
Strategies die of old age or starve, and random survivors breed in their places.
Every generation the strategies age by one, the ones that reach lifespan die of old age, and the fraction
starvation of the population with the lowest scores starves. Random pairs of survivors, whatever their score,
exchange the tails of their strategies after a random cut, mutate every action to another one with probability
mutation, and place their child, of age 0, in the place of a dead strategy, until the population is whole again.
With a switch period, the starvation is on for switch generations and off for the next switch, in turn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of aged strategies. |
required |
lifespan
|
int
|
The age at which a strategy dies. |
7
|
starvation
|
float
|
The fraction of the population, of the lowest scores, that starves every generation. |
0.3
|
mutation
|
float
|
The probability that an action of a child is replaced by another one. |
0.005
|
switch
|
int | None
|
The number of generations after which the starvation switches on or off, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import CircumstanceInteraction
>>> CircumstanceInteraction('circumstance', target='strategies').lifespan
7
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
CovarianceAdaptationSpec(sigma=0.1)
dataclass
Bases: BaseSpec
Settings of covariance matrix adaptation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
float
|
The initial step size, relative to the box of the candidates. |
0.1
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import CovarianceAdaptationSpec
>>> CovarianceAdaptationSpec().sigma
0.1
DifferentialEvolutionSpec(variant='DE/best/1/bin', weight=0.5, crossover=0.2)
dataclass
Bases: BaseSpec
Settings of differential evolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variant
|
str
|
The scheme of pymoo's differential evolution, such as |
'DE/best/1/bin'
|
weight
|
float
|
The differential weight of the mutation, by default that of pymoo. |
0.5
|
crossover
|
float
|
The crossover probability, by default that of pymoo. |
0.2
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import DifferentialEvolutionSpec
>>> DifferentialEvolutionSpec().variant
'DE/best/1/bin'
EvolutionStrategySpec(offspring=None)
dataclass
Bases: BaseSpec
Settings of an evolution strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offspring
|
int | None
|
The number of offspring per generation, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import EvolutionStrategySpec
>>> EvolutionStrategySpec(offspring=200).offspring
200
EvolutionaryOptimisersSystem(n_candidates=40, *, algorithm='differential-evolution', landscape='rastrigin', temperature=5.0, width=0.5, generations=100.0, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A population of candidates that searches a two-dimensional landscape.
Each step is one generation. A population optimiser moves the candidates through OptimizationInteraction, and
reports search.best. Simulated annealing samples every coordinate through AnnealingInteraction at a temperature
that falls geometrically to 1e-3 of its start over generations steps, moved by a MetropolisSolver, and
tracker/record.best holds the lowest energy it has reached, from the observable search/lowest that the catalog
lists, so a run passes it in observables (Raman 2021 §6.3.3.3, §6.3.4). The system has the group candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_candidates
|
int
|
The number of candidates. |
40
|
algorithm
|
str
|
|
'differential-evolution'
|
landscape
|
str
|
|
'rastrigin'
|
temperature
|
float
|
The initial temperature of annealing. |
5.0
|
width
|
float
|
The standard deviation of a move of annealing. |
0.5
|
generations
|
float
|
The number of steps over which annealing cools. |
100.0
|
random_state
|
int | Generator | None
|
The seed or generator of the initial candidates. |
0
|
label
|
str
|
The label of the system, which names it when it is nested in another one. |
'system'
|
solver
|
BaseSolver | str
|
The solver of the problem, or |
'auto'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
The algorithm or the landscape is unknown. |
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import EvolutionaryOptimisersSystem
>>> EvolutionaryOptimisersSystem(10).prepare().system_.interactions[0].label
'search'
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
OptimizationInteraction(label, *, target, state, objective, bounds, algorithm=DifferentialEvolutionSpec(), phase=None)
Bases: Interaction
Move a group of candidates on an objective.
It runs one generation of a pymoo algorithm per step of discrete time. Every candidate holds a vector state in a
box, from which the first generation starts. Every step asks the algorithm for candidates, evaluates the objective
on their array, tells the algorithm the values, and writes the candidates of the generation into the members. The
interaction reports best, the lowest value so far, and mean, the mean value of the generation, every step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of candidates. |
required |
state
|
str
|
The name of the vector state of the candidates. |
required |
objective
|
Callable[[Array], ArrayLike]
|
The function of the array of candidates, one row per candidate, that returns their values. |
required |
bounds
|
tuple[ArrayLike, ArrayLike]
|
The lower and the upper bounds of the box, each a number or one number per component. |
required |
algorithm
|
AlgorithmSpec
|
The settings of the algorithm, which select it. |
DifferentialEvolutionSpec()
|
phase
|
str | None
|
The name of the phase of the step the rule runs in, or |
None
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
The algorithm is not an algorithm spec, or the objective is not callable, raised by |
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import OptimizationInteraction
>>> objective = lambda values: (values**2).sum(axis=1)
>>> search = OptimizationInteraction('search', target='swarm', state='x', objective=objective, bounds=(-1, 1))
>>> search.label
'search'
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
ParticleSwarmSpec(inertia=0.9, cognitive=2.0, social=2.0)
dataclass
Bases: BaseSpec
Settings of particle swarm optimisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inertia
|
float
|
The weight of the previous velocity. |
0.9
|
cognitive
|
float
|
The attraction to the best position of a particle. |
2.0
|
social
|
float
|
The attraction to the best position of the swarm. |
2.0
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import ParticleSwarmSpec
>>> ParticleSwarmSpec().inertia
0.9
ReproductionInteraction(label, *, target, death=0.1, mutation=0.05, zero_values=(), one_values=())
Bases: EvolutionInteraction
Death at a constant rate and replacement by mutated copies of fit parents.
The parents are chosen in proportion to their fitness. It is the death-birth replacement of EvolutionInteraction,
with parents drawn among all agents, on genomes stored as the integers their bits spell. A newborn has one random
bit flipped with probability mutation. The fitness of a genome is the mean over its loci of a value given to each
allele of each locus, an additive landscape with one value for a 0 and one for a 1 at every locus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of bit strings. |
required |
death
|
Annotated[float, closed(0, 1)]
|
The probability that an agent dies in a step. |
0.1
|
mutation
|
Annotated[float, closed(0, 1)]
|
The probability that a newborn has one random bit flipped. |
0.05
|
zero_values
|
tuple[float, ...]
|
The value of a 0 at every locus. |
()
|
one_values
|
tuple[float, ...]
|
The value of a 1 at every locus. |
()
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import ReproductionInteraction
>>> ReproductionInteraction(
... 'reproduction', target='genomes', zero_values=(0.1, 0.2), one_values=(0.9, 0.3)
... ).mutation
0.05
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_landscapes.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
RobbyGASystem(n_strategies=50, *, mutation=0.005, sessions=10, n_actions=100, regime='generational', lifespan=7, starvation=0.3, switch=None, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Mitchell's genetic algorithm that evolves strategies for Robby.
Robby is a robot that collects cans in a room. Each step is one generation. Parents are selected with probabilities
proportional to their rank, every pair of parents exchanges the tails of their strategies after a random cut, and
every action of a child mutates to another action with probability mutation. Then every child is scored in random
rooms. Random strategies score far below zero because they bump into walls, and evolution finds strategies that
avoid walls and collect cans.
Gooding's evolution by circumstance, the regime 'circumstance', replaces the selection with deaths: every strategy
starts at a random age, dies at the age lifespan, and the fraction starvation with the lowest scores starves
every generation. Random pairs of survivors, whatever their scores, breed children in the places of the dead. The
starvation is the only selection, so with it the scores rise as under the genetic algorithm, and without it they
drift. With a switch period the starvation switches on and off every switch generations, as in Gooding's runs
that switch it every 3000 iterations. The system has the group strategies of states actions and score, and
age in the evolution by circumstance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_strategies
|
int
|
The size of the population. |
50
|
mutation
|
float
|
The probability that an action of a child is replaced by another one. |
0.005
|
sessions
|
int
|
The number of rooms a strategy cleans to be scored. |
10
|
n_actions
|
int
|
The number of actions in a session. |
100
|
regime
|
str
|
The evolution, |
'generational'
|
lifespan
|
int
|
The age at which a strategy dies, in the evolution by circumstance. |
7
|
starvation
|
float
|
The fraction of the lowest scores that starves every generation, in the evolution by circumstance. |
0.3
|
switch
|
int | None
|
The number of generations after which the starvation switches on or off, or |
None
|
random_state
|
int | Generator | None
|
The seed or generator of the random first generation and of its scores. |
0
|
label
|
str
|
The label of the system, which names it when it is nested in another one. |
'system'
|
solver
|
BaseSolver | str
|
The solver of the problem, or |
'auto'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Raises:
| Type | Description |
|---|---|
ParameterError
|
The regime is unknown. |
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import RobbyGASystem
>>> RobbyGASystem(5).prepare().system_.elements[0].actions.shape
(5, 243)
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
RoomCleaningInteraction(label, *, target, sessions=10, n_actions=100, side=10, density=0.5, phase=None)
Bases: Interaction
Score of every strategy of Robby.
It is the mean of its scores in sessions of cleaning random rooms. Every strategy cleans sessions random rooms of
side side, with a can in each cell with probability density, for n_actions actions, and its state score
takes the mean score. A picked can scores 10, picking up nothing scores -1, and bumping into a wall scores -5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of strategies. |
required |
sessions
|
int
|
The number of rooms a strategy cleans to be scored. |
10
|
n_actions
|
int
|
The number of actions in a session. |
100
|
side
|
int
|
The side of a room. |
10
|
density
|
float
|
The probability that a cell holds a can. |
0.5
|
phase
|
str | None
|
The phase of the step the scoring runs in, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import RoomCleaningInteraction
>>> RoomCleaningInteraction('scoring', target='strategies').sessions
10
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
score(strategies, rng)
Return the mean score of strategies in random rooms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategies
|
IntArray
|
The actions of every strategy, one row per strategy. |
required |
rng
|
Generator
|
The random number generator of the rooms and the random moves. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scores |
Array
|
The mean score of every strategy. |
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
RunningBestElement(label, best=1e+300)
Bases: Element
Lowest objective value of a search so far.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the element. |
required |
best
|
float
|
The value before the search starts. |
1e+300
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import RunningBestElement
>>> RunningBestElement('record', best=5.0).best
5.0
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
439 440 441 | |
RunningBestInteraction(label, *, target, source)
Bases: Interaction
Keep the lowest value of an observable.
It sets best <- value if value < best else best.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the element that holds |
required |
source
|
Role
|
The path of the observable. |
required |
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import RunningBestInteraction
>>> RunningBestInteraction('update', target='record', source='search/lowest').source
'search/lowest'
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
491 492 493 494 495 496 497 498 499 500 | |
SearchCandidateElement(label, x=(0.0, 0.0), size=None)
Bases: Element
Candidate solution that holds a vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the candidate or the group. |
required |
x
|
ArrayLike
|
The vector of the candidate, one row per candidate. |
(0.0, 0.0)
|
size
|
int | None
|
The number of candidates, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import SearchCandidateElement
>>> SearchCandidateElement('candidate', x=(0.0, 1.0)).x
(0.0, 1.0)
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_optimisers.py
463 464 465 | |
StrategyElement(label, *, actions=0.0, score=0.0, size=None)
Bases: Element
Strategy of Robby, with its last measured score.
The strategy gives the action for each of the 243 situations he can see. The actions are 0 to 3 for moving north, south, east, and west, 4 to stay, 5 to pick up a can, and 6 to move in a random direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
actions
|
object
|
The initial action per situation, an array of 243 integers from 0 to 6. |
0.0
|
score
|
object
|
The initial score. |
0.0
|
size
|
int | None
|
The number of strategies, or |
None
|
Examples:
>>> from skcomplex.systems.computer_science.evolutionary_computation import StrategyElement
>>> StrategyElement('strategies', size=3).get_params()['size']
3
Source code in src/skcomplex/systems/computer_science/evolutionary_computation/_genetic_algorithms.py
71 72 73 74 75 76 77 78 79 80 81 | |