Ejemplo n.º 1
0
 def __init__(self, mujoco_env, variations):
     """
     Set variations with the node in the XML file at file_path.
     """
     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)
Ejemplo n.º 2
0
    def __init__(self, observation_space, action_space):
        """
        :type observation_space: Space
        :type action_space: Space
        """
        self._observation_space = observation_space
        self._action_space = action_space

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        super().__init__(self.model_path("cartpole.xml.mako"), *args, **kwargs)

        self.max_cart_pos = 3
        self.max_reward_cart_pos = 3
        self.cart = find_body(self.world, "cart")
        self.pole = find_body(self.world, "pole")

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 4
0
 def __init__(
         self,
         env,
         action_delay=3,
 ):
     assert action_delay > 0, "Should not use this env transformer"
     Serializable.quick_init(self, locals())
     super(DelayedActionEnv, self).__init__(env)
     self.action_delay = action_delay
     self._action_flat_dim = flat_dim(self.action_space)
     self._queued_actions = None
Ejemplo n.º 5
0
    def __init__(self, env=None, env_name=""):
        if env_name:
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        self.action_space = self._to_metaworlds_space(self.env.action_space)
        self.observation_space = self._to_metaworlds_space(
            self.env.observation_space)

        Parameterized.__init__(self)
        Serializable.quick_init(self, locals())
Ejemplo n.º 6
0
    def __init__(
            self,
            env,
            obs_noise=1e-1,
    ):
        super().__init__(env)

        self.obs_noise = obs_noise
        self._action_flat_dim = flat_dim(self.action_space)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 7
0
    def __init__(
        self,
        goal=np.array((1., 1.), dtype=np.float32),
        done_bonus=0.,
        never_done=False,
    ):
        self._goal = np.array(goal, dtype=np.float32)
        self._done_bonus = done_bonus
        self._never_done = never_done

        self._point = np.zeros_like(self._goal)

        Serializable.quick_init(self, locals())
Ejemplo n.º 8
0
    def __init__(self, mdp_cls, mdp_args):
        self.mdp_cls = mdp_cls
        self.mdp_args = dict(mdp_args)
        # Leaving this commented out so that tests can pass. It will be
        # removable (along with this class, possibly) as soon as we move out
        # the metaworlds.envs.box2d and metaworlds.envs.mujoco
        # See https://github.com/rlworkgroup/metaworlds/issues/359
        # self.mdp_args["template_args"] = dict(noise=True)
        mdp = self.gen_mdp()
        super().__init__(mdp)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 9
0
    def __init__(
        self,
        env,
        n_steps=4,
        axis=0,
    ):
        super().__init__(env)

        self.n_steps = n_steps
        self.axis = axis
        self.buffer = None

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 10
0
    def __init__(self, env, sensor_idx):
        """
        :param sensor_idx: list or ndarray of indices to be shown.
         Other indices will be occluded. Can be either list of integer
         indices or boolean mask.
        """
        self._set_sensor_mask(env, sensor_idx)
        super().__init__(env)
        self._dt = 1
        if isinstance(env, MujocoEnv):
            self._dt = env.sim.opt.timestep * env.frame_skip

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 11
0
    def __init__(self,
                 vel_deviation_cost_coeff=1e-2,
                 alive_bonus=0.2,
                 ctrl_cost_coeff=1e-3,
                 impact_cost_coeff=1e-5,
                 *args,
                 **kwargs):
        self.vel_deviation_cost_coeff = vel_deviation_cost_coeff
        self.alive_bonus = alive_bonus
        self.ctrl_cost_coeff = ctrl_cost_coeff
        self.impact_cost_coeff = impact_cost_coeff

        super().__init__(*args, **kwargs)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 12
0
    def __init__(self, desc='4x4'):
        if isinstance(desc, str):
            desc = MAPS[desc]
        desc = np.array(list(map(list, desc)))
        desc[desc == '.'] = 'F'
        desc[desc == 'o'] = 'H'
        desc[desc == 'x'] = 'W'
        self.desc = desc
        self.n_row, self.n_col = desc.shape
        (start_x, ), (start_y, ) = np.nonzero(desc == 'S')
        self.start_state = start_x * self.n_col + start_y
        self.state = None
        self.domain_fig = None

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 13
0
    def __init__(self,
                 difficulty=1.0,
                 texturedir='/tmp/mujoco_textures',
                 hfield_dir='/tmp/mujoco_terrains',
                 regen_terrain=True,
                 *args,
                 **kwargs):
        self.difficulty = max(difficulty, self.MIN_DIFFICULTY)
        self.texturedir = texturedir
        self.hfield_dir = hfield_dir

        model_cls = self.__class__.MODEL_CLASS
        if not model_cls:
            raise NotImplementedError("MODEL_CLASS unspecified!")

        template_file_name = 'hill_' + model_cls.__module__.split(
            '.')[-1] + '.xml.mako'

        template_options = dict(
            difficulty=self.difficulty,
            texturedir=self.texturedir,
            hfield_file=os.path.join(self.hfield_dir, self.HFIELD_FNAME))

        file_path = os.path.join(MODEL_DIR, template_file_name)
        lookup = mako.lookup.TemplateLookup(directories=[MODEL_DIR])
        with open(file_path) as template_file:
            template = mako.template.Template(
                template_file.read(), lookup=lookup)
        content = template.render(opts=template_options)

        tmp_f, file_path = tempfile.mkstemp(suffix=".xml", text=True)
        with open(file_path, 'w') as f:
            f.write(content)

        if self._iam_terrain_generator(regen_terrain):
            self._gen_terrain(regen_terrain)
            os.remove(self._get_lock_path())

        inner_env = model_cls(
            *args, file_path=file_path,
            **kwargs)  # file to the robot specifications
        super().__init__(inner_env)

        os.close(tmp_f)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 14
