Random walks
Random walkers on a network.
- Class:
NetworkRandomWalkSystem - Import:
from skcomplex.systems.mathematics.networks import NetworkRandomWalkSystem - Field:
mathematics/networks - Topics:
network,random walk,agents - References:
- Thurner et al. 2018
- Jensen 2023
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.mathematics.networks import NetworkRandomWalkSystem
trajectory = NetworkRandomWalkSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
graph |
None |
n_walkers |
50 |
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 | integer |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class NetworkRandomWalkSystem(BaseSystem):
"""Random walkers on a network.
The walkers visit every node of a connected graph in proportion to its degree.
The system has the group `walkers`.
Args:
graph:
The undirected NetworkX graph, or `None` for Zachary's karate club.
n_walkers:
The number of walkers, started at random nodes.
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:
>>> import networkx as nx
>>> from skcomplex.systems.mathematics.networks import NetworkRandomWalkSystem
>>> NetworkRandomWalkSystem(nx.cycle_graph(6), n_walkers=3).prepare().system_.elements[0].size
3
"""
field = 'mathematics/networks'
topics = ('network', 'random walk', 'agents')
references = ('Thurner et al. 2018', 'Jensen 2023')
until = 100
@staticmethod
def _get_graph_or_default(graph: nx.Graph | None) -> nx.Graph:
"""Return the graph, or Zachary's karate club when none is given."""
return nx.karate_club_graph() if graph is None else graph
def __init__(
self,
graph: nx.Graph | None = None,
*,
n_walkers: int = 50,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.graph = graph
self.n_walkers = n_walkers
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the walkers on the network and their steps."""
super()._prepare(context)
self.system_ = System(
self.label,
elements=[NetworkWalkerElement('walkers', size=self.n_walkers)],
interactions=[NetworkStepInteraction('walk', target='walkers')],
space=NetworkSpace(NetworkRandomWalkSystem._get_graph_or_default(self.graph)),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
NetworkWalkerElement, fromElement
Interactions:
NetworkStepInteraction, fromInteraction
class NetworkWalkerElement(Element):
"""Walkers on the nodes of a network, each with its number `steps` of steps taken.
Args:
label:
The label of the walkers.
steps:
The initial number of steps taken, one per walker or shared.
size:
The number of walkers, or `None` for one.
Examples:
>>> from skcomplex.systems.mathematics.networks import NetworkWalkerElement
>>> NetworkWalkerElement('walkers', size=5).size
5
"""
visual = VisualSpec('agents', {'color_by': 'steps'})
def __init__(
self,
label: str,
*,
steps: Annotated[object, NN] = 0.0,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.steps = steps
class NetworkStepInteraction(Interaction):
"""Step of a walker on a network to a neighbouring node drawn uniformly, counted in `steps`.
Every walker runs the rule once per step.
Args:
label:
The label of the interaction.
target:
The walkers that step.
Examples:
>>> from skcomplex.systems.mathematics.networks import NetworkStepInteraction
>>> NetworkStepInteraction('walk').target
'walkers'
"""
law_classes = (UpdateLaw,)
draws = True
def __init__(self, label: str, *, target: Role = 'walkers') -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the step of every walker as the rule.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
self.laws_: list[BaseLaw] = [UpdateLaw(context.target, self._step)]
def _step(self, agent: NetworkWalkerView, system: SystemView) -> None:
"""Move a walker to a random neighbouring node and count the step."""
nodes = agent.get_neighbor_cells()
if nodes:
agent.move_to(nodes[int(system.rng.integers(len(nodes)))])
agent.steps += 1