Barabási priority queues
The Barabási priority queue.
- Class:
PriorityQueuesSystem - Import:
from skcomplex.systems.social.organizations import PriorityQueuesSystem - Field:
social/organizations - Topics:
queues,agents,bursts,power law - References:
- Barabási 2005
- 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.organizations import PriorityQueuesSystem
trajectory = PriorityQueuesSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_tasks |
2 |
greed |
0.99999 |
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 | none |
| 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 PriorityQueuesSystem(BaseSystem):
"""The Barabási priority queue.
Its waiting times follow a power law as the choice becomes greedy. The system has the group `tasks` and the
execution `execution`, which reports the waiting time of the task it executes at every step.
Args:
n_tasks:
The length of the list of tasks.
greed:
The probability that the highest priority task is executed rather than a random one.
random_state:
The seed or generator of the initial priorities.
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.organizations import PriorityQueuesSystem
>>> PriorityQueuesSystem(3, random_state=0).prepare().system_.elements[0].size
3
"""
field = 'social/organizations'
topics = ('queues', 'agents', 'bursts', 'power law')
references = ('Barabási 2005', 'Castellano, Fortunato, and Loreto 2009')
until = 200
def __init__(
self,
n_tasks: int = 2,
*,
greed: float = 0.99999,
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.n_tasks = n_tasks
self.greed = greed
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)
priorities = np.random.default_rng(self.random_state).random(self.n_tasks)
tasks = TaskElement('tasks', priority=priorities, greed=self.greed, size=self.n_tasks)
self.system_ = System(
self.label,
elements=[tasks],
interactions=[ExecutionInteraction('execution', target='tasks')],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
TaskElement, fromElement
Interactions:
ExecutionInteraction, fromInteraction
class TaskElement(Element):
"""Task of the Barabási priority queue.
It holds a priority, an age, and the waiting time when it was last executed.
Args:
label:
The label of the agent or group.
priority:
The priority, uniform on 0 to 1 for a new task.
age:
The number of steps the task has waited.
wait:
The waiting time of the last task executed from this slot of the list.
greed:
The probability that the highest priority task is executed rather than a random one.
size:
The number of members, or `None` for one.
Examples:
>>> from skcomplex.systems.social.organizations import TaskElement
>>> group = TaskElement('tasks', priority=[0.2, 0.7], size=2)
>>> group.label, type(group).__name__, group.size
('tasks', 'TaskElement', 2)
"""
visual = VisualSpec('histograms', {'color_by': 'age', 'shown': ('agents',)})
def __init__(
self,
label: str,
*,
priority: Annotated[object, RealSet.closed_open(0, oo)] = 0.5,
age: Annotated[object, NN] = 0.0,
wait: Annotated[object, NN] = 0.0,
greed: Annotated[object, RealSet.closed_open(0, oo)] = 0.99999,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.priority = priority
self.age = age
self.wait = wait
self.greed = greed
class ExecutionInteraction(Interaction):
"""Execution of one task per step from the list of the Barabási system.
The highest priority task is executed with probability `greed` and a random task otherwise. The executed task is
replaced by a new task with a uniform random priority, and every other task ages by one step. The waiting time of
the executed task is reported as `wait`, so the run records one waiting time per step.
Args:
label:
The label of the interaction.
target:
The path of the group of tasks.
Examples:
>>> from skcomplex.systems.social.organizations import ExecutionInteraction
>>> ExecutionInteraction('execution', target='tasks').target
'tasks'
"""
draws = True
law_classes = (UpdateLaw,)
reports: ClassVar[tuple[str, ...]] = ('wait',)
def __init__(
self,
label: str,
*,
target: Role,
) -> None:
super().__init__(label)
self.target = target
def _prepare(self, s: SimpleNamespace) -> None:
"""Set the execution rule.
Args:
s:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(s)
self.laws_: list[BaseLaw] = [UpdateLaw(s, self.execute)]
def execute(self, system: SystemView) -> None:
"""Execute one task and replace it with a new one.
Args:
system:
The system.
"""
tasks = cast(list[TaskView], system.get_agents(self.target))
if not tasks:
return
priorities = [task.priority for task in tasks]
greedy = system.rng.random() < tasks[0].greed
chosen = int(np.argmax(priorities)) if greedy else int(system.rng.integers(len(tasks)))
for index, task in enumerate(tasks):
task.age += 1
if index == chosen:
task.wait, task.age, task.priority = task.age, 0.0, float(system.rng.random())
system.report('wait', task.wait)
Equations
The equations are those that symbolic returns for the system with its default parameters.