|
| 1 | +from queue import PriorityQueue |
| 2 | + |
| 3 | +import mesa |
| 4 | +from mesa.discrete_space import FixedAgent |
| 5 | + |
| 6 | + |
| 7 | +class InventoryAgent(FixedAgent): |
| 8 | + """ |
| 9 | + Represents an inventory item in the warehouse. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, model, cell, item: str): |
| 13 | + super().__init__(model, key_by_name=True) |
| 14 | + self.cell = cell |
| 15 | + self.item = item |
| 16 | + self.quantity = 1000 # Default quantity |
| 17 | + |
| 18 | + |
| 19 | +class RouteAgent(mesa.Agent): |
| 20 | + """ |
| 21 | + Handles path finding for agents in the warehouse. |
| 22 | +
|
| 23 | + Intended to be a pseudo onboard GPS system for robots. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, model): |
| 27 | + super().__init__(model, key_by_name=True) |
| 28 | + |
| 29 | + def find_path(self, start, goal) -> list[tuple[int, int, int]] | None: |
| 30 | + """ |
| 31 | + Determines the path for a robot to take using the A* algorithm. |
| 32 | + """ |
| 33 | + |
| 34 | + def heuristic(a, b) -> int: |
| 35 | + dx = abs(a[0] - b[0]) |
| 36 | + dy = abs(a[1] - b[1]) |
| 37 | + return dx + dy |
| 38 | + |
| 39 | + open_set = PriorityQueue() |
| 40 | + open_set.put((0, start.coordinate)) |
| 41 | + came_from = {} |
| 42 | + g_score = {start.coordinate: 0} |
| 43 | + |
| 44 | + while not open_set.empty(): |
| 45 | + _, current = open_set.get() |
| 46 | + |
| 47 | + if current[:2] == goal.coordinate[:2]: |
| 48 | + path = [] |
| 49 | + while current in came_from: |
| 50 | + path.append(current) |
| 51 | + current = came_from[current] |
| 52 | + path.reverse() |
| 53 | + path.insert(0, start.coordinate) |
| 54 | + path.pop() # Remove the last location (inventory) |
| 55 | + return path |
| 56 | + |
| 57 | + for n_cell in self.model.warehouse[current].neighborhood: |
| 58 | + coord = n_cell.coordinate |
| 59 | + |
| 60 | + # Only consider orthogonal neighbors |
| 61 | + if abs(coord[0] - current[0]) + abs(coord[1] - current[1]) != 1: |
| 62 | + continue |
| 63 | + |
| 64 | + tentative_g_score = g_score[current] + 1 |
| 65 | + if not n_cell.is_empty: |
| 66 | + tentative_g_score += 50 # Penalty for non-empty cells |
| 67 | + |
| 68 | + if coord not in g_score or tentative_g_score < g_score[coord]: |
| 69 | + g_score[coord] = tentative_g_score |
| 70 | + f_score = tentative_g_score + heuristic(coord, goal.coordinate) |
| 71 | + open_set.put((f_score, coord)) |
| 72 | + came_from[coord] = current |
| 73 | + |
| 74 | + return None |
| 75 | + |
| 76 | + |
| 77 | +class SensorAgent(mesa.Agent): |
| 78 | + """ |
| 79 | + Detects entities in the area and handles movement along a path. |
| 80 | +
|
| 81 | + Intended to be a pseudo onboard sensor system for robot. |
| 82 | + """ |
| 83 | + |
| 84 | + def __init__(self, model): |
| 85 | + super().__init__(model, key_by_name=True) |
| 86 | + |
| 87 | + def move( |
| 88 | + self, coord: tuple[int, int, int], path: list[tuple[int, int, int]] |
| 89 | + ) -> str: |
| 90 | + """ |
| 91 | + Moves the agent along the given path. |
| 92 | + """ |
| 93 | + if coord not in path: |
| 94 | + raise ValueError("Current coordinate not in path.") |
| 95 | + |
| 96 | + idx = path.index(coord) |
| 97 | + if idx + 1 >= len(path): |
| 98 | + return "movement complete" |
| 99 | + |
| 100 | + next_cell = self.model.warehouse[path[idx + 1]] |
| 101 | + if next_cell.is_empty: |
| 102 | + self.meta_agent.cell = next_cell |
| 103 | + return "moving" |
| 104 | + |
| 105 | + # Handle obstacle |
| 106 | + neighbors = self.model.warehouse[self.meta_agent.cell.coordinate].neighborhood |
| 107 | + empty_neighbors = [n for n in neighbors if n.is_empty] |
| 108 | + if empty_neighbors: |
| 109 | + self.meta_agent.cell = self.random.choice(empty_neighbors) |
| 110 | + |
| 111 | + # 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 | + ) |
| 115 | + self.meta_agent.path = new_path |
| 116 | + return "recalculating" |
| 117 | + |
| 118 | + |
| 119 | +class WorkerAgent(mesa.Agent): |
| 120 | + """ |
| 121 | + Represents a robot worker responsible for collecting and loading items. |
| 122 | + """ |
| 123 | + |
| 124 | + def __init__(self, model, ld, cs): |
| 125 | + super().__init__(model, key_by_name=True) |
| 126 | + self.loading_dock = ld |
| 127 | + self.charging_station = cs |
| 128 | + self.path: list[tuple[int, int, int]] | None = None |
| 129 | + self.carrying: str | None = None |
| 130 | + self.item: InventoryAgent | None = None |
| 131 | + |
| 132 | + def initiate_task(self, item: InventoryAgent): |
| 133 | + """ |
| 134 | + Initiates a task for the robot to perform. |
| 135 | + """ |
| 136 | + self.item = item |
| 137 | + self.path = self.find_path(self.cell, item.cell) |
| 138 | + |
| 139 | + def continue_task(self): |
| 140 | + """ |
| 141 | + Continues the task if the robot is able to perform it. |
| 142 | + """ |
| 143 | + status = self.meta_agent.get_subagent_instance(SensorAgent).move( |
| 144 | + self.cell.coordinate, self.path |
| 145 | + ) |
| 146 | + |
| 147 | + if status == "movement complete" and self.meta_agent.status == "inventory": |
| 148 | + # Pick up item and bring to loading dock |
| 149 | + self.meta_agent.cell = self.model.warehouse[ |
| 150 | + *self.meta_agent.cell.coordinate[:2], self.item.cell.coordinate[2] |
| 151 | + ] |
| 152 | + self.meta_agent.status = "loading" |
| 153 | + self.carrying = self.item.item |
| 154 | + self.item.quantity -= 1 |
| 155 | + self.meta_agent.cell = self.model.warehouse[ |
| 156 | + *self.meta_agent.cell.coordinate[:2], 0 |
| 157 | + ] |
| 158 | + self.path = self.find_path(self.cell, self.loading_dock) |
| 159 | + |
| 160 | + if status == "movement complete" and self.meta_agent.status == "loading": |
| 161 | + # Load item onto truck and return to charging station |
| 162 | + self.carrying = None |
| 163 | + self.meta_agent.status = "open" |
0 commit comments