Skip to content

Commit

Permalink
whitespace fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamamer20 committed Sep 22, 2024
1 parent 801a98c commit dff31aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
42 changes: 21 additions & 21 deletions examples/sugarscape_ig/performance_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def __init__(self, n: int):
else:
density = 0.04 # mesa
self.n = n
self.seed = 42
self.seed = 42
dimension = math.ceil(math.sqrt(n / density))
random_gen = np.random.default_rng(self.seed)
random_gen = np.random.default_rng(self.seed)
self.sugar_grid = random_gen.integers(0, 4, (dimension, dimension))
self.initial_sugar = random_gen.integers(6, 25, n)
self.metabolism = random_gen.integers(2, 4, n)
self.vision = random_gen.integers(1, 6, n)
self.initial_positions = pl.DataFrame(
self.initial_positions = pl.DataFrame(
schema={"dim_0": pl.Int64, "dim_1": pl.Int64}
)
while self.initial_positions.shape[0] < n:
Expand All @@ -55,21 +55,21 @@ def __init__(self, n: int):
def mesa_implementation(setup: SugarScapeSetup):
return SugarscapeMesa(
setup.n,
setup.sugar_grid,
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.sugar_grid,
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.seed,
).run_model(100)


def mesa_frames_pandas_concise(setup: SugarScapeSetup):
return SugarscapePandas(
setup.n,
setup.sugar_grid,
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.sugar_grid,
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.seed,
).run_model(100)

Expand All @@ -82,8 +82,8 @@ def mesa_frames_polars_loop_DF(setup: SugarScapeSetup):
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.initial_positions,
setup.seed,
setup.initial_positions,
setup.seed,
)
model.run_model(100)

Expand All @@ -96,8 +96,8 @@ def mesa_frames_polars_loop_no_vec(setup: SugarScapeSetup):
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.initial_positions,
setup.seed,
setup.initial_positions,
setup.seed,
)
model.run_model(100)

Expand All @@ -110,8 +110,8 @@ def mesa_frames_polars_numba_cpu(setup: SugarScapeSetup):
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.initial_positions,
setup.seed,
setup.initial_positions,
setup.seed,
)
model.run_model(100)

Expand All @@ -124,8 +124,8 @@ def mesa_frames_polars_numba_gpu(setup: SugarScapeSetup):
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.initial_positions,
setup.seed,
setup.initial_positions,
setup.seed,
)
model.run_model(100)

Expand All @@ -138,8 +138,8 @@ def mesa_frames_polars_numba_parallel(setup: SugarScapeSetup):
setup.initial_sugar,
setup.metabolism,
setup.vision,
setup.initial_positions,
setup.seed,
setup.initial_positions,
setup.seed,
)
model.run_model(100)

Expand Down
32 changes: 16 additions & 16 deletions examples/sugarscape_ig/ss_polars/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _prepare_neighborhood(self, neighborhood, agent_order):
(pl.col("agent_order") >= pl.col("blocking_agent_order"))
| pl.col("blocking_agent_order").is_null()
)

# Sort neighborhood by agent_order & max_sugar (max_sugar because we will check anyway if the cell is empty)
# However, we need to make sure that the current agent cell is ordered by current sugar (since it's 0 until agent hasn't moved)
neighborhood = neighborhood.with_columns(
Expand All @@ -113,9 +113,9 @@ def _prepare_neighborhood(self, neighborhood, agent_order):
.otherwise(pl.col("max_sugar"))
).sort(
["agent_order", "max_sugar", "radius", "dim_0"],
descending=[False, True, False, False],
descending=[False, True, False, False],
)
return neighborhood
return neighborhood

def get_best_moves(self, neighborhood, agent_order):
raise NotImplementedError("This method should be implemented by subclasses")
Expand All @@ -127,7 +127,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame, agent_order):
# While there are agents that do not have a best move, keep looking for one

while len(best_moves) < len(self.agents):
# Check if there are previous agents that might make the same move
# Check if there are previous agents that might make the same move
neighborhood = neighborhood.with_columns(
priority=pl.col("agent_order").cum_count().over(["dim_0", "dim_1"])
)
Expand All @@ -137,16 +137,16 @@ def get_best_moves(self, neighborhood: pl.DataFrame, agent_order):
new_best_moves = (
neighborhood.group_by("agent_id_center", maintain_order=True)
.first()
.unique(subset=["dim_0", "dim_1"], keep="first", maintain_order=True)
.unique(subset=["dim_0", "dim_1"], keep="first", maintain_order=True)
)
# Agents can make the move if:
# - There is no blocking agent
# - The agent is in its own cell
# - The blocking agent has moved before him
# - There isn't a higher priority agent that might make the same move
# - There isn't a higher priority agent that might make the same move

