def _setup_world(self, filename): """ Helper method for handling setup of the MuJoCo world. Args: filename: Path to XML file containing the world information. """ self._world = [] self._model = [] # Initialize Mujoco worlds. If there's only one xml file, create a single world object, # otherwise create a different world for each condition. if not isinstance(filename, list): self._world = mjcpy.MJCWorld(filename) self._model = self._world.get_model() self._world = [ self._world for _ in range(self._hyperparams['conditions']) ] self._model = [ copy.deepcopy(self._model) for _ in range(self._hyperparams['conditions']) ] else: for i in range(self._hyperparams['conditions']): self._world.append( mjcpy.MJCWorld(self._hyperparams['filename'][i])) self._model.append(self._world[i].get_model()) for i in range(self._hyperparams['conditions']): for j in range(len(self._hyperparams['pos_body_idx'][i])): idx = self._hyperparams['pos_body_idx'][i][j] self._model[i]['body_pos'][idx, :] += \ self._hyperparams['pos_body_offset'][i] self._world[i].set_model(self._model[i]) x0 = self._hyperparams['x0'][i] idx = len(x0) // 2 data = {'qpos': x0[:idx], 'qvel': x0[idx:]} self._world[i].set_data(data) self._world[i].kinematics() self._joint_idx = list(range(self._model[0]['nq'])) self._vel_idx = [i + self._model[0]['nq'] for i in self._joint_idx] # Initialize x0. self.x0 = [] for i in range(self._hyperparams['conditions']): if END_EFFECTOR_POINTS in self.x_data_types: eepts = self._world[i].get_data()['site_xpos'].flatten() self.x0.append( np.concatenate([ self._hyperparams['x0'][i], eepts, np.zeros_like(eepts) ])) else: self.x0.append(self._hyperparams['x0'][i]) cam_pos = self._hyperparams['camera_pos'] for i in range(self._hyperparams['conditions']): self._world[i].init_cam(cam_pos[0], cam_pos[1], cam_pos[2], cam_pos[3], cam_pos[4], cam_pos[5])
def _setup_world(self, filename): """ Helper method for handling setup of the MuJoCo world. Args: filename: Path to XML file containing the world information. """ self._world = [] self._model = [] # Initialize Mujoco worlds. If there's only one xml file, create a single world object, # otherwise create a different world for each condition. if not isinstance(filename, list): world = mjcpy.MJCWorld(filename) self._world = [world for _ in range(self._hyperparams['conditions'])] self._model = [self._world[i].get_model().copy() for i in range(self._hyperparams['conditions'])] else: for i in range(self._hyperparams['conditions']): self._world.append(mjcpy.MJCWorld(self._hyperparams['filename'][i])) self._model.append(self._world[i].get_model()) for i in range(self._hyperparams['conditions']): for j in range(len(self._hyperparams['pos_body_idx'][i])): idx = self._hyperparams['pos_body_idx'][i][j] self._model[i]['body_pos'][idx, :] += \ self._hyperparams['pos_body_offset'][i][j] self._joint_idx = list(range(self._model[0]['nq'])) self._vel_idx = [i + self._model[0]['nq'] for i in self._joint_idx] # Initialize x0. self.x0 = [] for i in range(self._hyperparams['conditions']): if END_EFFECTOR_POINTS in self.x_data_types: # TODO: this assumes END_EFFECTOR_VELOCITIES is also in datapoints right? self._init(i) eepts = self._world[i].get_data()['site_xpos'].flatten() self.x0.append( np.concatenate([self._hyperparams['x0'][i], eepts, np.zeros_like(eepts)]) ) elif END_EFFECTOR_POINTS_NO_TARGET in self.x_data_types: self._init(i) eepts = self._world[i].get_data()['site_xpos'].flatten() eepts_notgt = np.delete(eepts, self._hyperparams['target_idx']) self.x0.append( np.concatenate([self._hyperparams['x0'][i], eepts_notgt, np.zeros_like(eepts_notgt)]) ) else: self.x0.append(self._hyperparams['x0'][i]) if IMAGE_FEAT in self.x_data_types: self.x0[i] = np.concatenate([self.x0[i], np.zeros((self._hyperparams['sensor_dims'][IMAGE_FEAT],))]) cam_pos = self._hyperparams['camera_pos'] for i in range(self._hyperparams['conditions']): self._world[i].init_viewer(AGENT_MUJOCO['image_width'], AGENT_MUJOCO['image_height'], cam_pos[0], cam_pos[1], cam_pos[2], cam_pos[3], cam_pos[4], cam_pos[5])
def _setup_world(self, filename): """ Helper method for handling setup of the MuJoCo world. Args: filename: Path to XML file containing the world information. """ self._world = mjcpy.MJCWorld(filename) self._model = self._world.get_model() self._model = [ copy.deepcopy(self._model) for _ in range(self._hyperparams['conditions']) ] for i in range(self._hyperparams['conditions']): for j in range(len(self._hyperparams['pos_body_idx'][i])): idx = self._hyperparams['pos_body_idx'][i][j] self._model[i]['body_pos'][idx, :] += \ self._hyperparams['pos_body_offset'][i] self._world.set_model(self._model[i]) x0 = self._hyperparams['x0'][i] idx = len(x0) // 2 data = {'qpos': x0[:idx], 'qvel': x0[idx:]} self._world.set_data(data) self._world.kinematics() # Initialize x0. self._data = self._world.get_data() eepts = self._data['site_xpos'].flatten() self._joint_idx = list(range(self._model[0]['nq'])) self._vel_idx = [i + self._model[0]['nq'] for i in self._joint_idx] self.x0 = [] for x0 in self._hyperparams['x0']: if END_EFFECTOR_POINTS in self.x_data_types: self.x0.append( np.concatenate([x0, eepts, np.zeros_like(eepts)])) else: self.x0.append(x0)