Skip to content

Commit

Permalink
Address duplicate position warning (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Sep 24, 2024
1 parent 875bfe2 commit bbec7d7
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 53 deletions.
4 changes: 0 additions & 4 deletions examples/boid_flockers/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(
self,
unique_id,
model,
pos,
speed,
direction,
vision,
Expand All @@ -42,7 +41,6 @@ def __init__(
Args:
unique_id: Unique agent identifier.
pos: Starting position
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 @@ -52,7 +50,6 @@ def __init__(
match: the relative importance of matching neighbors' headings
"""
super().__init__(unique_id, model)
self.pos = np.array(pos)
self.speed = speed
self.direction = direction
self.vision = vision
Expand Down Expand Up @@ -140,7 +137,6 @@ def make_agents(self):
boid = Boid(
unique_id=i,
model=self,
pos=pos,
speed=self.speed,
direction=direction,
vision=self.vision,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ def network_portrayal(G):
"id": node_id,
"size": 3 if agents else 1,
"color": "#CC0000" if not agents or agents[0].wealth == 0 else "#007959",
"label": None
if not agents
else f"Agent:{agents[0].unique_id} Wealth:{agents[0].wealth}",
"label": (
None
if not agents
else f"Agent:{agents[0].unique_id} Wealth:{agents[0].wealth}"
),
}
for (node_id, agents) in G.nodes.data("agent")
]
Expand Down
1 change: 1 addition & 0 deletions examples/color_patches/color_patches/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
handles the definition of the canvas parameters and
the drawing of the model representation on the canvas
"""

# import webbrowser

import mesa
Expand Down
7 changes: 3 additions & 4 deletions examples/forest_fire/forest_fire/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class TreeCell(mesa.Agent):
practice to give one to each agent anyway.
"""

def __init__(self, pos, model):
def __init__(self, unique_id, model):
"""
Create a new tree.
Args:
pos: The tree's coordinates on the grid.
unique_id: Unique identifier for the agent.
model: standard model reference for agent.
"""
super().__init__(pos, model)
self.pos = pos
super().__init__(unique_id, model)
self.condition = "Fine"

def step(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/forest_fire/forest_fire/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, width=100, height=100, density=0.65):
for contents, (x, y) in self.grid.coord_iter():
if self.random.random() < density:
# Create a tree
new_tree = TreeCell((x, y), self)
new_tree = TreeCell(self.next_id(), self)
# Set all trees in the first column on fire.
if x == 0:
new_tree.condition = "On Fire"
Expand Down
7 changes: 3 additions & 4 deletions examples/pd_grid/pd_grid/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
class PDAgent(mesa.Agent):
"""Agent member of the iterated, spatial prisoner's dilemma model."""

def __init__(self, pos, model, starting_move=None):
def __init__(self, unique_id, model, starting_move=None):
"""
Create a new Prisoner's Dilemma agent.
Args:
pos: (x, y) tuple of the agent's position.
unique_id: Unique identifier for the agent.
model: model instance
starting_move: If provided, determines the agent's initial state:
C(ooperating) or D(efecting). Otherwise, random.
"""
super().__init__(pos, model)
self.pos = pos
super().__init__(unique_id, model)
self.score = 0
if starting_move:
self.move = starting_move
Expand Down
2 changes: 1 addition & 1 deletion examples/pd_grid/pd_grid/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
# Create agents
for x in range(width):
for y in range(height):
agent = PDAgent((x, y), self)
agent = PDAgent(self.next_id(), self)
self.grid.place_agent(agent, (x, y))
self.schedule.add(agent)

Expand Down
8 changes: 3 additions & 5 deletions examples/schelling_experimental/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ class SchellingAgent(mesa.Agent):
Schelling segregation agent
"""

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

def step(self):
Expand Down Expand Up @@ -57,7 +55,7 @@ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=
for _, pos in self.grid.coord_iter():
if self.random.random() < density:
agent_type = 1 if self.random.random() < minority_pc else 0
agent = SchellingAgent(pos, self, agent_type)
agent = SchellingAgent(self.next_id(), self, agent_type)
self.grid.place_agent(agent, pos)

self.datacollector.collect(self)
Expand Down
7 changes: 2 additions & 5 deletions examples/sugarscape_cg/sugarscape_cg/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ def get_distance(pos_1, pos_2):


class SsAgent(mesa.Agent):
def __init__(
self, unique_id, pos, model, moore=False, sugar=0, metabolism=0, vision=0
):
def __init__(self, unique_id, model, moore=False, sugar=0, metabolism=0, vision=0):
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore
self.sugar = sugar
self.metabolism = metabolism
Expand Down Expand Up @@ -74,7 +71,7 @@ def step(self):


class Sugar(mesa.Agent):
def __init__(self, unique_id, pos, model, max_sugar):
def __init__(self, unique_id, model, max_sugar):
super().__init__(unique_id, model)
self.amount = max_sugar
self.max_sugar = max_sugar
Expand Down
4 changes: 2 additions & 2 deletions examples/sugarscape_cg/sugarscape_cg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, width=50, height=50, initial_population=100):
agent_id = 0
for _, (x, y) in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
sugar = Sugar(agent_id, (x, y), self, max_sugar)
sugar = Sugar(agent_id, self, max_sugar)
agent_id += 1
self.grid.place_agent(sugar, (x, y))
self.schedule.add(sugar)
Expand All @@ -62,7 +62,7 @@ def __init__(self, width=50, height=50, initial_population=100):
sugar = self.random.randrange(6, 25)
metabolism = self.random.randrange(2, 4)
vision = self.random.randrange(1, 6)
ssa = SsAgent(agent_id, (x, y), self, False, sugar, metabolism, vision)
ssa = SsAgent(agent_id, self, False, sugar, metabolism, vision)
agent_id += 1
self.grid.place_agent(ssa, (x, y))
self.schedule.add(ssa)
Expand Down
3 changes: 1 addition & 2 deletions examples/sugarscape_g1mt/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(
for _, (x, y) in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
max_spice = spice_distribution[x, y]
resource = Resource(agent_id, self, (x, y), max_sugar, max_spice)
resource = Resource(agent_id, self, max_sugar, max_spice)
self.schedule.add(resource)
self.grid.place_agent(resource, (x, y))
agent_id += 1
Expand All @@ -123,7 +123,6 @@ def __init__(
trader = Trader(
agent_id,
self,
(x, y),
moore=False,
sugar=sugar,
spice=spice,
Expand Down
3 changes: 1 addition & 2 deletions examples/sugarscape_g1mt/sugarscape_g1mt/resource_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class Resource(mesa.Agent):
- grows 1 amount of spice at each turn
"""

def __init__(self, unique_id, model, pos, max_sugar, max_spice):
def __init__(self, unique_id, model, max_sugar, max_spice):
super().__init__(unique_id, model)
self.pos = pos
self.sugar_amount = max_sugar
self.max_sugar = max_sugar
self.spice_amount = max_spice
Expand Down
2 changes: 0 additions & 2 deletions examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(
self,
unique_id,
model,
pos,
moore=False,
sugar=0,
spice=0,
Expand All @@ -40,7 +39,6 @@ def __init__(
vision=0,
):
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore
self.sugar = sugar
self.spice = spice
Expand Down
21 changes: 8 additions & 13 deletions examples/wolf_sheep/wolf_sheep/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Sheep(RandomWalker):

energy = None

def __init__(self, unique_id, pos, model, moore, energy=None):
super().__init__(unique_id, pos, model, moore=moore)
def __init__(self, unique_id, model, moore, energy=None):
super().__init__(unique_id, model, moore=moore)
self.energy = energy

def step(self):
Expand Down Expand Up @@ -44,9 +44,7 @@ def step(self):
# Create a new sheep:
if self.model.grass:
self.energy /= 2
lamb = Sheep(
self.model.next_id(), self.pos, self.model, self.moore, self.energy
)
lamb = Sheep(self.model.next_id(), self.model, self.moore, self.energy)
self.model.grid.place_agent(lamb, self.pos)
self.model.schedule.add(lamb)

Expand All @@ -58,8 +56,8 @@ class Wolf(RandomWalker):

energy = None

def __init__(self, unique_id, pos, model, moore, energy=None):
super().__init__(unique_id, pos, model, moore=moore)
def __init__(self, unique_id, model, moore, energy=None):
super().__init__(unique_id, model, moore=moore)
self.energy = energy

def step(self):
Expand All @@ -86,10 +84,8 @@ def step(self):
if self.random.random() < self.model.wolf_reproduce:
# Create a new wolf cub
self.energy /= 2
cub = Wolf(
self.model.next_id(), self.pos, self.model, self.moore, self.energy
)
self.model.grid.place_agent(cub, cub.pos)
cub = Wolf(self.model.next_id(), self.model, self.moore, self.energy)
self.model.grid.place_agent(cub, self.pos)
self.model.schedule.add(cub)


Expand All @@ -98,7 +94,7 @@ class GrassPatch(mesa.Agent):
A patch of grass that grows at a fixed rate and it is eaten by sheep
"""

def __init__(self, unique_id, pos, model, fully_grown, countdown):
def __init__(self, unique_id, model, fully_grown, countdown):
"""
Creates a new patch of grass
Expand All @@ -109,7 +105,6 @@ def __init__(self, unique_id, pos, model, fully_grown, countdown):
super().__init__(unique_id, model)
self.fully_grown = fully_grown
self.countdown = countdown
self.pos = pos

def step(self):
if not self.fully_grown:
Expand Down
6 changes: 3 additions & 3 deletions examples/wolf_sheep/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
energy = self.random.randrange(2 * self.sheep_gain_from_food)
sheep = Sheep(self.next_id(), (x, y), self, True, energy)
sheep = Sheep(self.next_id(), self, True, energy)
self.grid.place_agent(sheep, (x, y))
self.schedule.add(sheep)

Expand All @@ -107,7 +107,7 @@ def __init__(
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
energy = self.random.randrange(2 * self.wolf_gain_from_food)
wolf = Wolf(self.next_id(), (x, y), self, True, energy)
wolf = Wolf(self.next_id(), self, True, energy)
self.grid.place_agent(wolf, (x, y))
self.schedule.add(wolf)

Expand All @@ -121,7 +121,7 @@ def __init__(
else:
countdown = self.random.randrange(self.grass_regrowth_time)

patch = GrassPatch(self.next_id(), (x, y), self, fully_grown, countdown)
patch = GrassPatch(self.next_id(), self, fully_grown, countdown)
self.grid.place_agent(patch, (x, y))
self.schedule.add(patch)

Expand Down
3 changes: 1 addition & 2 deletions examples/wolf_sheep/wolf_sheep/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RandomWalker(mesa.Agent):
y = None
moore = True

def __init__(self, unique_id, pos, model, moore=True):
def __init__(self, unique_id, model, moore=True):
"""
grid: The MultiGrid object in which the agent lives.
x: The agent's current x coordinate
Expand All @@ -27,7 +27,6 @@ def __init__(self, unique_id, pos, model, moore=True):
Otherwise, only up, down, left, right.
"""
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore

def random_move(self):
Expand Down

0 comments on commit bbec7d7

Please sign in to comment.