def __init__(self, xml_filepath, max_steps, history_len, image_dimensions, neg_reward, steps_per_action, frames_per_step=20): fullpath = os.path.join(os.path.dirname(__file__), xml_filepath) if not fullpath.startswith("/"): fullpath = os.path.join(os.path.dirname(__file__), "assets", fullpath) self.sim = mujoco.Sim(fullpath) self.init_qpos = self.sim.qpos.ravel().copy() self.init_qvel = self.sim.qvel.ravel().copy() self._frames_per_step = frames_per_step super().__init__(max_steps, history_len, image_dimensions, neg_reward, steps_per_action)
#! /usr/bin/env python import mujoco import numpy as np sim = mujoco.Sim('xml/humanoid.xml') while True: sim.step() sim.ctrl[:] = np.random.random((sim.ctrl.shape)) * sim.actuator_ctrlrange print(sim.render_offscreen(4, 5, 'rgb'))
def __init__(self, xml_filepath: Path, steps_per_action: int, geofence: float, goal_space: spaces.Box, block_space: spaces.Box, min_lift_height: float = None, randomize_pose: bool = False, obs_type: str = None, image_dimensions: Tuple[int] = None, render: bool = False, record_path: Path = None, record_freq: int = None, record: bool = False, record_separate_episodes: bool = False, render_freq: int = None, no_random_reset: bool = False): if not xml_filepath.is_absolute(): xml_filepath = Path(Path(__file__).parent, xml_filepath) self.no_random_reset = no_random_reset self.geofence = geofence self._obs_type = obs_type self._block_name = 'block' left_finger_name = 'hand_l_distal_link' self._finger_names = [ left_finger_name, left_finger_name.replace('_l_', '_r_') ] self._episode = 0 self._time_steps = 0 # required for OpenAI code self.metadata = {'render.modes': 'rgb_array'} self.reward_range = -np.inf, np.inf self.spec = None self.render_freq = 20 if (render and render_freq is None) else render_freq self.steps_per_action = steps_per_action # record stuff self._video_recorder = None self._record_separate_episodes = record_separate_episodes self._record = any( (record_separate_episodes, record_path, record_freq, record)) if self._record: self._record_path = record_path or Path('/tmp/training-video') image_dimensions = image_dimensions or (1000, 1000) self._record_freq = record_freq or 20 if not record_separate_episodes: self._video_recorder = self.reset_recorder(self._record_path) else: image_dimensions = image_dimensions or [] self._image_dimensions = image_dimensions self.sim = mujoco.Sim(str(xml_filepath), *image_dimensions, n_substeps=1) # initial values self.initial_qpos = self.sim.qpos.ravel().copy() self.initial_qvel = self.sim.qvel.ravel().copy() self.initial_block_pos = np.copy(self.block_pos()) # goal space self._min_lift_height = min_lift_height self.goal_space = goal_space epsilon = .0001 too_close = self.goal_space.high - self.goal_space.low < 2 * epsilon self.goal_space.high[too_close] += epsilon self.goal_space.low[too_close] -= epsilon self.goal = None # block space self._block_space = block_space try: for i in itertools.count(): self.sim.name2id(ObjType.BODY, f'block{i}') self.n_blocks = i + 1 except MujocoError: pass def using_joint(name): return self.sim.contains(ObjType.JOINT, name) self._base_joints = list(filter(using_joint, ['slide_x', 'slide_y'])) if obs_type == 'openai': raw_obs_space = spaces.Box( low=-np.inf, high=np.inf, shape=(25, ), dtype=np.float32, ) else: raw_obs_space = spaces.Box( low=-np.inf, high=np.inf, shape=(self.sim.nq + len(self._base_joints), ), dtype=np.float32, ) self.observation_space = spaces.Tuple( Observation(observation=raw_obs_space, goal=self.goal_space)) block_joint = self.sim.get_jnt_qposadr('block0joint') self._block_qposadrs = block_joint + np.array([0, 1, 3, 6]) offset = np.array([ 0, # x 1, # y 3, # quat0 6, # quat3 ]) self._block_qposadrs = [ self.sim.get_jnt_qposadr(f'block{i}joint') + offset for i in range(self.n_blocks) ] # joint space all_joints = [ 'slide_x', 'slide_y', 'arm_lift_joint', 'arm_flex_joint', 'wrist_roll_joint', 'hand_l_proximal_joint' ] self._joints = list(filter(using_joint, all_joints)) jnt_range_idx = [ self.sim.name2id(ObjType.JOINT, j) for j in self._joints ] self._joint_space = spaces.Box(*map( np.array, zip(*self.sim.jnt_range[jnt_range_idx])), dtype=np.float32) self._joint_qposadrs = [ self.sim.get_jnt_qposadr(j) for j in self._joints ] self.randomize_pose = randomize_pose # action space self.action_space = spaces.Box(low=self.sim.actuator_ctrlrange[:-1, 0], high=self.sim.actuator_ctrlrange[:-1, 1], dtype=np.float32)
#! /usr/bin/env python import numpy as np import argparse import mujoco parser = argparse.ArgumentParser() parser.add_argument('--height', type=int) parser.add_argument('--width', type=int) args = parser.parse_args() sim = mujoco.Sim('xml/humanoid.xml', n_substeps=1, height=args.height, width=args.width) try: while True: sim.step() sim.ctrl[:] = -np.ones((sim.ctrl.shape)) sim.render() except KeyboardInterrupt: pass
#! /usr/bin/env python import argparse import numpy as np from PIL import Image import mujoco parser = argparse.ArgumentParser() parser.add_argument('--height', type=int) parser.add_argument('--width', type=int) # parser.add_argument('--video-path', type=str, default='build/video.mp4') args = parser.parse_args() print(args.height, args.width) path = 'xml/humanoid.xml' sim = mujoco.Sim(path, height=args.height, width=args.width) try: while True: sim.step() sim.ctrl[:] = -np.ones((sim.ctrl.shape)) sim.render_offscreen() except KeyboardInterrupt: pass