Skip to content

Commit 96137d4

Browse files
committed
fix warehouse
1 parent fd77f43 commit 96137d4

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

examples/warehouse/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This model demonstrates deliberate meta-agent creation. It shows the basics of m
1616

1717
In its current configuration, agents being part of multiple meta-agents is not supported
1818

19+
An additional item of note is that to reference the RobotAgent created in model you will see `type(self.RobotAgent)` or `type(model.RobotAgent)` in various places. If you have any ideas for how to make this more user friendly please let us know or do a pull request.
20+
1921
## Installation
2022

2123
This model requires Mesa's recommended install

examples/warehouse/agents.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class InventoryAgent(FixedAgent):
1010
"""
1111

1212
def __init__(self, model, cell, item: str):
13-
super().__init__(model, key_by_name=True)
13+
super().__init__(model)
1414
self.cell = cell
1515
self.item = item
1616
self.quantity = 1000 # Default quantity
@@ -24,7 +24,7 @@ class RouteAgent(mesa.Agent):
2424
"""
2525

2626
def __init__(self, model):
27-
super().__init__(model, key_by_name=True)
27+
super().__init__(model)
2828

2929
def find_path(self, start, goal) -> list[tuple[int, int, int]] | None:
3030
"""
@@ -82,7 +82,7 @@ class SensorAgent(mesa.Agent):
8282
"""
8383

8484
def __init__(self, model):
85-
super().__init__(model, key_by_name=True)
85+
super().__init__(model)
8686

8787
def move(
8888
self, coord: tuple[int, int, int], path: list[tuple[int, int, int]]
@@ -109,9 +109,9 @@ def move(
109109
self.meta_agent.cell = self.random.choice(empty_neighbors)
110110

111111
# Recalculate path
112-
new_path = self.meta_agent.get_subagent_instance(RouteAgent).find_path(
113-
self.meta_agent.cell, self.meta_agent.item.cell
114-
)
112+
new_path = self.meta_agent.get_constituting_agent_instance(
113+
RouteAgent
114+
).find_path(self.meta_agent.cell, self.meta_agent.item.cell)
115115
self.meta_agent.path = new_path
116116
return "recalculating"
117117

@@ -122,7 +122,7 @@ class WorkerAgent(mesa.Agent):
122122
"""
123123

124124
def __init__(self, model, ld, cs):
125-
super().__init__(model, key_by_name=True)
125+
super().__init__(model)
126126
self.loading_dock = ld
127127
self.charging_station = cs
128128
self.path: list[tuple[int, int, int]] | None = None
@@ -140,7 +140,7 @@ def continue_task(self):
140140
"""
141141
Continues the task if the robot is able to perform it.
142142
"""
143-
status = self.meta_agent.get_subagent_instance(SensorAgent).move(
143+
status = self.meta_agent.get_constituting_agent_instance(SensorAgent).move(
144144
self.cell.coordinate, self.path
145145
)
146146

examples/warehouse/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import matplotlib.pyplot as plt
22
import pandas as pd
33
import solara
4+
from agents import InventoryAgent
45
from mesa.visualization import SolaraViz
56
from mesa.visualization.utils import update_counter
6-
7-
from examples.warehouse.model import WarehouseModel
7+
from model import WarehouseModel
88

99
# Constants
1010
LOADING_DOCKS = [(0, 0, 0), (0, 2, 0), (0, 4, 0), (0, 6, 0), (0, 8, 0)]
@@ -53,8 +53,8 @@ def plot_warehouse(model):
5353
update_counter.get()
5454

5555
# Prepare data for inventory and robot agents
56-
inventory_data = prepare_agent_data(model, "InventoryAgent", "Inventory")
57-
robot_data = prepare_agent_data(model, "RobotAgent", "Robot")
56+
inventory_data = prepare_agent_data(model, InventoryAgent, "Inventory")
57+
robot_data = prepare_agent_data(model, type(model.RobotAgent), "Robot")
5858

5959
# Combine data into a single DataFrame
6060
data = pd.DataFrame(inventory_data + robot_data)

examples/warehouse/model.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import mesa
2-
from mesa.discrete_space import OrthogonalMooreGrid
3-
from mesa.discrete_space.cell_agent import CellAgent
4-
from mesa.experimental.meta_agents.meta_agent import create_meta_agent
5-
6-
from examples.warehouse.agents import (
2+
from agents import (
73
InventoryAgent,
84
RouteAgent,
95
SensorAgent,
106
WorkerAgent,
117
)
12-
from examples.warehouse.make_warehouse import make_warehouse
8+
from make_warehouse import make_warehouse
9+
from mesa.discrete_space import OrthogonalMooreGrid
10+
from mesa.discrete_space.cell_agent import CellAgent
11+
from mesa.experimental.meta_agents.meta_agent import create_meta_agent
1312

1413
# Constants for configuration
1514
LOADING_DOCKS = [(0, 0, 0), (0, 2, 0), (0, 4, 0), (0, 6, 0), (0, 8, 0)]
@@ -63,7 +62,7 @@ def __init__(self, seed=42):
6362

6463
# Create Robot Agents
6564
for idx in range(len(self.loading_docks)):
66-
# Create sub-agents
65+
# Create constituting_agents
6766
router = RouteAgent(self)
6867
sensor = SensorAgent(self)
6968
worker = WorkerAgent(
@@ -73,17 +72,17 @@ def __init__(self, seed=42):
7372
)
7473

7574
# Create meta-agent and place in warehouse
76-
create_meta_agent(
75+
self.RobotAgent = create_meta_agent(
7776
self,
7877
"RobotAgent",
7978
[router, sensor, worker],
80-
mesa_agent_type=CellAgent,
79+
CellAgent,
8180
meta_attributes={
8281
"cell": self.warehouse[self.charging_stations[idx]],
8382
"status": "open",
8483
},
85-
assume_subagent_attributes=True,
86-
assume_subagent_methods=True,
84+
assume_constituting_agent_attributes=True,
85+
assume_constituting_agent_methods=True,
8786
)
8887

8988
def central_move(self, robot):
@@ -99,9 +98,9 @@ def step(self):
9998
"""
10099
Advance the model by one step.
101100
"""
102-
for robot in self.agents_by_type["RobotAgent"]:
101+
for robot in self.agents_by_type[type(self.RobotAgent)]:
103102
if robot.status == "open": # Assign a task to the robot
104-
item = self.random.choice(self.agents_by_type["InventoryAgent"])
103+
item = self.random.choice(self.agents_by_type[InventoryAgent])
105104
if item.quantity > 0:
106105
robot.initiate_task(item)
107106
robot.status = "inventory"

0 commit comments

Comments
 (0)