Mutualism
Two species that grow logistically and feed each other.
- Class:
MutualismSystem - Import:
from skcomplex.systems.biology.ecology import MutualismSystem - Field:
biology/ecology - Topics:
mutualism,ode,handling time - References:
- May 1981
- Wright 1989
- Argun et al. 2021
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.ecology import MutualismSystem
trajectory = MutualismSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
first |
10.0 |
second |
10.0 |
r1 |
1.0 |
r2 |
1.0 |
K1 |
50.0 |
K2 |
50.0 |
a |
0.1 |
b |
0.5 |
handling |
0.5 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | none |
| Laws | rate |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class MutualismSystem(BaseSystem):
"""Two species that grow logistically and feed each other.
They can grow above their carrying capacities.
With a positive handling time the benefit saturates and the populations settle above their capacities. Without it,
strong mutualism grows without bound. The system has the populations `first` and `second`.
Args:
first:
The initial size of the first population.
second:
The initial size of the second population.
r1:
The growth rate of the first population.
r2:
The growth rate of the second population.
K1:
The carrying capacity of the first population alone.
K2:
The carrying capacity of the second population alone.
a:
The effective search rate.
b:
The rate of encounters.
handling:
The handling time of the resource of the partner, 0 for type I mutualism.
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.ecology import MutualismSystem
>>> [interaction.label for interaction in MutualismSystem().prepare().system_.interactions]
['growth_first', 'growth_second', 'mutualism']
"""
field = 'biology/ecology'
topics = ('mutualism', 'ode', 'handling time')
references = ('May 1981', 'Wright 1989', 'Argun et al. 2021')
until = 50.0
dt = 0.1
def __init__(
self,
first: float = 10.0,
second: float = 10.0,
*,
r1: float = 1.0,
r2: float = 1.0,
K1: float = 50.0,
K2: float = 50.0,
a: float = 0.1,
b: float = 0.5,
handling: float = 0.5,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.first = first
self.second = second
self.r1 = r1
self.r2 = r2
self.K1 = K1
self.K2 = K2
self.a = a
self.b = b
self.handling = handling
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of two species that grow logistically and feed each other.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
self.system_ = System(
self.label,
elements=[PopulationElement('first', x=self.first), PopulationElement('second', x=self.second)],
interactions=[
LogisticGrowthInteraction('growth_first', target='first', r=self.r1, K=self.K1),
LogisticGrowthInteraction('growth_second', target='second', r=self.r2, K=self.K2),
MutualismInteraction(
'mutualism',
first='first',
second='second',
a=self.a,
b=self.b,
handling=self.handling,
),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
PopulationElement, fromElement, through the shared recipeNonnegativeElement
Interactions:
LogisticGrowthInteraction, fromInteractionMutualismInteraction, fromInteraction
class PopulationElement(CompartmentElement):
"""Population of one species, a compartment whose state `x` holds its size.
Args:
label:
The label of the population.
x:
The initial size.
Examples:
>>> from skcomplex.systems.biology.common.populations import PopulationElement
>>> PopulationElement('rabbits', x=40).x
40
"""
def __init__(
self,
label: str,
*,
x: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
) -> None:
super().__init__(label, x=x)
class LogisticGrowthInteraction(GrowthInteraction):
"""Verhulst's growth limited by a carrying capacity, without harvest.
The population follows `dx/dt = r x (1 - x(t - tau) / K)`.
A delay `tau` above 0 gives Hutchinson's delayed logistic equation. The generalized logistic of Richards, with an
exponent on the crowding, is `GrowthInteraction(form='logistic', p=...)`.
Args:
label:
The label of the interaction.
target:
The path of the element or group whose population grows.
r:
The per capita growth rate at low density.
K:
The carrying capacity.
tau:
The delay of the limiting factor, 0 for none.
state:
The name of the state of the target that holds the population.
Examples:
>>> from skcomplex.systems.biology.common.kinetics import CompartmentElement
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.biology.common.populations import LogisticGrowthInteraction
>>> system = System(
... elements=[CompartmentElement('rabbits', x=1.0)],
... interactions=[LogisticGrowthInteraction('growth', target='rabbits', r=1.0, K=50.0)],
... )
>>> trajectory = system.prepare().simulate(until=30.0, dt=0.1)
>>> round(float(trajectory.get_values('rabbits.x')[-1]), 3)
50.0
"""
def __init__(
self,
label: str,
*,
target: Role,
r: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
K: Annotated[float, RealSet.open(0, oo)] = 100.0,
tau: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
state: str = 'x',
) -> None:
super().__init__(label, target=target, r=r, K=K, tau=tau, form='logistic', state=state)
def _get_crowding(self, namespace: SimpleNamespace) -> Expression:
"""Return the crowding `x / K` of Verhulst's logistic.
Args:
namespace:
The namespace of the symbols the rate can use, with the namespace of the target.
Returns:
crowding:
The ratio of the population the limiting factor reads to the capacity.
"""
crowding: Expression = self._get_limited(namespace) / self._get_capacity(namespace)
return crowding
def _get_rate(self, namespace: SimpleNamespace) -> Expression:
"""Return the logistic growth, with no harvest.
Args:
namespace:
The namespace of the symbols the rate can use, with the namespace of the target.
Returns:
rate:
The rate of the population.
"""
return self._get_growth(namespace)
class MutualismInteraction(Interaction):
"""Mutualism with a handling time, where encounters feed both species.
Each species gains `a b n1 n2 / (1 + a T n)`.
The benefit to each species saturates with the abundance of its partner, as the handling time `T` limits how much
of the partner's resource it can use. A handling time of 0 gives the unbounded type I mutualism `a b n1 n2`.
Args:
label:
The label of the interaction.
first:
The path of the first population.
second:
The path of the second population.
a:
The effective search rate.
b:
The rate of encounters.
handling:
The handling time `T` of the resource of the partner.
Examples:
>>> from skcomplex.systems.biology.ecology import MutualismInteraction
>>> MutualismInteraction('pollination', first='orchids', second='bees', handling=0.5).handling
0.5
"""
law_classes = (RateLaw,)
def __init__(
self,
label: str,
*,
first: Role,
second: Role,
a: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
b: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
handling: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
) -> None:
super().__init__(label)
self.first = first
self.second = second
self.a = a
self.b = b
self.handling = handling
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the saturating benefits of both species.
They are `a b n1 n2 / (1 + a T n2)` and `a b n1 n2 / (1 + a T n1)`.
Args:
context:
The namespace of the bound elements, the time `t`, and the parameters.
"""
super()._prepare(context)
s = context
first, second = s.first.x, s.second.x
encounters = s.a * s.b * first * second
self.laws_ = [
RateLaw(first, encounters / (1 + s.a * s.handling * second)),
RateLaw(second, encounters / (1 + s.a * s.handling * first)),
]
Equations
The equations are those that symbolic returns for the system with its default parameters.
first.x: diff(x_first(t), t) == -r_growth_first*(x_first(t)/K_growth_first - 1)*x_first(t) + a_mutualism*b_mutualism*x_first(t)*x_second(t)/(a_mutualism*handling_mutualism*x_second(t) + 1)
second.x: diff(x_second(t), t) == -r_growth_second*(x_second(t)/K_growth_second - 1)*x_second(t) + a_mutualism*b_mutualism*x_first(t)*x_second(t)/(a_mutualism*handling_mutualism*x_first(t) + 1)