From dff31aa0e6f5e0abe4ddeb58cd792fb1c41373e2 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 22 Sep 2024 20:34:22 +0200 Subject: [PATCH] whitespace fixes --- .../sugarscape_ig/performance_comparison.py | 42 +++++++++---------- examples/sugarscape_ig/ss_polars/agents.py | 32 +++++++------- examples/sugarscape_ig/ss_polars/model.py | 10 ++--- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/examples/sugarscape_ig/performance_comparison.py b/examples/sugarscape_ig/performance_comparison.py index bb461b2..23a8273 100644 --- a/examples/sugarscape_ig/performance_comparison.py +++ b/examples/sugarscape_ig/performance_comparison.py @@ -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: @@ -55,10 +55,10 @@ 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) @@ -66,10 +66,10 @@ def mesa_implementation(setup: SugarScapeSetup): 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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py index c428b39..08afdc7 100644 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ b/examples/sugarscape_ig/ss_polars/agents.py @@ -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( @@ -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") @@ -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"]) ) @@ -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: @@ -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"]) ) @@ -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 @@ -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 @@ -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( @@ -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 @@ -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 @@ -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 diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index 22df018..be9768c 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -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, @@ -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()