Spaces:
Sleeping
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
- Containers Arrive: Containers arrive sequentially, each with a retrieval priority (1=earliest, 3=latest)
- Placement Decision: Agent must choose a valid stack index (0 to num_stacks-1) for the current task
- Rehandle Penalty: If a high-priority container is placed below a low-priority container, it must be rehandled during retrieval
- Reward Signal: Agent receives immediate feedback based on placement efficiency
Action & Observation Spaces
Observation Space
{
"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
{
"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:
- Task Completion: All containers placed (done=true)
- Rehandle Efficiency: Score = 1.0 - (rehandles / num_containers)
- Baseline Success: Rehandles β€ 3 for easy, β€ 6 for medium, β€ 10 for hard
Setup & Usage
Installation
pip install -e .
Running Inference
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
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
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:
# 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:
# 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:
- Validate that the directory is an OpenEnv environment (checks for
openenv.yaml) - Prepare a custom build for Hugging Face Docker space (enables web interface)
- 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
# 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 IDscontainers_placed(int) - Number of placed containerstotal_containers(int) - Episode container countcurrent_container_id(int) - Next container to place,-1if donecurrent_container_priority(int) - Priority in range1..3rehandles_so_far(int) - Accumulated rehandlesnum_stacks(int) - Number of stacks in the current taskmax_stack_height(int) - Capacity per stackaction_error(Optional[str]) - Validation error for invalid/full stack actionsreward(float) - Step rewarddone(bool) - Whether episode is complete
Reward
The reward is calculated per valid placement:
+0.1base reward for a valid placement-0.5 * rehandles_causedpenalty for new rehandles introduced by this move+0.3bonus when placement causes zero rehandles+0.2bonus 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:
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:
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:
# 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:
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:
# 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:
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