Adaptive climbing in finite populations
A finite population that climbs a random fitness landscape.
- Class:
AdaptiveClimbingSystem - Import:
from skcomplex.systems.biology.evolution import AdaptiveClimbingSystem - Field:
biology/evolution - Topics:
fitness landscape,agents,random energy system,stochastic escape - References:
- Gros 2015
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.biology.evolution import AdaptiveClimbingSystem
trajectory = AdaptiveClimbingSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
size |
10 |
u |
0.1 |
length |
20 |
seed |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | none |
| Laws | update |
| Randomness | stochastic |
| Domains | integer, real |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class AdaptiveClimbingSystem(BaseSystem):
"""A finite population that climbs a random fitness landscape.
It climbs by mutation and strong selection.
The population climbs to a local optimum in an adaptive walk, ever slower near the top. When every individual
mutates in the same generation, which is likely only in small populations with a large `u`, it escapes to a lower
genome and starts a new walk, the wandering regime. The system has the group `population`.
Args:
size:
The number of individuals.
u:
The probability that an individual mutates in a generation.
length:
The number of loci of a genome.
seed:
The seed of the fitness landscape.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.biology.evolution import AdaptiveClimbingSystem
>>> AdaptiveClimbingSystem(5).prepare().system_.elements[0].size
5
"""
field = 'biology/evolution'
topics = ('fitness landscape', 'agents', 'random energy system', 'stochastic escape')
references = ('Gros 2015',)
until = 200
def __init__(
self,
size: int = 10,
*,
u: float = 0.1,
length: int = 20,
seed: int = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.size = size
self.u = u
self.length = length
self.seed = seed
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of a finite population.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
fitness = AdaptiveClimbingInteraction._get_random_fitness(0, self.seed)
self.system_ = System(
self.label,
elements=[ClimbingIndividualElement('population', trait=0.0, fitness=fitness, size=self.size)],
interactions=[
AdaptiveClimbingInteraction(
'climbing',
target='population',
mutation=self.u,
length=self.length,
seed=self.seed,
),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
ClimbingIndividualElement, fromElement
Interactions:
AdaptiveClimbingInteraction, fromInteraction, through the shared recipeEvolutionInteraction
class ClimbingIndividualElement(IndividualElement):
"""Individual of a climbing population, drawn by its fitness."""
visual = VisualSpec('agents', {'color_by': 'fitness'})
class AdaptiveClimbingInteraction(EvolutionInteraction):
"""One generation of a population that climbs a random fitness landscape.
The population climbs as a whole, under strong selection.
All individuals share one genome. Each mutates one random locus with probability `u`, to a genome of random fitness
in [0, 1], with probability `mutation`. If a mutant is fitter, the whole population takes the fittest mutant. If
every individual mutated to a less fit genome, the population escapes to the fittest of them. Otherwise it stays.
It is the best-mutant
replacement of `EvolutionInteraction` on genomes stored as the integers their bits spell.
Args:
label:
The label of the interaction.
target:
The path of the group of individuals.
mutation:
The probability that an individual mutates in a generation.
length:
The number of loci of a genome.
seed:
The seed of the fitness landscape.
Examples:
>>> from skcomplex.systems.biology.evolution import AdaptiveClimbingInteraction
>>> AdaptiveClimbingInteraction('climbing', target='population', mutation=0.1).replacement
'best-mutant'
"""
def __init__(
self,
label: str,
*,
target: Role,
mutation: Annotated[float, RealSet.closed(0, 1)] = 0.1,
length: int = 20,
seed: int = 0,
) -> None:
super().__init__(label, target=target, strategy='trait', replacement='best-mutant', mutation=mutation)
self.length = length
self.seed = seed
def _evaluate(self, strategies: Array, system: SystemViewProtocol) -> Array:
"""Return the random fitness of every genome.
Args:
strategies:
The genomes.
system:
The system view of the interaction.
Returns:
fitness:
The fitness of every genome.
"""
return np.array(
[
AdaptiveClimbingInteraction._get_random_fitness(int(genome), self.seed)
for genome in np.ravel(strategies)
],
)
def _mutate(self, children: Array, probability: float, rng: np.random.Generator, population: Array) -> Array:
"""Return the genomes, each with one random locus flipped.
Each locus flips with the probability of mutation.
Args:
children:
The genomes, one row per individual.
probability:
The probability that a genome mutates.
rng:
The random number generator.
population:
The genomes of the population.
Returns:
children:
The mutated genomes.
"""
flips = (rng.random(len(children)) < probability).astype(np.int64)
loci = rng.integers(self.length, size=len(children))
return (children.astype(np.int64) ^ (flips << loci)[:, None]).astype(float)
@staticmethod
def _get_random_fitness(genome: int, seed: int) -> float:
"""Return the fitness of a genome in the random energy system.
The fitness is uniform in [0, 1] and fixed by the seed.
"""
return float(np.random.default_rng([seed, genome]).random())
Equations
The equations are those that symbolic returns for the system with its default parameters.