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

Agent creation order and color-coding with ipysimulate #23

Open
fl-hat opened this issue Jun 29, 2021 · 1 comment
Open

Agent creation order and color-coding with ipysimulate #23

fl-hat opened this issue Jun 29, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@fl-hat
Copy link

fl-hat commented Jun 29, 2021

I'm not sure if this is necessarily a bug, but I've found that when I'm using ipysimulate to visualize multiple types of agents, color coding is assigned to agents not by their ID, but by the order they appear in self.agents. In other words, if the order of agents in the model's self.agents does not match the order they were initialized in, then the color of an agent is associated with an entirely different agent's attributes.

It's entirely possible that this was intentional and just could use some more documentation, but it was confusing to me at first so I thought I'd point it out. (I'm also not sure whether this is more of an agentpy or an ipysimulate thing.)

In case there's any confusion, I should note that I also posted about this model in the discussion forums (#22). I've since implemented your suggestions for splitting my agents into multiple spaces, and have been able to get the visualization working using the code you provided. However, I noticed the same color-coding issue I've described happening there too. The code below is from the version of my model where agents are in a single space- at the end I've included some notes on what specifically happened with multiple spaces.

Local setup
OS Catalina 10.15.5, using jupyter notebooks
agentpy version 0.1.2
ipysimulate version 0.2.0

Context:
Below is a part of my model class. The resources, the hive, and the bees are all different types of agents, but they all have a self.state.

class ForagingModel(ap.Model):
    """ 
    ABM simulating a very simplified version of bee foraging behavior 
    """

    def setup(self):
        
        # Create ground
        self.space = ap.Space(self, shape=[self.p.land_size]*2, Torus = False)
        
        # Create flowering plants
        self.resources = ap.AgentList(self, self.p.n_resources, Resource)
        self.space.add_agents(self.resources, random=True) # Scatter them randomly
        self.resources.setup_pos(self.space)
        
        # Create hive
        self.hive= ap.AgentList(self, 1, Hive)
        self.space.add_agents(self.hive, [(self.p.land_size/2,self.p.land_size/2)], random=False)
        
        # Create bees
        self.bees = ap.AgentList(self, self.p.population, Bee)
        # Place them at the hive
        self.space.add_agents(self.bees, [[self.p.land_size/2,self.p.land_size/2]]*self.p.population , random=False) 
        self.bees.setup_pos(self.space)
        
        self.agents = self.resources +  self.hive + self.bees 

I visualized the positions and movement of the agents using

scatterplot = ips.Scatterplot(
    control,
    xy=lambda m: m.space.positions.values(),
    c=lambda m: m.agents.state
)

In my model, the bees change their state (ex. inactive, searching, returning), but the hive and the resources do not.

Issue (with agents in one space)
In the above code, the order agents are added to self.agents:
self.agents = self.resources + self.hive + self.bees
matches the order they're created, so everything works as expected. The bees change color when they switch between states (ex. when they switch from foraging to returning to the hive), and both the hive and resource agents stay the same color.

However, if I replace this line with:
self.agents = self.bees + self.hive + self.resources
then suddenly the resources are changing their colors instead of the bees. If I have more bees than resources, then all the resources change color and a couple of the bees do too. However, those bees have their colors determined by other bees' states (ex. bee 150 might have its color determined by the state of bee 20).

Issue (with agents in two spaces)
As you recommended in the discussion forum, I've since changed my code so that resource and bee agents are placed in separate spaces. Following the code you provided, I've changed my visualization code to include:

model47 = ForagingModel(parameters)
control = ips.Control(model47, parameters, variables = ('t', 'num_searching'))

def both_space_positions(m):
    i1 = m.plantspace.positions.values()
    i2 = m.beespace.positions.values()
    return itertools.chain(i1, i2)

scatterplot = ips.Scatterplot(
    control,
    xy=lambda m: both_space_positions(model47), 
    c=lambda m: m.agents.state
)

I noticed that if I switched the order of i1 and i2 like:

def both_space_positions(m):
    i1 = m.beespace.positions.values()
    i2 = m.plantspace.positions.values()
    return itertools.chain(i1, i2)

I encountered the same type of issue- because I initialize the plants before I create the bees, in the visualization the plants change color according to the states of the bees.

Let me know if any other details or code would be helpful. Thank you so much!

@jofmi jofmi added the enhancement New feature or request label Jun 30, 2021
@jofmi
Copy link
Owner

jofmi commented Jun 30, 2021

Hi @fl-hat, thanks for the detailed report!

Indeed, the two inputs for scatterplot simply go through the returned iterators.
If the two iterators don't have the same order of agents, the output will be false.

This can definitely be improved.

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

No branches or pull requests

2 participants