Voter
A voter system with one voter per node of a network.
- Class:
VoterSystem - Import:
from skcomplex.systems.social.opinion_dynamics import VoterSystem - Field:
social/opinion_dynamics - Topics:
opinion,agents,network - References:
- Holley and Liggett 1975
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 VoterSystem
trajectory = VoterSystem().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 VoterSystem(BaseSystem):
"""A voter system with one voter per node of a network.
The system has the group `voters`.
Args:
graph:
The NetworkX graph, or `None` for a ring of 50 nodes each linked to its four nearest neighbours.
share:
The share of voters 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 VoterSystem
>>> VoterSystem(nx.cycle_graph(8), random_state=0).prepare().system_.elements[0].size
8
"""
field = 'social/opinion_dynamics'
topics = ('opinion', 'agents', 'network')
references = ('Holley and Liggett 1975',)
until = 100
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
rng = np.random.default_rng(self.random_state)
graph = nx.watts_strogatz_graph(50, 4, 0.0) if graph is None else graph
size = nx.number_of_nodes(graph)
opinions = (rng.random(size) < self.share).astype(float)
self.system_ = System(
self.label,
elements=[VoterElement('voters', opinion=opinions, size=size)],
interactions=[VoterImitationInteraction('imitation', target='voters')],
space=NetworkSpace(graph),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
VoterElement, fromElement
Interactions:
VoterImitationInteraction, fromInteraction
class VoterElement(Element):
"""Agent of the voter system on a network.
The imitation sets its opinion to that of a random neighbour.
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 VoterElement
>>> group = VoterElement('voters', opinion=1.0, size=20)
>>> group.label, type(group).__name__, group.size
('voters', 'VoterElement', 20)
"""
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 VoterImitationInteraction(Interaction):
"""Imitation of the voter system.
Every voter in turn adopts the opinion of a random neighbour. The voters update one after the other in a new random
order every step, each reading the opinions the voters before it set. A voter without a neighbouring voter keeps its
opinion.
Args:
label:
The label of the interaction.
target:
The path of the group of voters.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import VoterImitationInteraction
>>> VoterImitationInteraction('imitation', target='voters').target
'voters'
"""
law_classes = (UpdateLaw,)
draws = True
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the opinion of the agents, one after the other in a new random order, as the rule of the system.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
self.laws_: list[BaseLaw] = [UpdateLaw(context, self._update)]
def _update(self, system: SystemView) -> None:
"""Set the opinion of the agents one after the other in a new random order."""
agents = cast(list[VoterView], system.get_agents('target'))
for index in system.rng.permutation(len(agents)):
agent = agents[int(index)]
if agent.alive:
agent.opinion = float(self._get_opinion(agent, system))
def _get_opinion(self, agent: VoterView, system: SystemViewProtocol) -> float:
"""Return the opinion of a random neighbouring voter.
Without one, it is the voter's own.
"""
neighbors = cast(list[VoterView], [item for item in agent.get_neighbors() if item.group == agent.group])
if not neighbors:
return float(agent.opinion)
return float(neighbors[int(system.rng.integers(len(neighbors)))].opinion)