NetworkX Integration¶
Functions to create agent connections from NetworkX graph structures and NetworkX graphs from agent connections.
Note
NetworkX has to be installed in order for the functions in this module to work. It is not installed as a default dependency.
Use pip install creamas[extras]
to install extra requirements, including NetworkX.
-
creamas.nx.
connections_from_graph
(env, G, edge_data=False)[source]¶ Create connections for agents in the given environment from the given NetworkX graph structure.
- Parameters
env – Environment where the agents live. The environment should be derived from
Environment
,MultiEnvironment
orDistributedEnvironment
.G – NetworkX graph structure, either
networkx.graph.Graph
ornetworkx.digraph.DiGraph
. The graph needs to have the same number of nodes as the environment has agents (excluding the managers).edge_data (bool) – If
True
, edge data from the given graph is copied to the agents’connections
.
Note
By design, manager agents are excluded from the connections and should not be counted towards environment’s agent count.
The created connections are stored in each agent’s
connections
and the possible edge data is stored as key-value pairs in the connection dictionary.The agents are sorted by their environments’ hosts and ports before each agent is mapped to a node in G. This should cause some network generation methods in NetworkX, e.g.
connected_watts_strogatz_graph()
, to create more connections between agents in the same environment and/or node when usingMultiEnvironment
orDistributedEnvironment
.
-
creamas.nx.
graph_from_connections
(env, directed=False)[source]¶ Create NetworkX graph from agent connections in a given environment.
- Parameters
env – Environment where the agents live. The environment must be derived from
Environment
,MultiEnvironment
orDistributedEnvironment
.directed (bool) – If
True
, creates an instance ofDiGraph
, otherwise creates an instance ofGraph
.
- Returns
The created NetworkX graph.
- Return type
DiGraph
orGraph
Note
If the created graph is undirected and two connected agents have different data stored for each other, then the data for the given edge is chosen randomly between the two agents.