-
Notifications
You must be signed in to change notification settings - Fork 87
Add maze environment and example #106
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
VivekSil
wants to merge
16
commits into
meta-pytorch:main
Choose a base branch
from
VivekSil:maze
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
16 commits
Select commit
Hold shift + click to select a range
2bc12df
Add maze environment and example
VivekSil e3eafcf
Add maze environment to workflow matrix
VivekSil d458434
Add dependencies to Dockerfile
VivekSil e9e1c0f
Add reward function
VivekSil f47d107
Update maze example for reward
VivekSil 6c0c7a8
Implement done flag for win
VivekSil 373ff5b
Minor fix
VivekSil cc336cf
Typo fix
VivekSil 72a6476
Merge branch 'main' into maze
VivekSil 6979679
Remove unused imports and minor fix
VivekSil 15fe367
Remove unused import and function
VivekSil 11347ef
Add maze env to hf build
VivekSil 71e6f97
Fix hf deployment for maze env
VivekSil b055050
Fix hf deployment
VivekSil 3656fc6
Update deploy to hf script
VivekSil d1c5c50
Merge branch 'main' into maze
VivekSil 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
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
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,99 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """ | ||
| Simple example of using Maze environment with OpenEnv. | ||
|
|
||
| This demonstrates: | ||
| 1. Connecting to the Maze environment server | ||
| 2. Resetting the environment | ||
| 3. Taking actions | ||
| 4. Observing rewards | ||
| 5. Inspecting environment state | ||
|
|
||
| Usage: | ||
| python examples/maze_simple.py | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Add src to path | ||
| sys.path.insert(0, str(Path(__file__).parent.parent / "src")) | ||
| import numpy as np | ||
| from envs.maze_env import MazeEnv, MazeAction | ||
|
|
||
|
|
||
| def main(): | ||
| print("🧩 Simple Maze Environment Example") | ||
| print("=" * 60) | ||
|
|
||
| # Connect to environment server | ||
| # Ensure server is running: python -m envs.maze_env.server.app | ||
| env = MazeEnv(base_url="http://localhost:8000") | ||
| maze = np.array([ | ||
| [0, 1, 0, 0, 0, 0, 0, 0], | ||
| [0, 1, 0, 1, 0, 1, 0, 0], | ||
| [0, 0, 0, 1, 1, 0, 1, 0], | ||
| [0, 1, 0, 1, 0, 0, 0, 0], | ||
| [1, 0, 0, 1, 0, 1, 0, 0], | ||
| [0, 0, 0, 1, 0, 1, 1, 1], | ||
| [0, 1, 1, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 1, 0, 0] | ||
| ]) | ||
| try: | ||
| # Reset environment | ||
| print("\n📍 Resetting environment...") | ||
| result = env.reset() | ||
|
|
||
| print(f" Initial position: {result.observation.position}") | ||
| print(f" Legal actions: {result.observation.legal_actions}") | ||
| # Note: Initial total reward is 0 however it is observed it doesn't resets if you run this example again during the same server app session | ||
| print(f" Initial Total reward: {result.observation.total_reward}") | ||
| # Run one episode | ||
| print("\n🚶 Navigating through maze...") | ||
| step = 0 | ||
|
|
||
| while not result.done and step < 25: | ||
| # Choose random legal action | ||
| print(f" Current position: {result.observation.position}") | ||
| print(f" Legal actions: {result.observation.legal_actions}") | ||
| env.render_ascii_maze(maze,result.observation.position,[0,0],[maze.shape[0],maze.shape[1]]) | ||
VivekSil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| action_id = int(input("Make any move from the legal actions")) | ||
| # Take action | ||
| result = env.step(MazeAction(action=action_id)) | ||
| reward = result.observation.total_reward or 0 | ||
|
|
||
| print(f" Step {step + 1}: action={action_id}, pos={result.observation.position}, reward={reward:.2f}, done={result.done}") | ||
| step += 1 | ||
| print("-----------------------------------------------------") | ||
|
|
||
| print("\n✅ Episode finished!") | ||
| print(f" Total steps: {step}") | ||
| print(f" Total reward: {reward}") | ||
|
|
||
| # Get environment state | ||
| state = env.state() | ||
| print("\n📊 Environment State:") | ||
| print(f" Episode ID: {state.episode_id}") | ||
| print(f" Step count: {state.step_count}") | ||
| print(f" Done: {state.done}") | ||
|
|
||
| except Exception as e: | ||
| print(f"\n❌ Error: {e}") | ||
| print("\nMake sure the server is running:") | ||
| print(" python -m envs.maze_env.server.app") | ||
| print("\nOr start with Docker:") | ||
| print(" docker run -p 8000:8000 maze-env:latest") | ||
|
|
||
| finally: | ||
| env.close() | ||
| print("\n👋 Done!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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,103 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """ | ||
| Simple example of using Maze environment with OpenEnv. | ||
|
|
||
| This demonstrates: | ||
| 1. Connecting to the Maze environment server | ||
| 2. Resetting the environment | ||
| 3. Taking actions | ||
| 4. Observing rewards | ||
| 5. Inspecting environment state | ||
|
|
||
| Usage: | ||
| python examples/maze_simple.py | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Add src to path | ||
| sys.path.insert(0, str(Path(__file__).parent.parent / "src")) | ||
| import numpy as np | ||
| from envs.maze_env import MazeEnv, MazeAction | ||
|
|
||
|
|
||
| def main(): | ||
| print("🧩 Simple Maze Environment Example") | ||
| print("=" * 60) | ||
|
|
||
| # Connect to environment server | ||
| # Ensure server is running: python -m envs.maze_env.server.app | ||
| env = MazeEnv(base_url="http://localhost:8000") | ||
| maze = np.array([ | ||
| [0, 1, 0, 0, 0, 0, 0, 0], | ||
| [0, 1, 0, 1, 0, 1, 0, 0], | ||
| [0, 0, 0, 1, 1, 0, 1, 0], | ||
| [0, 1, 0, 1, 0, 0, 0, 0], | ||
| [1, 0, 0, 1, 0, 1, 0, 0], | ||
| [0, 0, 0, 1, 0, 1, 1, 1], | ||
| [0, 1, 1, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 1, 0, 0] | ||
| ]) | ||
| try: | ||
| # Reset environment | ||
| print("\n📍 Resetting environment...") | ||
| result = env.reset() | ||
|
|
||
| print(f" Initial position: {result.observation.position}") | ||
| print(f" Legal actions: {result.observation.legal_actions}") | ||
| # Note: Initial total reward is 0 however it is observed it doesn't resets if you run this example again during the same server app session | ||
| print(f" Initial Total reward: {result.observation.total_reward}") | ||
|
|
||
| # Run one episode | ||
| print("\n🚶 Navigating through maze...") | ||
| step = 0 | ||
| total_reward = 0 | ||
|
|
||
| while not result.done and step < 20: | ||
| # Choose random legal action | ||
| print(f" Current position: {result.observation.position}") | ||
| print(f" Legal actions: {result.observation.legal_actions}") | ||
| env.render_ascii_maze(maze,result.observation.position,[0,0],[maze.shape[0],maze.shape[1]]) | ||
VivekSil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| action_id = result.observation.legal_actions[step % len(result.observation.legal_actions)] | ||
| # Take action | ||
| result = env.step(MazeAction(action=action_id)) | ||
|
|
||
| reward = result.reward or 0 | ||
| total_reward += reward | ||
|
|
||
| print(f" Step {step + 1}: action={action_id}, pos={result.observation.position}, reward={reward:.2f}, done={result.done}") | ||
| step += 1 | ||
| print("-----------------------------------------------------") | ||
|
|
||
| print("\n✅ Episode finished!") | ||
| print(f" Total steps: {step}") | ||
| print(f" Total reward: {total_reward}") | ||
|
|
||
| # Get environment state | ||
| state = env.state() | ||
| print("\n📊 Environment State:") | ||
| print(f" Episode ID: {state.episode_id}") | ||
| print(f" Step count: {state.step_count}") | ||
| print(f" Done: {state.done}") | ||
|
|
||
| except Exception as e: | ||
| print(f"\n❌ Error: {e}") | ||
| print("\nMake sure the server is running:") | ||
| print(" python -m envs.maze_env.server.app") | ||
| print("\nOr start with Docker:") | ||
| print(" docker run -p 8000:8000 maze-env:latest") | ||
|
|
||
| finally: | ||
| env.close() | ||
| print("\n👋 Done!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.