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 visualize dynamically evolving networks #1020

Open
Fedebindi97 opened this issue Apr 9, 2021 · 10 comments
Open

How to visualize dynamically evolving networks #1020

Fedebindi97 opened this issue Apr 9, 2021 · 10 comments

Comments

@Fedebindi97
Copy link

Hey everybody!

First of all, I wanted to thank you all for the amazing work you're doing with Mesa. Keep the awesomeness!

I have an issue: I want to build a model whose network evolves at every step (i.e. new links are formed, some old links are dropped, etc.). The code per se is not difficult; the problem comes with the visualization.
In fact, I don't understand how to use the NetworkGrid object to handle such a situation. The two network models in the example folder, the Boltzmann wealth model and the virus on a network model, both have fixed edges, as far as I can understand. Does NetworkGrid have methods that allow me to pop off or insert new links? From the reference page, it doesn't seem so.

Thank you in advance for the help!

@rht
Copy link
Contributor

rht commented Apr 10, 2021

From the Boltzmann wealth model example, you can add/remove grid using standard Networkx operation (add_edge, remove_edge), operated on self.grid.G.

@Fedebindi97
Copy link
Author

Fedebindi97 commented Apr 10, 2021

@rht, I get an error: "NetworkGrid object has no method "graph"". And furthermore, in the Boltzmann model. there is no example of the "add_edge" method being used on a grid (at least in the version that is currently uploaded in GitHub).
In general, I would like to know if it is possible to send a whole network to a grid, without having to fill the nodes with agents each time. I could then implement a single function making the transfer.

@rht
Copy link
Contributor

rht commented Apr 10, 2021

.G, not .graph.

I could then implement a single function making the transfer.

Can you elaborate what this "transfer" is?

@Fedebindi97
Copy link
Author

Fedebindi97 commented Apr 11, 2021

Ah ok, I thought "G" was the generic name of the graph I wanted to put into the grid.

For "transfer" I mean putting the graph entirely into the grid (with the nodes already inserted, without having to put them in one by one).

@rht
Copy link
Contributor

rht commented Apr 11, 2021

You can't bypass doing place_agent one by one because inside that place_agent operation, the agent.pos needs to be informed of which node id it is located at.

Though if you meant specifically putting the graph entirely into the grid without involving agent, you can just pass in the entire graph self.grid = NetworkGrid(self.G). This doesn't seem to be a problem. So not sure what you meant exactly.

@Fedebindi97
Copy link
Author

But in the second case, doesn't it simply create a grid with empty nodes?

@rht
Copy link
Contributor

rht commented Apr 11, 2021

Yes, is that a problem? As I said, agent.pos needs to know its node id, and you have to do the place_agent loop for each agent no matter what.

@BogdanBalcau
Copy link

Hi, so could you give an example on how to remove/add edges/nodes in a model step, please?

@rht
Copy link
Contributor

rht commented Feb 22, 2022

@BogdanBalcau you can do this:

def step(self):
    self.G.add_node(node_idx)
    self.G.remove_node(node_idx)
    self.G.add_edge(from_idx, to_idx)
    self.G.remove_edge(from_idx, to_idx)

Where self.G is your networkx object. I haven't tested this. There is a concern that self.G is immutable, and so the network graph passed into self.grid (as a NetworkGrid) isn't changed. But if this works, let me know. Improving the documentation for NetworkGrid is welcomed.

@BogdanBalcau
Copy link

@BogdanBalcau you can do this:

def step(self):
    self.G.add_node(node_idx)
    self.G.remove_node(node_idx)
    self.G.add_edge(from_idx, to_idx)
    self.G.remove_edge(from_idx, to_idx)

Where self.G is your networkx object. I haven't tested this. There is a concern that self.G is immutable, and so the network graph passed into self.grid (as a NetworkGrid) isn't changed. But if this works, let me know. Improving the documentation for NetworkGrid is welcomed.

Will try that, thank you!

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