Skip to content

Adds move_to_optimal function in DiscreteSpaceDF class #118

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

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

suryanshgargbpgc
Copy link

@suryanshgargbpgc suryanshgargbpgc commented Mar 17, 2025

Added the move_to function to allow agent movement based on specified attributes and ranking orders. The function considers neighborhood radius, sorting preferences, and optional shuffling for random movement. It ensures conflict resolution using priority-based selection, optimizing movement allocation.

Added the move_to function to allow agent movement based on specified attributes and ranking orders. The function considers neighborhood radius, sorting preferences, and optional shuffling for random movement. It ensures conflict resolution using priority-based selection, optimizing movement allocation. This enhances the agent-based model's flexibility and realism.
Copy link

codecov bot commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 83.21060% with 114 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@d7756a5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
mesa_frames/abstract/space.py 56.63% 49 Missing ⚠️
mesa_frames/concrete/pandas/agentset.py 88.49% 26 Missing ⚠️
mesa_frames/concrete/pandas/mixin.py 89.80% 21 Missing ⚠️
mesa_frames/concrete/polars/agentset.py 20.00% 8 Missing ⚠️
mesa_frames/abstract/agents.py 37.50% 5 Missing ⚠️
mesa_frames/concrete/agents.py 20.00% 4 Missing ⚠️
mesa_frames/types_.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #118   +/-   ##
=======================================
  Coverage        ?   89.24%           
=======================================
  Files           ?       14           
  Lines           ?     2344           
  Branches        ?        0           
=======================================
  Hits            ?     2092           
  Misses          ?      252           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This reverts commit df26a23.
@suryanshgargbpgc
Copy link
Author

Hi, @adamamer20 I have added the move_to method in the DiscreteSpaceDF class, It is more generalized version of get_best_moves. I hope it works well

@adamamer20 adamamer20 self-requested a review March 18, 2025 17:39
Copy link
Member

@adamamer20 adamamer20 left a comment

Choose a reason for hiding this comment

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

The implementation seems good!

There are 3 main points I'd like you to address before we merge:

  1. Add proper docstrings (NumPy style)
  2. Include unit tests in tests\polars\test_grid_polars.py
  3. Consider adding an interface for this functionality to the AgentContainer classes (AgentSetDF and AgentsDF)

I'll benchmark soon to check if performance remains solid with the numba UDF approach, especially using lazyframes. If performance scales badly, we might need to iterate over columns directly or explore another solution.

raise ValueError("attr_names and rank_order must have the same length")
if radius is None:
if "vision" in agents.columns:
radius = agents["vision"]
Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea but make sure to mention it in the docstring as it's a strong assumption.

adamamer20 and others added 14 commits March 19, 2025 22:31
1.move_to function is renamed as move_to_optimize
2. Docstrings added
3. unit tests added to tests/polars/test_grid_polars
4. Interface for move_to_optimize added to AgentContainer, AgentsDF, AgentSetDF
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
@suryanshgargbpgc
Copy link
Author

Hi @adamamer20,
I have implemented all the changes you mentioned earlier, however for the unit tests,
I'm getting a SchemaError in the tests that says the "agent_id" column should be a Struct, but it's coming through as an integer. the move_to_optimal function, so I'm wondering if I need to change my implementation to convert agent_id into a struct, or if the test should be updated instead. Could you please advise on the best approach?

Unit tests for move_to_optimal method added
updated move_to_optimal method and the docstring
This reverts commit 3d28340.
@suryanshgargbpgc suryanshgargbpgc changed the title Adds move_to function in DiscreteSpaceDF class Adds move_to_optimal function in DiscreteSpaceDF class Mar 23, 2025
@adamamer20
Copy link
Member

Hi @adamamer20,

I have implemented all the changes you mentioned earlier, however for the unit tests,

I'm getting a SchemaError in the tests that says the "agent_id" column should be a Struct, but it's coming through as an integer. the move_to_optimal function, so I'm wondering if I need to change my implementation to convert agent_id into a struct, or if the test should be updated instead. Could you please advise on the best approach?

unique_id should be an integer. can you clarify where you are getting this error? if it's a test you created yourself, then correct it please.

@suryanshgargbpgc
Copy link
Author

suryanshgargbpgc commented Mar 24, 2025

@adamamer20 I have fixed the test
However I have not implemented the auto unique id feature for the move_to_optimal function and for the tests unique id is defined explicitly, should I add that feature or is it fine?

obj = self._get_obj(inplace)

# Apply move_to_optimal to each agent set in the container
for agent_set in obj.agent_sets.values():
Copy link
Member

@adamamer20 adamamer20 Mar 29, 2025

Choose a reason for hiding this comment

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

obj does not have the agent_sets attribute. Here in the abstract file we define only the interface.

@suryanshgargbpgc
Copy link
Author

@adamamer20 is it fine now ?

@adamamer20 adamamer20 self-requested a review April 9, 2025 15:04
Comment on lines +1068 to +1078
for agent_set in obj:
agent_set.move_to_optimal(
attr_names=attr_names,
rank_order=rank_order,
radius=radius,
include_center=include_center,
shuffle=shuffle,
inplace=True,
)

return obj
Copy link
Member

Choose a reason for hiding this comment

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

again, no actual implementation should go here becuase it's an abstract interface. remove it completely. For the AgentSet you should do the implementation for AgentSetPolars.

@@ -616,6 +616,19 @@ def __str__(self) -> str:
"""
...

@abstractmethod
def move_to_optimal(
Copy link
Member

Choose a reason for hiding this comment

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

Create a complete docstring like you did for GridPolars

Copy link

coderabbitai bot commented Apr 15, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

This reverts commit 90351ba, reversing
changes made to f437548.
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.

None yet

2 participants