Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- envs/warehouse_env/server/app.py +65 -14
- server/app.py +65 -14
envs/warehouse_env/server/app.py
CHANGED
|
@@ -94,7 +94,7 @@ async def auto_step():
|
|
| 94 |
)
|
| 95 |
|
| 96 |
def _get_greedy_action() -> int:
|
| 97 |
-
"""Simple greedy policy
|
| 98 |
robot_x, robot_y = warehouse_env.robot_position
|
| 99 |
|
| 100 |
# Determine target location
|
|
@@ -121,21 +121,72 @@ def _get_greedy_action() -> int:
|
|
| 121 |
if package:
|
| 122 |
target_x, target_y = package.dropoff_location
|
| 123 |
else:
|
| 124 |
-
return
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
if robot_x
|
| 128 |
-
return 3 # RIGHT
|
| 129 |
-
elif robot_x > target_x:
|
| 130 |
-
return 2 # LEFT
|
| 131 |
-
elif robot_y < target_y:
|
| 132 |
-
return 1 # DOWN
|
| 133 |
-
elif robot_y > target_y:
|
| 134 |
-
return 0 # UP
|
| 135 |
-
else:
|
| 136 |
-
# At target location
|
| 137 |
return 4 if warehouse_env.robot_carrying is None else 5
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Add health check endpoint
|
| 141 |
@app.get("/health")
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
def _get_greedy_action() -> int:
|
| 97 |
+
"""Simple greedy policy with obstacle avoidance."""
|
| 98 |
robot_x, robot_y = warehouse_env.robot_position
|
| 99 |
|
| 100 |
# Determine target location
|
|
|
|
| 121 |
if package:
|
| 122 |
target_x, target_y = package.dropoff_location
|
| 123 |
else:
|
| 124 |
+
return 5 # Try to drop off
|
| 125 |
+
|
| 126 |
+
# Check if at target location
|
| 127 |
+
if robot_x == target_x and robot_y == target_y:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return 4 if warehouse_env.robot_carrying is None else 5
|
| 129 |
|
| 130 |
+
# Try to move toward target, checking for obstacles
|
| 131 |
+
# Priority: move on axis with larger distance first
|
| 132 |
+
dx = target_x - robot_x
|
| 133 |
+
dy = target_y - robot_y
|
| 134 |
+
|
| 135 |
+
# List of possible moves in order of preference
|
| 136 |
+
moves = []
|
| 137 |
+
|
| 138 |
+
if abs(dx) > abs(dy):
|
| 139 |
+
# Prioritize horizontal movement
|
| 140 |
+
if dx > 0:
|
| 141 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 142 |
+
elif dx < 0:
|
| 143 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 144 |
+
|
| 145 |
+
if dy > 0:
|
| 146 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 147 |
+
elif dy < 0:
|
| 148 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 149 |
+
else:
|
| 150 |
+
# Prioritize vertical movement
|
| 151 |
+
if dy > 0:
|
| 152 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 153 |
+
elif dy < 0:
|
| 154 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 155 |
+
|
| 156 |
+
if dx > 0:
|
| 157 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 158 |
+
elif dx < 0:
|
| 159 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 160 |
+
|
| 161 |
+
# Add perpendicular moves as fallback
|
| 162 |
+
if dx == 0 and dy != 0:
|
| 163 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 164 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 165 |
+
elif dy == 0 and dx != 0:
|
| 166 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 167 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 168 |
+
|
| 169 |
+
# Try moves in order until we find a valid one
|
| 170 |
+
WALL = 1
|
| 171 |
+
SHELF = 2
|
| 172 |
+
|
| 173 |
+
for action_id, new_x, new_y in moves:
|
| 174 |
+
# Check bounds
|
| 175 |
+
if 0 <= new_x < warehouse_env.grid_width and 0 <= new_y < warehouse_env.grid_height:
|
| 176 |
+
# Check if cell is passable
|
| 177 |
+
if warehouse_env.grid[new_y][new_x] not in [WALL, SHELF]:
|
| 178 |
+
return action_id
|
| 179 |
+
|
| 180 |
+
# If no valid move toward target, try any valid move
|
| 181 |
+
for action_id, dx, dy in [(0, 0, -1), (1, 0, 1), (2, -1, 0), (3, 1, 0)]:
|
| 182 |
+
new_x, new_y = robot_x + dx, robot_y + dy
|
| 183 |
+
if 0 <= new_x < warehouse_env.grid_width and 0 <= new_y < warehouse_env.grid_height:
|
| 184 |
+
if warehouse_env.grid[new_y][new_x] not in [WALL, SHELF]:
|
| 185 |
+
return action_id
|
| 186 |
+
|
| 187 |
+
# Last resort: try pickup/dropoff
|
| 188 |
+
return 4 if warehouse_env.robot_carrying is None else 5
|
| 189 |
+
|
| 190 |
|
| 191 |
# Add health check endpoint
|
| 192 |
@app.get("/health")
|
server/app.py
CHANGED
|
@@ -94,7 +94,7 @@ async def auto_step():
|
|
| 94 |
)
|
| 95 |
|
| 96 |
def _get_greedy_action() -> int:
|
| 97 |
-
"""Simple greedy policy
|
| 98 |
robot_x, robot_y = warehouse_env.robot_position
|
| 99 |
|
| 100 |
# Determine target location
|
|
@@ -121,21 +121,72 @@ def _get_greedy_action() -> int:
|
|
| 121 |
if package:
|
| 122 |
target_x, target_y = package.dropoff_location
|
| 123 |
else:
|
| 124 |
-
return
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
if robot_x
|
| 128 |
-
return 3 # RIGHT
|
| 129 |
-
elif robot_x > target_x:
|
| 130 |
-
return 2 # LEFT
|
| 131 |
-
elif robot_y < target_y:
|
| 132 |
-
return 1 # DOWN
|
| 133 |
-
elif robot_y > target_y:
|
| 134 |
-
return 0 # UP
|
| 135 |
-
else:
|
| 136 |
-
# At target location
|
| 137 |
return 4 if warehouse_env.robot_carrying is None else 5
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Add health check endpoint
|
| 141 |
@app.get("/health")
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
def _get_greedy_action() -> int:
|
| 97 |
+
"""Simple greedy policy with obstacle avoidance."""
|
| 98 |
robot_x, robot_y = warehouse_env.robot_position
|
| 99 |
|
| 100 |
# Determine target location
|
|
|
|
| 121 |
if package:
|
| 122 |
target_x, target_y = package.dropoff_location
|
| 123 |
else:
|
| 124 |
+
return 5 # Try to drop off
|
| 125 |
+
|
| 126 |
+
# Check if at target location
|
| 127 |
+
if robot_x == target_x and robot_y == target_y:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return 4 if warehouse_env.robot_carrying is None else 5
|
| 129 |
|
| 130 |
+
# Try to move toward target, checking for obstacles
|
| 131 |
+
# Priority: move on axis with larger distance first
|
| 132 |
+
dx = target_x - robot_x
|
| 133 |
+
dy = target_y - robot_y
|
| 134 |
+
|
| 135 |
+
# List of possible moves in order of preference
|
| 136 |
+
moves = []
|
| 137 |
+
|
| 138 |
+
if abs(dx) > abs(dy):
|
| 139 |
+
# Prioritize horizontal movement
|
| 140 |
+
if dx > 0:
|
| 141 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 142 |
+
elif dx < 0:
|
| 143 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 144 |
+
|
| 145 |
+
if dy > 0:
|
| 146 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 147 |
+
elif dy < 0:
|
| 148 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 149 |
+
else:
|
| 150 |
+
# Prioritize vertical movement
|
| 151 |
+
if dy > 0:
|
| 152 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 153 |
+
elif dy < 0:
|
| 154 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 155 |
+
|
| 156 |
+
if dx > 0:
|
| 157 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 158 |
+
elif dx < 0:
|
| 159 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 160 |
+
|
| 161 |
+
# Add perpendicular moves as fallback
|
| 162 |
+
if dx == 0 and dy != 0:
|
| 163 |
+
moves.append((3, robot_x + 1, robot_y)) # RIGHT
|
| 164 |
+
moves.append((2, robot_x - 1, robot_y)) # LEFT
|
| 165 |
+
elif dy == 0 and dx != 0:
|
| 166 |
+
moves.append((1, robot_x, robot_y + 1)) # DOWN
|
| 167 |
+
moves.append((0, robot_x, robot_y - 1)) # UP
|
| 168 |
+
|
| 169 |
+
# Try moves in order until we find a valid one
|
| 170 |
+
WALL = 1
|
| 171 |
+
SHELF = 2
|
| 172 |
+
|
| 173 |
+
for action_id, new_x, new_y in moves:
|
| 174 |
+
# Check bounds
|
| 175 |
+
if 0 <= new_x < warehouse_env.grid_width and 0 <= new_y < warehouse_env.grid_height:
|
| 176 |
+
# Check if cell is passable
|
| 177 |
+
if warehouse_env.grid[new_y][new_x] not in [WALL, SHELF]:
|
| 178 |
+
return action_id
|
| 179 |
+
|
| 180 |
+
# If no valid move toward target, try any valid move
|
| 181 |
+
for action_id, dx, dy in [(0, 0, -1), (1, 0, 1), (2, -1, 0), (3, 1, 0)]:
|
| 182 |
+
new_x, new_y = robot_x + dx, robot_y + dy
|
| 183 |
+
if 0 <= new_x < warehouse_env.grid_width and 0 <= new_y < warehouse_env.grid_height:
|
| 184 |
+
if warehouse_env.grid[new_y][new_x] not in [WALL, SHELF]:
|
| 185 |
+
return action_id
|
| 186 |
+
|
| 187 |
+
# Last resort: try pickup/dropoff
|
| 188 |
+
return 4 if warehouse_env.robot_carrying is None else 5
|
| 189 |
+
|
| 190 |
|
| 191 |
# Add health check endpoint
|
| 192 |
@app.get("/health")
|