Laws
A law is the mathematical object an interaction returns, such as a rate, a noise, a jump, or an update. Its kind is a tag, and so are the times and spaces it runs in and its randomness, which says whether it draws random numbers. The law kinds of a system are those of its interactions, and a solver or a backend runs a system only when it supports every one of them. The table lists the kind, time, space, and randomness that each law class declares.
| Law | Kind | Time | Space | Randomness |
|---|---|---|---|---|
ConstraintLaw |
equation |
continuous, static |
any | deterministic |
EventLaw |
update |
continuous |
any | deterministic |
JumpLaw |
jump |
continuous |
any | stochastic |
NoiseLaw |
noise |
continuous |
any | stochastic |
ObjectiveLaw |
objective |
static |
any | deterministic |
RateLaw |
rate |
continuous |
any | deterministic |
UpdateLaw |
update |
discrete |
any | deterministic |
Comparison
| Class | Summary | Uses | See also |
|---|---|---|---|
BaseLaw |
An abstract law, one equation or rule that changes the values of elements. | Its subclasses give the spread of an epidemic, the next generation of a cellular automaton, a chemical reaction one molecule at a time, and the currents of a circuit. | RateLaw, UpdateLaw, Interaction |
ConstraintLaw |
A law that states an equation the compiler solves for derivatives of values. | The swings of a double pendulum, the currents of a circuit, and the joints of a robot arm follow such equations. | RateLaw, ObjectiveLaw |
EventLaw |
A law that sets values or ends the run when an expression crosses zero. | A ball bounces off the floor, a neuron fires when its voltage reaches a threshold, and a thermostat switches a heater at a set temperature. | JumpLaw, UpdateLaw |
JumpLaw |
A law of a random event that changes values by whole amounts. | Molecules react one at a time in a cell, people are born and die in a small population, customers arrive at a queue, and a walker jumps between sites. | RateLaw, EventLaw |
NoiseLaw |
A law that adds a random change to a time derivative of a value. | A stock price moves at random, a particle diffuses in a fluid, molecules shake the expression of a gene, and turbulence stirs the temperature of a fluid. | RateLaw, WienerProcessSpec |
ObjectiveLaw |
A law that gives a cost of values, which the system makes least. | A loaded structure rests at its least energy, a cell routes its fluxes to its greatest growth, a model takes the weights of its least loss, and a health authority times a vaccination campaign at the least cost. | ConstraintLaw, UpdateLaw |
RateLaw |
A law that adds a rate to a time derivative of a value. | Infected people recover at a rate, a balance grows by interest, a reactant is used up by a reaction, and a pendulum swings under gravity. | NoiseLaw, UpdateLaw, ConstraintLaw |
UpdateLaw |
A law that gives what a value becomes after one step of discrete time. | A population breeds from year to year, a cellular automaton steps its cells, an account earns interest every month, and a player revises a strategy every round. | RateLaw, TopologyRequestSpec, TotalisticSpec |
Fact sheets
BaseLaw
An abstract law, one equation or rule that changes the values of elements.
- Law kind: from a subclass
- Randomness:
deterministic - Times: any
- Solvers: any
- Backends: any
- Arguments: none of its own
- Errors: none of its own
- Catalog systems: active_passive_flocking, activity_driven_epidemics, adaptive_climbing, adaptive_delay_network, adaptive_flow_network, adaptive_sis, adding_machine, adler_equation, ageing_random_walk, ajanis_choice, and more in the catalog
- Field realizations:
DissipatorLaw,GeneratorLaw,ProductionLaw,SubstitutionLaw - See also: reference, API
ConstraintLaw
A law that states an equation the compiler solves for derivatives of values.
- Law kind:
equation - Randomness:
deterministic - Times: any
- Solvers:
CoSimulationSolver,CollocationSolver,CrankNicolsonSolver,ETDRK4Solver,EulerMaruyamaSolver,FractionalABMSolver,FractionalEulerMaruyamaSolver,GrunwaldLetnikovSolver,HeunSolver,IMEX2Solver,IMEXEulerSolver,L1Solver,LammpsSolver,MilsteinSolver,MinimizeSolver,PontryaginSolver,RK4Solver,RenewalSolver,RootSolver,ScipyODESolver,SplitSolver,VelocityVerletSolver,Yoshida4Solver - Backends: any
- Arguments:
lhs: The left side, or a Sage relationlhs == rhs.rhs: The right side, orNonewhenlhsis a relation.target: The value whose equation this is, so that the equations of one target add, orNonefor an equation of its own.label: The name of the law, or the empty string for a law without one.
- Errors: none of its own
- Catalog systems: carbon13_mfa, chua, comets, community_fba, dc_motor, dynamic_fba, dynamic_multispecies_fba, elementary_flux_modes, flux_balance, gravity_urban_scaling, and more in the catalog
- Field realizations: none yet
- See also: reference, API
EventLaw
A law that sets values or ends the run when an expression crosses zero.
- Law kind:
update - Randomness:
deterministic - Times:
continuous,discrete - Solvers:
CoSimulationSolver,CrankNicolsonSolver,ETDRK4Solver,EulerMaruyamaSolver,EulerSolver,FractionalABMSolver,FractionalEulerMaruyamaSolver,GillespieSolver,GrunwaldLetnikovSolver,HeunSolver,IMEX2Solver,IMEXEulerSolver,L1Solver,LammpsSolver,MilsteinSolver,PontryaginSolver,RK4Solver,RenewalSolver,SSPRKSolver,ScipyODESolver,SplitSolver,VelocityVerletSolver,Yoshida4Solver - Backends: any
- Arguments:
guard: A symbolic expression of values and time whose crossing of zero sets off the event, a functionf(t, values)of time and the mapping from the dotted name of every value and parameter to its value, orCallSpec(f, relative=True)to read the names of the component.reset: A mapping from the symbol of a value to its new value, all set at once, orNoneto keep the values.direction:1for crossings from negative to positive,-1for the opposite, and0for both.terminal: Whether the crossing ends the run.label: The name of the crossing in the trajectory, or the empty string for the path of the element.
- Errors: none of its own
- Catalog systems: adler_equation, ballistic_pendulum, billiards, bouncing_ball, cart_pendulum, chua, circular_billiard, continuous_androgen_suppression, coupled_pendulums, delayed_lif_network, and more in the catalog
- Field realizations: none yet
- See also: reference, API
JumpLaw
A law of a random event that changes values by whole amounts.
- Law kind:
jump - Randomness:
stochastic - Times:
continuous,discrete - Solvers:
CoSimulationSolver,EulerMaruyamaSolver,GillespieSolver,RK4Solver,RenewalSolver - Backends:
NumbaBackend,NumpyBackend - Arguments:
rate: How often the event fires per unit of time, a symbolic expression, aCallSpec, or a functionf(t, values), orNonewith a waiting time.changes: A mapping from the symbol of a scalar value to the number it adds when the event fires.waiting: The time until the next firing, usually a draw from a distribution of positive numbers, such asdraw('wait', 'pareto', b=alpha), orNonefor a jump at a rate.reset: With a waiting time, a mapping from the symbol of a value to its new value, which may hold draws, orNone.label: The name of the event, such as'infection', or the empty string for a law without one.
- Errors: none of its own
- Catalog systems: absorbing_birth_death, activity_driven_epidemics, adaptive_sis, ageing_random_walk, annealed_transient_time_motion, asep, bernoulli_process, birth_death_queue, cell_cycle_invasion, continuous_time_random_walk, and more in the catalog
- Field realizations: none yet
- See also: reference, API
NoiseLaw
A law that adds a random change to a time derivative of a value.
- Law kind:
noise - Randomness:
stochastic - Times:
continuous,discrete - Solvers:
CoSimulationSolver,EulerMaruyamaSolver,FractionalEulerMaruyamaSolver,HeunSolver,LammpsSolver,MilsteinSolver,RenewalSolver - Backends:
NumbaBackend,NumpyBackend - Arguments:
target: The symbol of a scalar value, a derivative of it such asx.diff(t), or the column of symbols of a vector value.value: The size of the random change, a symbolic expression, aCallSpec, or a functionf(t, values).convention: How the random change reads the value it changes,'ito'at the start of the step,'stratonovich'at its middle, or'anti-ito'at its end.process:Nonefor random steps of its own, a label that laws of the element share, a tuple of labels with one per entry of a vector target, or the random stepss.process.dWors.process.dW[k]of aWienerProcessSpecthat interactions share.conserved: Whether the noise moves amounts between neighbouring sites of a lattice, with random steps of its own on every link, so the total of the field changes only by its rate laws.hurst: The Hurst index0 < H < 1, a number or a parameter, whose steps go on in the same direction above 1/2 and turn back below it, orNonefor independent steps.label: The name of the law, or the empty string for a law without one.
- Errors: none of its own
- Catalog systems: active_brownian_particles, active_ornstein_uhlenbeck_particles, active_passive_flocking, active_polymer, active_system_b, ageing_random_walk, aligning_active_brownian_particles, allen_cahn, anisotropic_brownian_particles, annealed_transient_time_motion, and more in the catalog
- Field realizations: none yet
- See also: reference, API
ObjectiveLaw
A law that gives a cost of values, which the system makes least.
- Law kind:
objective - Randomness:
deterministic - Times:
static - Solvers:
GradientFlowSolver,MetropolisSolver,MinimizeSolver - Backends: any
- Arguments:
value: The scalar symbolic expression to minimise, or a sequence of them linear in the targets, minimised in order.targets: The symbol of a value, or the symbols of the values, that the minimum is sought over, orNonewith controls.controls: The symbols of the values the law steers over a run, orNonefor a minimum overtargets.terminal: With controls, the cost at the end of the run, a symbolic expression or a number.active: For a linear value, a condition under which the minimum is sought, such asbiomass > 0, orNonefor always.infeasible: For a linear value, what a run does when the constraints cannot all hold,'raise'to raise an error,'zero'to set the targets to zero, or'hold'to keep their last values, as a culture that stops growing when its medium runs out.duals: For a linear value, whether a run records the dual value of each constraint, under<path>.dual_<k>in the order of the constraints.label: The name of the law, or the empty string for a law without one.
- Errors: none of its own
- Catalog systems: carbon13_mfa, comets, community_fba, dynamic_fba, dynamic_multispecies_fba, edwards_anderson, elementary_flux_modes, evolutionary_optimisers, flux_balance, ising, and more in the catalog
- Field realizations: none yet
- See also: reference, API
RateLaw
A law that adds a rate to a time derivative of a value.
- Law kind:
rate - Randomness:
deterministic - Times:
continuous,discrete - Solvers:
CoSimulationSolver,CollocationSolver,CrankNicolsonSolver,ETDRK4Solver,EulerMaruyamaSolver,EulerSolver,FractionalABMSolver,FractionalEulerMaruyamaSolver,GrunwaldLetnikovSolver,HeunSolver,IMEX2Solver,IMEXEulerSolver,KineticSolver,L1Solver,LammpsSolver,MilsteinSolver,PontryaginSolver,RK4Solver,RenewalSolver,SSPRKSolver,ScipyODESolver,SemiLagrangianSolver,SplitSolver,StrangSplittingSolver,VelocityVerletSolver,Yoshida4Solver - Backends: any
- Arguments:
target: The symbol of a scalar value, a derivative of it such asx.diff(t, 2), the column of symbols of a vector value, the matrix of a tensor value, or, for a flow, a mapping from the symbol of each stock to the sign of its change,-1or+1.value: A symbolic expression, a matrix of the shape of a tensor value, aCallSpec, or a functionf(t, values), wherevaluesmaps the dotted name of every value and parameter to its current value.label: The name of a flow, which the trajectory records under the element of its stocks, such aspopulation.deaths, the name of the rate in messages, or the empty string for none.
- Errors: none of its own
- Catalog systems: active_brownian_particles, active_gels, active_ornstein_uhlenbeck_particles, active_passive_flocking, active_polymer, active_system_b, active_viscoelastic_gel, active_walkers, adaptive_delay_network, adaptive_flow_network, and more in the catalog
- Field realizations: none yet
- See also: reference, API
UpdateLaw
A law that gives what a value becomes after one step of discrete time.
- Law kind:
update - Randomness:
deterministic - Times:
continuous,discrete - Solvers:
CoSimulationSolver,GradientDescentSolver,IterationSolver,LatticeSolver,MesaSolver - Backends: any
- Arguments:
target: The symbol of a scalar value, the column of symbols of a vector value, aTopologyRequestSpec, whose value is the chance that the request fires, the group of a role, whose members run a rule, or a tuple of values that a local rule sets together.value: A symbolic expression, aCallSpec, or a functionf(t, values)that gives the next value, the rulerule(agent, system)of the members of a group, the rule of the cells of a local rule, or witharraysa NumPy functionf(values, **kwargs)of the whole array of the value.label: The name of the law, or the empty string for a law without one.increment: Whether the value is the value plus a change, such asx + 1, whose change adds to the changes of the other update laws of the same value.source: The value the law moves members from, such as0.0for the healthy people of an epidemic, keeping every other value as it is, orNone.order: A symbolic expression of the values, parameters, and draws of a member, such as a random number, whose values order the members that update one after the other, each reading the values set before it, orNonefor members that update at once.phase: The name of the phase of the step the law runs in, which aPhasesScheduleSpecof the discrete time orders, so each phase reads the values the phases before it set, orNonefor the default phase.neighborhood: The ordered neighbours of every cell that a local rule reads, such asRadiusNeighborhood(1, metric='chebyshev'), orNonefor a law of one member.reads: The values whose neighbours a local rule reads, orNonefor its targets.schedule: The order in which a local rule updates the cells, orNonefor all at once.score: A functionscore(values, **kwargs)of the whole array of a lattice value, which returns one number per row and which a run records at every frame as<owner>.score, orNone.arrays: Whether the value is a NumPy function of the whole array of the value, shaped like the lattice or with one row per member, which replaces the value of every member at once.stochastic: Whether the function of a local rule or of a whole array draws numbers.
- Errors:
ValidationError: The value is a NumPy array, an increment law has a source, a local rule has a neighbourhood that is no neighbourhood, an unknown schedule, or a setting of a law of one member, a law has a score but no function of the whole array,arraysis given with a value that is no function or a target that is no value, orstochasticis given with a value that is no function of a local rule or of a whole array.
- Catalog systems: active_brownian_particles, active_ornstein_uhlenbeck_particles, active_walkers, activity_driven_epidemics, adaptive_climbing, adaptive_sis, adding_machine, ageing_random_walk, agent_based_lotka_volterra, agent_based_sir, and more in the catalog
- Field realizations: none yet
- See also: reference, API
A compatible law
An arrival adds one unit to a queue at random times, at a rate of 2 per time unit. It returns a jump law, which
GillespieSolver supports.
from skcomplex.core.components.elements import Element
from skcomplex.core.engine.solvers import GillespieSolver
from skcomplex.core.systems import System
from skcomplex.systems.general.reactions import ReactionInteraction
queue = Element('queue', values={'x': 0.0})
arrive = ReactionInteraction('arrive', target='queue', products={'x': 1}, k=2.0, mode='jump')
sorted(arrive.get_tags().laws)
system = System(elements=[queue], interactions=[arrive], solver=GillespieSolver()).prepare()
system.simulate(until=5, random_state=0).get_values('queue.x')[-1]
['jump']
11.0
The law kind is jump, and with the seed 0 the queue holds 11 after five time units, near the mean of 10.
An incompatible law
EulerSolver integrates rates and does not draw jumps, 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
queue = Element('queue', values={'x': 0.0})
arrive = ReactionInteraction('arrive', target='queue', products={'x': 1}, k=2.0, mode='jump')
try:
System(elements=[queue], interactions=[arrive], solver=EulerSolver()).prepare()
except TagError as error:
message = str(error)
message
assert 'laws jump' in message
The solver EulerSolver does not support the laws jump of the system. Pass a solver that supports them, or none for the default.
The message says that EulerSolver does not support the law kind jump, and asks for a solver that supports it or
for none, which lets the tags choose the default.