Model

class pop2net.Model(parameters=None, _run_id=None, enable_p2n_warnings=True, **kwargs)[source]

Class the encapsulates a full simluation.

This very closely follows the logic of the agentpy.Model package. See agentpy.Model for more information.

add_agent(agent)[source]

Add an agent to the environment.

The added agent will have no connections to other agents or locatons by default. If the agent is already in the current environment, this methods does nothing.

Parameters:

agent (Agent) – Agent to be added to the environment.

Return type:

None

add_agent_to_location(location, agent, weight=None, **kwargs)[source]

Add an agent to a specific location.

Both the agent and the location have to be defined beforehand. All additional keyword arguments will be edge attributes for this connection.

Parameters:
  • location (Location) – Location the agent is to be added to.

  • agent (Agent) – Agent to be added to the location.

  • weight (Optional[float]) – An optional weight for the connection.

  • **kwargs – Additional edge attributes.

Raises:
  • Exception – Raised if the location does not exist in the environment.

  • Exception – Raised if the agent does not exist in the environment.

Return type:

None

add_agents(agents)[source]

Add agents to the environment.

Parameters:

agents (list) – A list of the agents to be added.

Return type:

None

add_location(location)[source]

Add a location to the environment.

The added location will have no connections to other agents or locatons by default. If the location is already in the current environment, this methods does nothing.

Parameters:

location (Location) – Location to be added to the environment.

Return type:

None

add_locations(locations)[source]

Add multiple locations to the environment at once.

Parameters:

locations (list) – An iterable over multiple locations.

Return type:

None

property agents: AgentList

Show a iterable view of all agents in the environment.

Returns:

A non-mutable AgentList of all agents in the environment.

Return type:

AgentList

agents_between_locations(location1, location2, agent_types=None)[source]

Return all agents between two locations.

Parameters:
  • location1 (Location) – Location 1.

  • location2 (Location) – Location 2.

  • agent_types (tuple, optional) – Constrain the agents to the following types. Defaults to None.

Returns:

A list of agents.

Return type:

AgentList

property agents_by_id: dict

Returns a dictionary which stores the model’s agents by their id.

Returns:

A dictionary which stores the model’s agents by their id.

Return type:

dict

agents_of_location(location)[source]

Return the list of agents associated with a specific location.

Parameters:

location (Location) – The desired location.

Return type:

AgentList

Returns:

A list of agents.

connect_agents(agents, location_cls, weight=None)[source]

Connects multiple agents via an instance of a given location class.

Parameters:
  • agents (list) – A list of agents.

  • location_cls (type) – The location class that is used to create a location instance.

  • weight (float | None) – The edge weight between the agents and the location. Defaults to None.

disconnect_agents(agents, location_labels=None, remove_locations=False)[source]

Disconnects agents by removing them from shared locations.

If a list of location types is given, only shared locations of the given types are considered. Turn on remove_locations in order to not only remove the given agents from the given location instance but also to remove the location instance from the model. Use this method with care because removing agents from locations also disconnects those agents from all other agents connected to the location. Removing the location instance from the model could have even more sideeffects to those agents still connected with this location!

Parameters:
  • agents (list) – A list of agents.

  • location_labels (list | None, optional) – A list of location types to specify which

  • None. (shared locations are considered. Defaults to)

  • remove_locations (bool, optional) – A bool that determines whether the shared locations shall be removed from the model. Defaults to False.

export_agent_network(node_attrs=None, include_0_weights=True)[source]

Creates a projection of the model’s bipartite network.

Parameters:
  • node_attrs (Optional[list]) – A list of agent attributes

  • include_0_weights (bool) – Should edges with weight 0 be displayed?

Return type:

Graph

Returns:

A weighted graph created from a model’s agent list. Agents are connected if they are neighbors in the model. Their connecting edge include the contact_weight as “weight” attribute.

get_weight(agent, location)[source]

Get the weight of an agent at a location.

Parameters:
  • agent (Agent) – The agent.

  • location (Location) – The location.

Returns:

The weight.

Return type:

int

property locations: LocationList

Show a iterable view of all locations in the environment.

Returns:

a non-mutable LocationList of all locations in the environment.

Return type:

LocationList

locations_between_agents(agent1, agent2, location_labels=None)[source]

Return all locations the connect two agents.

Parameters:
  • agent1 (Agent) – Agent 1.

  • agent2 (Agent) – Agent 2.

  • location_labels (tuple, optional) – Constrain the locations to the following types. Defaults to None.

Returns:

A list of locations.

Return type:

LocationList

property locations_by_id: dict

Returns a dictionary which stores the model’s locations by their id.

Returns:

A dictionary which stores the model’s locations by their id.

Return type:

dict

locations_of_agent(agent)[source]

Return the list of locations associated with a specific agent.

Parameters:

agent (Agent) – The desired agent.

Return type:

LocationList

Returns:

A list of locations.

neighbors_of_agent(agent, location_labels=None)[source]

Return a list of neighboring agents for a specific agent.

The locations to be considered can be defined with location_labels.

Parameters:
  • agent (Agent) – Agent of whom the neighbors are to be returned.

  • location_labels (Optional[list]) – A list of location_labels.

Return type:

AgentList

Returns:

The list of neighbors for the specified agent.

remove_agent(agent)[source]

Remove an agent from the environment.

If the agent does not exist in the environment, this method does nothing.

Parameters:

agent (Agent) – Agent to be removed.

Return type:

None

remove_agent_from_location(location, agent)[source]

Remove an agent from a location.

Parameters:
  • location (Location) – Location, the agent is to be removed from.

  • agent (Agent) – Agent to be disassociated with the location.

Raises:
  • Exception – Raised if the location does not exist in the environment.

  • Exception – Raised if the agent does not exist in the environment.

Return type:

None

remove_agents(agents)[source]

Remove multiple agents from the environment at once.

Parameters:

agents (list) – An iterable over multiple agents.

Return type:

None

remove_location(location)[source]

Remove a location from the environment.

If the location does not exist in the environment, this method does nothing.

Parameters:

location (Location) – Location to be removed.

Return type:

None

remove_locations(locations)[source]

Remove multiple locations at once.

Parameters:

locations (list) – An iterable over locations.

Return type:

None

set_weight(agent, location, weight=None)[source]

Set the weight of an agent at a location.

If weight is None the method location.weight() will be used to generate a weight.

Parameters:
  • agent (Agent) – The agent.

  • location (Location) – The location.

  • weight (int) – The weight

Return type:

None

update_weights(location_labels=None)[source]

Updates the edge weights between agents and locations.

If you only want to update the weights of specific types of locations specify those types in location_labels.

Parameters:

location_labels (list | None, optional) – A list of location classes that specifiy for which location types the weights should be updated. If location_labels is None all locations are considered. Defaults to None.

Return type:

None