|
| 1 | +""" |
| 2 | +Simple example script demonstrating the Warehouse Environment. |
| 3 | +
|
| 4 | +This script shows basic usage of the warehouse environment with both |
| 5 | +random and greedy agents. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import random |
| 10 | +import sys |
| 11 | + |
| 12 | +# Add src directory to path for imports |
| 13 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) |
| 14 | + |
| 15 | +from envs.warehouse_env import WarehouseAction, WarehouseEnv |
| 16 | + |
| 17 | + |
| 18 | +def random_agent_example(): |
| 19 | + """Run a simple random agent in the warehouse.""" |
| 20 | + print("=" * 60) |
| 21 | + print("RANDOM AGENT EXAMPLE") |
| 22 | + print("=" * 60) |
| 23 | + |
| 24 | + # Connect to server (assumes server is running on localhost:8000) |
| 25 | + # Or use from_docker_image() to automatically start a container |
| 26 | + try: |
| 27 | + env = WarehouseEnv(base_url="http://localhost:8000") |
| 28 | + except Exception as e: |
| 29 | + print(f"Error connecting to server: {e}") |
| 30 | + print("\nPlease start the server first:") |
| 31 | + print(" docker run -p 8000:8000 warehouse-env:latest") |
| 32 | + print("Or run the server locally:") |
| 33 | + print( |
| 34 | + " python -m uvicorn envs.warehouse_env.server.app:app --host 0.0.0.0 --port 8000" |
| 35 | + ) |
| 36 | + return |
| 37 | + |
| 38 | + # Run one episode |
| 39 | + result = env.reset() |
| 40 | + print(f"\nStarting episode...") |
| 41 | + |
| 42 | + # Debug: print what we got |
| 43 | + print(f"Grid data: {result.observation.grid}") |
| 44 | + print(f"Robot position: {result.observation.robot_position}") |
| 45 | + |
| 46 | + if result.observation.grid and len(result.observation.grid) > 0: |
| 47 | + print( |
| 48 | + f"Grid size: {len(result.observation.grid)}x{len(result.observation.grid[0])}" |
| 49 | + ) |
| 50 | + else: |
| 51 | + print("Warning: Grid is empty!") |
| 52 | + |
| 53 | + print(f"Packages to deliver: {result.observation.total_packages}") |
| 54 | + print(f"Max steps: {result.observation.time_remaining}") |
| 55 | + |
| 56 | + step = 0 |
| 57 | + done = False |
| 58 | + |
| 59 | + while not done and step < 50: # Limit to 50 steps for demo |
| 60 | + # Random action |
| 61 | + action = WarehouseAction(action_id=random.randint(0, 5)) |
| 62 | + result = env.step(action) |
| 63 | + |
| 64 | + step += 1 |
| 65 | + if result.reward != -0.1: # Only print interesting events |
| 66 | + print( |
| 67 | + f"Step {step}: {action.action_name} -> {result.observation.message} (reward: {result.reward:.1f})" |
| 68 | + ) |
| 69 | + |
| 70 | + done = result.done |
| 71 | + |
| 72 | + # Print final results |
| 73 | + print(f"\nEpisode finished!") |
| 74 | + print(f"Steps taken: {step}") |
| 75 | + print( |
| 76 | + f"Packages delivered: {result.observation.packages_delivered}/{result.observation.total_packages}" |
| 77 | + ) |
| 78 | + print(f"Total reward: {env.state().cum_reward:.2f}") |
| 79 | + |
| 80 | + env.close() |
| 81 | + |
| 82 | + |
| 83 | +def greedy_agent_example(): |
| 84 | + """Run a simple greedy agent that moves toward targets.""" |
| 85 | + print("\n" + "=" * 60) |
| 86 | + print("GREEDY AGENT EXAMPLE") |
| 87 | + print("=" * 60) |
| 88 | + |
| 89 | + def get_greedy_action(obs): |
| 90 | + """Simple greedy policy: move toward nearest target.""" |
| 91 | + robot_x, robot_y = obs.robot_position |
| 92 | + |
| 93 | + # Determine target location |
| 94 | + if obs.robot_carrying is None: |
| 95 | + # Not carrying: move toward nearest waiting package |
| 96 | + for pkg in obs.packages: |
| 97 | + if pkg["status"] == "waiting": |
| 98 | + target_x, target_y = pkg["pickup_location"] |
| 99 | + break |
| 100 | + else: |
| 101 | + # No packages waiting, try to pick up |
| 102 | + return 4 |
| 103 | + else: |
| 104 | + # Carrying: move toward dropoff zone |
| 105 | + pkg = next((p for p in obs.packages if p["id"] == obs.robot_carrying), None) |
| 106 | + if pkg: |
| 107 | + target_x, target_y = pkg["dropoff_location"] |
| 108 | + else: |
| 109 | + return 4 # Try action |
| 110 | + |
| 111 | + # Simple pathfinding: move closer on one axis at a time |
| 112 | + if robot_x < target_x: |
| 113 | + return 3 # RIGHT |
| 114 | + elif robot_x > target_x: |
| 115 | + return 2 # LEFT |
| 116 | + elif robot_y < target_y: |
| 117 | + return 1 # DOWN |
| 118 | + elif robot_y > target_y: |
| 119 | + return 0 # UP |
| 120 | + else: |
| 121 | + # At target location |
| 122 | + return 4 if obs.robot_carrying is None else 5 |
| 123 | + |
| 124 | + try: |
| 125 | + env = WarehouseEnv(base_url="http://localhost:8000") |
| 126 | + except Exception as e: |
| 127 | + print(f"Error connecting to server: {e}") |
| 128 | + return |
| 129 | + |
| 130 | + # Run 3 episodes |
| 131 | + for episode in range(3): |
| 132 | + result = env.reset() |
| 133 | + print(f"\nEpisode {episode + 1}") |
| 134 | + print(f"Packages: {result.observation.total_packages}") |
| 135 | + |
| 136 | + done = False |
| 137 | + steps = 0 |
| 138 | + |
| 139 | + while not done and steps < 200: |
| 140 | + action_id = get_greedy_action(result.observation) |
| 141 | + action = WarehouseAction(action_id=action_id) |
| 142 | + result = env.step(action) |
| 143 | + steps += 1 |
| 144 | + |
| 145 | + # Print delivery events |
| 146 | + if "delivered" in result.observation.message.lower(): |
| 147 | + print(f" Step {steps}: {result.observation.message}") |
| 148 | + |
| 149 | + done = result.done |
| 150 | + |
| 151 | + state = env.state() |
| 152 | + print( |
| 153 | + f" Result: {state.packages_delivered}/{state.total_packages} delivered, " |
| 154 | + f"reward: {state.cum_reward:.2f}, steps: {steps}" |
| 155 | + ) |
| 156 | + |
| 157 | + env.close() |
| 158 | + |
| 159 | + |
| 160 | +def visualization_example(): |
| 161 | + """Demonstrate ASCII visualization.""" |
| 162 | + print("\n" + "=" * 60) |
| 163 | + print("VISUALIZATION EXAMPLE") |
| 164 | + print("=" * 60) |
| 165 | + |
| 166 | + try: |
| 167 | + env = WarehouseEnv(base_url="http://localhost:8000") |
| 168 | + except Exception as e: |
| 169 | + print(f"Error connecting to server: {e}") |
| 170 | + return |
| 171 | + |
| 172 | + # Reset and show initial state |
| 173 | + result = env.reset() |
| 174 | + print("\nInitial warehouse state:") |
| 175 | + print(env.render_ascii()) |
| 176 | + |
| 177 | + # Take a few actions and show updates |
| 178 | + actions = [3, 3, 1, 1, 4] # RIGHT, RIGHT, DOWN, DOWN, PICKUP |
| 179 | + for i, action_id in enumerate(actions): |
| 180 | + action = WarehouseAction(action_id=action_id) |
| 181 | + result = env.step(action) |
| 182 | + print(f"\nAfter action {i+1} ({action.action_name}):") |
| 183 | + print(env.render_ascii()) |
| 184 | + |
| 185 | + if result.done: |
| 186 | + break |
| 187 | + |
| 188 | + env.close() |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + print("Warehouse Environment - Example Script") |
| 193 | + print("=" * 60) |
| 194 | + print("\nThis script demonstrates the warehouse environment.") |
| 195 | + print("Make sure the server is running on http://localhost:8000") |
| 196 | + print("\nTo start the server:") |
| 197 | + print(" docker run -p 8000:8000 warehouse-env:latest") |
| 198 | + print("\n" + "=" * 60) |
| 199 | + |
| 200 | + # Run examples |
| 201 | + try: |
| 202 | + random_agent_example() |
| 203 | + greedy_agent_example() |
| 204 | + visualization_example() |
| 205 | + |
| 206 | + print("\n" + "=" * 60) |
| 207 | + print("All examples completed successfully!") |
| 208 | + print("=" * 60) |
| 209 | + |
| 210 | + except KeyboardInterrupt: |
| 211 | + print("\n\nInterrupted by user") |
| 212 | + except Exception as e: |
| 213 | + print(f"\n\nError: {e}") |
| 214 | + import traceback |
| 215 | + |
| 216 | + traceback.print_exc() |
0 commit comments