Social impact theory
The social impact system on a full grid.
- Class:
SocialImpactSystem - Import:
from skcomplex.systems.social.opinion_dynamics import SocialImpactSystem - Field:
social/opinion_dynamics - Topics:
opinion,agents,grid,clusters - References:
- Nowak, Szamrej, and Latané 1990
- 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 SocialImpactSystem
trajectory = SocialImpactSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
width |
15 |
height |
15 |
share |
0.5 |
exponent |
2.0 |
noise |
0.0 |
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 | stochastic |
| Domains | integer, real |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class SocialImpactSystem(BaseSystem):
"""The social impact system on a full grid.
The persuasiveness and the supportiveness are random. The system has the group `people`.
Args:
width:
The number of columns.
height:
The number of rows.
share:
The share of agents with opinion 1, the others holding -1.
exponent:
The exponent of the distance by which impact decays.
noise:
The half-width of the uniform random field.
random_state:
The seed or generator of the opinions and strengths.
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.social.opinion_dynamics import SocialImpactSystem
>>> SocialImpactSystem(4, 5, random_state=0).prepare().system_.elements[0].size
20
"""
field = 'social/opinion_dynamics'
topics = ('opinion', 'agents', 'grid', 'clusters')
references = ('Nowak, Szamrej, and Latané 1990', 'Castellano, Fortunato, and Loreto 2009')
until = 20
def __init__(
self,
width: int = 15,
height: int = 15,
*,
share: float = 0.5,
exponent: float = 2.0,
noise: float = 0.0,
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.width = width
self.height = height
self.share = share
self.exponent = exponent
self.noise = noise
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)
rng = np.random.default_rng(self.random_state)
size = self.width * self.height
people = InfluenceeElement(
'people',
opinion=2 * SznajdSystem._get_shares(size, self.share, rng) - 1,
persuasiveness=rng.random(size),
supportiveness=rng.random(size),
exponent=self.exponent,
noise=self.noise,
x=np.arange(size) // self.height,
y=np.arange(size) % self.height,
size=size,
space=LatticeSpace((self.width, self.height), boundary='fixed', capacity=1),
)
self.system_ = System(
self.label,
elements=[people],
interactions=[SocialImpactInteraction('impact', target='people')],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
InfluenceeElement, fromElement
Interactions:
SocialImpactInteraction, fromInteraction
class InfluenceeElement(Element):
"""Agent of the social impact system.
It holds an opinion of -1 or 1, a persuasiveness, and a supportiveness.
Args:
label:
The label of the agent or group.
opinion:
The initial opinion, -1 or 1.
persuasiveness:
The strength with which the agent persuades agents of the other opinion.
supportiveness:
The strength with which the agent supports agents of its opinion.
exponent:
The exponent of the distance by which impact decays.
noise:
The half-width of the uniform random field added to the impact on every agent and step.
x:
The column of the cell of the agent on a lattice, one per agent or shared.
y:
The row of the cell of the agent on a lattice, one per agent or shared.
size:
The number of agents, or `None` for one.
space:
The lattice the agents live on, whose axes `x` and `y` make `x` and `y` their cell, or `None`.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import InfluenceeElement
>>> group = InfluenceeElement('people', opinion=-1.0, size=4)
>>> group.label, type(group).__name__, group.size
('people', 'InfluenceeElement', 4)
"""
visual = VisualSpec('agents', {'color_by': 'opinion', 'color_range': (-1.0, 1.0)})
def __init__(
self,
label: str,
*,
opinion: Annotated[object, ZZ] = 1.0,
persuasiveness: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
supportiveness: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
exponent: Annotated[object, RealSet.open(0, oo)] = 2.0,
noise: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
x: object = 0,
y: object = 0,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, size=size, space=space)
self.opinion = opinion
self.persuasiveness = persuasiveness
self.supportiveness = supportiveness
self.exponent = exponent
self.noise = noise
self.x = x
self.y = y
class SocialImpactInteraction(Interaction):
"""Synchronous update of the social impact system.
Every agent takes the opinion of the stronger impact. The impact on agent `i` is
`I_i = sum_j t_j (1 - o_i o_j) / d_ij^a - sum_j s_j (1 + o_i o_j) / d_ij^a`, with the persuasiveness `t`, the
supportiveness `s`, and the distance `d` on the grid, where an agent supports itself from distance 1. The impact is
the net pressure to change, whatever the opinion, so an agent flips its opinion when `I_i + h_i > 0`, with a uniform
random field `h_i`. Every agent decides from the opinions at the start of the step.
Args:
label:
The label of the interaction.
target:
The path of the group of agents.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import SocialImpactInteraction
>>> SocialImpactInteraction('impact', target='people').target
'people'
"""
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 update of every opinion, from the opinions at the start of the step, 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 every agent from the opinions at the start of the step."""
agents = cast(list[InfluenceeView], system.get_agents('target'))
opinions = [float(self._get_opinion(agent, system)) for agent in agents]
for agent, opinion in zip(agents, opinions, strict=True):
agent.opinion = opinion
def _get_impact(self, agent: InfluenceeView, system: SystemViewProtocol) -> float:
"""Return the net pressure to change on an agent from every agent of the group.
The agent itself counts from distance 1.
"""
agents = [item for item in system.get_agents('target') if isinstance(item, InfluenceeView)]
locations = np.array([item.location for item in agents], dtype=float)
distances = np.linalg.norm(locations - np.asarray(agent.location, dtype=float), axis=-1)
distances[[item.id == agent.id for item in agents]] = 1.0
weights = distances**-agent.exponent
agreement = agent.opinion * np.array([item.opinion for item in agents], dtype=float)
persuasion = np.array([item.persuasiveness for item in agents], dtype=float)
support = np.array([item.supportiveness for item in agents], dtype=float)
return float((weights * persuasion * (1 - agreement)).sum() - (weights * support * (1 + agreement)).sum())
def _get_opinion(self, agent: InfluenceeView, system: SystemViewProtocol) -> float:
"""Return the opposite opinion of an agent under a positive net pressure to change.
Otherwise it is its own.
"""
field = system.rng.uniform(-agent.noise, agent.noise)
return -agent.opinion if self._get_impact(agent, system) + field > 0 else float(agent.opinion)