-
Notifications
You must be signed in to change notification settings - Fork 880
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
Generalize CellAgent #2292
Merged
Merged
Generalize CellAgent #2292
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ff611c9
add some agents
Corvince ec36c08
restructure and rename
Corvince 86f8cbc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1bcf17d
Merge branch 'main' of https://github.com/projectmesa/mesa into cell-…
Corvince a02eed5
Merge branch 'cell-space-agents' of https://github.com/projectmesa/me…
Corvince 28379ed
Restructure mixins
Corvince a5f5930
rename and update Grid2DMovement
Corvince 13a9dc0
Merge branch 'main' of https://github.com/projectmesa/mesa into cell-…
Corvince 9e38f6a
use direction map instead of match
Corvince b0e95a0
Add Patch
Corvince 181582c
Merge remote-tracking branch 'upstream/main' into cell-space-agents
quaquel 9ad1a2a
tests for all new stuff
quaquel 052cd7c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 51946ba
Update cell_agent.py
quaquel 05597a5
Merge branch 'cell-space-agents' of https://github.com/projectmesa/me…
quaquel 1d8a111
Update test_cell_space.py
quaquel 87d6025
Rename Patch to FixedAgent
Corvince 5d280ff
Rename Patch to FixedAgent in tests
EwoutH ddecebc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d6b3015
Use FixedAgent in examples/benchmarks
EwoutH 5d605c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,83 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import TYPE_CHECKING, Any, Protocol, TypeVar | ||
|
||
from mesa import Agent, Model | ||
from mesa.experimental.cell_space.discrete_space import DiscreteSpace | ||
|
||
if TYPE_CHECKING: | ||
from mesa.experimental.cell_space.cell import Cell | ||
from mesa.experimental.cell_space import Cell | ||
|
||
T = TypeVar("T", bound="Cell") | ||
|
||
class CellAgent(Agent): | ||
"""Cell Agent is an extension of the Agent class and adds behavior for moving in discrete spaces | ||
|
||
class DiscreteSpaceAgent(Protocol[T]): | ||
cell: T | None | ||
space: DiscreteSpace[T] | ||
|
||
def move_to(self, cell: T) -> None: ... | ||
|
||
def move_relative(self, directions: tuple[int, ...], distance: int = 1): ... | ||
|
||
|
||
class CellAgent: | ||
"""Cell Agent is an Agent class that adds behavior for moving in discrete spaces | ||
|
||
Attributes: | ||
unique_id (int): A unique identifier for this agent. | ||
model (Model): The model instance to which the agent belongs | ||
pos: (Position | None): The position of the agent in the space | ||
cell: (Cell | None): the cell which the agent occupies | ||
space (DiscreteSpace): the discrete space the agent is in | ||
cell (Cell): the cell the agent is in | ||
""" | ||
|
||
def __init__(self, model: Model) -> None: | ||
""" | ||
Create a new agent. | ||
def __init__( | ||
self, | ||
space: DiscreteSpace[Cell], | ||
cell: Cell | None = None, | ||
*args: tuple[Any], | ||
**kwargs: dict[str, Any], | ||
): | ||
super().__init__(*args, **kwargs) | ||
self.space = space | ||
self.cell = cell | ||
if cell is not None: | ||
cell.add_agent(self) | ||
|
||
Args: | ||
unique_id (int): A unique identifier for this agent. | ||
model (Model): The model instance in which the agent exists. | ||
""" | ||
super().__init__(model) | ||
self.cell: Cell | None = None | ||
@property | ||
def coordinate(self) -> tuple[int, ...]: | ||
return self.cell.coordinate if self.cell else () | ||
|
||
def move_to(self, cell) -> None: | ||
def move_to(self, cell: Cell) -> None: | ||
if self.cell is not None: | ||
self.cell.remove_agent(self) | ||
self.cell = cell | ||
cell.add_agent(self) | ||
|
||
def move_relative(self, directions: tuple[int, ...], distance: int = 1): | ||
new_position = tuple( | ||
self.cell.coordinate[i] + directions[i] * distance | ||
for i in range(len(directions)) | ||
if self.cell | ||
) | ||
new_cell = self.space[new_position] | ||
self.move_to(new_cell) | ||
|
||
|
||
class Grid2DMovingAgent: | ||
def move(self: DiscreteSpaceAgent[Cell], direction: str, distance: int = 1): | ||
match direction: | ||
case "N" | "North" | "Up": | ||
self.move_relative((-1, 0), distance) | ||
case "S" | "South" | "Down": | ||
self.move_relative((1, 0), distance) | ||
case "E" | "East" | "Right": | ||
self.move_relative((0, 1), distance) | ||
case "W" | "West" | "Left": | ||
self.move_relative((0, -1), distance) | ||
case "NE" | "NorthEast" | "UpRight": | ||
self.move_relative((-1, 1), distance) | ||
case "NW" | "NorthWest" | "UpLeft": | ||
self.move_relative((-1, -1), distance) | ||
case "SE" | "SouthEast" | "DownRight": | ||
self.move_relative((1, 1), distance) | ||
case "SW" | "SouthWest" | "DownLeft": | ||
self.move_relative((1, -1), distance) | ||
case _: | ||
raise ValueError(f"Invalid direction: {direction}") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
direction = direction.lower()
and then match against the lower case strings for example.I think none of these are blocking for this PR, just a few thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiscreteSpace