Skip to content
Open
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
df26a23
test commit
suryanshgargbpgc Mar 11, 2025
7afe600
Added move_to function in DiscreteSpaceDF class
suryanshgargbpgc Mar 17, 2025
1d75987
Merge branch 'main' into optimizefunction
suryanshgargbpgc Mar 17, 2025
d26561e
Revert "test commit"
suryanshgargbpgc Mar 18, 2025
94daca7
moving type hint to types_ module
adamamer20 Mar 19, 2025
4bd0533
tests for move_to function
suryanshgargbpgc Mar 21, 2025
25e3baf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 21, 2025
4859e2c
Update test_grid_polars.py
suryanshgargbpgc Mar 21, 2025
a9d9e8f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 21, 2025
06ac4eb
Update test_grid_polars.py
suryanshgargbpgc Mar 22, 2025
ab6f845
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2025
fc6be7c
Update space.py
suryanshgargbpgc Mar 22, 2025
3f7c26d
Merge branch 'optimizefunction' of https://github.com/suryanshgargbpg…
suryanshgargbpgc Mar 22, 2025
002a5d9
Update space.py
suryanshgargbpgc Mar 22, 2025
08c927d
Update agents.py
suryanshgargbpgc Mar 22, 2025
ec10744
adds blank line
suryanshgargbpgc Mar 22, 2025
0747f98
Update test_grid_polars.py
suryanshgargbpgc Mar 22, 2025
8cd426b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2025
5b8f8db
adds tests for move_to_optimal
suryanshgargbpgc Mar 23, 2025
3d28340
Update test_grid_polars.py
suryanshgargbpgc Mar 23, 2025
20b3968
Revert "Update test_grid_polars.py"
suryanshgargbpgc Mar 23, 2025
3a2dc16
update test_grid_polars
suryanshgargbpgc Mar 23, 2025
891f296
resolve conflicts
suryanshgargbpgc Mar 23, 2025
04246e8
Fix whitespace in AgentSetPandas docstring
suryanshgargbpgc Mar 24, 2025
f278e56
Fix docstring conflict in _prepare_cells method with proper format
suryanshgargbpgc Mar 24, 2025
56fdabc
Merge branch 'main' into optimizefunction
suryanshgargbpgc Mar 24, 2025
efc65e7
Merge branch 'main' into optimizefunction
adamamer20 Mar 29, 2025
f437548
update agenets.py
suryanshgargbpgc Mar 30, 2025
90351ba
Merge upstream changes and resolve conflicts
suryanshgargbpgc Apr 15, 2025
02e49c2
Revert "Merge upstream changes and resolve conflicts"
suryanshgargbpgc Apr 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 108 additions & 2 deletions mesa_frames/abstract/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@
from abc import abstractmethod
from collections.abc import Callable, Collection, Sequence, Sized
from itertools import product
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING, Literal, TypeVar, Union
from warnings import warn

import numpy as np
import polars as pl
from numpy.random import Generator
from typing_extensions import Any, Self

from mesa_frames import AgentsDF

