Skip to content

Commit

Permalink
bring in line with main
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Oct 7, 2024
1 parent 51b116b commit 53b4ca8
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/aco_tsp/aco_tsp/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mesa
import networkx as nx
import numpy as np
from mesa.spaces import CellAgent, Network
from mesa.experimental.cell_space import CellAgent, Network


@dataclass
Expand Down Expand Up @@ -116,7 +116,7 @@ def decide_next_city(self):
# Random
# new_city = self.random.choice(list(self.model.all_cities - set(self.cities_visited)))
# Choose closest city not yet visited
neighbors = self.cell.neighborhood()
neighbors = self.cell.neighborhood
candidates = [n for n in neighbors if n not in self._cities_visited]
if len(candidates) == 0:
return self.cell
Expand Down
4 changes: 2 additions & 2 deletions examples/bank_reserves/bank_reserves/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Northwestern University, Evanston, IL.
"""

from mesa.spaces import CellAgent
from mesa.experimental.cell_space import CellAgent


class Bank:
Expand Down Expand Up @@ -176,7 +176,7 @@ def take_out_loan(self, amount):

def step(self):
# move to a cell in my Moore neighborhood
self.move_to(self.cell.neighborhood().select_random_cell())
self.cell = self.cell.neighborhood.select_random_cell()
# trade
self.do_business()
# deposit money or take out a loan
Expand Down
2 changes: 1 addition & 1 deletion examples/bank_reserves/bank_reserves/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import mesa
import numpy as np
from mesa.spaces import OrthogonalMooreGrid
from mesa.experimental.cell_space import OrthogonalMooreGrid

from .agents import Bank, Person

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BoltzmannWealthModel(mesa.Model):
def __init__(self, N=100, width=10, height=10):
super().__init__()
self.num_agents = N
self.grid = mesa.spaces.OrthogonalMooreGrid(
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
(width, height), torus=True, random=self.random
)

Expand Down Expand Up @@ -49,7 +49,7 @@ def run_model(self, n):
self.step()


class MoneyAgent(mesa.spaces.CellAgent):
class MoneyAgent(mesa.experimental.cell_space.CellAgent):
"""An agent with fixed initial wealth."""

def __init__(self, model):
Expand All @@ -66,6 +66,6 @@ def give_money(self):
self.wealth -= 1

def step(self):
self.move_to(self.cell.neighborhood().select_random_cell())
self.cell = self.cell.neighborhood.select_random_cell()
if self.wealth > 0:
self.give_money()
6 changes: 3 additions & 3 deletions examples/boltzmann_wealth_model_experimental/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BoltzmannWealthModel(mesa.Model):
def __init__(self, N=100, width=10, height=10):
super().__init__()
self.num_agents = N
self.grid = mesa.spaces.OrthogonalMooreGrid(
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
(width, height), torus=True, random=self.random
)

Expand Down Expand Up @@ -49,7 +49,7 @@ def run_model(self, n):
self.step()


class MoneyAgent(mesa.spaces.CellAgent):
class MoneyAgent(mesa.experimental.cell_space.CellAgent):
"""An agent with fixed initial wealth."""

def __init__(self, model):
Expand All @@ -64,6 +64,6 @@ def give_money(self):
self.wealth -= 1

def step(self):
self.move_to(self.cell.neighborhood().select_random_cell())
self.cell = self.cell.neighborhood.select_random_cell()
if self.wealth > 0:
self.give_money()
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, num_agents=7, num_nodes=10):
self.num_agents = num_agents
self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)
self.grid = mesa.spaces.Network(self.G, random=self.random, capacity=1)
self.grid = mesa.experimental.cell_space.Network(self.G, random=self.random, capacity=1)

self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini},
Expand Down Expand Up @@ -47,24 +47,24 @@ def run_model(self, n):
self.step()


class MoneyAgent(mesa.spaces.CellAgent):
class MoneyAgent(mesa.experimental.cell_space.CellAgent):
"""An agent with fixed initial wealth."""

def __init__(self, model):
super().__init__(model)
self.wealth = 1

def give_money(self):
neighbors = [agent for agent in self.cell.neighborhood().agents if not self]
neighbors = [agent for agent in self.cell.neighborhood.agents if not self]
if len(neighbors) > 0:
other = self.random.choice(neighbors)
other.wealth += 1
self.wealth -= 1

def step(self):
empty_neighbors = [cell for cell in self.cell.neighborhood() if cell.is_empty]
empty_neighbors = [cell for cell in self.cell.neighborhood if cell.is_empty]
if empty_neighbors:
self.move_to(self.random.choice(empty_neighbors))
self.cell = self.random.choice(empty_neighbors)

if self.wealth > 0:
self.give_money()
4 changes: 2 additions & 2 deletions examples/charts/charts/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Northwestern University, Evanston, IL.
"""

