Skip to content

Commit

Permalink
Fix bug in MujocoModelGenerator
Browse files Browse the repository at this point in the history
Before this commit, when there is an error raised when loading the xml
object, only the worker_thread terminates. This commit fixes this bug by
terminating all the processes.

Fix some typo in the last commit.
  • Loading branch information
CatherineSue authored and Angel Gonzalez committed Jun 4, 2018
1 parent 3192d1d commit 83c2d48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
23 changes: 14 additions & 9 deletions rllab/dynamics_randomization/mujoco_model_gen.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from lxml import etree
from mujoco_py import load_model_from_xml
from rllab.dynamics_randomization import VariationMethod
from rllab.dynamics_randomization import VariationDistribution
import atexit
import sys
from threading import Event
from threading import RLock
from threading import Thread

import atexit
from lxml import etree
from mujoco_py import load_model_from_xml
import numpy as np

from rllab.dynamics_randomization import VariationDistribution
from rllab.dynamics_randomization import VariationMethod


class MujocoModelGenerator:
"""
Expand All @@ -33,7 +34,7 @@ def __init__(self, file_path, variations):
self._file_path = file_path
# Worker Thread
self._worker_thread = Thread(
target=self._generator_routine, daemon=True)
target=self._generator_routine, daemon=True, name="Worker-Thread")
# Reference to the generated model
self._mujoco_model = None
# Communicates the calling thread with the worker thread by awaking
Expand All @@ -58,6 +59,10 @@ def get_model(self):
A MuJoCo model with randomized dynamic parameters specified by the
user in this class.
"""
if not self._worker_thread.is_alive():
# If worker thread is dead because of an error, raise an error in main thread
raise ChildProcessError("Error raised in Worker-Thread")

if not self._model_ready.is_set():
# If the model is not ready yet, wait for it to be finished.
self._model_ready.wait()
Expand Down Expand Up @@ -101,7 +106,7 @@ def _generator_routine(self):
raise AttributeError("Range shape != default value shape")

# Generate model with randomized dynamic parameters
while (not self._stop_event.is_set()):
while not self._stop_event.is_set():
for v in self._variations.get_list():
e = v.elem
if v.distribution == VariationDistribution.GAUSSIAN:
Expand All @@ -111,7 +116,7 @@ def _generator_routine(self):
c = np.random.uniform(
low=v.var_range[0], high=v.var_range[1])
else:
raise NotImplementedError("Unkown distribution")
raise NotImplementedError("Unknown distribution")
if v.method == VariationMethod.COEFFICIENT:
e.attrib[v.attrib] = str(c * v.default)
elif v.method == VariationMethod.ABSOLUTE:
Expand Down
10 changes: 7 additions & 3 deletions rllab/dynamics_randomization/randomized_env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os.path as osp

from mujoco_py import MjSim

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

import os.path as osp


class RandomizedEnv(Env, Serializable):
"""
Expand Down Expand Up @@ -33,7 +34,10 @@ def reset(self):
corresponding parameters in the MuJoCo environment class are
set.
"""
self._wrapped_env.model = self._model_generator.get_model()
try:
self._wrapped_env.model = self._model_generator.get_model()
except AttributeError as e:
raise e
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
Expand Down
8 changes: 4 additions & 4 deletions rllab/dynamics_randomization/variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class VariationMethod(Enum):
"""
The random coefficient is applied according to these methods.
"""
""" The randomization is the product of the coeffcient and the dynamic parameter """
""" The randomization is the product of the coefficient and the dynamic parameter """
COEFFICIENT = 1
""" The randomization is equal to the coefficient """
ABSOLUTE = 2


class VariationDistribution(Enum):
"""
The different ways to produce the random cofficient.
The different ways to produce the random coefficient.
"""
""" Guassian distribution """
GAUSSIAN = 1
Expand Down Expand Up @@ -151,9 +151,9 @@ def with_method(self, method):
Parameters
----------
method : VariationMethod
if equal to "absolute", it sets the dyanmic parameter
if equal to "absolute", it sets the dynamic parameter
equal to the random coefficient obtained from the distribution, or
if equal to "coefficient", it multiplies the default value provieded
if equal to "coefficient", it multiplies the default value provided
in the XML file by the random coefficient.
"""
if self._list:
Expand Down

0 comments on commit 83c2d48

Please sign in to comment.