Random failures and targeted attacks on networks
Failures or attacks on a scale-free network.
- Class:
NetworkAttackSystem - Import:
from skcomplex.systems.mathematics.networks import NetworkAttackSystem - Field:
mathematics/networks - Topics:
network,robustness,percolation,attack,scale-free - References:
- Albert, Jeong, and Barabási 2000
- Cohen et al. 2000
- Ramasco 2020
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.mathematics.networks import NetworkAttackSystem
trajectory = NetworkAttackSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_nodes |
300 |
edges |
2 |
strategy |
'targeted' |
rate |
0.01 |
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 |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class NetworkAttackSystem(BaseSystem):
"""Failures or attacks on a scale-free network.
The network is robust to random failures but fragile to attacks on hubs.
The network is a Barabási-Albert graph. Under random failures every node fails with probability `rate` per step, and
the giant component shrinks slowly. Under a targeted attack the nodes of the largest degree are removed every step,
and the giant component breaks up after a few percent of the nodes are gone. The system has the groups `nodes` and
`links`.
Args:
n_nodes:
The number of nodes.
edges:
The number of edges every node of the Barabási-Albert graph joins with.
strategy:
`'random'` for random failures or `'targeted'` for attacks on the largest degree.
rate:
The probability that a node fails in a step under random failures.
random_state:
The seed or generator of the network.
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.
Raises:
ValidationError:
The strategy is unknown.
Examples:
>>> from skcomplex.systems.mathematics.networks import NetworkAttackSystem
>>> trajectory = NetworkAttackSystem(50).prepare().simulate(until=3, random_state=0)
>>> int(trajectory.get_network(-1).nodes.size) < 50
True
"""
field = 'mathematics/networks'
topics = ('network', 'robustness', 'percolation', 'attack', 'scale-free')
references = ('Albert, Jeong, and Barabási 2000', 'Cohen et al. 2000', 'Ramasco 2020')
until = 100
def __init__(
self,
n_nodes: int = 300,
*,
edges: int = 2,
strategy: str = 'targeted',
rate: float = 0.01,
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_nodes = n_nodes
self.edges = edges
self.strategy = strategy
self.rate = rate
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the Barabási-Albert network and the removal of its nodes.
Args:
context:
The observables to compile with the problem, or `None` for none.
Raises:
ValidationError:
The strategy is unknown.
"""
super()._prepare(context)
if self.strategy not in STRATEGIES:
error_msg = f'Unknown strategy {self.strategy!r}. Use one of {", ".join(STRATEGIES)}.'
raise ValidationError(error_msg)
space = NetworkSpace(nx.barabasi_albert_graph(self.n_nodes, self.edges, seed=self.random_state))
removal = AttackInteraction('removal', target='nodes', rate=self.rate, targeted=self.strategy == 'targeted')
self.system_ = System(self.label, elements=[FailingElement('nodes', space=space)], interactions=[removal])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
FailingElement, fromElement
Interactions:
AttackInteraction, fromInteraction
class FailingElement(Element):
"""Nodes of a network under failures or attacks, each with its degree `degree`.
Args:
label:
The label of the nodes.
degree:
The initial number of edges of every node, one per node or shared.
space:
The network with one node per site, or `None` for one node.
Examples:
>>> from skcomplex.systems.mathematics.networks import FailingElement
>>> FailingElement('nodes', degree=3.0).degree
3.0
"""
visual = DEGREE_VISUAL
def __init__(
self,
label: str,
*,
degree: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, space=space)
self.degree = degree
class AttackInteraction(Interaction):
"""Removal of the nodes of a network by random failures or by an attack on the nodes of the largest degree.
The degree of every node is read from the edges at the start of every step. Under random failures every node fails
with the probability `rate` per step, and under a targeted attack the nodes of the largest degree are removed, one
degree class per step.
Args:
label:
The label of the interaction.
target:
The nodes whose degrees the interaction sets and that fail.
rate:
The probability that a node fails in a step under random failures.
targeted:
Whether the nodes of the largest degree are attacked, in place of random failures.
Examples:
>>> from skcomplex.systems.mathematics.networks import AttackInteraction
>>> AttackInteraction('removal', targeted=False).targeted
False
"""
law_classes = (UpdateLaw,)
draws = True
def __init__(self, label: str, *, target: Role = 'nodes', rate: float = 0.01, targeted: bool = True) -> None:
super().__init__(label)
self.target = target
self.rate = rate
self.targeted = targeted
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the degrees and the removals as the laws.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
degree = neighbors(1)
removal = where(degree - maximum(degree) >= 0, 1, 0) if self.targeted else context.rate
self.laws_: list[BaseLaw] = [
UpdateLaw(context.target.degree, degree),
UpdateLaw(TopologyRequestSpec('remove-node'), removal),
]