Skip to content
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

Updated to Gymnasium v0.26.2 #72

Merged
merged 14 commits into from
Oct 21, 2022
Merged
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ venv.bak/

# mypy
.mypy_cache/

# tests
test_result.txt
test_results.txt

# VS Code
.vscode

# MacOS
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This simulator was created as part of work done at [Mila](https://mila.quebec/).

Requirements:
- Python 3.5+
- OpenAI Gym
- Gymnasium
- NumPy
- Pyglet (OpenGL 3D graphics)
- GPU for 3D graphics acceleration (optional)
Expand Down
2 changes: 1 addition & 1 deletion gym_miniworld/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect

import gym
import gymnasium as gym

from gym_miniworld.envs.collecthealth import CollectHealth
from gym_miniworld.envs.fourrooms import FourRooms
Expand Down
6 changes: 3 additions & 3 deletions gym_miniworld/envs/collecthealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _gen_world(self):
self.health = 100

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

self.health -= 2

Expand All @@ -62,9 +62,9 @@ def step(self, action):
reward = 2
else:
reward = -100
done = True
termination = True

# Pass current health value in info dict
info["health"] = self.health

return obs, reward, done, info
return obs, reward, termination, truncation, info
8 changes: 4 additions & 4 deletions gym_miniworld/envs/fourrooms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Box
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -37,10 +37,10 @@ def _gen_world(self):
self.place_agent()

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if self.near(self.box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info
8 changes: 4 additions & 4 deletions gym_miniworld/envs/hallway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Box
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -34,10 +34,10 @@ def _gen_world(self):
)

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if self.near(self.box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info
8 changes: 4 additions & 4 deletions gym_miniworld/envs/maze.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Box
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -102,13 +102,13 @@ def visit(i, j):
self.place_agent()

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if self.near(self.box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info


class MazeS2(Maze):
Expand Down
8 changes: 4 additions & 4 deletions gym_miniworld/envs/oneroom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Box
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -27,13 +27,13 @@ def _gen_world(self):
self.place_agent()

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if self.near(self.box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info


class OneRoomS6(OneRoom):
Expand Down
8 changes: 4 additions & 4 deletions gym_miniworld/envs/pickupobjs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Ball, Box, Key
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -49,7 +49,7 @@ def _gen_world(self):
self.num_picked_up = 0

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if self.agent.carrying:
self.entities.remove(self.agent.carrying)
Expand All @@ -58,6 +58,6 @@ def step(self, action):
reward = 1

if self.num_picked_up == self.num_objs:
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info
6 changes: 3 additions & 3 deletions gym_miniworld/envs/putnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def _gen_world(self):
self.place_agent()

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if not self.agent.carrying:
if self.near(self.red_box, self.yellow_box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info
40 changes: 24 additions & 16 deletions gym_miniworld/envs/remotebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import math

import gym
import gymnasium as gym
import numpy
import numpy as np
import pyglet
from gym import spaces
from gym.utils import seeding
from gymnasium import spaces

# Try importing ZMQ
from pyglet.gl import (
Expand All @@ -23,6 +22,9 @@

from gym_miniworld.miniworld import MiniWorldEnv

# from gym.utils import seeding


try:
import zmq
except ImportError:
Expand Down Expand Up @@ -70,6 +72,7 @@ def __init__(
serverPort=SERVER_PORT,
obs_width=80,
obs_height=60,
render_mode=None,
):
assert zmq is not None, "Please install zmq (pip3 install zmq)"

Expand All @@ -94,6 +97,7 @@ def __init__(

# For rendering
self.window = None
self.render_mode = render_mode

# We continually stream in images and then just take the latest one.
self.latest_img = None
Expand All @@ -113,7 +117,6 @@ def __init__(
self.socket.connect(addr_str)

# Initialize the state
self.seed()
self.reset()
print("Connected")

Expand All @@ -128,7 +131,7 @@ def _recv_frame(self):

self.img = img

def reset(self):
def reset(self, seed, options):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add type hints

Copy link
Collaborator Author

@BolunDai0216 BolunDai0216 Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I noticed that the type hint situation is not consistent, some functions have it while others don't. Is there a guideline? Or should we just add type hints to all of the functions?

Copy link
Member

@pseudo-rnd-thoughts pseudo-rnd-thoughts Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, it would be great to type hint the whole project but for now just having reset and step would be easiest. We can make another PR that adds type hinting for more of the project.

# Step count since episode start
self.step_count = 0

Expand All @@ -143,11 +146,7 @@ def reset(self):
# Receive a camera image from the server
self._recv_frame()

return self.img

def seed(self, seed=None):
self.np_random, _ = seeding.np_random(seed)
return [seed]
return self.img, {}

def step(self, action):
# Send the action to the server
Expand All @@ -162,17 +161,26 @@ def step(self, action):

# We don't care about rewards or episodes since we're not training
reward = 0
done = False

return self.img, reward, done, {}
termination = False
truncation = False

return self.img, reward, termination, truncation, {}

def render(self, close=False):
BolunDai0216 marked this conversation as resolved.
Show resolved Hide resolved
if self.render_mode is None:
gym.logger.warn(
"You are calling render method without specifying any render mode. "
"You can specify the render_mode at initialization, "
f'e.g. gym("{self.spec.id}", render_mode="rgb_array")'
)
return

def render(self, mode="human", close=False):
if close:
if self.window:
self.window.close()
return

if mode == "rgb_array":
if self.render_mode == "rgb_array":
return self.img

if self.window is None:
Expand Down Expand Up @@ -209,6 +217,6 @@ def render(self, mode="human", close=False):

# If we are not running the Pyglet event loop,
# we have to manually flip the buffers and dispatch events
if mode == "human":
if self.render_mode == "human":
self.window.flip()
self.window.dispatch_events()
4 changes: 2 additions & 2 deletions gym_miniworld/envs/roomobjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def _gen_world(self):
self.place_agent()

def step(self, action):
obs, reward, done, info = super().step(action)
return obs, reward, done, info
obs, reward, termination, truncation, info = super().step(action)
return obs, reward, termination, truncation, info
10 changes: 5 additions & 5 deletions gym_miniworld/envs/sidewalk.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math

import numpy as np
from gym import spaces
from gymnasium import spaces

from gym_miniworld.entity import Box, MeshEnt
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -65,15 +65,15 @@ def _gen_world(self):
self.place_agent(room=sidewalk, min_z=0, max_z=1.5)

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

# Walking into the street ends the episode
if self.street.point_inside(self.agent.pos):
reward = 0
done = True
termination = True

if self.near(self.box):
reward += self._reward()
done = True
termination = True

return obs, reward, done, info
return obs, reward, termination, truncation, info
26 changes: 10 additions & 16 deletions gym_miniworld/envs/sign.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import math
from typing import Optional, Tuple, Union

import gym
from gym.core import ObsType
from gym.spaces import Dict, Discrete
import gymnasium as gym
from gymnasium.core import ObsType
from gymnasium.spaces import Dict, Discrete

from gym_miniworld.entity import COLOR_NAMES, Box, Key, MeshEnt, TextFrame
from gym_miniworld.miniworld import MiniWorldEnv
Expand Down Expand Up @@ -126,14 +126,15 @@ def _gen_world(self):
self.place_agent(min_x=4, max_x=5, min_z=4, max_z=6)

def step(self, action):
obs, reward, done, info = super().step(action)
obs, reward, termination, truncation, info = super().step(action)

if action == self.actions.move_forward + 1: # custom end episode action
done = True
termination = True

for obj_index, object_pair in enumerate(self._objects):
for color_index, obj in enumerate(object_pair):
if self.near(obj):
done = True
termination = True
reward = (
float(
color_index == self._color_index and obj_index == self._goal
Expand All @@ -143,20 +144,13 @@ def step(self, action):
)

state = {"obs": obs, "goal": self._goal}
return state, reward, done, info
return state, reward, termination, truncation, info

def reset(
self,
*,
seed: Optional[int] = None,
return_info: bool = False,
options: Optional[dict] = None,
) -> Union[ObsType, Tuple[ObsType, dict]]:
BolunDai0216 marked this conversation as resolved.
Show resolved Hide resolved
if return_info:
obs, info = super().reset(
seed=seed, return_info=return_info, options=options
)
return {"obs": obs, "goal": self._goal}, info
else:
obs = super().reset(seed=seed, return_info=return_info, options=options)
return {"obs": obs, "goal": self._goal}
obs, info = super().reset(seed=seed, options=options)
return {"obs": obs, "goal": self._goal}, info
Loading