condition = (
pl.col("blocking_agent_id").is_null()
pl.col("blocking_agent_id").is_null()
| (pl.col("blocking_agent_id") == pl.col("agent_id_center"))
) & (pl.col("priority") == 1)
if len(best_moves) > 0:
Expand All @@ -167,7 +167,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame, agent_order):
best_moves.select(["dim_0", "dim_1"]), on=["dim_0", "dim_1"], how="anti"
)

# Recompute priority
# Recompute priority
neighborhood = neighborhood.with_columns(
priority=pl.col("agent_order").cum_count().over(["dim_0", "dim_1"])
)
Expand Down Expand Up @@ -208,7 +208,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame, agent_order):

best_moves = (
neighborhood.fill_null(-1)
.cast({"agent_order": pl.Int32, "blocking_agent_order": pl.Int32})
.cast({"agent_order": pl.Int32, "blocking_agent_order": pl.Int32})
.select(
pl.struct(["agent_order", "blocking_agent_order"]).map_batches(
map_batches_func
Expand All @@ -220,7 +220,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame, agent_order):
)
.drop("agent_order")
)
assert best_moves.n_unique() == len(
assert best_moves.n_unique() == len(
best_moves
), "Duplicates found in best_moves"
return best_moves
Expand All @@ -233,7 +233,7 @@ def _prepare_cells(self, neighborhood: pl.DataFrame):
.with_columns(
flattened=(pl.col("dim_0") * self.space.dimensions[1] + pl.col("dim_1"))
)
.sort("agent_order")["flattened"]
.sort("agent_order")["flattened"]
.to_numpy()
)
free_cells = np.ones(
Expand Down Expand Up @@ -262,9 +262,9 @@ def inner_get_best_moves(
best_moves: np.ndarray,
) -> np.ndarray:
for i, agent in enumerate(agent_id_center):
# If the agent has not moved yet
# If the agent has not moved yet
if not processed_agents[agent]:
# If the target cell is free
# If the target cell is free
if free_cells[target_cells[i]] or blocking_agent[i] == agent:
best_moves[agent] = target_cells[i]
# Free current cell
Expand Down Expand Up @@ -294,7 +294,7 @@ def _get_best_moves(self):
"(n), (m), (p), (p), (p), (n)->(n)",
nopython=True,
target=self.numba_target,
writable_args=(
writable_args=(
"free_cells",
"processed_agents",
), # Writable inputs have to be declared
Expand All @@ -309,9 +309,9 @@ def vectorized_get_best_moves(
best_moves,
):
for i, agent in enumerate(agent_id_center):
# If the agent has not moved yet
# If the agent has not moved yet
if not processed_agents[agent]:
# If the target cell is free
# If the target cell is free
if free_cells[target_cells[i]] or blocking_agent[i] == agent:
best_moves[agent] = target_cells[i]
# Free current cell
Expand Down
10 changes: 5 additions & 5 deletions examples/sugarscape_ig/ss_polars/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(
initial_sugar: np.ndarray | None = None,
metabolism: np.ndarray | None = None,
vision: np.ndarray | None = None,
initial_positions: pl.DataFrame | None = None,
initial_positions: pl.DataFrame | None = None,
seed: int | None = None,
width: int | None = None,
height: int | None = None,
Expand All @@ -34,20 +34,20 @@ def __init__(
)
self.space.set_cells(sugar_grid)
self.agents += agent_type(self, n_agents, initial_sugar, metabolism, vision)
if initial_positions is not None:
if initial_positions is not None:
self.space.place_agents(self.agents, initial_positions)
else:
self.space.place_to_empty(self.agents)
self.space.place_to_empty(self.agents)

def run_model(self, steps: int) -> list[int]:
for _ in range(steps):
if len(self.agents) == 0:
return
empty_cells = self.space.empty_cells
empty_cells = self.space.empty_cells
full_cells = self.space.full_cells
max_sugar = self.space.cells.join(
empty_cells, on=["dim_0", "dim_1"]
).select(pl.col("max_sugar"))
self.space.set_cells(full_cells, {"sugar": 0})
self.space.set_cells(empty_cells, {"sugar": max_sugar})
self.step()
self.step()

0 comments on commit dff31aa

Please sign in to comment.