Network

class phantom.network.Network(agents=None, resolver=None, connections=None, ignore_connection_errors=False)[source]

P2P messaging network.

This class is responsible for monitoring connections and tracking state/flow between adjacent agents in a peer-to-peer network. The underlying representation is based on dictionaries via the NetworkX library.

Parameters:
  • agents (Optional[Iterable[Agent]]) – Optional list of agents to add to the network.

  • resolver (Optional[Resolver]) – Optional custom resolver to use, by default will use the BatchResolver with a round_limit of 2.

  • connections (Optional[Iterable[Tuple[Hashable, Hashable]]]) – Optional initial list of connections to create in the network.

  • ignore_connection_errors (bool) – If True will not raise errors if an attempt is made to send a message along an non-existant connection.

agents

Mapping between IDs and the corresponding agents in the network.

graph

Directed graph modelling the connections between agents.

add_agent(agent)[source]

Add a new agent node to the network.

Parameters:

agent (Agent) – The new agent instance type to be added.

Return type:

None

add_agents(agents)[source]

Add new agent nodes to the network.

Parameters:

agents (Iterable[Agent]) – An iterable object over the agents to be added.

Return type:

None

add_connection(u, v)[source]

Connect the agents with IDs u and v.

Parameters:
Return type:

None

add_connections_between(us, vs)[source]

Connect all agents in us to all agents in vs.

Parameters:
Return type:

None

add_connections_from(ebunch)[source]

Connect all agent ID pairs in ebunch.

Parameters:

ebunch (Iterable[Tuple[Hashable, Hashable]]) – Pairs of vertices to be connected.

Return type:

None

add_connections_with_adjmat(agent_ids, adjacency_matrix)[source]

Connect a subset of agents to one another via an adjacency matrix.

Parameters:
  • agent_ids (Sequence[Hashable]) – Sequence of agent IDs that correspond to each dimension of the adjacency matrix.

  • adjacency_matrix (ndarray) – A square, symmetric, hollow matrix with entries in {0, 1}. A value of 1 indicates a connection between two agents.

Return type:

None

property agent_ids: KeysView[Hashable]

Iterator over the IDs of active agents in the network.

context_for(agent_id, env_view)[source]

Returns the local context for agent agent_id.

Here we define a neighbourhood as being the first-order ego-graph with agent_id set as the focal node.

Parameters:

agent_id (Hashable) – The ID of the focal agent.

Return type:

Context

get_agents_where(pred)[source]

Returns the set of agents in the network that satisfy a predicate.

Parameters:

pred (Callable[[Agent], bool]) – The filter predicate; should return True iff the agent should be included in the set. This method is akin to the standard Python function filter.

Return type:

Dict[Hashable, Agent]

get_agents_with_type(agent_type)[source]

Returns a collection of agents in the network with a given type.

Parameters:

agent_type (Type) – The class type of agents to include in the set.

Return type:

Dict[Hashable, Agent]

get_agents_without_type(agent_type)[source]

Returns a collection of agents in the network without a given type.

Parameters:

agent_type (Type) – The class type of agents you want to exclude.

Return type:

Dict[Hashable, Agent]

has_edge(sender_id, receiver_id)[source]

Returns whether two agents are connected.

Parameters:
  • sender_id (Hashable) – The sender ID.

  • receiver_id (Hashable) – The receiver ID.

Return type:

bool

reset()[source]

Reset the message queues along each edge.

Return type:

None

resolve(contexts)[source]

Resolve all messages in the network and clear volatile memory.

Parameters:

contexts (Mapping[Hashable, Context]) – The current contexts for all agents for the current step.

Return type:

None

send(sender_id, receiver_id, payload)[source]

Send message batches across the network.

Parameters:
  • sender_id (Hashable) – The sender ID.

  • receiver_id (Hashable) – The receiver ID.

  • payload (MsgPayload) – The contents of the message.

Return type:

None

subnet_for(agent_id)[source]

Returns a Sub Network associated with a given agent

Parameters:

agent_id (Hashable) – The ID of the focal agent

Return type:

Network

class phantom.network.StochasticNetwork(agents=None, resolver=None, connections=None, ignore_connection_errors=False)[source]