from mesa_frames.concrete.polars.agentset import AgentSetPolars
from mesa_frames.concrete.agents import AgentsDF
from mesa_frames.abstract.agents import AgentContainer, AgentSetDF
from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin
from mesa_frames.types_ import (
Expand All @@ -79,6 +81,9 @@

ESPG = int


AgentLike = Union[AgentSetPolars, pl.DataFrame]

if TYPE_CHECKING:
from mesa_frames.concrete.model import ModelDF

Expand Down Expand Up @@ -1036,6 +1041,107 @@
def __str__(self) -> str:
return f"{self.__class__.__name__}\n{str(self.cells)}"

def move_to(
self,
agents: AgentLike,
attr_names: str | list[str],
rank_order: str | list[str] = "max",
radius: int | pl.Series = None,
include_center: bool = True,
shuffle: bool = True
) -> None:
if isinstance(attr_names, str):
attr_names = [attr_names]
if isinstance(rank_order, str):
rank_order = [rank_order] * len(attr_names)
if len(attr_names) != len(rank_order):
raise ValueError("attr_names and rank_order must have the same length")
if radius is None:
if "vision" in agents.columns:
radius = agents["vision"]

Check warning on line 1061 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1053-L1061

Added lines #L1053 - L1061 were not covered by tests
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.

else:
raise ValueError("radius must be specified if agents do not have a 'vision' attribute")
neighborhood = self.get_neighborhood(

Check warning on line 1064 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1063-L1064

Added lines #L1063 - L1064 were not covered by tests
radius=radius,
agents=agents,
include_center=include_center
)
neighborhood = neighborhood.join(self.cells, on=["dim_0", "dim_1"])
neighborhood = neighborhood.with_columns(

Check warning on line 1070 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1069-L1070

Added lines #L1069 - L1070 were not covered by tests
agent_id_center=neighborhood.join(
agents.pos,
left_on=["dim_0_center", "dim_1_center"],
right_on=["dim_0", "dim_1"],
)["unique_id"]
)
if shuffle:
agent_order = (

Check warning on line 1078 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1077-L1078

Added lines #L1077 - L1078 were not covered by tests
neighborhood
.unique(subset=["agent_id_center"], keep="first")
.select("agent_id_center")
.sample(fraction=1.0, seed=self.model.random.integers(0, 2**31-1))
.with_row_index("agent_order")
)
else:
agent_order = (

Check warning on line 1086 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1086

Added line #L1086 was not covered by tests
neighborhood
.unique(subset=["agent_id_center"], keep="first", maintain_order=True)
.with_row_index("agent_order")
.select(["agent_id_center", "agent_order"])
)
neighborhood = neighborhood.join(agent_order, on="agent_id_center")
sort_cols = []
sort_desc = []
for attr, order in zip(attr_names, rank_order):
sort_cols.append(attr)
sort_desc.append(order.lower() == "max")
neighborhood = neighborhood.sort(

Check warning on line 1098 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1092-L1098

Added lines #L1092 - L1098 were not covered by tests
sort_cols + ["radius", "dim_0", "dim_1"],
descending=sort_desc + [False, False, False]
)
neighborhood = neighborhood.join(

Check warning on line 1102 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1102

Added line #L1102 was not covered by tests
agent_order.select(
pl.col("agent_id_center").alias("agent_id"),
pl.col("agent_order").alias("blocking_agent_order"),
),
on="agent_id",
how="left",
).rename({"agent_id": "blocking_agent_id"})
best_moves = pl.DataFrame()
while len(best_moves) < len(agents):
neighborhood = neighborhood.with_columns(

Check warning on line 1112 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1110-L1112

Added lines #L1110 - L1112 were not covered by tests
priority=pl.col("agent_order").cum_count().over(["dim_0", "dim_1"])
)
new_best_moves = (

Check warning on line 1115 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1115

Added line #L1115 was not covered by tests
neighborhood.group_by("agent_id_center", maintain_order=True)
.first()
.unique(subset=["dim_0", "dim_1"], keep="first", maintain_order=True)
)
condition = pl.col("blocking_agent_id").is_null() | (

Check warning on line 1120 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1120

Added line #L1120 was not covered by tests
pl.col("blocking_agent_id") == pl.col("agent_id_center")
)
if len(best_moves) > 0:
condition = condition | pl.col("blocking_agent_id").is_in(

Check warning on line 1124 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1123-L1124

Added lines #L1123 - L1124 were not covered by tests
best_moves["agent_id_center"]
)
condition = condition & (pl.col("priority") == 1)
new_best_moves = new_best_moves.filter(condition)
if len(new_best_moves) == 0:
break
best_moves = pl.concat([best_moves, new_best_moves])
neighborhood = neighborhood.filter(

Check warning on line 1132 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1127-L1132

Added lines #L1127 - L1132 were not covered by tests
~pl.col("agent_id_center").is_in(best_moves["agent_id_center"])
)
neighborhood = neighborhood.join(

Check warning on line 1135 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1135

Added line #L1135 was not covered by tests
best_moves.select(["dim_0", "dim_1"]), on=["dim_0", "dim_1"], how="anti"
)
if len(best_moves) > 0:
self.move_agents(

Check warning on line 1139 in mesa_frames/abstract/space.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/space.py#L1138-L1139

Added lines #L1138 - L1139 were not covered by tests
best_moves.sort("agent_order")["agent_id_center"],
best_moves.sort("agent_order").select(["dim_0", "dim_1"])
)


@property
def cells(self) -> DataFrame:
"""
Expand Down