Skip to content

Commit 067acc1

Browse files
bug fix and code simplification in hotelling (#223)
* bug fix and code simplification * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9a05b05 commit 067acc1

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

examples/hotelling_law/hotelling_law/agents.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class StoreAgent(CellAgent):
99
"""An agent representing a store with a price and ability to move
1010
and adjust prices."""
1111

12-
def __init__(self, model, price=10, can_move=True, strategy="Budget"):
12+
def __init__(self, model, cell, price=10, can_move=True, strategy="Budget"):
1313
# Initializes the store agent with a unique ID,
1414
# the model it belongs to,its initial price,
1515
# and whether it can move.
@@ -20,6 +20,7 @@ def __init__(self, model, price=10, can_move=True, strategy="Budget"):
2020
self.previous_market_share = 0 # Initialize previous market share
2121
self.strategy = strategy # Store can be low cost (Budget)
2222
# / differential (Premium)
23+
self.cell = cell
2324

2425
def estimate_market_share(self, new_position=None):
2526
position = new_position if new_position else self.cell
@@ -157,12 +158,13 @@ class ConsumerAgent(CellAgent):
157158
"""A consumer agent that chooses a store
158159
based on price and distance."""
159160

160-
def __init__(self, model):
161+
def __init__(self, model, cell, consumer_preferences):
161162
super().__init__(model)
162163
self.preferred_store = None
164+
self.cell = cell
165+
self.preference = consumer_preferences
163166

164167
def determine_preferred_store(self):
165-
consumer_preference = self.model.agents_by_type[ConsumerAgent]
166168
stores = self.model.agents_by_type[StoreAgent]
167169

168170
if len(stores) == 0: # Check if the stores AgentSet is empty
@@ -173,11 +175,11 @@ def determine_preferred_store(self):
173175

174176
for store in stores:
175177
# Calculate score based on consumer preference
176-
if consumer_preference == "proximity":
178+
if self.preference == "proximity":
177179
score = self.euclidean_distance(
178180
self.cell.coordinate, store.cell.coordinate
179181
)
180-
elif consumer_preference == "price":
182+
elif self.preference == "price":
181183
score = store.price
182184
else: # Default case includes both proximity and price
183185
score = store.price + self.euclidean_distance(

examples/hotelling_law/hotelling_law/model.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,20 @@ def _initialize_agents(self):
186186
if can_move:
187187
mobile_agents_assigned += 1
188188

189-
agent = StoreAgent(self, can_move=can_move, strategy=strategy)
190-
191-
# Randomly place agents on the grid for a grid environment.
192-
x = self.random.randrange(self.grid.dimensions[0])
193-
y = self.random.randrange(self.grid.dimensions[1])
194-
agent.cell = self.grid[(x, y)]
189+
StoreAgent(
190+
self,
191+
self.grid.all_cells.select_random_cell(),
192+
can_move=can_move,
193+
strategy=strategy,
194+
)
195195

196196
# Place consumer agents
197197
for _ in range(self.num_consumers):
198-
# Ensure unique ID across all agents
199-
consumer = ConsumerAgent(self)
200-
201-
# Place consumer randomly on the grid
202-
x = self.random.randrange(self.grid.dimensions[0])
203-
y = self.random.randrange(self.grid.dimensions[1])
204-
consumer.cell = self.grid[(x, y)]
198+
ConsumerAgent(
199+
self,
200+
self.grid.all_cells.select_random_cell(),
201+
self.consumer_preferences,
202+
)
205203

206204
# Method to advance the simulation by one step.
207205
def step(self):

0 commit comments

Comments
 (0)