Cognitive consistency agents
Agents whose attitudes spread by conversation and settle in a belief network.
- Class:
CognitiveConsistencySystem - Import:
from skcomplex.systems.social.opinion_dynamics import CognitiveConsistencySystem - Field:
social/opinion_dynamics - Topics:
opinion,agents,network,cognition - References:
- Lakkaraju and Speed 2019
- Carmichael et al. 2019
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 CognitiveConsistencySystem
trajectory = CognitiveConsistencySystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
graph |
None |
positive |
0.7 |
decay |
0.05 |
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 | real, vector |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class CognitiveConsistencySystem(BaseSystem):
"""Agents whose attitudes spread by conversation and settle in a belief network.
The belief network seeks consistency. The system has the group `thinkers`.
Args:
graph:
The NetworkX graph, or `None` for a complete graph of 40 agents.
positive:
The share of agents that start in the positive state, F1 = X = 1 and F2 = -1, the others starting in the
negative state.
decay:
The fraction of every valence lost per update of the belief network.
random_state:
The seed or generator of the initial states.
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 CognitiveConsistencySystem
>>> system = CognitiveConsistencySystem(nx.complete_graph(6), random_state=0).prepare().system_
>>> system.elements[0].size
6
"""
field = 'social/opinion_dynamics'
topics = ('opinion', 'agents', 'network', 'cognition')
references = ('Lakkaraju and Speed 2019', 'Carmichael et al. 2019')
until = 30
def __init__(
self,
graph: object = None,
*,
positive: float = 0.7,
decay: float = 0.05,
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.positive = positive
self.decay = decay
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.complete_graph(40) if graph is None else graph
size = nx.number_of_nodes(graph)
signs = np.where(np.random.default_rng(self.random_state).random(size) < self.positive, 1.0, -1.0)
valences = signs[:, None] * np.array(POSITIVE)
thinkers = ThinkerElement('thinkers', valences=valences, attitude=valences[:, 1], decay=self.decay, size=size)
self.system_ = System(
self.label,
elements=[thinkers],
interactions=[ConversationsInteraction('talks', target='thinkers')],
space=NetworkSpace(graph),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
ThinkerElement, fromElement
Interactions:
ConversationsInteraction, fromInteraction
class ThinkerElement(Element):
"""Agent of the cognitive consistency system.
It holds the valences of the concepts of its belief network.
Args:
label:
The label of the agent or group.
valences:
The initial valences of the concepts F1, X, and F2, between -1 and 1.
attitude:
The attitude of the agent, the valence of the concept X once the belief network settles.
decay:
The fraction of every valence lost per update of the belief network.
size:
The number of members, or `None` for one.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import ThinkerElement
>>> group = ThinkerElement('thinkers', valences=[1.0, 1.0, -1.0], size=5)
>>> group.label, type(group).__name__, group.size
('thinkers', 'ThinkerElement', 5)
"""
visual = VisualSpec('agents', {'color_by': 'attitude', 'color_range': (-1.0, 1.0)})
def __init__(
self,
label: str,
*,
valences: npt.ArrayLike = 0.0,
attitude: object = 0.0,
decay: Annotated[object, RealSet.closed_open(0, oo)] = 0.05,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.valences = valences
self.attitude = attitude
self.decay = decay
class ConversationsInteraction(NeighborhoodsInteraction):
"""Conversations of the cognitive consistency system.
There is one per agent and step, the agents speaking in random order. A speaker talks to a random neighbour about F1
or F2. The hearer averages its valence of the topic with the speaker's and settles its belief network, and its
attitude is its valence of X.
Args:
label:
The label of the interaction.
target:
The path of the group of thinkers.
phase:
The phase of the step the conversations run in, or `None` for the default phase.
Examples:
>>> from skcomplex.systems.social.opinion_dynamics import ConversationsInteraction
>>> ConversationsInteraction('talks', target='thinkers').target
'thinkers'
"""
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:
"""Let the first member speak to a random neighbour.
The neighbour averages the topic and settles its beliefs.
Args:
members:
The speaker, followed by its neighbours.
system:
The system view of the rule.
"""
speaker, *neighbors = cast(list[ThinkerView], members)
if not neighbors:
return
hearer = neighbors[int(system.rng.integers(len(neighbors)))]
topic = TOPICS[int(system.rng.integers(len(TOPICS)))]
valences = np.array(hearer.valences, dtype=np.float64)
valences[topic] = (valences[topic] + np.asarray(speaker.valences, dtype=np.float64)[topic]) / 2
settled = settle(valences, hearer.decay)
hearer.valences, hearer.attitude = settled, float(settled[1])