Skip to content
Snippets Groups Projects
Commit 7c6c9cb7 authored by Lanti Davide's avatar Lanti Davide
Browse files

update random-hunter to gymnasium

parent 9e295afc
No related branches found
No related tags found
No related merge requests found
......@@ -6,12 +6,15 @@ The package has been written to be used in the master course of AI taught at the
## Install
You can download the source code from <https://gitlab.inf.unibz.it/tessaris/wumpus> and use `pip install .`, or install directly from the repository using
You can download the source code from <https://gitlab.inf.unibz.it/Davide.Lanti1/wumpus-tessaris> and use `pip install .`,
or install directly from the repository using:
```
pip install git+https://gitlab.inf.unibz.it/tessaris/wumpus.git@master
pip install git+https://gitlab.inf.unibz.it/Davide.Lanti1/wumpus-tessaris
```
The present project is a fork of the original work by Prof. Sergio Tessaris: <https://gitlab.inf.unibz.it/tessaris/wumpus>.
## Usage
To write your own player you should create a subclass of `OnlinePlayer` or `OfflinePlayer` (defined in [player.py](https://gitlab.inf.unibz.it/Davide.Lanti1/wumpus-tessaris/blob/master/wumpus/player.py)) and then use an instance as a parameter of the `run_episode` function (defined in [runner.py](https://gitlab.inf.unibz.it/Davide.Lanti1/wumpus-tessaris/-/blob/master/wumpus/runner.py)).
......
......@@ -4,52 +4,65 @@ import argparse
import json
# Register the Wumpus world environment
import gym_wumpus
from gym_wumpus.envs import WumpusEnv
from gym import envs, error, make
from gymnasium import envs, error, make
def run_episode(env: WumpusEnv):
obs = env.reset()
obs, info = env.reset()
total_reward = 0
print('Observation: {}'.format(env.space_to_percept(obs)))
print(f'Observation: {env.space_to_percept(obs)}')
for step in range(1000):
env.render(mode='human')
env.render() # This will use the render_mode set during environment creation
action = env.action_space.sample() # take a random action
obs, reward, done, info = env.step(action)
print('{} -> {}, [{}], {}{}'.format(env.space_to_action(action), reward, env.space_to_percept(obs), 'done ' if done else '', info))
obs, reward, truncated, terminated, info = env.step(action)
print(f'{env.space_to_action(action)} -> {reward}, [{env.space_to_percept(obs)}], {"done " if truncated or terminated else ""}{info}')
total_reward += reward
if done:
if truncated or terminated:
break
print('Reward {} after {} steps'.format(total_reward, step))
env.close()
def main():
default_env = 'wumpus-random-v0'
# default_env = 'wumpus-random-v0'
# default_env = 'wumpus-custom-v0'
RENDER_MODE='human'
parser = argparse.ArgumentParser()
parser.add_argument('id', nargs='?', default=default_env, help='Environment name')
parser.add_argument('--list', help='Show available environments', action="store_true", default=False)
parser.add_argument('--file', type=argparse.FileType('r'), help='Read the JSON description of the world from the file')
args = parser.parse_args()
# List available Wumpus environments
if args.list:
print([e.id for e in envs.registry.all() if str(e.id).lower().find('wumpus') >= 0])
return
# Load environment
if args.file is not None:
env = make('wumpus-custom-v0', desc=json.load(args.file))
env = make('wumpus-custom-v0', desc=json.load(args.file), render_mode=RENDER_MODE)
else:
try:
env = make(args.id)
env = make(args.id, render_mode=RENDER_MODE)
print("Observation space:", env.observation_space)
print("Action space", env.action_space)
except error.Error as e:
print('Bad Gym environment {}, using {}. Error is {}'.format(args.id, default_env, e))
env = make(default_env)
print(f'Error while creating environment {args.id}, using {default_env}. Error: {e}')
env = make(default_env, render_mode=RENDER_MODE)
# Check if the environment has the required methods
if hasattr(env, 'space_to_percept'):
run_episode(env)
else:
print('Environment {} is not a Wumpus world.'.format(env))
print(f'Environment {env} is not a Wumpus world.')
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment