-
Notifications
You must be signed in to change notification settings - Fork 942
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
Visualizing Network Models with More Agents than Nodes #2691
Comments
The latest version of the model uses the new style The offending code is in # gather agent data
s_default = (180 / max(width, height)) ** 2
arguments = collect_agent_data(space, agent_portrayal, size=s_default)
# this assumes that nodes are identified by an integer
# which is true for default nx graphs but might user changeable
pos = np.asarray(list(pos.values()))
arguments["loc"] = pos[arguments["loc"]] What is needed here is to have a more intelligent way of mapping from And #2593 is not the way to cleanly fix this. |
I was on 3.1.0 and just updated to 3.1.4. On both versions I am getting an error trying to load |
Yes, I am referring to the master branch here on Git Hub. For the future 3.2 release, cell_spaces are being stablized, moved, and renamed from |
So, replacing the snippet I showed with # this assumes that nodes are identified by an integer
# which is true for default nx graphs but might user changeable
agent_nodes = arguments["loc"]
agent_position = [pos[node] for node in agent_nodes]
arguments["loc"] = np.asarray(agent_position) Should be sufficient. I will do some proper testing tomorrow. |
Thanks for your help! I updated that section of Oddly, I have noticed that I am only getting the error when including the second set of agents (commented out in the screenshot below). They are both versions of the same base class (modeled after the wolf/sheep/animal example). I can add the DCO agents, but as soon as I add the OCO agents the error is raised (even if they are the only dynamic agents added). |
What is your agent_portrayal function? |
|
I need to some testing. It could be that the error you reported is not unique to networks, but due to having to handle marker and zorder at the same time. |
Got it, I appreciate your help. The zorder was my attempt to visualize multiple agents on the same cell, is there another (or better) way to achieve that effect? Perhaps add an x-y offset by agent type? |
At the moment there is not, unless you write your own fully custom function for drawing space. What you could test is to have only the marker or only the zorder and see if that still gives the error. |
Yes, it appears to still throw the error with only marker or zorder per agent in the |
I cannot reproduce your error. My test code is the following class MyAgent(CellAgent):
def __init__(self, model, cell, batch):
super().__init__(model)
self.cell = cell
self.batch = batch
num_nodes = 10
avg_node_degree = 3
model = Model(seed=42)
prob = avg_node_degree / num_nodes
graph = nx.erdos_renyi_graph(n=num_nodes, p=prob)
grid = Network(graph, capacity=2, random=model.random)
cell = grid.all_cells.select_random_cell()
MyAgent.create_agents(model, num_nodes, list(grid.all_cells), 1)
MyAgent.create_agents(model, num_nodes, list(grid.all_cells), 2)
def portray_agent(agent):
return {"color":"tab:blue" if (agent.batch %2==0) else "tab:orange",
"size":25,
"zorder":1 if (agent.batch %2==1) else 0,
"marker":'o' if (agent.batch %2==0) else 'D', }
draw_network(grid, portray_agent)
plt.show() I create 2 sets of agents, each equal, in my case, to the number of cells. Agent portrayal uses both marker and zorder, tied to whether it is an agent in the first or the second batch. It all visualizes normally (but on top of each other). I don't get the error you are getting. Any idea, looking at your own code where I differ from what you are trying to do? |
I have found the issue: you have You only specify this for 1 agent class (OCO), not for any of the others. This causes the error. The content of the return from agent portrayal must contain the same field for all agents. Whether this is desirable is a different question. |
Yes, I was wondering why the one agent class would cause the issue. Adding edgecolors for all (or removing from all) appears to resolve the issue. Thanks for your help! |
Describe the bug
When trying to visualize a network-based model, the draw_network function raises an error. I was working on making the error reproducible with the Virus on Network example code and noticed that the Stable and Latest versions use two different methods of creating the Network. The latest version seemed to tolerate visualizing the model with more agents than nodes (assuming the cell capacity was large enough). After adapting my code it visualizes the model with some agents greater than the number of nodes but fails when other agents are introduced, so it does not seem like it is strictly a function of the number of agents.
Expected behavior
The draw_network code uses the agent_portrayal function to visualize the various type and number of agents in each node of the network.
To Reproduce
I haven't been able to reproduce the error with the VoN example.
Additional context
I am using Mesa Version 3.1.0 in JupyterLab on MacOS Sequoia
The text was updated successfully, but these errors were encountered: