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

Moving examples to use the new discrete spaces #198

Merged
merged 35 commits into from
Oct 15, 2024

Conversation

quaquel
Copy link
Member

@quaquel quaquel commented Sep 10, 2024

This PR moves over all relevant examples to the experimental grid spaces. While making this move, the experimental grid spaces continue to be refined.

@EwoutH
Copy link
Member

EwoutH commented Sep 13, 2024

If you change this line:

pip install -U git+https://github.com/projectmesa/mesa@main#egg=mesa

To:

pip install -U git+https://github.com/quaquel/mesa@move_spaces#egg=mesa

You can see if it works in CI.

@quaquel
Copy link
Member Author

quaquel commented Sep 13, 2024

I am testing locally against my branch for #2286, but I'll update the the yml in this branch to show CI here as well.

@EwoutH
Copy link
Member

EwoutH commented Sep 26, 2024

This PR just need the imports switched right?

@quaquel
Copy link
Member Author

quaquel commented Sep 26, 2024

The smart thing to do would be to change the imports to experimental.cell_space. This would make this pr compatible with main.

@EwoutH
Copy link
Member

EwoutH commented Sep 26, 2024

Awesome, sounds like a plan!

@EwoutH
Copy link
Member

EwoutH commented Oct 7, 2024

If you need anything on this PR let me know!

@quaquel
Copy link
Member Author

quaquel commented Oct 7, 2024

If you need anything on this PR let me know!

How to get the build to succeed? It now errors out on reaction?

Other than that, I think this is another half our of work later today and then its done.

@EwoutH
Copy link
Member

EwoutH commented Oct 7, 2024

Awesome progress!

How to get the build to succeed? It now errors out on reaction?

I think the error is this init (https://github.com/projectmesa/mesa/blob/main/mesa/experimental/__init__.py) in combination with that we now don’t install Solara by default anymore. Let me look into it.

Copy link
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how clean the API is looking, and how easy old components are replaced. Good stuff!

store.market_share = 0

for consumer in self.consumer_agents:
for consumer in self.agents_by_type[ConsumerAgent]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a space too much here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll run ruff once I am done and fix it all

@quaquel
Copy link
Member Author

quaquel commented Oct 12, 2024

I really like how clean the API is looking, and how easy old components are replaced. Good stuff!

I am also pleasantly surprised by (1) how easy the switch is; and (2) how, in many places, it reduces code complexity.

@quaquel quaquel requested a review from EwoutH October 12, 2024 13:04
@EwoutH
Copy link
Member

EwoutH commented Oct 12, 2024

Really awesome, I see some really neat changes in here.

Curious, did you encounter any limitations or go any ideas to do things more elegantly in the cell space?

Let's discuss Wednesday how we want to structure the examples for experimental features. Would be awesome if we can stabilize those together with features in future PRs, once the core examples are moved back.

@quaquel
Copy link
Member Author

quaquel commented Oct 12, 2024

Curious, did you encounter any limitations or go any ideas to do things more elegantly in the cell space?

I have made various PRs related to grid spaces while working on this. For example, the way we resolved movement #2333 and #2296, the split of neighborhood and get_neigborhood #2309, and the width/height properties #2348 all emerged out of making these examples work with experimental grid spaces. Some of these examples can be refined even further by using property layers (e.g., sugarscape) or using some of the features introduced in the aforementioned PRs. But I am quite happy with how expressive and complete the grid spaces are.

There is also room for further improvement:

  • We have FixedAgent and CellAgent. I agree with @EwoutH that CellAgent might need a better name. It currently is not a good opposition to FixedAgent. However, I would like to try my hand at mimicking #2296 for Continous Spaces and see what we get there.
  • I would like to address #2221. In many places, I pass the cell to the init of the agent, but then you can funny code were you instantiate an agent and don't do anything with it. Having a simple factory method to create all agents in one go would be be some much cleaner API wise. Compare the code below:
# current
for cell in self.grid.all_cells:
    MyAgent(self, cell)


# with some create method
MyAgent.create_agents(cell=self.grid.all_cells)
  • At the moment. CellCollection.agents returns a list of agents. This is primarily because of performance reasons. But API wise it would be nicer if this became an AgentSet. It is quite common in many models to get the neighboring agents and then select one at random or have some selection criterion. If CellCollection.agents would return an agentset, most models would become even shorter and more readable. Just check the the sugarscape_g1mt Trader agent for an example and image that you could use AgentSet functionality in the move and trade method.

@quaquel
Copy link
Member Author

quaquel commented Oct 14, 2024

I have updated Schelling after #222. @EwoutH any further comments or can this be approved?

@EwoutH
Copy link
Member

EwoutH commented Oct 14, 2024

Could you split of the changes in the "basic" (as in projectmesa/mesa#2349 (comment)) examples into a new, separate branch and save that somewhere where you can find it back in a few weeks/months? Then we will merge those during/after cell space stabilization.

All "complex" and "showcase" examples can stay in this PR, then we will merge that ASAP.

Sorry it's a bit of a messy process, your effort is appreciated, and it looks like we now have a clear policy for experimental features in examples.

@quaquel
Copy link
Member Author

quaquel commented Oct 15, 2024

Could you split of the changes in the "basic" (as in projectmesa/mesa#2349 (comment)) examples into a new, separate branch and save that somewhere where you can find it back in a few weeks/months? Then we will merge those during/after cell space stabilization.

It will be annoying but doable if I have time in a few days. Would it not be simpler to move the basis versions over to that PR? If I am not mistaken, only Schelling is missing at the moment. Next, we merge this, continue the move of advanced examples and once that PR is merged remove all moved over examples from mesa-examples.

Mainly imports
Revert the basic models back to the current stable grids
@EwoutH
Copy link
Member

EwoutH commented Oct 15, 2024

For a proper port of the git history its way easier if we can move the examples at once, otherwise we're merging multiple related git histories which makes it even more complex.

I reverted the basic examples, and saved the changes in a backup branch: https://github.com/projectmesa/mesa-examples/tree/pr_198_examples_backup

projectmesa/mesa#2357 should unblock the CI and then we're good to go.

@quaquel
Copy link
Member Author

quaquel commented Oct 15, 2024

For a proper port of the git history its way easier if we can move the examples at once, otherwise we're merging multiple related git histories which makes it even more complex.

Fair point. I had not considered that.

@EwoutH
Copy link
Member

EwoutH commented Oct 15, 2024

Main CI passes, awesome.

Thanks a lot for this huge effort!

@quaquel quaquel merged commit 9a05b05 into projectmesa:main Oct 15, 2024
2 of 3 checks passed
Copy link
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit enthosiastic on my approval, only after realizing I didn't do a proper code review yet.

Overal, it looks amazing, with some really nice cleanups and elegant constructs! I have a few small things, then can (but should) be dealt with in a follow-up PR.

Especially check if if all your changes were ported correctly after #220, and then remove the duplicate files.

@@ -93,6 +94,7 @@ def __init__(self, model, alpha: float = 1.0, beta: float = 5.0):
self._traveled_distance = 0
self.tsp_solution = []
self.tsp_distance = 0
self.graph = self.model.grid.G
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to make this a property or function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you are proposing. This is in the agent class.

@@ -173,14 +180,15 @@ def __init__(
self.num_cities = tsp_graph.num_cities
self.all_cities = set(range(self.num_cities))
self.max_steps = max_steps
self.grid = mesa.space.NetworkGrid(tsp_graph.g)
self.grid = Network(tsp_graph.g, random=self.random)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call it space here, since it isn't really a grid?

Suggested change
self.grid = Network(tsp_graph.g, random=self.random)
self.space = Network(tsp_graph.g, random=self.random)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want this data file included?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, but the entire batch run example stuff seems incorrect so this needs a bit more attention

examples/aco_tsp/aco_tsp/model.py Show resolved Hide resolved
examples/hotelling_law/hotelling_law/model.py Show resolved Hide resolved
examples/hotelling_law/hotelling_law/model.py Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed I think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

examples/shape_example/shape_example/model.py Show resolved Hide resolved
@quaquel
Copy link
Member Author

quaquel commented Oct 15, 2024

Thanks for these comments. I just like to point out that most of these are comments on the original code from which I started, not the changes made in this PR. Some of the other comments are about using features that were added based on what was learned while doing this. I'll try to go through these in follow up PRs when I have time. A quick look suggests only 1 is pointing out a mistake in my code.

@EwoutH
Copy link
Member

EwoutH commented Oct 15, 2024

Oh certainly, and none of them were blocking. Just things I noticed when going through the diff.

Do you prefer to fix some of them before the example move, or after?

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

Successfully merging this pull request may close these issues.

2 participants