Society

class piperabm.society.Society(average_income: float = 1000, neighbor_radius: float = 0, max_time_outside: float = 28800, activity_cycle: float = 86400, transportation_resource_rates: dict = {'energy': 1.1574074074074073e-05, 'food': 2.3148148148148147e-05, 'water': 1.1574074074074073e-05}, idle_resource_rates: dict = {'energy': 2.3148148148148147e-05, 'food': 2.3148148148148147e-05, 'water': 2.3148148148148147e-05}, speed: float = 1.3888888888888888, transportation_degradation: float = 1)

Bases: Query, Generate, Update, Serialize, Graphics, Stat

Represent society network

accessibility(id: int) dict

Calculate accessibility for all resources

accessibility_resource(id: int, name: str)

Calculate accessibility for certain resource

add_agent(home_id: int = None, id: int = None, name: str = '', socioeconomic_status: float = 1, resources: dict | ~piperabm.resource.Resource = <piperabm.resource.Resource object>, enough_resources: dict | ~piperabm.resource.Resource | None = None, balance: float = 0)

Add an agent node to the society network.

Agents are represented as nodes in the society graph. Each agent is assigned to a home node in the infrastructure and initialized with resources and a balance. When an agent is added, family and neighbor relationships may be created automatically based on the agent’s home assignment.

Parameters:
  • home_id (int, optional) – ID of the home node that the agent belongs to. If None, a home is chosen at random from existing infrastructure homes.

  • id (int, optional) – Unique identifier of the agent node. If None, a unique ID is automatically generated. If the provided ID already exists, a new unique ID is generated instead (the existing agent is not overwritten).

  • name (str, optional) – Optional human-readable name for the agent.

  • socioeconomic_status (float, optional) – Socioeconomic status of the agent (used by the decision-making and/or behavior models). Defaults to 1.

  • resources (dict or Resource, optional) – Initial resource inventory for the agent. This can be provided either as a dictionary (e.g., {'food': 10, 'water': 10, 'energy': 10}) or as a piperabm.Resource instance. If a Resource object is provided, it is converted internally to a dictionary before being attached to the underlying graph representation.

  • enough_resources (dict or Resource or None, optional) – The per-resource “enough” thresholds used by the agent’s satisfaction/utility model. If provided as a Resource object, it is converted internally to a dictionary. If None, the system initializes the threshold for each resource to match the corresponding initial resources value.

  • balance (float, optional) – Initial monetary balance of the agent. Defaults to 0.

Returns:

The ID of the newly added agent node.

Return type:

int

Raises:

ModelNotBakedError – If the infrastructure has not been baked. Agents require a finalized infrastructure network (via bake()) to ensure physically consistent routing and access edges.

Notes

Internally, agent state (including resources and thresholds) is stored as plain attributes on the NetworkX society graph. The piperabm.Resource class is provided as an optional convenience wrapper for validation/readability and does not change the internal representation.

add_family(id_1: int, id_2: int)

Add a family relationship edge between two agents.

A family relationship is created automatically when two distinct agents are assigned to the same home. This method adds a family-typed edge between the two agent nodes in the society graph, provided that both agents share the same home_id.

Parameters:
  • id_1 (int) – ID of the first agent.

  • id_2 (int) – ID of the second agent.

Notes

  • A family edge is only added if id_1 and id_2 are different agents and both are associated with the same home node.

  • The edge is stored in the underlying NetworkX multi-graph with type='family' and includes the shared home_id as an edge attribute.

  • If the agents do not share the same home, no edge is added and the method exits silently.

See also

add_neighbor

Add a neighbor relationship between agents in nearby homes.

add_friend

Add a user-defined friendship relationship between agents.

add_friend(id_1: int, id_2: int)

Add a friendship relationship edge between two agents.

A friendship relationship represents an explicit, user-defined social tie between two agents. Unlike family or neighbor relationships, friendship edges are not created automatically and must be added explicitly by the user.

Parameters:
  • id_1 (int) – ID of the first agent.

  • id_2 (int) – ID of the second agent.

Notes

  • Friendship edges are stored in the underlying NetworkX multi-graph with type='friend' as an edge attribute.

  • No structural constraints are enforced: the agents do not need to share the same home or be geographically close.

  • This method does not prevent duplicate friendship edges from being added; multiple friendship edges between the same pair of agents may exist.

See also

add_family

Add a family relationship between agents sharing the same home.

