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, e.g. pip install 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,MultiEnvironmentorDistributedEnvironment.G – NetworkX graph structure, either
networkx.graph.Graphornetworkx.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
connectionsand 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 usingMultiEnvironmentorDistributedEnvironment.
-
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,MultiEnvironmentorDistributedEnvironment.directed (bool) – If
True, creates an instance ofDiGraph, otherwise creates an instance ofGraph.
- Returns
The created NetworkX graph.
- Return type
DiGraphorGraph
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.