Skip to content

Majority rule automaton

Voters that take the majority opinion of their neighbourhood.

  • Class: MajorityRuleSystem
  • Import: from skcomplex.systems.social.opinion_dynamics import MajorityRuleSystem
  • Field: social/opinion_dynamics
  • Topics: lattice, opinion, voting
  • References:
    • Argun et al. 2021
    • Gärtner and Zehmakan 2017

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 MajorityRuleSystem

trajectory = MajorityRuleSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
shape (100, 100)
density 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 lattice
Laws update
Randomness unknown
Domains boolean
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class MajorityRuleSystem(BaseSystem):
    """Voters that take the majority opinion of their neighbourhood.

    The opinions coarsen into domains. Every step every voter takes the opinion of the majority of its eight Moore
    neighbours and keeps its own when they tie, the majority rule of Argun et al. (2021, section 4.3). The system has
    the opinions `voters.opinion`.

    Args:
        shape:
            The number of rows and columns.

        density:
            The probability that a voter starts with opinion 1.

        random_state:
            The seed or generator of the initial 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:
        >>> from skcomplex.systems.social.opinion_dynamics import MajorityRuleSystem
        >>> MajorityRuleSystem((10, 10)).prepare().system_.elements[0].opinion.size
        100
    """

    field = 'social/opinion_dynamics'
    topics = ('lattice', 'opinion', 'voting')
    references = ('Argun et al. 2021', 'Gärtner and Zehmakan 2017')
    until = 50
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (100, 100),
        *,
        density: 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.shape = shape
        self.density = density
        self.random_state = random_state

    @staticmethod
    def _get_random_cells(
        shape: Sequence[int],
        density: float,
        random_state: int | np.random.Generator | None,
    ) -> Array:
        """Return cells that are 1 with a probability and 0 otherwise."""
        return (np.random.default_rng(random_state).random(tuple(shape)) < density).astype(float)

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components built from the model parameters."""
        super()._prepare(context)
        values = self._get_random_cells(self.shape, self.density, self.random_state)
        self.system_ = System(
            self.label,
            elements=[
                MajorityVoterElement('voters', opinion=values, space=LatticeSpace(tuple(self.shape))),
            ],
            interactions=[
                MajorityRuleInteraction('majority', target='voters'),
            ],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • MajorityVoterElement, from Element

Interactions:

  • MajorityRuleInteraction, from Interaction
class MajorityVoterElement(Element):
    """Cell that takes the majority opinion of its Moore neighbourhood.

    The neighbourhood includes the cell itself. The majority rule is a `MajorityRuleInteraction` of the lattice, which
    `MajorityRuleSystem` adds.

    Args:
        label:
            The label of the cell or the lattice group.

        opinion:
            The initial opinion, 0 or 1.

        space:
            The space with one member per site, or `None` for one member.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import MajorityVoterElement
        >>> MajorityVoterElement('cell', opinion=1).opinion
        1
    """

    def __init__(
        self,
        label: str,
        *,
        opinion: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.opinion = opinion

class MajorityRuleInteraction(Interaction):
    """Majority rule of voters on a lattice: every voter takes the opinion of most of its Moore neighbourhood.

    The neighbourhood holds the voter and its eight neighbours, so it has no ties, and a voter takes opinion 1 when at
    least five of the nine hold it.

    Args:
        label:
            The label of the interaction.

        target:
            The voters whose state `opinion` the rule updates.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import MajorityRuleInteraction
        >>> MajorityRuleInteraction('majority', target='voters').target
        'voters'
    """

    def __init__(self, label: str, *, target: Role = 'voters') -> None:
        super().__init__(label)
        self.target = target

    @staticmethod
    def _is_majority(total: IntArray) -> npt.NDArray[np.bool_]:
        """Return whether the opinions 1 of a Moore neighbourhood are a majority.

        The neighbourhood has nine cells.
        """
        return total >= MAJORITY

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the majority rule of the opinions as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        opinion = context.target.opinion
        rule = TotalisticSpec(self._is_majority)
        self.laws_: list[BaseLaw] = [UpdateLaw(opinion, rule, neighborhood=MOORE, label=self.label)]