Skip to content

Commit

Permalink
working on ant environment
Browse files Browse the repository at this point in the history
  • Loading branch information
AOS55 committed Oct 31, 2022
1 parent 573a262 commit ef8983f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
5 changes: 3 additions & 2 deletions libraries/dmc/tasks/quadruped.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from dm_control.utils import containers
from dm_control.utils import rewards
from dm_control.utils import xml_tools
from dm_control.utils import io as resources
from lxml import etree
import numpy as np
from scipy import ndimage
Expand Down Expand Up @@ -73,7 +74,7 @@ def get_model_and_assets():
"""Returns a tuple containing the model XML string and a dict of assets."""
root_dir = os.path.dirname(os.path.dirname(__file__))
xml = resources.GetResource(
os.path.join(root_dir, 'custom_dmc_tasks', 'quadruped.xml'))
os.path.join(root_dir, 'tasks', 'quadruped.xml'))
return xml, common.ASSETS


Expand All @@ -84,7 +85,7 @@ def make_model(floor_size=None,
"""Returns the model XML string."""
root_dir = os.path.dirname(os.path.dirname(__file__))
xml_string = common.read_model(
os.path.join(root_dir, 'custom_dmc_tasks', 'quadruped.xml'))
os.path.join(root_dir, 'tasks', 'quadruped.xml'))
parser = etree.XMLParser(remove_blank_text=True)
mjcf = etree.XML(xml_string, parser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ def make(task,
env.task.visualize_reward = visualize_reward
return env

@SUITE.add('benchmarking')
def navigate(time_limit=_DEFAULT_TIME_LIMIT, random=None, environment_kwargs=None):
@SUITE.add()
def navigate(time_limit=_DEFAULT_TIME_LIMIT,
random=None,
environment_kwargs=None):
"""Navigate to a goal position"""
walker = Ant()
arena = obstacles.Obstacle()
Expand Down
70 changes: 55 additions & 15 deletions libraries/safe/dmc/obstacles.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

_GROUNDPLANE_QUAD_SIZE = 0.25
_TOP_CAMERA_DISTANCE = 7.0
_TOP_CAMERA_Y_PADDING_FACTOR = 2.2
_TOP_CAMERA_Y_PADDING_FACTOR = 2.5


class Obstacle(composer.Arena):
Expand All @@ -19,7 +19,7 @@ def _build(self, size=(11, 11), name='obstacle'):
super()._build(name=name)

self._aesthetic = 'outdoor_natural'
self._size = size
self.size = size

sky_info = locomotion_arenas_assets.get_sky_texture_info(self._aesthetic)

Expand Down Expand Up @@ -55,27 +55,71 @@ def _build(self, size=(11, 11), name='obstacle'):
pos = [0, 0, 0]
)

#TODO: Work out why the ground doesn't render correctly
#TODO: Work out why the ground doesn't render correctly
ground_size = max(size)
top_camera_fovy = (360 / np.pi) * np.arctan2(
_TOP_CAMERA_Y_PADDING_FACTOR * ground_size / 2,
_TOP_CAMERA_DISTANCE)
print(f'top_camera_distance: {_TOP_CAMERA_DISTANCE}')
self._top_camera = self._mjcf_root.worldbody.add(
'camera',
name='top_camera',
pos=[0, 0, _TOP_CAMERA_DISTANCE],
zaxis=[0, 0, 1],
fovy = top_camera_fovy
fovy=top_camera_fovy
)

self._obstacle = self._mjcf_root.worldbody.add(
'site',
name='obstacle',
'geom',
type='box',
name='box_obstacle',
size=[2.0, 2.0, 0.5],
rgba=[1, 0, 0, 1],
pos = [0.5, 0, 0]
)

self._wall_height = 1.0
self._wall_thickness = 0.5
self._wall_colour = [0, 0, 0, 0.0]
self._wall_left = self._mjcf_root.worldbody.add(
'geom',
type='box',
name='wall_left',
size=[self._wall_thickness, size[1], self._wall_height],
rgba=self._wall_colour,
pos=[size[0], 0.0, 0.0]
)
self._wall_right = self._mjcf_root.worldbody.add(
'geom',
type='box',
name='wall_right',
size=[self._wall_thickness, size[1], self._wall_height],
rgba=self._wall_colour,
pos=[-size[0], 0.0, 0.0]
)
self._wall_front = self._mjcf_root.worldbody.add(
'geom',
type='box',
name='wall_front',
size=[size[0], self._wall_thickness, self._wall_height],
rgba=self._wall_colour,
pos=[0.0, size[1], 0.0]
)
self._wall_back = self._mjcf_root.worldbody.add(
'geom',
type='box',
pos=(0., 0., 0.),
size=(1.0, 2.0, 2.0),
rgba=(1.0, 0.0, 0.0, 1.0)
name='wall_back',
size=[size[0], self._wall_thickness, self._wall_height],
rgba=self._wall_colour,
pos=[0.0, -size[1], 0.0]
)

self._objective = self._mjcf_root.worldbody.add(
'geom',
type='sphere',
name='goal_position',
size=[0.7],
rgba=[0, 0, 1, 1],
pos=[-5.0, 0.0, 0.0]
)

def _build_observables(self):
Expand All @@ -89,10 +133,6 @@ def ground_geoms(self):
def top_camera(self):
return self._top_camera

@property
def size(self):
return self._size

def regenerate(self, random_state):
pass

Expand Down
5 changes: 5 additions & 0 deletions libraries/safe/simple_point_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def step(self, a):
constr = self.obstacle(next_state)
self.done = self._episode_steps >= self.horizon

# Terminate episode if we hit the wall
if 0+1 >= self.state[0] or self.state[0] >= WINDOW_WIDTH-1:
self.done = True
if 0+1 >= self.state[1] or self.state[1] >= WINDOW_HEIGHT-1:
self.done = True
if self._from_pixels:
obs = self._state_to_image(self.state)
else:
Expand Down
4 changes: 3 additions & 1 deletion utils/env_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import libraries.dmc as cdmc
from libraries.safe import SimplePointBot, SimpleVelocityBot
from libraries.safe.dmc import ant_obstacle
from .wrappers import GymWrapper
from .wrappers import FrameStack

Expand All @@ -22,7 +23,8 @@
'CarRacing-v2': 'gym',
'LunarLander-v2': 'gym',
'SimplePointBot': 'safe',
'SimpleVelocityBot': 'safe'
'SimpleVelocityBot': 'safe',
'ant_obstacle': 'dmc'
}

SAFE_ENVS = {
Expand Down

0 comments on commit ef8983f

Please sign in to comment.