Skip to content

Commit

Permalink
remove unique_id and model.next_id
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Sep 5, 2024
1 parent ec07d89 commit 98283c7
Show file tree
Hide file tree
Showing 38 changed files with 102 additions and 127 deletions.
9 changes: 4 additions & 5 deletions examples/aco_tsp/aco_tsp/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ class AntTSP(mesa.Agent):
An agent
"""

def __init__(self, unique_id, model, alpha: float = 1.0, beta: float = 5.0):
def __init__(self, model, alpha: float = 1.0, beta: float = 5.0):
"""
Customize the agent
"""
self.unique_id = unique_id
super().__init__(model)
self.alpha = alpha
self.beta = beta
super().__init__(unique_id, model)
self._cities_visited = []
self._traveled_distance = 0
self.tsp_solution = []
Expand Down Expand Up @@ -176,8 +175,8 @@ def __init__(
self.max_steps = max_steps
self.grid = mesa.space.NetworkGrid(tsp_graph.g)

for i in range(self.num_agents):
agent = AntTSP(unique_id=i, model=self, alpha=ant_alpha, beta=ant_beta)
for _ in range(self.num_agents):
agent = AntTSP(model=self, alpha=ant_alpha, beta=ant_beta)

city = tsp_graph.cities[self.random.randrange(self.num_cities)]
self.grid.place_agent(agent, city)
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 @@ -45,9 +45,9 @@ def bank_balance(self):

# subclass of RandomWalker, which is subclass to Mesa Agent
class Person(RandomWalker):
def __init__(self, unique_id, model, moore, bank, rich_threshold):
def __init__(self, model, moore, bank, rich_threshold):
# init parent class with required parameters
super().__init__(unique_id, model, moore=moore)
super().__init__(model, moore=moore)
# the amount each person has in savings
self.savings = 0
# total loan amount person has outstanding
Expand Down
4 changes: 2 additions & 2 deletions examples/bank_reserves/bank_reserves/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ def __init__(
self.bank = Bank(self, self.reserve_percent)

# create people for the model according to number of people set by user
for i in range(self.init_people):
for _ in range(self.init_people):
# set x, y coords randomly within the grid
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
p = Person(i, self, True, self.bank, self.rich_threshold)
p = Person(self, True, self.bank, self.rich_threshold)
# place the Person object on the grid at coordinates (x, y)
self.grid.place_agent(p, (x, y))

Expand Down
4 changes: 2 additions & 2 deletions examples/bank_reserves/bank_reserves/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class RandomWalker(mesa.Agent):
# use a Moore neighborhood
moore = True

def __init__(self, unique_id, model, moore=True):
def __init__(self, model, moore=True):
"""
grid: The MultiGrid object in which the agent lives.
x: The agent's current x coordinate
y: The agent's current y coordinate
moore: If True, may move in all 8 directions.
Otherwise, only up, down, left, right.
"""
super().__init__(unique_id, model)
super().__init__(model)
self.moore = moore

def random_move(self):
Expand Down
7 changes: 2 additions & 5 deletions examples/boid_flockers/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Boid(mesa.Agent):

def __init__(
self,
unique_id,
model,
speed,
direction,
Expand All @@ -40,7 +39,6 @@ def __init__(
Create a new Boid flocker agent.
Args:
unique_id: Unique agent identifier.
speed: Distance to move per step.
direction: numpy vector for the Boid's direction of movement.
vision: Radius to look around for nearby Boids.
Expand All @@ -49,7 +47,7 @@ def __init__(
separate: the relative importance of avoiding close neighbors
match: the relative importance of matching neighbors' headings
"""
super().__init__(unique_id, model)
super().__init__(model)
self.speed = speed
self.direction = direction
self.vision = vision
Expand Down Expand Up @@ -129,13 +127,12 @@ def make_agents(self):
"""
Create self.population agents, with random positions and starting headings.
"""
for i in range(self.population):
for _ in range(self.population):
x = self.random.random() * self.space.x_max
y = self.random.random() * self.space.y_max
pos = np.array((x, y))
direction = np.random.random(2) * 2 - 1
boid = Boid(
unique_id=i,
model=self,
speed=self.speed,
direction=direction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, N=100, width=10, height=10):
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
a = MoneyAgent(self)

# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
Expand All @@ -50,8 +50,8 @@ def run_model(self, n):
class MoneyAgent(mesa.Agent):
"""An agent with fixed initial wealth."""

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

def move(self):
Expand Down
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 @@ -27,7 +27,7 @@ def __init__(self, N=100, width=10, height=10):
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
a = MoneyAgent(self)

# Add the agent to a random grid cell
x = self.random.randrange(self.grid.width)
Expand All @@ -50,8 +50,8 @@ def run_model(self, n):
class MoneyAgent(mesa.Agent):
"""An agent with fixed initial wealth."""

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

def move(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, num_agents=7, num_nodes=10):

# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
a = MoneyAgent(self)

# Add the agent to a random node
self.grid.place_agent(a, list_of_random_nodes[i])
Expand All @@ -50,8 +50,8 @@ def run_model(self, n):
class MoneyAgent(mesa.Agent):
"""An agent with fixed initial wealth."""

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

def move(self):
Expand Down
7 changes: 3 additions & 4 deletions examples/caching_and_replay/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ class SchellingAgent(mesa.Agent):
Schelling segregation agent
"""

def __init__(self, unique_id, model, agent_type):
def __init__(self, model, agent_type):
"""
Create a new Schelling agent.
Args:
unique_id: Unique identifier for the agent.
x, y: Agent initial location.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(unique_id, model)
super().__init__(model)
self.type = agent_type

