Sznajd
The Sznajd system, where agreeing pairs of agents convince their neighbours.
- Class:
SznajdSystem - Import:
from skcomplex.systems.social.opinion_dynamics import SznajdSystem - Field:
social/opinion_dynamics - Topics:
opinion,agents,network,consensus - References:
- Sznajd-Weron and Sznajd 2000
- Castellano, Fortunato, and Loreto 2009
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.social.opinion_dynamics import SznajdSystem
trajectory = SznajdSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
graph |
None |
share |
0.5 |
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 | 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 SznajdSystem(BaseSystem):
"""The Sznajd system, where agreeing pairs of agents convince their neighbours.
The system has the group `people` and the persuasion `persuasion`.
Args:
graph:
The NetworkX graph, or `None` for a ring of 50 agents.
share:
The share of agents with opinion 1.
random_state:
The seed or generator of the opinions.
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.social.opinion_dynamics import SznajdSystem
>>> SznajdSystem(nx.cycle_graph(8), random_state=0).prepare().system_.elements[0].size
8
"""
field = 'social/opinion_dynamics'
topics = ('opinion', 'agents', 'network', 'consensus')
references = ('Sznajd-Weron and Sznajd 2000', 'Castellano, Fortunato, and Loreto 2009')
until = 100
@staticmethod
def _get_shares(size: int, share: float, random_state: int | np.random.Generator | None) -> Array:
"""Return opinions that are 1 with a probability and 0 otherwise."""
return (np.random.default_rng(random_state).random(size) < share).astype(float)
def __init__(
self,
graph: object = None,
*,
share: float = 0.5,
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.graph = graph
self.share = share
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to the components built from the model parameters."""
super()._prepare(context)
graph = self.graph
graph = nx.cycle_graph(50) if graph is None else graph
size = nx.number_of_nodes(graph)
people = PersuaderElement(
'people',
opinion=SznajdSystem._get_shares(size, self.share, self.random_state),
size=size,
)
self.system_ = System(
self.label,
elements=[people],
interactions=[PersuasionInteraction('persuasion', target='people')],
space=NetworkSpace(graph),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
PersuaderElement, fromElement
Interactions:
PersuasionInteraction, fromInteraction
class PersuaderElement(Element):
"""Agent of the Sznajd system, who convinces the neighbours of an agreeing pair.
A step of an agent picks a random neighbour, and when both share an opinion every neighbour of either adopts it.
On a ring this is the original one-dimensional rule, where the pair convinces the two agents beside it.
Args:
label:
The label of the agent or group.
opinion:
The initial opinion, 0 or 1.
size:
The number of members, or `None` for one.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import PersuaderElement
>>> group = PersuaderElement('people', opinion=1.0, size=10)
>>> group.label, type(group).__name__, group.size
('people', 'PersuaderElement', 10)
"""
visual = VisualSpec('agents', {'color_by': 'opinion', 'color_range': (0.0, 1.0)})
def __init__(
self,
label: str,
*,
opinion: Annotated[object, NN] = 0.0,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.opinion = opinion
class PersuasionInteraction(NeighborhoodsInteraction):
"""Persuasion of the Sznajd system.
An agreeing pair of neighbours convinces the neighbours of both. Every step every agent, in random order, picks a
random neighbour, and when both share an opinion every neighbour of either adopts it. On a ring this is the original
one-dimensional rule, where the pair convinces the two agents beside it.
Args:
label:
The label of the interaction.
target:
The path of the group of persuaders.
phase:
The phase of the step the persuasion runs in, or `None` for the default phase.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import PersuasionInteraction
>>> PersuasionInteraction('persuasion', target='people').target
'people'
"""
def __init__(
self,
label: str,
*,
target: Role,
phase: str | None = None,
) -> None:
super().__init__(label, target=target, phase=phase)
def act_on(self, members: list[AgentViewProtocol], system: SystemView) -> None:
"""Convince the neighbours of the first member and of a random neighbour of it.
This happens when the two agree.
Args:
members:
The persuader, followed by its neighbours.
system:
The system view of the rule.
"""
agent, *neighbors = cast(list[PersuaderView], members)
if not neighbors:
return
partner = neighbors[int(system.rng.integers(len(neighbors)))]
if partner.opinion != agent.opinion:
return
for other in [*neighbors, *partner.get_neighbors()]:
if isinstance(other, PersuaderView):
other.opinion = agent.opinion