Stochastic P2P messaging network.

This class builds on the base Network class but adds the ability to resample the connectivity of all connections.

Parameters:
  • agents (Optional[Iterable[Agent]]) – Optional list of agents to add to the network.

  • resolver (Optional[Resolver]) – Optional custom resolver to use, by default will use the BatchResolver with a round_limit of 2.

  • connections (Optional[Iterable[Tuple[Hashable, Hashable]]]) – Optional initial list of connections to create in the network.

  • ignore_connection_errors (bool) – If True will not raise errors if an attempt is made to send a message along an non-existant connection.

agents

Mapping between IDs and the corresponding agents in the network.

graph

Directed graph modelling the connections between agents.

add_agent(agent)

Add a new agent node to the network.

Parameters:

agent (Agent) – The new agent instance type to be added.

Return type:

None

add_agents(agents)

Add new agent nodes to the network.

Parameters:

agents (Iterable[Agent]) – An iterable object over the agents to be added.

Return type:

None

add_connection(u, v, rate=1.0)[source]

Connect the agents with IDs u and v.

Parameters:
  • u (Hashable) – One agent’s ID.

  • v (Hashable) – The other agent’s ID.

  • rate (float) – The connectivity of this connection.

Return type:

None

add_connections_between(us, vs, rate=1.0)[source]

Connect all agents in us to all agents in vs.

Parameters:
Return type:

None

add_connections_from(ebunch)[source]

Connect all agent ID pairs in ebunch.

Parameters:

ebunch (Iterable[Union[Tuple[Hashable, Hashable], Tuple[Hashable, Hashable, float]]]) – Pairs of vertices to be connected.

Return type:

None

add_connections_with_adjmat(agent_ids, adjacency_matrix)

Connect a subset of agents to one another via an adjacency matrix.

Parameters:
  • agent_ids (Sequence[Hashable]) – Sequence of agent IDs that correspond to each dimension of the adjacency matrix.

  • adjacency_matrix (ndarray) – A square, symmetric, hollow matrix with entries in {0, 1}. A value of 1 indicates a connection between two agents.

Return type:

None

property agent_ids: KeysView[Hashable]

Iterator over the IDs of active agents in the network.

context_for(agent_id, env_view)

Returns the local context for agent agent_id.

Here we define a neighbourhood as being the first-order ego-graph with agent_id set as the focal node.

Parameters:

agent_id (Hashable) – The ID of the focal agent.

Return type:

Context

get_agents_where(pred)

Returns the set of agents in the network that satisfy a predicate.

Parameters:

pred (Callable[[Agent], bool]) – The filter predicate; should return True iff the agent should be included in the set. This method is akin to the standard Python function filter.

Return type:

Dict[Hashable, Agent]

get_agents_with_type(agent_type)

Returns a collection of agents in the network with a given type.

Parameters:

agent_type (Type) – The class type of agents to include in the set.

Return type:

Dict[Hashable, Agent]

get_agents_without_type(agent_type)

Returns a collection of agents in the network without a given type.

Parameters:

agent_type (Type) – The class type of agents you want to exclude.

Return type:

Dict[Hashable, Agent]

has_edge(sender_id, receiver_id)

Returns whether two agents are connected.

Parameters:
  • sender_id (Hashable) – The sender ID.

  • receiver_id (Hashable) – The receiver ID.

Return type:

bool

reset()[source]

Reset the message queues along each edge.

Return type:

None

resolve(contexts)

Resolve all messages in the network and clear volatile memory.

Parameters:

contexts (Mapping[Hashable, Context]) – The current contexts for all agents for the current step.

Return type:

None

send(sender_id, receiver_id, payload)

Send message batches across the network.

Parameters:
  • sender_id (Hashable) – The sender ID.

  • receiver_id (Hashable) – The receiver ID.

  • payload (MsgPayload) – The contents of the message.

Return type:

None

subnet_for(agent_id)

Returns a Sub Network associated with a given agent

Parameters:

agent_id (Hashable) – The ID of the focal agent

Return type:

Network

class phantom.network.NetworkError[source]
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.