def step(self):
Expand Down Expand Up @@ -84,7 +83,7 @@ def __init__(
for _, pos in self.grid.coord_iter():
if self.random.random() < self.density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
agent = SchellingAgent(self.next_id(), self, agent_type)
agent = SchellingAgent(self, agent_type)
self.grid.place_agent(agent, pos)

self.datacollector.collect(self)
Expand Down
4 changes: 2 additions & 2 deletions examples/charts/charts/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def bank_balance(self):

# subclass of RandomWalker, which is subclass to Mesa Agent
class Person(RandomWalker):
def __init__(self, unique_id, model, moore, bank, rich_threshold):
def __init__(self, model, moore, bank, rich_threshold):
# init parent class with required parameters
super().__init__(unique_id, model, moore=moore)
super().__init__(model, moore=moore)
# the amount each person has in savings
self.savings = 0
# total loan amount person has outstanding
Expand Down
4 changes: 2 additions & 2 deletions examples/charts/charts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def __init__(
self.bank = Bank(self, self.reserve_percent)

# create people for the model according to number of people set by user
for i in range(self.init_people):
for _ in range(self.init_people):
# set x, y coords randomly within the grid
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
p = Person(i, self, True, self.bank, self.rich_threshold)
p = Person(self, True, self.bank, self.rich_threshold)
# place the Person object on the grid at coordinates (x, y)
self.grid.place_agent(p, (x, y))

Expand Down
4 changes: 2 additions & 2 deletions examples/charts/charts/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class RandomWalker(mesa.Agent):
# use a Moore neighborhood
moore = True

def __init__(self, unique_id, model, moore=True):
def __init__(self, model, moore=True):
"""
grid: The MultiGrid object in which the agent lives.
x: The agent's current x coordinate
y: The agent's current y coordinate
moore: If True, may move in all 8 directions.
Otherwise, only up, down, left, right.
"""
super().__init__(unique_id, model)
super().__init__(model)
self.moore = moore

def random_move(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, pos, model, initial_state):
"""
Create a cell, in the given state, at the given row, col position.
"""
super().__init__(pos, model)
super().__init__(model)
self._row = pos[0]
self._col = pos[1]
self._state = initial_state
Expand Down
2 changes: 1 addition & 1 deletion examples/conways_game_of_life/conways_game_of_life/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, pos, model, init_state=DEAD):
"""
Create a cell, in the given state, at the given x, y position.
"""
super().__init__(pos, model)
super().__init__(model)
self.x, self.y = pos
self.state = init_state
self._nextState = None
Expand Down
4 changes: 2 additions & 2 deletions examples/el_farol/el_farol/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class BarCustomer(mesa.Agent):
def __init__(self, unique_id, model, memory_size, crowd_threshold, num_strategies):
super().__init__(unique_id, model)
def __init__(self, model, memory_size, crowd_threshold, num_strategies):
super().__init__(model)
# Random values from -1.0 to 1.0
self.strategies = np.random.rand(num_strategies, memory_size + 1) * 2 - 1
self.best_strategy = self.strategies[0]
Expand Down
4 changes: 2 additions & 2 deletions examples/el_farol/el_farol/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def __init__(
# strategies would have worked.
self.history = np.random.randint(0, 100, size=memory_size * 2).tolist()
self.attendance = self.history[-1]
for i in range(self.num_agents):
BarCustomer(i, self, memory_size, crowd_threshold, num_strategies)
for _ in range(self.num_agents):
BarCustomer(self, memory_size, crowd_threshold, num_strategies)

self.datacollector = mesa.DataCollector(
model_reporters={"Customers": "attendance"},
Expand Down
10 changes: 3 additions & 7 deletions examples/epstein_civil_violence/epstein_civil_violence/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Citizen(mesa.Agent):
Summary of rule: If grievance - risk > threshold, rebel.
Attributes:
unique_id: unique int
x, y: Grid coordinates
hardship: Agent's 'perceived hardship (i.e., physical or economic
privation).' Exogenous, drawn from U(0,1).
Expand All @@ -30,7 +29,6 @@ class Citizen(mesa.Agent):

def __init__(
self,
unique_id,
model,
pos,
hardship,
Expand All @@ -42,7 +40,6 @@ def __init__(
"""
Create a new Citizen.
Args:
unique_id: unique int
x, y: Grid coordinates
hardship: Agent's 'perceived hardship (i.e., physical or economic
privation).' Exogenous, drawn from U(0,1).
Expand All @@ -55,7 +52,7 @@ def __init__(
agent can inspect. Exogenous.
model: model instance
"""
super().__init__(unique_id, model)
super().__init__(model)
self.breed = "citizen"
self.pos = pos
self.hardship = hardship
Expand Down Expand Up @@ -129,17 +126,16 @@ class Cop(mesa.Agent):
able to inspect
"""

def __init__(self, unique_id, model, pos, vision):
def __init__(self, model, pos, vision):
"""
Create a new Cop.
Args:
unique_id: unique int
x, y: Grid coordinates
vision: number of cells in each direction (N, S, E and W) that
agent can inspect. Exogenous.
model: model instance
"""
super().__init__(unique_id, model)
super().__init__(model)
self.breed = "cop"
self.pos = pos
self.vision = vision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@ def __init__(
self.datacollector = mesa.DataCollector(
model_reporters=model_reporters, agent_reporters=agent_reporters
)
unique_id = 0
if self.cop_density + self.citizen_density > 1:
raise ValueError("Cop density + citizen density must be less than 1")

for contents, (x, y) in self.grid.coord_iter():
if self.random.random() < self.cop_density:
cop = Cop(unique_id, self, (x, y), vision=self.cop_vision)
unique_id += 1
cop = Cop(self, (x, y), vision=self.cop_vision)
self.grid[x][y] = cop

elif self.random.random() < (self.cop_density + self.citizen_density):
citizen = Citizen(
unique_id,
self,
(x, y),
hardship=self.random.random(),
Expand All @@ -98,7 +96,6 @@ def __init__(
threshold=self.active_threshold,
vision=self.citizen_vision,
)
unique_id += 1
self.grid[x][y] = citizen

self.running = True
Expand Down
Loading

0 comments on commit 98283c7

Please sign in to comment.