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

Dynamics randomization for MuJoCo #109

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cff37ed
First draft to specify variations in dynamics randomization
May 30, 2018
0986def
Apply PEP8 format with YEPF
May 30, 2018
84ca64d
Reimplement the fluent interface to create individual setters
May 31, 2018
12c7f36
Apply PEP8 format with YEPF
May 31, 2018
9104c48
Add fisrt draft of randomize environment
May 31, 2018
9eba2da
Rename RandomizeEnv.py to randomized_env.py
May 31, 2018
6fc661b
Add features and remove variables in RandomizedEnv
May 31, 2018
e4ce9b1
Fix bugs in error handling
May 31, 2018
28d73e8
Add thread to generate Mujoco models
Jun 2, 2018
21e42ee
Add thread terminate in mujoco_model_gen
Jun 4, 2018
009ddbf
Add interrupt handler to mujoco model gen
Jun 4, 2018
5485bfb
Add documentation to new classes and sort import headers
Jun 4, 2018
4442f12
Fix bug in MujocoModelGenerator
Jun 4, 2018
4a96217
Change multi-thread MujocoModelGen to n-length queue
Jun 5, 2018
c1a4a81
Miscellaneous changes to improve the code
Jun 5, 2018
cf45d0c
Fix PEP8 formatting in file mujoco_model_gen.py
Jun 5, 2018
735ed80
Add miscellaneous changes to improve the code
Jun 5, 2018
25db0fe
Add local cache of elems in MujocoModelGenerator
Jun 5, 2018
22d69af
Add miscellaneous changes to improve the code
Jun 6, 2018
a4c8442
Fix PEP formatting with YAPF
Jun 6, 2018
43594ea
Add error handling to MujocoModelGenerator
Jun 6, 2018
4221002
Rename RandomizedDynamicsEnv to RandomizedEnv
Jun 6, 2018
133e2cb
Fix mujoco exception caused by cached propery action_space
Jun 7, 2018
27ec748
Add miscellaneous changes to improve the code
Jun 7, 2018
f18780e
Rename mujoco_model_gen to mujoco_model_generator
Jun 7, 2018
62331f2
Delete trpo_swimmer in dynamics_randomization
Jun 7, 2018
f154042
Initialize variations and generation of XML string in Variations
Jun 7, 2018
a1b9f2a
Return to single thread, add default values and fix imports
Jun 8, 2018
ac0d449
Sort modules in the package, add newline at EOF and defaults
Jun 8, 2018
56d2b60
Fix PEP8 formatting in randomized_env.py
Jun 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rllab/envs/mujoco/dynamics_randomization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from rllab.envs.mujoco.dynamics_randomization.randomized_env import randomize
from rllab.envs.mujoco.dynamics_randomization.variation import Distribution
from rllab.envs.mujoco.dynamics_randomization.variation import Method
from rllab.envs.mujoco.dynamics_randomization.variation import Variations
87 changes: 87 additions & 0 deletions rllab/envs/mujoco/dynamics_randomization/randomized_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os.path as osp

from mujoco_py import MjSim
from mujoco_py import load_model_from_xml

from rllab.core import Serializable
from rllab.envs import Env
from rllab.envs.mujoco.mujoco_env import MODEL_DIR


class RandomizedEnv(Env, Serializable):
"""
This class is just a wrapper class for the MujocoEnv to perform
the training using Dynamics Randomization.
Only code in the methods reset and terminate has been added.
"""

def __init__(self, mujoco_env, variations):
"""
An instance of the class MujocoModelGenerator is created to
generate the Mujoco models with the randomization of the
requested dynamic parameters.
"""
Serializable.quick_init(self, locals())
self._wrapped_env = mujoco_env
self._variations = variations
self._file_path = osp.join(MODEL_DIR, mujoco_env.FILE)
self._variations.initialize_variations(self._file_path)

def reset(self):
"""
The new model with randomized parameters is requested and the
corresponding parameters in the MuJoCo environment class are
set.
"""
self._wrapped_env.model = load_model_from_xml(
self._variations.get_randomized_xml_model())
if hasattr(self._wrapped_env, 'action_space'):
del self._wrapped_env.__dict__['action_space']
self._wrapped_env.sim = MjSim(self._wrapped_env.model)
self._wrapped_env.data = self._wrapped_env.sim.data
self._wrapped_env.init_qpos = self._wrapped_env.sim.data.qpos
self._wrapped_env.init_qvel = self._wrapped_env.sim.data.qvel
self._wrapped_env.init_qacc = self._wrapped_env.sim.data.qacc
self._wrapped_env.init_ctrl = self._wrapped_env.sim.data.ctrl
return self._wrapped_env.reset()

def step(self, action):
return self._wrapped_env.step(action)

def render(self, *args, **kwargs):
return self._wrapped_env.render(*args, **kwargs)

def log_diagnostics(self, paths, *args, **kwargs):
self._wrapped_env.log_diagnostics(paths, *args, **kwargs)

def get_param_values(self):
return self._wrapped_env.get_param_values()

def set_param_values(self, params):
self._wrapped_env.set_param_values(params)

def terminate(self):
"""
Besides regular termination, the MuJoCo model generator is
stopped.
"""
self._wrapped_env.terminate()

@property
def wrapped_env(self):
return self._wrapped_env

@property
def action_space(self):
return self._wrapped_env.action_space

@property
def observation_space(self):
return self._wrapped_env.observation_space

@property
def horizon(self):
return self._wrapped_env.horizon


randomize = RandomizedEnv
21 changes: 21 additions & 0 deletions rllab/envs/mujoco/dynamics_randomization/test_dynamics_rand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rllab.envs.mujoco import SwimmerEnv
from rllab.envs.mujoco.dynamics_randomization import Distribution
from rllab.envs.mujoco.dynamics_randomization import Method
from rllab.envs.mujoco.dynamics_randomization import randomize
from rllab.envs.mujoco.dynamics_randomization import Variations

variations = Variations()
variations.randomize() \
.at_xpath(".//geom[@name='torso']") \
.attribute("density") \
.with_method(Method.COEFFICIENT) \
.sampled_from(Distribution.UNIFORM) \
.with_range(0.5, 1.5) \
.add()

env = randomize(SwimmerEnv(), variations)

for i in range(1000):
env.reset()
for j in range(1000):
env.step(env.action_space.sample())
Loading