0
    def __init__(self,
                 random_start=True,
                 random_start_range=1.0,
                 *args,
                 **kwargs):
        self.random_start = random_start
        self.random_start_range = random_start_range
        super().__init__(self.model_path("car_parking.xml"), *args, **kwargs)
        self.goal = find_body(self.world, "goal")
        self.car = find_body(self.world, "car")
        self.wheels = [
            body for body in self.world.bodies if "wheel" in _get_name(body)
        ]
        self.front_wheels = [
            body for body in self.wheels if "front" in _get_name(body)
        ]
        self.max_deg = 30.
        self.goal_radius = 1.
        self.vel_thres = 1e-1
        self.start_radius = 5.

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 15
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 16
0
    def __init__(self, ctrl_cost_coeff=1e-2, *args, **kwargs):
        self.ctrl_cost_coeff = ctrl_cost_coeff
        super().__init__(*args, **kwargs)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())
Ejemplo n.º 17
0
 def __getstate__(self):
     d = Serializable.__getstate__(self)
     d["params"] = self.get_param_values()
     return d
Ejemplo n.º 18
0
 def __setstate__(self, d):
     Serializable.__setstate__(self, d)
     global load_params
     if load_params:
         self.set_param_values(d["params"])
Ejemplo n.º 19
0
    def __init__(
            self,
            n_bins=20,
            sensor_range=10.,
            sensor_span=math.pi,
            maze_id=0,
            length=1,
            maze_height=0.5,
            maze_size_scaling=2,
            # a coef of 0 gives no reward to the maze from the wrapped env.
            coef_inner_rew=0.,
            goal_rew=1.,  # reward obtained when reaching the goal
            *args,
            **kwargs):
        self._n_bins = n_bins
        self._sensor_range = sensor_range
        self._sensor_span = sensor_span
        self._maze_id = maze_id
        self.length = length
        self.coef_inner_rew = coef_inner_rew
        self.goal_rew = goal_rew

        model_cls = self.__class__.MODEL_CLASS
        if not model_cls:
            raise NotImplementedError("MODEL_CLASS unspecified!")
        xml_path = osp.join(MODEL_DIR, model_cls.FILE)
        tree = ET.parse(xml_path)
        worldbody = tree.find(".//worldbody")

        self.MAZE_HEIGHT = height = maze_height
        self.MAZE_SIZE_SCALING = size_scaling = maze_size_scaling
        self.MAZE_STRUCTURE = structure = construct_maze(maze_id=self._maze_id,
                                                         length=self.length)

        torso_x, torso_y = self._find_robot()
        self._init_torso_x = torso_x
        self._init_torso_y = torso_y

        for i in range(len(structure)):
            for j in range(len(structure[0])):
                if str(structure[i][j]) == '1':
                    # offset all coordinates so that robot starts at the origin
                    ET.SubElement(
                        worldbody,
                        "geom",
                        name="block_%d_%d" % (i, j),
                        pos="%f %f %f" %
                        (j * size_scaling - torso_x, i * size_scaling -
                         torso_y, height / 2 * size_scaling),
                        size="%f %f %f" %
                        (0.5 * size_scaling, 0.5 * size_scaling,
                         height / 2 * size_scaling),
                        type="box",
                        material="",
                        contype="1",
                        conaffinity="1",
                        rgba="0.4 0.4 0.4 1")

        torso = tree.find(".//body[@name='torso']")
        geoms = torso.findall(".//geom")
        for geom in geoms:
            if 'name' not in geom.attrib:
                raise Exception("Every geom of the torso must have a name "
                                "defined")

        if self.__class__.MAZE_MAKE_CONTACTS:
            contact = ET.SubElement(tree.find("."), "contact")
            for i in range(len(structure)):
                for j in range(len(structure[0])):
                    if str(structure[i][j]) == '1':
                        for geom in geoms:
                            ET.SubElement(contact,
                                          "pair",
                                          geom1=geom.attrib["name"],
                                          geom2="block_%d_%d" % (i, j))

        _, file_path = tempfile.mkstemp(suffix=".xml", text=True)
        tree.write(
            file_path
        )  # here we write a temporal file with the robot specifications.
        # Why not the original one??

        self._goal_range = self._find_goal_range()
        self._cached_segments = None

        inner_env = model_cls(*args, file_path=file_path,
                              **kwargs)  # file to the robot specifications
        super().__init__(inner_env)

        # Redefine observation space
        shp = self.get_current_obs().shape
        ub = BIG * np.ones(shp)
        self.observation_space = gym.spaces.Box(ub * -1, ub, dtype=np.float32)

        # Always call Serializable constructor last
        Serializable.quick_init(self, locals())