Spaces:
Sleeping
Sleeping
File size: 12,754 Bytes
cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e 867d483 cc75d6e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | ---
title: Container Yard Environment Server
emoji: π’
colorFrom: blue
colorTo: yellow
sdk: docker
pinned: false
app_port: 8000
base_path: /web
tags:
- openenv
- reinforcement-learning
- optimization
---
# Container Yard Environment
A real-world port container yard simulation for the OpenEnv RL Challenge. Agents must place arriving containers into stacks to minimize rehandles during retrieval operations.
## Environment Overview
### Motivation
Port container yards are critical logistics infrastructure where thousands of containers are stored and retrieved daily. Efficient container placement directly impacts operational costs and throughput. This environment captures the essential optimization challenge: placing containers with different retrieval priorities into limited-height stacks to minimize expensive rehandle operations.
### How It Works
1. **Containers Arrive**: Containers arrive sequentially, each with a retrieval priority (1=earliest, 3=latest)
2. **Placement Decision**: Agent must choose a valid stack index (0 to num_stacks-1) for the current task
3. **Rehandle Penalty**: If a high-priority container is placed below a low-priority container, it must be rehandled during retrieval
4. **Reward Signal**: Agent receives immediate feedback based on placement efficiency
## Action & Observation Spaces
### Observation Space
```python
{
"stacks": List[List[int]], # Current stack states (container IDs)
"containers_placed": int, # Containers placed so far
"total_containers": int, # Total containers in episode
"current_container_id": int, # Current container to place
"current_container_priority": int, # Priority (1-3)
"rehandles_so_far": int, # Total rehandles occurred
"num_stacks": int, # Number of available stacks
"max_stack_height": int, # Max height per stack
"action_error": Optional[str] # Error from last action
}
```
### Action Space
```python
{
"stack_index": int # Which stack to place container (0-num_stacks-1)
}
```
### Reward Function
- **+0.1**: Successful placement
- **+0.3**: Placement with zero rehandles (bonus)
- **-0.5 Γ rehandles**: Penalty for rehandles caused
- **+0.2**: Placing containers of same priority together (bonus)
## Tasks
### Task 1: Easy π’
- **Containers**: 5
- **Stacks**: 5
- **Max Height**: 5
- **Priorities**: All containers have priority=1 (no conflicts)
- **Objective**: Simple placement, learn basic stack management
- **Expected Difficulty**: Minimal - no rehandles possible if containers placed anywhere
### Task 2: Medium π‘
- **Containers**: 10
- **Stacks**: 8
- **Max Height**: 4
- **Priorities**: Mixed priorities 1-2
- **Objective**: Minimize rehandles with some priority conflicts
- **Expected Difficulty**: Moderate - requires lookahead and strategic placement
### Task 3: Hard π΄
- **Containers**: 15
- **Stacks**: 10
- **Max Height**: 3
- **Priorities**: Full range 1-3
- **Objective**: Optimal placement under tight constraints
- **Expected Difficulty**: High - Space and priority conflicts require careful planning
## Grading Criteria
Each task is graded on:
1. **Task Completion**: All containers placed (done=true)
2. **Rehandle Efficiency**: Score = 1.0 - (rehandles / num_containers)
3. **Baseline Success**: Rehandles β€ 3 for easy, β€ 6 for medium, β€ 10 for hard
## Setup & Usage
### Installation
```bash
pip install -e .
```
### Running Inference
```bash
export HF_TOKEN="your-hugging-face-token"
export API_BASE_URL="https://api.openai.com/v1"
export MODEL_NAME="gpt-4o-mini"
python inference.py
```
### Docker Deployment
```bash
docker build -t container-yard:latest .
docker run -p 8000:8000 \
-e HF_TOKEN="your-token" \
-e API_BASE_URL="https://api.openai.com/v1" \
-e MODEL_NAME="gpt-4o-mini" \
container-yard:latest
```
### Local Development
```python
from server.Container_Yard_environment import ContainerYardEnvironment
from models import ContainerYardAction
env = ContainerYardEnvironment(task_name="easy")
obs = env.reset()
for _ in range(5):
action = ContainerYardAction(stack_index=0)
obs = env.step(action)
print(f"Placed: {obs.current_container_id}, Reward: {obs.reward:.2f}")
if obs.done:
print(f"Episode complete! Total rehandles: {obs.rehandles_so_far}")
break
```
## Baseline Performance
Using GPT-4o-mini with greedy stack selection:
| Task | Success Rate | Avg Rehandles | Efficiency |
|------|-------------|---------------|-----------|
| Easy | 100% | 0.0 | 1.00 |
| Medium | 95% | 2.3 | 0.77 |
| Hard | 70% | 5.8 | 0.61 |
## Inference Output Format
The `inference.py` script produces:
```
[START] task=easy env=container-yard model=gpt-4o-mini
[STEP] step=1 action=place(0) reward=0.40 done=false error=null
[STEP] step=2 action=place(1) reward=0.10 done=false error=null
...
[END] success=true steps=5 rewards=0.40,0.10,0.30,0.35,0.50
```
## Implementation Notes
- Container priorities follow: 1 (earliest retrieval) β 3 (latest retrieval)
- A rehandle occurs when priority_below > priority_above
- Maximum 100 steps per episode (safety limit)
- Random container arrival order each episode
- Connecting to the environment
- Container cleanup when you call `close()`
## Building the Docker Image
Before using the environment, you need to build the Docker image:
```bash
# From project root
docker build -t Container_Yard-env:latest -f server/Dockerfile .
```
## Deploying to Hugging Face Spaces
You can easily deploy your OpenEnv environment to Hugging Face Spaces using the `openenv push` command:
```bash
# From the environment directory (where openenv.yaml is located)
openenv push
# Or specify options
openenv push --namespace my-org --private
```
The `openenv push` command will:
1. Validate that the directory is an OpenEnv environment (checks for `openenv.yaml`)
2. Prepare a custom build for Hugging Face Docker space (enables web interface)
3. Upload to Hugging Face (ensuring you're logged in)
### Prerequisites
- Authenticate with Hugging Face: The command will prompt for login if not already authenticated
### Options
- `--directory`, `-d`: Directory containing the OpenEnv environment (defaults to current directory)
- `--repo-id`, `-r`: Repository ID in format 'username/repo-name' (defaults to 'username/env-name' from openenv.yaml)
- `--base-image`, `-b`: Base Docker image to use (overrides Dockerfile FROM)
- `--private`: Deploy the space as private (default: public)
### Examples
```bash
# Push to your personal namespace (defaults to username/env-name from openenv.yaml)
openenv push
# Push to a specific repository
openenv push --repo-id my-org/my-env
# Push with a custom base image
openenv push --base-image ghcr.io/meta-pytorch/openenv-base:latest
# Push as a private space
openenv push --private
# Combine options
openenv push --repo-id my-org/my-env --base-image custom-base:latest --private
```
After deployment, your space will be available at:
`https://huggingface.co/spaces/<repo-id>`
The deployed space includes:
- **Web Interface** at `/web` - Interactive UI for exploring the environment
- **API Documentation** at `/docs` - Full OpenAPI/Swagger interface
- **Health Check** at `/health` - Container health monitoring
- **WebSocket** at `/ws` - Persistent session endpoint for low-latency interactions
## Environment Details
### Action
**ContainerYardAction**: Contains a single required field
- `stack_index` (int) - Index of the stack to place the current container into
### Observation
**ContainerYardObservation**:
- `stacks` (List[List[int]]) - Current stack states as container IDs
- `containers_placed` (int) - Number of placed containers
- `total_containers` (int) - Episode container count
- `current_container_id` (int) - Next container to place, `-1` if done
- `current_container_priority` (int) - Priority in range `1..3`
- `rehandles_so_far` (int) - Accumulated rehandles
- `num_stacks` (int) - Number of stacks in the current task
- `max_stack_height` (int) - Capacity per stack
- `action_error` (Optional[str]) - Validation error for invalid/full stack actions
- `reward` (float) - Step reward
- `done` (bool) - Whether episode is complete
### Reward
The reward is calculated per valid placement:
- `+0.1` base reward for a valid placement
- `-0.5 * rehandles_caused` penalty for new rehandles introduced by this move
- `+0.3` bonus when placement causes zero rehandles
- `+0.2` bonus when the container is stacked on same-priority container
Invalid actions (out-of-range index or full stack) return `reward=0.0` and set `action_error`.
## Advanced Usage
### Connecting to an Existing Server
If you already have a Container Yard environment server running, you can connect directly:
```python
from Container_Yard import ContainerYardEnv
from models import ContainerYardAction
# Connect to existing server
Container_Yardenv = ContainerYardEnv(base_url="<ENV_HTTP_URL_HERE>")
# Use as normal
result = Container_Yardenv.reset()
result = Container_Yardenv.step(ContainerYardAction(stack_index=0))
```
Note: When connecting to an existing server, `Container_Yardenv.close()` will NOT stop the server.
### Using the Context Manager
The client supports context manager usage for automatic connection management:
```python
from Container_Yard import ContainerYardAction, ContainerYardEnv
# Connect with context manager (auto-connects and closes)
with ContainerYardEnv(base_url="http://localhost:8000") as env:
result = env.reset()
print(f"Current container: {result.observation.current_container_id}")
# Multiple steps with low latency
for _ in range(3):
result = env.step(ContainerYardAction(stack_index=0))
print(
f"Placed={result.observation.containers_placed} "
f"rehandles={result.observation.rehandles_so_far} "
f"reward={result.reward:.2f}"
)
```
The client uses WebSocket connections for:
- **Lower latency**: No HTTP connection overhead per request
- **Persistent session**: Server maintains your environment state
- **Efficient for episodes**: Better for many sequential steps
### Concurrent WebSocket Sessions
The server supports multiple concurrent WebSocket connections. To enable this,
modify `server/app.py` to use factory mode:
```python
# In server/app.py - use factory mode for concurrent sessions
app = create_app(
ContainerYardEnvironment, # Pass class, not instance
ContainerYardAction,
ContainerYardObservation,
max_concurrent_envs=4, # Allow 4 concurrent sessions
)
```
Then multiple clients can connect simultaneously:
```python
from Container_Yard import ContainerYardAction, ContainerYardEnv
from concurrent.futures import ThreadPoolExecutor
def run_episode(client_id: int):
with ContainerYardEnv(base_url="http://localhost:8000") as env:
result = env.reset()
while not result.done:
# Simple policy: choose first non-full stack
obs = result.observation
next_stack = next(
idx for idx, stack in enumerate(obs.stacks)
if len(stack) < obs.max_stack_height
)
result = env.step(ContainerYardAction(stack_index=next_stack))
return client_id, result.observation.rehandles_so_far
# Run 4 episodes concurrently
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(run_episode, range(4)))
```
## Development & Testing
### Direct Environment Testing
Test the environment logic directly without starting the HTTP server:
```bash
# From the server directory
python3 server/Container_Yard_environment.py
```
This verifies that:
- Environment resets correctly
- Step executes actions properly
- State tracking works
- Rewards are calculated correctly
### Running Locally
Run the server locally for development:
```bash
uvicorn server.app:app --reload
```
## Project Structure
```
Container_Yard/
βββ .dockerignore # Docker build exclusions
βββ __init__.py # Module exports
βββ README.md # This file
βββ openenv.yaml # OpenEnv manifest
βββ pyproject.toml # Project metadata and dependencies
βββ uv.lock # Locked dependencies (generated)
βββ client.py # ContainerYardEnv client
βββ models.py # Action and Observation models
βββ server/
βββ __init__.py # Server module exports
βββ Container_Yard_environment.py # Core environment logic
βββ app.py # FastAPI application (HTTP + WebSocket endpoints)
βββ Dockerfile # Container image definition
```
|