from mesa.spaces import CellAgent
from mesa.experimental.cell_space import CellAgent


class Bank:
Expand Down Expand Up @@ -178,7 +178,7 @@ def take_out_loan(self, amount):

def step(self):
# move to a cell in my Moore neighborhood
self.move_to(self.cell.neighborhood().select_random_cell())
self.cell = self.cell.neighborhood.select_random_cell()
# trade
self.do_business()
# deposit money or take out a loan
Expand Down
2 changes: 1 addition & 1 deletion examples/charts/charts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np

from .agents import Bank, Person
from mesa.spaces import OrthogonalMooreGrid
from mesa.experimental.cell_space import OrthogonalMooreGrid

"""
If you want to perform a parameter sweep, call batch_run.py instead of run.py.
Expand Down
6 changes: 3 additions & 3 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import mesa


class ColorCell(mesa.spaces.CellAgent):
class ColorCell(mesa.experimental.cell_space.CellAgent):
"""
Represents a cell's opinion (visualized by a color)
"""
Expand Down Expand Up @@ -37,7 +37,7 @@ def determine_opinion(self):
A choice is made at random in case of a tie
The next state is stored until all cells have been polled
"""
neighbors = self.cell.neighborhood().agents
neighbors = self.cell.neighborhood.agents
neighbors_opinion = Counter(n.state for n in neighbors)
# Following is a a tuple (attribute, occurrences)
polled_opinions = neighbors_opinion.most_common()
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, width=20, height=20):
The agents next state is first determined before updating the grid
"""
super().__init__()
self._grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=False)
self._grid = mesa.experimental.cell_space.OrthogonalMooreGrid((width, height), torus=False)

# self._grid.coord_iter()
# --> should really not return content + col + row
Expand Down
4 changes: 2 additions & 2 deletions examples/conways_game_of_life/conways_game_of_life/cell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mesa


class Cell(mesa.spaces.CellAgent):
class Cell(mesa.experimental.cell_space.CellAgent):
"""Represents a single ALIVE or DEAD cell in the simulation."""

DEAD = 0
Expand All @@ -21,7 +21,7 @@ def is_alive(self):

@property
def neighbors(self):
return self.cell.neighborhood().agents
return self.cell.neighborhood.agents

def determine_state(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, width=50, height=50):
"""
super().__init__()
# Use a simple grid, where edges wrap around.
self.grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=True)
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid((width, height), torus=True)

# Place a cell at each location, with some initialized to
# ALIVE and some to DEAD.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import mesa


class EpsteinAgent(mesa.spaces.CellAgent):
class EpsteinAgent(mesa.experimental.cell_space.CellAgent):
def update_neighbors(self):
"""
Look around and see who my neighbors are
"""
self.neighborhood = self.cell.neighborhood(radius=self.vision)
self.neighborhood = self.cell.get_neighborhood(radius=self.vision)

self.neighbors = self.neighborhood.agents
self.empty_neighbors = [c for c in self.neighborhood if c.is_empty]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
self.max_iters = max_iters
self.iteration = 0

self.grid = mesa.spaces.OrthogonalMooreGrid(
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
(width, height), capacity=1, torus=True
)

Expand Down

0 comments on commit 53b4ca8

Please sign in to comment.