Skip to content

Jain-Krishna autocatalytic networks

The system of Jain and Krishna.

  • Class: JainKrishnaSystem
  • Import: from skcomplex.systems.biology.evolution import JainKrishnaSystem
  • Field: biology/evolution
  • Topics: adaptive network, autocatalytic set, Perron eigenvalue, origin of life, stochastic map
  • References:
    • Jain and Krishna 1998
    • Jain and Krishna 2001
    • Thurner et al. 2018

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 JainKrishnaSystem

trajectory = JainKrishnaSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n 40
p 0.02
h 0.5
period 10
seed 1
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 network
Laws update
Randomness stochastic
Domains boolean, real
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class JainKrishnaSystem(BaseSystem):
    """The system of Jain and Krishna.

    Selection of the fittest builds autocatalytic sets of molecules.

    The populations of `n` molecular species approach the Perron eigenvector of their catalytic network between graph
    updates, every `period` steps. At every graph update the least populated species is replaced by a new one, linked to
    the others with probability `p`. Once a cycle of catalysts forms by chance, the species it supports win, and the
    autocatalytic set grows until it spans the network, so the number of links and the populated species jump. The
    system has the populations `species.x` of the variable group `species` on the nodes and the variable group `links`
    on the edges, where an edge from `i` to `j` means that `j` catalyses `i`.

    Args:
        n:
            The number of species.

        p:
            The probability of every catalytic link.

        h:
            The growth of a step from the catalysts.

        period:
            The number of steps between two graph updates.

        seed:
            The seed of the initial network.

        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 JainKrishnaSystem
        >>> round(float(JainKrishnaSystem(n=10).prepare().system_.elements[0].x.sum()), 6)
        1.0
    """

    field = 'biology/evolution'
    topics = ('adaptive network', 'autocatalytic set', 'Perron eigenvalue', 'origin of life', 'stochastic map')
    references = ('Jain and Krishna 1998', 'Jain and Krishna 2001', 'Thurner et al. 2018')
    until = 1500
    dt = 5.0

    def __init__(
        self,
        n: int = 40,
        *,
        p: float = 0.02,
        h: float = 0.5,
        period: int = 10,
        seed: int | None = 1,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n = n
        self.p = p
        self.h = h
        self.period = period
        self.seed = seed

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of Jain and Krishna.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        rng = np.random.default_rng(self.seed)
        graph = nx.gnp_random_graph(self.n, self.p, seed=int(rng.integers(2**31)), directed=True)
        space = NetworkSpace(graph)
        species = ChemicalElement(
            'species',
            x=np.full(self.n, 1 / self.n),
            tie=rng.random(self.n),
            fresh=np.zeros(self.n),
            h=self.h,
            p=self.p,
            period=float(self.period),
            space=space,
        )
        links = CatalyticLinkElement('links', space=space, on='edges')
        growth = AutocatalyticGrowthInteraction('growth', target='species', k=max(1, round(self.p * self.n)))
        self.system_ = System(self.label, elements=[species, links], interactions=[growth])

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • AutocatalyticGrowthInteraction, from Interaction
class ChemicalElement(Element):
    """Molecular species of a pool whose population grows with its catalysts.

    The relative population grows with the populations of its catalysts under an `AutocatalyticGrowthInteraction`.

    Args:
        label:
            The label of the species or of the group of species.

        x:
            The relative population.

        tie:
            The random key that breaks ties between the least populated species.

        fresh:
            Whether the species appeared at the previous graph update.

        h:
            The growth of a step from the catalysts.

        p:
            The probability of every catalytic link of a new species.

        period:
            The number of steps between two graph updates.

        space:
            The network whose nodes hold one species each, or `None` for one species.

    Examples:
        >>> from skcomplex.systems.biology.evolution import ChemicalElement
        >>> ChemicalElement('species').period
        10.0
    """

    visual = VisualSpec('sites', {'states': ['x']})

    def __init__(
        self,
        label: str,
        *,
        x: Annotated[float | Array, RealSet.closed_open(0, oo)] = 0.0,
        tie: Annotated[float | Array, RealSet.closed_open(0, oo)] = 0.5,
        fresh: Annotated[float | Array, FiniteEnumeratedSet([0, 1])] = 1.0,
        h: Annotated[float | Array, RealSet.open(0, oo)] = 0.5,
        p: Annotated[float | Array, RealSet.closed_open(0, oo)] = 0.02,
        period: Annotated[float | Array, RealSet.open(0, oo)] = 10.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.x = x
        self.tie = tie
        self.fresh = fresh
        self.h = h
        self.p = p
        self.period = period

class CatalyticLinkElement(Element):
    """Catalytic link from one species to another, hidden from the drawing."""

    visual = VisualSpec('sites', {'states': []})

class AutocatalyticGrowthInteraction(Interaction):
    """Catalytic growth of the species of a pool and the replacement of the least populated one.

    The populations follow the catalytic network: every species gains `h` times the populations of the species that
    catalyse it, and the populations are normalised, so they approach the Perron eigenvector of the network. Every
    `period` steps the least populated species, with ties broken at random, is replaced by a new species with `k`
    random catalysts, and every species catalyses the new one with probability `p`. The growth `h`, the probability
    `p`, and the period are values of the species, a `ChemicalElement`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of species on the nodes.

        k:
            The number of catalysts of a new species.

    Examples:
        >>> from skcomplex.systems.biology.evolution import AutocatalyticGrowthInteraction
        >>> AutocatalyticGrowthInteraction('growth', target='species', k=2).k
        2
    """

    draws = True

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role, k: int = 1) -> None:
        super().__init__(label)
        self.target = target
        self.k = k

    @staticmethod
    def _get_fresh(node: SimpleNamespace) -> object:
        """Return the weight of a candidate catalysed species, 1 for the new species and 0 otherwise."""
        return node.fresh

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the catalytic growth and the replacement of the least populated species at every graph update.

        Args:
            context:
                The namespace of the group of species.
        """
        super()._prepare(context)
        s = context.target
        grown = s.x + s.h * neighbors(s.other.x) + 1e-6
        key = s.x + 1e-9 * s.tie
        update = mod(context.t, s.period) > s.period - 3 / 2
        least = where(both(key <= minimum(key), update), 1, 0)
        newcomer = where(total(s.fresh) > 1 / 2, 1, 0) * (1 - s.fresh)
        self.laws_: list[BaseLaw] = [
            UpdateLaw(s.x, grown / total(grown)),
            UpdateLaw(s.tie, uniform('tie')),
            UpdateLaw(s.fresh, 0),
            UpdateLaw(TopologyRequestSpec('add-node', edges=self.k), least),
            UpdateLaw(TopologyRequestSpec('remove-node'), least),
            UpdateLaw(TopologyRequestSpec('add-edge', toward=self._get_fresh), s.p * newcomer),
        ]