-
Notifications
You must be signed in to change notification settings - Fork 99
Add Nle env to OpenEnv #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
init27
wants to merge
15
commits into
main
Choose a base branch
from
nle_env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0f234cf
add example
init27 1f5437a
Create __init__.py
init27 9fb20dd
Create client.py
init27 1f1aa30
Create models.py
init27 41c5203
Create README.md
init27 73b5687
Create __init__.py
init27 ea73ebe
Create app.py
init27 92e482e
Create Dockerfile
init27 60fac83
Create nle_environment.py
init27 0867881
Create test_integration.py
init27 8e89fec
Create validate_structure.py
init27 7e519c7
Delete test_integration.py
init27 eb7a0eb
Delete validate_structure.py
init27 6dc12cc
mv fikes
init27 0614916
Update src/envs/nle_env/models.py
init27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Example: Random Agent Playing NetHack via OpenEnv | ||
|
|
||
| This script demonstrates how to use the NLE environment through OpenEnv's | ||
| HTTP interface. It runs a random agent for a few episodes. | ||
|
|
||
| Prerequisites: | ||
| 1. Build the Docker image: | ||
| cd src/envs/nle_env/server | ||
| docker build -t nle-env:latest . | ||
|
|
||
| 2. Run this script: | ||
| python examples/nle_random_agent.py | ||
| """ | ||
|
|
||
| import random | ||
| import time | ||
|
|
||
| # Add src to path if running directly | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| src_path = Path(__file__).parent.parent / "src" | ||
| sys.path.insert(0, str(src_path)) | ||
|
|
||
| from envs.nle_env import NLEEnv, NLEAction | ||
|
|
||
|
|
||
| def print_stats(observation): | ||
| """Print human-readable stats from observation.""" | ||
| if observation.blstats is None: | ||
| return | ||
|
|
||
| blstats = observation.blstats | ||
| # BLstats indices from NLE documentation | ||
| print(f" HP: {blstats[10]}/{blstats[11]}") | ||
| print(f" XP Level: {blstats[18]}") | ||
| print(f" Gold: {blstats[13]}") | ||
| print(f" Dungeon Level: {blstats[12]}") | ||
|
|
||
|
|
||
| def main(): | ||
| print("=" * 70) | ||
| print("NLE Random Agent Example") | ||
| print("=" * 70) | ||
|
|
||
| # Start environment (automatically launches Docker container) | ||
| print("\n[1/3] Starting NLE environment...") | ||
| print("(This may take a moment if container needs to start)") | ||
|
|
||
| env = NLEEnv.from_docker_image( | ||
| "nle-env:latest", | ||
| # Optional: customize container | ||
| # env_vars={"NLE_MAX_STEPS": "1000"} | ||
| ) | ||
|
|
||
| print("✓ Environment connected!") | ||
|
|
||
| # Run a few episodes | ||
| num_episodes = 3 | ||
| max_steps_per_episode = 100 | ||
|
|
||
| print(f"\n[2/3] Running {num_episodes} episodes...") | ||
|
|
||
| for episode in range(num_episodes): | ||
| print(f"\n--- Episode {episode + 1}/{num_episodes} ---") | ||
|
|
||
| # Reset environment | ||
| result = env.reset() | ||
| print("Environment reset") | ||
| print_stats(result.observation) | ||
|
|
||
| episode_reward = 0 | ||
| steps = 0 | ||
|
|
||
| # Play episode | ||
| for step in range(max_steps_per_episode): | ||
| # Random action (0-112) | ||
| action = NLEAction(action_id=random.randint(0, 112)) | ||
|
|
||
| # Take step | ||
| result = env.step(action) | ||
|
|
||
| episode_reward += result.reward or 0 | ||
| steps += 1 | ||
|
|
||
| # Print occasional updates | ||
| if step % 20 == 0: | ||
| print(f" Step {step}: reward={episode_reward:.1f}") | ||
|
|
||
| # Check if done | ||
| if result.done: | ||
| state = env.state() | ||
| print(f"\nEpisode ended after {steps} steps!") | ||
| print(f" Total reward: {episode_reward:.1f}") | ||
| print(f" End status: {state.end_status}") | ||
| print(f" Final stats:") | ||
| print_stats(result.observation) | ||
| break | ||
| else: | ||
| print(f"\nReached max steps ({max_steps_per_episode})") | ||
| print(f" Total reward: {episode_reward:.1f}") | ||
|
|
||
| time.sleep(0.5) # Brief pause between episodes | ||
|
|
||
| # Cleanup | ||
| print("\n[3/3] Cleaning up...") | ||
| env.close() | ||
| print("✓ Environment closed") | ||
|
|
||
| print("\n" + "=" * 70) | ||
| print("Example complete!") | ||
| print("=" * 70) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| main() | ||
| except KeyboardInterrupt: | ||
| print("\n\nInterrupted by user") | ||
| except Exception as e: | ||
| print(f"\n\nError: {e}") | ||
| import traceback | ||
|
|
||
| traceback.print_exc() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ POSITIVE: Excellent Example
This example is clear, well-documented, and demonstrates proper usage. Great work!