Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the neighbors for each agent? #54

Open
GuiyangLuo opened this issue Apr 25, 2019 · 2 comments
Open

How to get the neighbors for each agent? #54

GuiyangLuo opened this issue Apr 25, 2019 · 2 comments

Comments

@GuiyangLuo
Copy link

Hi, at first, I want to express my thanks to this awesome framework!

I want to obtain the neighbors of each agent, i.e., the spacial local view include the observed agents of both group 1 and group 2, I want to obtain the IDs and positions of all it's neighbors.

@merrymercy
Copy link
Collaborator

cc @KornbergFresnel

@outlace
Copy link

outlace commented May 10, 2019

This should get you half way there. For a given group, this will return the index positions:

from scipy.spatial.distance import cityblock

def get_neighbors(j,pos_list,r=6):
    """
    Given [x,y] positions of all agents in `pos_list`,
    return indices of agents that are within the radius of agent j (i.e. the j'th index in `pos_list`)
    """
    neighbors = []
    pos_j = pos_list[j]
    for i,pos in enumerate(pos_list):
        if i == j:
            continue
        dist = cityblock(pos,pos_j)
        if dist < r:
            neighbors.append(i)
    return neighbors

>>> get_neighbors(5,env.get_pos(team1))
[27, 33, 34, 58]

where team1 is one of the group handles and env is an environment instance. The env.get_pos(...) method returns a list of [x,y] positions for each agent in that group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants