Rock-paper-scissors
Rock, paper, and scissors on a lattice.
- Class:
RockPaperScissorsSystem - Import:
from skcomplex.systems.biology.evolution import RockPaperScissorsSystem - Field:
biology/evolution - Topics:
evolutionary game,lattice,cyclic dominance,spirals,stochastic - References:
- Szabó and Fáth 2007
- Reichenbach, Mobilia, and Frey 2007
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 RockPaperScissorsSystem
trajectory = RockPaperScissorsSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
shape |
(60, 60) |
random_state |
0 |
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 | lattice |
| Laws | update |
| Randomness | unknown |
| Domains | integer |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class RockPaperScissorsSystem(BaseSystem):
"""Rock, paper, and scissors on a lattice.
Cyclic invasions keep the three strategies coexisting.
Starting from random strategies, the strategies chase each other in domains and fronts, and each keeps about a third
of the lattice. The system has the strategies `lattice.strategy`.
Args:
shape:
The number of rows and columns of the periodic lattice.
random_state:
The seed or generator of the initial strategies.
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 RockPaperScissorsSystem
>>> RockPaperScissorsSystem((10, 10)).prepare().system_.elements[0].strategy.shape
(10, 10)
"""
field = 'biology/evolution'
topics = ('evolutionary game', 'lattice', 'cyclic dominance', 'spirals', 'stochastic')
references = ('Szabó and Fáth 2007', 'Reichenbach, Mobilia, and Frey 2007')
until = 100
dt = 1
def __init__(
self,
shape: Sequence[int] = (60, 60),
*,
random_state: int | np.random.Generator | None = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.shape = shape
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of rock, paper, and scissors on a lattice.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
strategies = (
np.random.default_rng(self.random_state).integers(0, STRATEGIES, size=tuple(self.shape)).astype(float)
)
self.system_ = System(
self.label,
elements=[
CyclicSpeciesElement('lattice', strategy=strategies, space=LatticeSpace(tuple(self.shape))),
],
interactions=[
CyclicInvasionInteraction('invasion', target='lattice'),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CyclicSpeciesElement, fromElement
Interactions:
CyclicInvasionInteraction, fromInteraction
class CyclicSpeciesElement(Element):
"""Site of a lattice of rock, paper, and scissors, 0, 1, and 2.
Each strategy beats the one before it.
The sites update one at a time in random order by a `CyclicInvasionInteraction`, and a site adopts the strategy of a
random neighbour that beats it.
Args:
label:
The label of the site or the lattice group.
strategy:
The initial strategy, 0, 1, or 2, one per site or shared.
space:
The lattice whose sites hold one strategy each, or `None` for one site.
Examples:
>>> from skcomplex.systems.biology.evolution import CyclicSpeciesElement
>>> CyclicSpeciesElement('site', strategy=2).strategy
2
"""
def __init__(
self,
label: str,
*,
strategy: Annotated[float | Array, NN] = 0,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, space=space)
self.strategy = strategy
class CyclicInvasionInteraction(Interaction):
"""Invasion of the sites of rock, paper, and scissors by a random neighbour that beats them.
The sites update one at a time in a random order, and each adopts the strategy of a random von Neumann neighbour
when that strategy beats its own, rock 0 beaten by paper 1, paper by scissors 2, and scissors by rock.
Args:
label:
The label of the interaction.
target:
The sites whose state `strategy` the rule updates.
Examples:
>>> from skcomplex.systems.biology.evolution import CyclicInvasionInteraction
>>> CyclicInvasionInteraction('invasion', target='lattice').target
'lattice'
"""
def __init__(self, label: str, *, target: Role = 'lattice') -> None:
super().__init__(label)
self.target = target
@staticmethod
def _invade(strategy: Array, *around: Array, random: Array, **kwargs: object) -> Array:
"""Return the strategy of every site after invasion.
A random neighbour that beats a site invades it.
"""
choice = np.minimum((random * len(around)).astype(int), len(around) - 1)
neighbor = np.choose(choice, np.stack(around))
beaten = neighbor == np.mod(strategy + 1, STRATEGIES)
return np.where(beaten, neighbor, strategy)
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the invasion of the sites as the law.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
strategy = context.target.strategy
self.laws_: list[BaseLaw] = [
UpdateLaw(
strategy,
self._invade,
neighborhood=NEAREST,
schedule='random-sequential',
label=self.label,
stochastic=True,
),
]