add_neighbor

Add a neighbor relationship between agents in nearby homes.

add_neighbor(id_1, id_2)

Add a neighbor relationship edge between two agents.

A neighbor relationship represents spatial proximity between agents whose assigned home nodes are distinct but located within a specified neighborhood radius. This method adds a neighbor-typed edge between two agents if they are not members of the same household.

Parameters:
  • id_1 (int) – ID of the first agent.

  • id_2 (int) – ID of the second agent.

Notes

  • A neighbor edge is only added if id_1 and id_2 are different agents and their associated home_id values are not equal.

  • Neighbor relationships are typically created automatically during agent initialization based on spatial proximity between home nodes.

  • The edge is stored in the underlying NetworkX multi-graph with type='neighbor' as an edge attribute.

  • If the agents share the same home, no edge is added and the method exits silently.

See also

add_family

Add a family relationship between agents sharing the same home.

add_friend

Add a user-defined friendship relationship between agents.

property agents: list

Return all agent nodes

agents_from(home_id: int) list

Return all agents from a certain home (family)

agents_in(id: int) list

Return all agents in a certain node

property alives: list

Return all alive agent nodes

check_id(id)

Check whether id already exists

property deads: list

Return all alive agent nodes

deserialize(data: dict) None

Deserialize

property edges: list

Return all edges ids

edges_from(id: int) list

All edges from a node

ego(id: int, type: str = None) list

Return agent nodes in the ego network

property families: list

Return all family edges

fig(relationships: bool = False, clf: bool = False)

Add society elements to plt fig ax

property friends: list

Return all friend edges

generate(num: int = 1, gini_index: float = 0, average_resources: dict = {'energy': 10, 'food': 10, 'water': 10}, average_balance: float = 0)

Generate agents

get_action_queue(id: int)

Get agent action queue

get_alive(id: str) bool

Get agent alive value

get_balance(id: int) float

Get agent balance value

get_current_node(id: str) int

Get agent current_node value

get_edge_attribute(ids: list, attribute: str, default=None)

Get edge attribute from networkx graph

get_edge_attributes(ids: list) dict

Get all edge attribute from networkx graph

get_edge_name(ids: list) str

Get edge name value

get_edge_type(ids: list) str

Get edge type value

get_enough_resource(id: int, name: str) float

Get agent enough_resource value

get_home_id(id: str) int

Get agent home_id value

get_income(id: str) float

Get agent income value

get_node_attribute(id: int, attribute: str, default=None)

Get node attribute from networkx graph

get_node_attributes(id: list) dict

Get all node attribute from networkx graph

get_node_name(id: int) str

Get node name value

get_node_type(id: int) str

Get node type value

get_pos(id: int)

Get node position value

get_resource(id: int, name: str, object=False) float | Resource

Get agent resource value. If object is False, return a float, otherwise return a Resource object.

get_resources(id: int, object=False) dict | Resource

Get agent resources value. If object is False, return a dict, otherwise return a Resource object.

get_socioeconomic_status(id: str) bool

Get agent socioeconomic_status value

property gini_index: float

Calculate the current gini index of society

has_edge(ids: list) bool

Check whether the network already contains the edge

has_node(id: int) bool

Check whether the network already contains the node

property infrastructure

Alias

is_home(id: int) bool
property neighbors: list

Return all friend edges

new_id() int

Generate new unique random id

property nodes: list

Return all nodes id

property prices: dict

Alias

property resource_names: list

Alias

resources_in(node_id, is_market: bool)

All resources available in a node

resources_value(id: int) float

Monetary value of resources that an agent possesses

serialize() dict

Serialize

set_balance(id: str, value: float) None

Set agent balance value

set_current_node(id: str, value: int) None

Set agent current_node value

set_decision_making(cls)

Set the decision making class to use for this society. The class must be a subclass of DecisionMaking.

set_edge_attribute(ids: list, attribute: str, value=None) None

Set edge attribute in networkx graph

set_node_attribute(id: int, attribute: str, value=None) None

Set node attribute in networkx graph

set_pos(id: int, value: list) None

Set node position

set_resource(id: int, name: str, value: float) None

Set agent resource value

set_resources(id: int, values: dict | Resource) None

Set agent resources values.

show(relationships=True)

Show society elements

property stat

Return stats of the network

type = 'society'
update(duration: float, measure: bool = False)

Update the network

wealth(id: int) float

Wealth of an agent