Skip to content

Commit 4ad5bb5

Browse files
committed
Merge branch 'new_spaces' of https://github.com/quaquel/mesa-examples into new_spaces
2 parents d2c8013 + 999d2e9 commit 4ad5bb5

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

examples/hotelling_law/hotelling_law/model.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ def get_store_price_lambda(unique_id):
140140
"""Return a lambda function that gets the
141141
price of a store by its unique ID."""
142142
return lambda m: next(
143-
(agent.price for agent in m.agents_by_type[StoreAgent] if agent.unique_id == unique_id), 0
143+
(
144+
agent.price
145+
for agent in m.agents_by_type[StoreAgent]
146+
if agent.unique_id == unique_id
147+
),
148+
0,
144149
)
145150

146151
@staticmethod
@@ -211,10 +216,10 @@ def step(self):
211216

212217
def recalculate_market_share(self):
213218
# Reset market share for all stores directly
214-
for store in self.agents_by_type[StoreAgent]:
219+
for store in self.agents_by_type[StoreAgent]:
215220
store.market_share = 0
216221

217-
for consumer in self.agents_by_type[ConsumerAgent]:
222+
for consumer in self.agents_by_type[ConsumerAgent]:
218223
preferred_store = consumer.determine_preferred_store()
219224
if preferred_store:
220225
preferred_store.market_share += 1
@@ -238,15 +243,17 @@ def export_data(self):
238243
def compute_average_price(self):
239244
if len(self.store_agents) == 0:
240245
return 0
241-
return np.mean([agent.price for agent in self.agents_by_type[StoreAgent]])
246+
return np.mean([agent.price for agent in self.agents_by_type[StoreAgent]])
242247

243248
# Function to compute the average market share for all store agents,
244249
def compute_average_market_share(self):
245250
if not self.store_agents:
246251
return 0
247252

248-
total_consumers = sum(agent.market_share for agent in self.agents_by_type[StoreAgent])
249-
average_market_share = total_consumers / len( self.agents_by_type[StoreAgent])
253+
total_consumers = sum(
254+
agent.market_share for agent in self.agents_by_type[StoreAgent]
255+
)
256+
average_market_share = total_consumers / len(self.agents_by_type[StoreAgent])
250257
return average_market_share
251258

252259
def compute_price_variance(self):

examples/pd_grid/pd_grid/agent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mesa.experimental.cell_space import CellAgent
44

5+
56
class PDAgent(CellAgent):
67
"""Agent member of the iterated, spatial prisoner's dilemma model."""
78

@@ -31,7 +32,9 @@ def step(self):
3132
if better than own score."""
3233

3334
# neighbors = self.model.grid.get_neighbors(self.pos, True, include_center=True)
34-
neighbors = list(self.cell.neighborhood.agents) + [self,]
35+
neighbors = list(self.cell.neighborhood.agents) + [
36+
self,
37+
]
3538
best_neighbor = max(neighbors, key=lambda a: a.score)
3639
self.next_move = best_neighbor.move
3740

examples/pd_grid/pd_grid/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .agent import PDAgent
55

6+
67
class PdGrid(mesa.Model):
78
"""Model class for iterated, spatial prisoner's dilemma model."""
89

examples/schelling_experimental/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mesa.experimental.cell_space import CellAgent, OrthogonalMooreGrid
44

5+
56
class SchellingAgent(CellAgent):
67
"""
78
Schelling segregation agent

examples/shape_example/shape_example/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from mesa.experimental.cell_space import OrthogonalMooreGrid
55

6+
67
class Walker(mesa.Agent):
78
def __init__(self, model, heading=(1, 0)):
89
super().__init__(model)

0 commit comments

Comments
 (0)