Skip to content

Learning

Learning realizations shared by the economics packages.

They are the scores of options that agents learn from realized payoffs.

PayoffLearningInteraction(label, *, target, scores, payoff, rate=0.1, rule='average', phase=None, chosen=None, schedule=None)

Bases: Interaction

Payoff learning of the scores of options.

Economics names it reinforcement learning of payoffs, as in fictitious play and the attractions of experience-weighted learning. Each score moves towards the payoff its option realized in the step. Every step, the score s_k of option k of every member moves towards the realized payoff P_k at the rate a, s_k <- s_k + a (P_k - s_k), so a score is an exponentially weighted mean of past payoffs, and the last payoff at the rate 1, by the rule 'average'. By the rule 'cumulative' the score adds the payoff, s_k <- s_k + P_k, so it is the sum of past payoffs, such as a virtual score. Without a schedule it sets update laws, and payoff(namespace, k) returns the payoff of option k as a symbolic expression, where namespace.target holds the states and parameters of the group. With a schedule it sets one rule that walks the agents of the group in that order, and payoff(agent, system, k) returns the payoff as a number, so it can read the agent and the history and storage of the system view. Its scores may then be the components of one vector state, one per option, and a group of one member holds scores that several groups share. With chosen, the name of a state that holds the index of the option a member chose, only the score of that option moves, and the payoff of the other options is not read. Every agent reinforces at once under the schedule 'synchronous', one after the other in their order or in a new random order every step under 'sequential' or 'random-sequential', and each in its own activation under 'member', together with the rules of its element and the other member interactions, in the order the system lists them, such as after a choice and a transfer in one visit.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the element or group whose members hold the scores.

required
scores str | Sequence[str]

The names of the states that hold the score of every option, in the order of the options, or with a schedule the name of one vector state whose components are the scores.

required
payoff Callable[..., float | Expression]

The function payoff(namespace, k) without a schedule, or payoff(agent, system, k) with one, that returns the realized payoff of option k.

required
rate Annotated[float, closed(0, 1)]

The rate a at which the scores move towards the payoffs, between 0 and 1, by the rule 'average'.

0.1
rule str

'average' to move the scores towards the payoffs, or 'cumulative' to add the payoffs to the scores.

'average'
phase str | None

The phase of the step the rule of a schedule runs in, or None for the default phase.

None
chosen str | None

The name of the state that holds the index of the option a member chose, so only its score moves, or None to move every score.

None
schedule str | None

The order in which a rule walks the agents, 'synchronous' for every agent at once, 'sequential' or 'random-sequential' for one agent after the other, or 'member' for every agent in its own activation, or None for update laws of the scores of every member.

None

Raises:

Type Description
ValidationError

The rule or the schedule is unknown, there are no scores, or the payoff is not callable.

Examples:

>>> from skcomplex.systems.economics.common.learning import PayoffLearningInteraction
>>> reinforcement = PayoffLearningInteraction(
...     'reinforcement', target='learners', scores=('first', 'second'), payoff=lambda s, k: k, rate=0.5
... )
>>> reinforcement.rate
0.5
>>> PayoffLearningInteraction(
...     'virtual', target='players', scores='scores', payoff=lambda a, m, k: k, rule='cumulative',
...     schedule='synchronous',
... ).rule
'cumulative'
>>> PayoffLearningInteraction(
...     'satisfaction', target='customers', scores=('s1', 's2'), payoff=lambda a, m, k: 1.0, chosen='shop',
...     schedule='member',
... ).chosen
'shop'
Source code in src/skcomplex/systems/economics/common/learning/_payoff_learning.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def __init__(
    self,
    label: str,
    *,
    target: Role,
    scores: str | Sequence[str],
    payoff: Callable[..., float | Expression],
    rate: Annotated[float, RealSet.closed(0, 1)] = 0.1,
    rule: str = 'average',
    phase: str | None = None,
    chosen: str | None = None,
    schedule: str | None = None,
) -> None:
    super().__init__(label)
    self.target = target
    self.scores = scores
    self.payoff = payoff
    self.rate = rate
    self.rule = rule
    self.chosen = chosen
    self.phase = phase
    self.schedule = schedule

update(system)

Move the scores of every agent by the schedule.

Under 'synchronous' every agent reads the scores at the start of the rule, and otherwise the agents move one after the other, in their order or in a new random order.

Parameters:

Name Type Description Default
system SystemViewProtocol

The system view of the learning.

required
Source code in src/skcomplex/systems/economics/common/learning/_payoff_learning.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def update(self, system: SystemViewProtocol) -> None:
    """Move the scores of every agent by the schedule.

    Under `'synchronous'` every agent reads the scores at the start of the rule, and otherwise the agents move one
    after the other, in their order or in a new random order.

    Args:
        system:
            The system view of the learning.
    """
    agents = list(system.get_agents('target'))
    if self.schedule == 'synchronous':
        values = [self._get_scores(agent, system) for agent in agents]
        for agent, value in zip(agents, values, strict=True):
            self._set_value(agent, value)
        return
    order = system.rng.permutation(len(agents)) if self.schedule == 'random-sequential' else range(len(agents))
    for index in order:
        self._update_member(agents[int(index)], system)