Network

class Melodie.network.NetworkAgent(agent_id: int)

Bases: Agent

Base Class for Agent s connected with Network.

category

Category indicating agent class, an int.

set_category()

Set the category of network agent.

Returns:

class Melodie.network.Edge(category_1: int, agent_1_id: int, category_2: int, agent_2_id: int, edge_properties: Dict[str, int | float | str | bool])

Bases: object

Base Class for Edges s on the Network.

Edge objects links the agents, and could store custom parameters.

Parameters:
  • category_1 – Source agent category

  • agent_1_id – Source agent id

  • category_2 – Target agent category

  • agent_2_id – Target agent id

  • edge_properties – a dict storing properties.

Returns:

None

setup()

Setup method. Be sure to inherit it for custom Edge.

Returns:

None

post_setup()

This method is executed after setup()

Returns:

None

class Melodie.network.Network(model=None, edge_cls: Type[Edge] | None = None, directed=False, name='network')

Bases: object

Base class for the Network. Network is constructed with edge, describes the network (if exists) that links the agents, and provides the relevant functions.

Parameters:
  • model – Current model instance.

  • edge_cls – Class of Edges in this Network.

  • directed – If this network is directed, else it is undirected.

  • name – The name for this network, used in saving and reading the layout file.

setup()

Setup function of network. Be sure to inherit this method for custom implementation.

Returns:

None

add_edge(source_id: Tuple[int, int], target_id: Tuple[int, int], edge: Edge)

Add an edge onto the network.

Parameters:
  • source_id – A tuple <agent_category, agent_id> stands for source agent.

  • target_id – A tuple <agent_category, agent_id> stands for target agent.

  • edge – An Edge object

Returns:

None

get_edge(source_id: Tuple[int, int], target_id: Tuple[int, int])

Get an edge from the network

Parameters:
  • source_id – A tuple <agent_category, agent_id> stands for source agent.

  • target_id – A tuple <agent_category, agent_id> stands for target agent.

Returns:

An Edge object

remove_edge(source_id: Tuple[int, int], target_id: Tuple[int, int])

Remove an edge from the network

Parameters:
  • source_id – A tuple <agent_category, agent_id> stands for source agent.

  • target_id – A tuple <agent_category, agent_id> stands for target agent.

Returns:

None

get_neighbors(agent: Agent)

Get surrounding neighbors of agent.

Parameters:

agentNetworkAgent object.

Returns:

remove_agent(agent: Agent)

Remove agent from network.

Parameters:

agentNetworkAgent

Returns:

None

add_agent(agent: Agent)

Add an agent onto the network.

Parameters:

agentNetworkAgent object

Returns:

create_edge(agent_1_id: int, category_1: int, agent_2_id: int, category_2: int, **edge_properties)

Create a new edge from one agent to another agent.

Parameters:
  • agent_1_idid of source agent

  • category_1category of source agent

  • agent_2_idid of target agent

  • category_2category of target agent

  • edge_properties – keyword arguments for edge properties.

Returns:

all_agents() Set[Tuple[int, int]]

Get all agents on the network.

Returns:

A list of (Agent category, Agent id)

get_node_edges(agent: Agent)

Get the edges from one node.

Parameters:

agentNetworkAgent object

Returns:

A list of Edge object

setup_layout_creator(layout_creator: Callable[[nx.Graph], Dict[NodeType, np.ndarray]])

Set the creator function to create network layout.

The input argument of layout_creator is a networkx graph, and the output was a dict type mapping from node id to position.

For example: .. code-block:: python

import networkx as nx n = Network() n.setup_layout_creator(lambda G: nx.spring_layout(G))

Parameters:

layout_creator – a callable creating network layout as the code example.

Returns:

None

update_layout()

Update the layout of network.

Returns:

None

get_position(agent_category: int, agent_id: int)

Get the position of agent.

Parameters:
  • agent_category – The category of agent

  • agent_id – The id of agent

Returns:

setup_agent_connections(agent_lists: List[AgentList], network_type: str, network_params: dict | None = None)

Set up the connection between agents. The name and parameters come from NetworkX package. For example, The documentation for BA scale-free network is here: https://networkx.org/documentation/stable/reference/generated/networkx.generators.random_graphs.barabasi_albert_graph.html. To create it, network_type is barabasi_albert_graph, and parameters should be a dict {“n”: 100, “m”: 3}.

Parameters:
  • agent_lists – Initial agent lists containing agents to be placed onto the network.

  • network_type – str, describing the type of network, which should be the corresponding to networkx.

  • network_params – A dictionary for parameter values.

Returns:

None