Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions examples/hotelling_law/hotelling_law/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StoreAgent(CellAgent):
"""An agent representing a store with a price and ability to move
and adjust prices."""

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

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

def __init__(self, model):
def __init__(self, model, cell, consumer_preferences):
super().__init__(model)
self.preferred_store = None
self.cell = cell
self.preference = consumer_preferences

def determine_preferred_store(self):
consumer_preference = self.model.agents_by_type[ConsumerAgent]
stores = self.model.agents_by_type[StoreAgent]

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

for store in stores:
# Calculate score based on consumer preference
if consumer_preference == "proximity":
if self.preference == "proximity":
score = self.euclidean_distance(
self.cell.coordinate, store.cell.coordinate
)
elif consumer_preference == "price":
elif self.preference == "price":
score = store.price
else: # Default case includes both proximity and price
score = store.price + self.euclidean_distance(
Expand Down
24 changes: 11 additions & 13 deletions examples/hotelling_law/hotelling_law/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,20 @@ def _initialize_agents(self):
if can_move:
mobile_agents_assigned += 1

agent = StoreAgent(self, can_move=can_move, strategy=strategy)

# Randomly place agents on the grid for a grid environment.
x = self.random.randrange(self.grid.dimensions[0])
y = self.random.randrange(self.grid.dimensions[1])
agent.cell = self.grid[(x, y)]
StoreAgent(
self,
self.grid.all_cells.select_random_cell(),
can_move=can_move,
strategy=strategy,
)

# Place consumer agents
for _ in range(self.num_consumers):
# Ensure unique ID across all agents
consumer = ConsumerAgent(self)

# Place consumer randomly on the grid
x = self.random.randrange(self.grid.dimensions[0])
y = self.random.randrange(self.grid.dimensions[1])
consumer.cell = self.grid[(x, y)]
ConsumerAgent(
self,
self.grid.all_cells.select_random_cell(),
self.consumer_preferences,
)

# Method to advance the simulation by one step.
def step(self):
Expand Down
Loading