Skip to content

Interactions

An interaction changes the states of the elements bound to its roles. A role is a named slot, such as target, and it lists the domains of the states it accepts. Each interaction also lists the kinds of the laws it can return, and its arguments choose among them. The law kinds fix the time the interaction runs in, so the components of one system must agree on it. get_tags() of an interaction returns the law kinds for the arguments you passed. The table lists the roles of each interaction class of the core, with the domains they accept, and its law kinds.

Interaction Roles and accepted domains Law kinds
BaseInteraction from a subclass unknown until run
Interaction from a subclass unknown until run
TrainingInteraction system: any domain from a subclass

Comparison

Class Summary Uses See also
BaseInteraction An abstract interaction, the component that changes the values of elements. Its subclasses spread an infection in an epidemic, run a chemical reaction, and pay wages from a firm to its workers. Interaction, BaseElement
Interaction An interaction whose laws run once for the elements its roles bind. It states predation between two populations, a reaction between two chemicals, and an infection along the contacts of a network. BaseInteraction, RandomPartition, TrainingInteraction
TrainingInteraction An interaction that trains the parameters of a nested system by gradient steps. A recurrent neural network learns to predict a time series, an echo state network fits its readout, and a learning agent improves its policy between rounds. Interaction, GradientDescentSolver

Fact sheets

BaseInteraction

An abstract interaction, the component that changes the values of elements.

Interaction

An interaction whose laws run once for the elements its roles bind.

  • Values: among, along, within
  • Law kinds: unknown until run
  • Times: any
  • Solvers: any
  • Backends: any
  • Arguments:
    • label: The name of the interaction, unique among the components of its system, such as 'predation'.
    • roles: The element each role binds, by the name of the role, such as {'target': 'ball'}, for a rule written without a class, or None.
    • laws: The function of the namespace that returns the laws of a rule written without a class, such as lambda s: [RateLaw(s.target.n.diff(s.t), s.target.n)], the laws themselves when they read no element, or None.
    • among: The group whose members sit at the ends of the edges, such as 'people', or None to bind elements by their roles only.
    • along: True for the edges of the network the group lives on, the name of a layer of a network with several, such as 'contacts', 'hyperedges' for its hyperedges, ('hyperedges', k) for its hyperedges of k members, RandomPartition(k) for random groups of k members drawn at every step, 'neighborhoods' for every member with the members around it, or None for no edges.
    • within: A RadiusNeighborhood(r) for laws that run once for every pair of members of the roles first and second closer than r, which read s.first and s.second and give the laws of both, such as the repulsion of walkers of two crowds, or None for no pairs.
  • Errors: none of its own
  • Catalog systems: absorbing_birth_death, active_brownian_particles, active_gels, active_ornstein_uhlenbeck_particles, active_passive_flocking, active_polymer, active_system_b, active_viscoelastic_gel, active_walkers, activity_driven_epidemics, and more in the catalog
  • Field realizations: AccountingInteraction, ActiveCahnHilliardInteraction, ActiveCurrentInteraction, ActiveDiffusionInteraction, ActivePercolationInteraction, ActiveRouseInteraction, AdaptiveAuctionInteraction, AdaptiveClimbingInteraction, AdaptiveCouplingInteraction, AdaptivePhaseCouplingInteraction, and more in the catalog
  • See also: reference, API

TrainingInteraction

An interaction that trains the parameters of a nested system by gradient steps.

  • Values: system, trained, loss, data, optimizer, learning_rate, momentum, beta1, beta2, batch_size, truncation, metrics
  • Law kinds: none of its own
  • Times: any
  • Solvers: any
  • Backends: any
  • Arguments:
    • label: The name of the interaction, unique among the components of its system, such as 'training'.
    • system: The path of the nested system whose parameters the interaction trains.
    • trained: The dotted paths of the parameters of the nested system to train, such as readout.W.
    • loss: The observable of the nested system whose weighted mean over the steps of a batch is minimized.
    • data: The sequences of the parameters of the nested system that set its inputs and targets at every step.
    • optimizer: The name of the optimizer, one of OptimizerKind.
    • learning_rate: The learning rate of the optimizer.
    • momentum: The momentum of 'momentum'.
    • beta1: The decay of the mean gradient of 'adam'.
    • beta2: The decay of the mean squared gradient of 'adam' and 'rmsprop'.
    • batch_size: The number of sequences of a batch.
    • truncation: The number of inner steps the gradient flows back through the inner states, or None for the whole run.
    • metrics: The observables of the nested system recorded at the end of every epoch.
  • Errors:
    • ValidationError: The optimizer is unknown, the batch size or the truncation is not a positive integer, no parameter is trained, or the loss, the data, or a metric has the wrong type or label.
  • Catalog systems: elman_network
  • Field realizations: none yet
  • See also: reference, API

A compatible interaction

A reaction of skcomplex.systems.general.reactions returns a rate law of continuous time, and its target accepts the real amount of an element.

from skcomplex.core.components.elements import Element
from skcomplex.core.systems import System
from skcomplex.systems.general.reactions import ReactionInteraction

tank = Element('tank', values={'x': 10.0})
decay = ReactionInteraction('decay', target='tank', reactants={'x': 1}, k=0.5)
sorted(decay.get_tags().laws), sorted(decay.get_tags().time)
trajectory = System(elements=[tank], interactions=[decay]).prepare().simulate(until=2)
round(trajectory.get_values('tank.x')[-1], 2)
(['rate'], ['continuous'])
3.68

The decay has the law kind rate and the time continuous, and the tank falls from 10 to 3.68 after two time units.

An incompatible interaction

The same reaction drawn as random events returns a jump law, which EulerSolver does not draw, so prepare raises a TagError that names the law kind.

from skcomplex.core.components.elements import Element
from skcomplex.core.engine.solvers import EulerSolver
from skcomplex.core.errors import TagError
from skcomplex.core.systems import System
from skcomplex.systems.general.reactions import ReactionInteraction

tank = Element('tank', values={'x': 10.0})
decay = ReactionInteraction('decay', target='tank', reactants={'x': 1}, k=0.5, mode='jump')
sorted(decay.get_tags().laws)
try:
    System(elements=[tank], interactions=[decay], solver=EulerSolver()).prepare()
except TagError as error:
    message = str(error)
message
assert 'laws jump' in message
['jump']
The solver EulerSolver does not support the laws jump of the system. Pass a solver that supports them, or none for the default.

The reaction has the law kind jump. The message says that EulerSolver does not support it, and asks for a solver that supports it or for none.