Skip to content

Commit

Permalink
formatting and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaostuni committed Jan 3, 2024
1 parent 3c6b013 commit bc3de29
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 55 deletions.
4 changes: 1 addition & 3 deletions examples/panda_pick_and_place.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ def move_fingers(
gazebo.run(paused=True)

# Insert the Panda manipulator
panda = gym_gz_environments.models.panda.Panda(
world=world, position=[-0.1, 0, 1.0]
)
panda = gym_gz_environments.models.panda.Panda(world=world, position=[-0.1, 0, 1.0])

# Disable joint velocity limits
_ = [j.to_gazebo().set_velocity_limit(1_000) for j in panda.joints()]
Expand Down
2 changes: 1 addition & 1 deletion examples/python/launch_cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


def make_env_from_id(env_id: str, **kwargs) -> gym.Env:
import gymnasium as gym
import gym_gz_environments
import gymnasium as gym

return gym.make(env_id, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion python/gym_gz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

def initialize_verbosity() -> None:

import gymnasium as gym
import gym_gz.utils.logger
import gymnasium as gym

import scenario

Expand Down
4 changes: 2 additions & 2 deletions python/gym_gz/base/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import gymnasium as gym
import numpy as np
from gymnasium.utils import seeding
from gym_gz.utils.typing import (
Action,
ActionSpace,
Expand All @@ -16,6 +15,7 @@
Reward,
SeedList,
)
from gymnasium.utils import seeding

from scenario import core

Expand Down Expand Up @@ -208,7 +208,7 @@ def is_terminated(self) -> bool:
Returns:
True if the environment terminated, False otherwise.
"""

@abc.abstractmethod
def is_truncated(self) -> bool:
"""
Expand Down
4 changes: 1 addition & 3 deletions python/gym_gz/randomizers/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ def randomize_model(

class ModelDescriptionRandomizer(abc.ABC):
@abc.abstractmethod
def randomize_model_description(
self, task: gym_gz.base.task.Task, **kwargs
) -> str:
def randomize_model_description(self, task: gym_gz.base.task.Task, **kwargs) -> str:
"""
Randomize the model description.
Expand Down
8 changes: 6 additions & 2 deletions python/gym_gz/randomizers/gazebo_env_randomizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def __init__(
# gym.Env methods
# ===============

def reset(self, seed: int = None, options : Dict = {}, **kwargs) -> typing.ResetReturn:
def reset(
self, seed: int = None, options: Dict = {}, **kwargs
) -> typing.ResetReturn:

# Reset the physics
if self._physics_randomizer.physics_expired() and seed is None:
Expand All @@ -94,7 +96,9 @@ def reset(self, seed: int = None, options : Dict = {}, **kwargs) -> typing.Reset
self._physics_randomizer.increase_rollout_counter()

# Reset the task through the TaskRandomizer
self.randomize_task(task=self.env.unwrapped.task, gazebo=self.env.unwrapped.gazebo, **kwargs)
self.randomize_task(
task=self.env.unwrapped.task, gazebo=self.env.unwrapped.gazebo, **kwargs
)

ok_paused_run = self.env.unwrapped.gazebo.run(paused=True)

Expand Down
37 changes: 26 additions & 11 deletions python/gym_gz/runtimes/gazebo_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

from typing import Optional, Dict
from typing import Dict, Optional

import gym_gz_models
import gymnasium as gym
import numpy as np

import gym_gz_models
from gym_gz import base
from gym_gz.base import runtime
from gym_gz.utils import logger
from gym_gz.utils.typing import Action, Info, Observation, Reward, SeedList, State, Terminated, Truncated, ResetReturn
from gym_gz.utils.typing import (
Action,
Info,
Observation,
ResetReturn,
Reward,
SeedList,
State,
Terminated,
Truncated,
)

from scenario import gazebo as scenario

Expand Down Expand Up @@ -143,9 +152,17 @@ def step(self, action: Action) -> State:
# Get info
info = self.task.get_info()

return State((Observation(observation), Reward(reward), Terminated(terminated), Truncated(truncated), Info(info)))
return State(
(
Observation(observation),
Reward(reward),
Terminated(terminated),
Truncated(truncated),
Info(info),
)
)

def reset(self, seed: int = None, options : Dict = {}, **kwargs) -> ResetReturn:
def reset(self, seed: int = None, options: Dict = {}, **kwargs) -> ResetReturn:

self.seed(seed)

Expand All @@ -170,8 +187,8 @@ def reset(self, seed: int = None, options : Dict = {}, **kwargs) -> ResetReturn:
# Render the environment
if self.render_mode == "human":
self.render()
return ResetReturn((Observation(observation), Info(info)))

return ResetReturn((Observation(observation), Info(info)))

def render(self, **kwargs) -> None:
mode = self.render_mode
Expand Down Expand Up @@ -282,9 +299,7 @@ def world(self) -> scenario.World:
if self._world_sdf == "":

# Insert the ground plane
ok_ground = world.insert_model(
gym_gz_models.get_model_file("ground_plane")
)
ok_ground = world.insert_model(gym_gz_models.get_model_file("ground_plane"))

if not ok_ground:
raise RuntimeError("Failed to insert the ground plane")
Expand Down
43 changes: 35 additions & 8 deletions python/gym_gz/runtimes/realtime_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

from gym_gz.base import runtime, task
from gym_gz.utils.typing import Action, Done, Info, Observation, State, Terminated, Truncated, ResetReturn, Dict
from typing import Optional

from gym_gz.base import runtime, task
from gym_gz.utils.typing import (
Action,
Dict,
Done,
Info,
Observation,
ResetReturn,
State,
Terminated,
Truncated,
)


class RealTimeRuntime(runtime.Runtime):
"""
Implementation of :py:class:`~gym_gz.base.runtime.Runtime` for real-time
Expand All @@ -15,7 +27,14 @@ class RealTimeRuntime(runtime.Runtime):
This class is not yet complete.
"""

def __init__(self, task_cls: type, robot_cls: type, agent_rate: float, render_mode: Optional[str] = None, **kwargs):
def __init__(
self,
task_cls: type,
robot_cls: type,
agent_rate: float,
render_mode: Optional[str] = None,
**kwargs
):

# Build the environment
task_object = task_cls(**kwargs)
Expand All @@ -27,7 +46,7 @@ def __init__(self, task_cls: type, robot_cls: type, agent_rate: float, render_mo

# Render mode
self.render_mode = render_mode

super().__init__(task=task_object, agent_rate=agent_rate)

raise NotImplementedError
Expand Down Expand Up @@ -72,17 +91,25 @@ def step(self, action: Action) -> State:
# Check termination
terminated = self.task.is_terminated()

#Check truncation
# Check truncation
truncated = self.task.is_truncated()

return State((observation, reward, Terminated(terminated), Truncated(truncated), Info({})))
return State(
(
observation,
reward,
Terminated(terminated),
Truncated(truncated),
Info({}),
)
)

def reset(self, seed: int = None, options : Dict = {}, **kwargs) -> ResetReturn:
def reset(self, seed: int = None, options: Dict = {}, **kwargs) -> ResetReturn:

# Get the observation
observation = self.task.get_observation()

return ResetReturn((observation, Info({})))
return ResetReturn((observation, Info({})))

def render(self, **kwargs) -> None:
mode = self.render_mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
ActionSpace,
Observation,
ObservationSpace,
Reward,
ResetReturn,
Reward,
)

from scenario import core as scenario
Expand Down Expand Up @@ -125,7 +125,7 @@ def is_terminated(self) -> bool:
terminated = not self.reset_space.contains(observation)

return terminated

def is_truncated(self) -> bool:
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def is_terminated(self) -> bool:
terminated = not self.reset_space.contains(observation)

return terminated

def is_truncated(self) -> bool:
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def is_terminated(self) -> bool:
terminated = not self.reset_space.contains(observation)

return terminated

def is_truncated(self) -> bool:
return False

Expand Down
2 changes: 1 addition & 1 deletion python/gym_gz_environments/tasks/pendulum_swingup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def is_terminated(self) -> bool:
terminated = not self.observation_space.contains(observation)

return terminated

def is_truncated(self) -> bool:
return False

Expand Down
4 changes: 1 addition & 3 deletions scenario/bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ def import_gazebo() -> None:
def create_home_dot_folder() -> None:

# Make sure that the dot folder in the user's home exists
Path("~/.gz/gazebo").expanduser().mkdir(
mode=0o755, parents=True, exist_ok=True
)
Path("~/.gz/gazebo").expanduser().mkdir(mode=0o755, parents=True, exist_ok=True)


# ===================
Expand Down
12 changes: 6 additions & 6 deletions tests/.python/test_pendulum_wrt_ground_truth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

from typing import Dict, Optional

import gymnasium as gym
import numpy as np
import pytest
from gymnasium.envs import registry
from gymnasium.envs.registration import register
from gym_gz.robots.sim import gazebo, pybullet
from gym_gz.tasks.pendulum_swingup import PendulumSwingUp
from gym_gz.utils import logger
from gym_gz.utils.typing import Observation, Reward, State
from typing import Dict, Optional
from gym_gz.utils.typing import Observation, Reward, State
from gymnasium.envs import registry
from gymnasium.envs.registration import register

# Set verbosity
logger.set_level(gym.logger.DEBUG)
Expand Down Expand Up @@ -70,7 +71,7 @@ def step(self, action: np.ndarray) -> State:

return State((Observation(observation), Reward(0.0), False, {}))

def reset(self, seed: int = None, options: Dict ={}, **kwargs):
def reset(self, seed: int = None, options: Dict = {}, **kwargs):
# Use set_state_from_obs
pass

Expand Down Expand Up @@ -134,7 +135,6 @@ def template_pendulum_wrt_ground_truth(env_name: str, max_error_in_deg: float):
# env.render('human')
# time.sleep(5)


for epoch in range(10):
# Reset the environment
logger.info("Resetting the environment")
Expand Down
8 changes: 3 additions & 5 deletions tests/.python/test_runtimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import gymnasium as gym
import numpy as np
import pytest
from gymnasium.envs import registry
from gymnasium.envs.registration import register
from gym_gz.robots.sim import gazebo, pybullet
from gym_gz.tasks.cartpole_discrete import CartPoleDiscrete
from gym_gz.tasks.pendulum_swingup import PendulumSwingUp
from gym_gz.utils import logger
from gymnasium.envs import registry
from gymnasium.envs.registration import register

# Set verbosity
logger.set_level(gym.logger.DEBUG)
Expand Down Expand Up @@ -49,9 +49,7 @@
},
)

if "CartPoleDiscrete-Gz-PyTest-v0" not in [
spec.id for spec in list(registry.all())
]:
if "CartPoleDiscrete-Gz-PyTest-v0" not in [spec.id for spec in list(registry.all())]:
register(
id="CartPoleDiscrete-Gz-PyTest-v0",
entry_point="gym_gz.runtimes.gazebo_runtime:GazeboRuntime",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gym_gz/test_reproducibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

def make_env(**kwargs) -> gym.Env:

import gymnasium as gym
import gym_gz_environments
import gymnasium as gym

return gym.make("CartPoleDiscreteBalancing-Gazebo-v0", **kwargs)

Expand Down
1 change: 0 additions & 1 deletion tests/test_scenario/test_link_velocities.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ def test_linear_acceleration(
if (np.absolute(np.array(world_linear_acceleration())) > 100.0).any():
continue


# Test the world acceleration (MIXED)
assert world_linear_acceleration() == pytest.approx(world_acceleration, abs=0.5)

Expand Down
4 changes: 1 addition & 3 deletions tests/test_scenario/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def get_model(

model_urdf = gym_gz_models.get_model_file(gym_gz_models_name)

assert world.insert_model(
model_urdf, core.Pose_identity(), gym_gz_models_name
)
assert world.insert_model(model_urdf, core.Pose_identity(), gym_gz_models_name)

# Get the model and cast it to a Gazebo model
model = world.get_model(gym_gz_models_name).to_gazebo()
Expand Down

0 comments on commit bc3de29

Please sign in to comment.