Create connections for agents in the given environment from the given
    NetworkX graph structure.

    :param env:
        Environment where the agents live. The environment should be derived
        from :class:`~creamas.core.environment.Environment`,
        :class:`~creamas.mp.MultiEnvironment` or
        :class:`~creamas.ds.DistributedEnvironment`.

    :param G:
        NetworkX graph structure, either :class:`networkx.graph.Graph` or
        :class:`networkx.digraph.DiGraph`. The graph needs to have the same
        number of nodes as the environment has agents (excluding the managers).

    :param bool edge_data:
        If ``True``, edge data from the given graph is copied to the agents'
        :attr:`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
    :attr:`~creamas.core.agent.CreativeAgent.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.
    :func:`~networkx.generators.random_graphs.connected_watts_strogatz_graph`,
    to create more connections between agents in the same environment and/or
    node when using :class:`~creamas.mp.MultiEnvironment` or
    :class:`~creamas.ds.DistributedEnvironment`.

def connections_from_graph(env, G, edge_data=False):
    """Create connections for agents in the given environment from the given
    NetworkX graph structure.

    :param env:
        Environment where the agents live. The environment should be derived
        from :class:`~creamas.core.environment.Environment`,
        :class:`~creamas.mp.MultiEnvironment` or
        :class:`~creamas.ds.DistributedEnvironment`.

    :param G:
        NetworkX graph structure, either :class:`networkx.graph.Graph` or
        :class:`networkx.digraph.DiGraph`. The graph needs to have the same
        number of nodes as the environment has agents (excluding the managers).

    :param bool edge_data:
        If ``True``, edge data from the given graph is copied to the agents'
        :attr:`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
    :attr:`~creamas.core.agent.CreativeAgent.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.
    :func:`~networkx.generators.random_graphs.connected_watts_strogatz_graph`,
    to create more connections between agents in the same environment and/or
    node when using :class:`~creamas.mp.MultiEnvironment` or
    :class:`~creamas.ds.DistributedEnvironment`.
    """
    if not issubclass(G.__class__, (Graph, DiGraph)):
        raise TypeError("Graph structure must be derived from Networkx's "
                        "Graph or DiGraph.")
    if not hasattr(env, 'get_agents'):
        raise TypeError("Parameter 'env' must have get_agents.")

    addrs = env.get_agents(addr=True)
    if len(addrs) != len(G):
        raise ValueError("The number of graph nodes and agents in the "
                         "environment (excluding the manager agent) must "
                         "match. Now got {} nodes and {} agents."
                         .format(len(G), len(addrs)))
    # Sort agent addresses to the order they were added to the environment.
    addrs = sort_addrs(addrs)
    _addrs2nodes(addrs, G)
    conn_map = _edges2conns(G, edge_data)
    env.create_connections(conn_map)