def _setup_world(self): """ Helper method for handling setup of the MuJoCo world. Args: filename: Path to XML file containing the world information. """ if "varying_mass" in self._hyperparams: self.create_xml() pdb.set_trace() self._model = mujoco_py.MjModel(self._hyperparams['filename']) self.model_nomarkers = mujoco_py.MjModel( self._hyperparams['filename_nomarkers']) gofast = True self._small_viewer = mujoco_py.MjViewer( visible=True, init_width=self._hyperparams['image_width'], init_height=self._hyperparams['image_height'], go_fast=gofast) self._small_viewer.start() self._small_viewer.cam.camid = 0 if self._hyperparams['additional_viewer']: self._large_viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480, go_fast=gofast) self._large_viewer.start() self._large_viewer.cam.camid = 0
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._model= mujoco_py.MjModel(filename) self.model_nomarkers = mujoco_py.MjModel(self._hyperparams['filename_nomarkers']) # changes here: self._joint_idx = range(self._hyperparams['joint_angles']) self._vel_idx = range( self._hyperparams['joint_angles'], self._hyperparams['joint_velocities'] + self._hyperparams['joint_angles']) gofast = True self._small_viewer = mujoco_py.MjViewer(visible=True, init_width=self._hyperparams['image_width'], init_height=self._hyperparams['image_height'], go_fast=gofast) self._small_viewer.start() self._small_viewer.cam.camid = 0 if self._hyperparams['additional_viewer']: self._large_viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480, go_fast=gofast) self._large_viewer.start() self._large_viewer.cam.camid = 0
def __init__(self, model_path, frame_skip, model_parameters): # assert "lengths" in model_parameters # assert len(model_parameters["lengths"]) == 7 assert "torques" in model_parameters assert len(model_parameters["torques"]) == 7 # assert "fov" in model_parameters # assert "colors" in model_parameters if model_path.startswith("/"): fullpath = model_path else: fullpath = os.path.join(os.path.dirname(__file__), "assets", model_path) if not os.path.exists(fullpath): raise IOError("File %s does not exist" % fullpath) modified_xml_path = self._modifyXml(fullpath, model_parameters) # print('new xml path: {}'.format(modified_xml_path)) self.model = mujoco_py.MjModel(modified_xml_path) self.frame_skip = frame_skip self.data = self.model.data self.viewer = None self.init_qpos = self.model.data.qpos.ravel().copy() self.init_qvel = self.model.data.qvel.ravel().copy()
def __init__(self, model_path, frame_skip): if model_path.startswith("/"): fullpath = model_path else: fullpath = os.path.join(os.path.dirname(__file__), "assets", model_path) if not path.exists(fullpath): raise IOError("File %s does not exist" % fullpath) self.frame_skip = frame_skip self.model = mujoco_py.MjModel(fullpath) self.data = self.model.data self.viewer = None self.metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': int(np.round(1.0 / self.dt)) } self.init_qpos = self.model.data.qpos.ravel().copy() self.init_qvel = self.model.data.qvel.ravel().copy() observation, _reward, done, _info = self.step(np.zeros(self.model.nu)) assert not done self.obs_dim = observation.size bounds = self.model.actuator_ctrlrange.copy() low = bounds[:, 0] high = bounds[:, 1] self.action_space = spaces.Box(low=low, high=high) high = np.inf * np.ones(self.obs_dim) low = -high self.observation_space = spaces.Box(low, high) self.seed()
def __init__( self, model_path, frame_skip=1, model_path_is_local=True, automatically_set_obs_and_action_space=False, ): if model_path_is_local: model_path = get_asset_xml(model_path) if automatically_set_obs_and_action_space: mujoco_env.MujocoEnv.__init__(self, model_path, frame_skip) else: """ Code below is copy/pasted from MujocoEnv's __init__ function. """ if model_path.startswith("/"): fullpath = model_path else: fullpath = os.path.join(os.path.dirname(__file__), "assets", model_path) if not path.exists(fullpath): raise IOError("File %s does not exist" % fullpath) self.frame_skip = frame_skip self.model = mujoco_py.MjModel(fullpath) self.data = self.model.data self.viewer = None self.metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': int(np.round(1.0 / self.dt)) } self.init_qpos = self.model.data.qpos.ravel().copy() self.init_qvel = self.model.data.qvel.ravel().copy() self._seed()
def __init__(self, write_assets=True): if write_assets: # generate and save pyramids for num_sides in range(3, 7): pyramid_mesh = create_pyramid_mesh(num_sides=num_sides) pyramid_mesh.save( os.path.join( gym.__path__[0], 'envs/mujoco/assets/pyramid%d.stl' % num_sides)) # generate and save prisms for num_sides in range(3, 7): prism_mesh = create_prism_mesh(num_sides=num_sides) prism_mesh.save( os.path.join(gym.__path__[0], 'envs/mujoco/assets/prism%d.stl' % num_sides)) # generate and save pusher model = create_shape_pusher() model.save( os.path.join(gym.__path__[0], 'envs/mujoco/assets/shape_pusher.xml')) # MujocoEnv's superclass init self.frame_skip = 5 self.model = mujoco_py.MjModel( os.path.join(gym.__path__[0], 'envs/mujoco/assets/shape_pusher.xml')) self.data = self.model.data self.width = 256 self.height = 256 self.viewer = mujoco_py.MjViewer(init_width=self.width, init_height=self.height) self.viewer.start() self.viewer.set_model(self.model) self.viewer_setup() self.metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': int(np.round(1.0 / self.dt)) } self.action_space = spaces.Box(-1, 1, shape=(2, )) joints_range = self.model.jnt_range.copy() self.observation_space = { 'image': spaces.Box(0, 255, shape=(self.width, self.height)), 'manip_xy': spaces.Box(joints_range[:2, 0], joints_range[:2, 1]), 'obj_pose': spaces.Box(-np.inf, np.inf, shape=(7, )) } self._seed() self.num_objs = len([ body_name for body_name in self.model.body_names if body_name.startswith(six.b('obj')) ]) self.orig_body_mass = self.model.body_mass.copy() self.obj_idx = None
def _setup_world(self): """ Helper method for handling setup of the MuJoCo world. """ self._model = mjcpy.MjModel(self.simParams['xmlfile']) #self._geoms = #self._joint_idx = list(range(self._model['nq'])) #self._vel_idx = [i + self._model['nq'] for i in self._joint_idx] self._setup_renderer()
def test_smoke(): model = mujoco_py.MjModel('tests/models/ant.xml') # Try stepping model.step() model._compute_subtree() # Try getting some data out of the model n = model.body_names idx = n.index(six.b('torso')) com = model.data.com_subtree[idx]
def generate_images(): model = mujoco_py.MjModel(filename) viewer = mujoco_py.MjViewer(visible=True, init_width=64, init_height=64) viewer.start() viewer.cam.camid = 0 viewer.set_model(model) viewer.cam.camid = 0 nseed = 3 for i_conf in range(20): for seed in range(nseed): i_traj = seed + i_conf * nseed dict = cPickle.load( open('goalimage/goalimg{0}_conf{1}.pkl'.format(i_traj, i_conf), "rb")) block_pose = dict['goal_object_pose'] d = 4 num = 64 / d # frames = np.zeros((num, num, 64, 64, 3)) frames, ballpos = [], [] for r in range(num): for c in range(num): mjc_pos = imagespace_to_mujoco(np.array([r * d, c * d])) print 'run', i_traj, ' r', r * d, 'c', c * d single_frame = mujoco_get_frame(mjc_pos, block_pose, viewer, model) frames.append(single_frame) ballpos.append(np.concatenate([mjc_pos, np.zeros(2)])) # single_frame = single_frame*255. # Image.fromarray(single_frame.astype(np.uint8)).show() print 'ballpos', mjc_pos # print np.sum(model.data.cfrc_ext) # if np.sum(model.data.cfrc_ext) == -0.983262569927: # single_frame = single_frame*255. # Image.fromarray(single_frame.astype(np.uint8)).show() out_dict = {} frames = np.stack(frames) ballpos = np.stack(ballpos) out_dict['goal_image'] = frames out_dict['goal_ballpos'] = ballpos cPickle.dump( out_dict, open( 'goalimage_var_ballpos/goalimg{0}_conf{1}.pkl'.format( i_traj, i_conf), 'wb'))
def __init__(self, model_path, frame_skip): if model_path.startswith("/"): fullpath = model_path else: fullpath = os.path.join(os.path.dirname(__file__), "assets", model_path) if not os.path.exists(fullpath): raise IOError("File %s does not exist"%fullpath) self.frame_skip= frame_skip self.model = mujoco_py.MjModel(fullpath) self.data = self.model.data self.viewer = None self.metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second' : int(np.round(1.0 / self.dt)) }
def __init__(self): parser = argparse.ArgumentParser(description='simulator') parser.add_argument('conf', type=str, help='config file') args = parser.parse_args() config = args.conf confmod = imp.load_source('conf', 'configs/' + config + '.py') self.conf = confmod.config self.model = mujoco_py.MjModel(self.conf['modelfile']) gofast = False self.viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480, go_fast=gofast) self.viewer.start() self.viewer.set_model(self.model)
def generate_images(): model = mujoco_py.MjModel(filename) viewer = mujoco_py.MjViewer(visible=True, init_width=64, init_height=64) viewer.start() viewer.cam.camid = 0 viewer.set_model(model) viewer.cam.camid = 0 for i in range(5): block_pos = np.random.uniform(-.35, .35, 2) alpha = np.random.uniform(0, np.pi * 2) ori = np.array([np.cos(alpha / 2), 0, 0, np.sin(alpha / 2)]) pose = np.concatenate((block_pos, np.array([0]), ori), axis=0) #format: x, y, z, quat print 'placing block at: ', block_pos, 'corresponds to pixel', mujoco_to_imagespace(block_pos, noflip=True) desig_pos = block_pos d = 1 num = 64/d frames = np.zeros((num, num, 64, 64, 3)) for r in range(num): for c in range(num): mjc_pos = imagespace_to_mujoco(np.array([r*d, c*d])) print 'run', i, ' r',r*d, 'c',c*d single_frame = mujoco_get_frame(mjc_pos, pose, viewer, model) frames[r,c] = single_frame if (r * 64 + c) % 1 == 0: single_frame = single_frame*255. Image.fromarray(single_frame.astype(np.uint8)).show() import pdb; pdb.set_trace()
import numpy as np import mujoco_py from mujoco_py.mjlib import mjlib from mujoco_py.mjtypes import * from PIL import Image import cPickle import os cwd = os.getcwd() BASE_DIR = '/'.join(str.split(cwd, '/')[:-2]) filename = BASE_DIR + '/mjc_models/pushing2d_controller_nomarkers.xml' model = mujoco_py.MjModel(filename) viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480) viewer.start() viewer.set_model(model) viewer.cam.camid = 0 print viewer.cam.camid T = 1 for t in range(T): viewer.loop_once() mj_U = np.zeros(2) model.data.ctrl = mj_U model.step()
def __init__(self, ag_params, policyparams, predictor=None): Policy.__init__(self) self.agentparams = ag_params self.policyparams = policyparams self.t = None if self.policyparams['low_level_ctrl']: self.low_level_ctrl = policyparams['low_level_ctrl']['type']( None, policyparams['low_level_ctrl']) self.model = mujoco_py.MjModel(self.agentparams['filename']) if 'verbose' in self.policyparams: self.verbose = True else: self.verbose = False if 'iterations' in self.policyparams: self.niter = self.policyparams['iterations'] else: self.niter = 10 # number of iterations if 'usenet' in self.policyparams: self.use_net = True else: self.use_net = False self.action_list = [] if self.use_net: hyperparams = imp.load_source('hyperparams', self.policyparams['netconf']) self.netconf = hyperparams.configuration self.nactions = self.policyparams['nactions'] self.repeat = self.policyparams['repeat'] if self.use_net: self.M = self.netconf['batch_size'] assert self.nactions * self.repeat == self.netconf[ 'sequence_length'] self.predictor = predictor self.K = 10 # only consider K best samples for refitting else: self.M = self.policyparams['num_samples'] self.K = 10 # only consider K best samples for refitting self.gtruth_images = [ np.zeros((self.M, 64, 64, 3)) for _ in range(self.nactions * self.repeat) ] # the full horizon is actions*repeat if 'action_cost_factor' in self.policyparams: self.action_cost_mult = self.policyparams['action_cost_factor'] else: self.action_cost_mult = 0.00005 self.adim = 2 # action dimension self.initial_std = policyparams['initial_std'] if 'exp_factor' in policyparams: self.exp_factor = policyparams['exp_factor'] gofast = True self.viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480, go_fast=gofast) self.viewer.start() self.viewer.set_model(self.model) self.viewer.cam.camid = 0 self.small_viewer = mujoco_py.MjViewer(visible=True, init_width=64, init_height=64, go_fast=gofast) self.small_viewer.start() self.small_viewer.set_model(self.model) self.small_viewer.cam.camid = 0 self.init_model = [] #history of designated pixels self.desig_pix = [] # predicted positions self.pred_pos = np.zeros( (self.M, self.niter, self.repeat * self.nactions, 2)) self.rec_target_pos = np.zeros( (self.M, self.niter, self.repeat * self.nactions, 2)) self.bestindices_of_iter = np.zeros((self.niter, self.K)) self.indices = [] self.target = np.zeros(2) self.rec_input_distrib = [] # record the input distributions self.corr_gen_images = [] self.corrector = None self.mean = None self.sigma = None
def __init__(self, ag_params, policyparams, predictor=None): Policy.__init__(self) self.agentparams = copy.deepcopy(AGENT_MUJOCO) self.agentparams.update(ag_params) self.policyparams = policyparams self.t = None if self.policyparams['low_level_ctrl']: self.low_level_ctrl = policyparams['low_level_ctrl']['type']( None, policyparams['low_level_ctrl']) self.model = mujoco_py.MjModel(self.agentparams['filename']) if 'verbose' in self.policyparams: self.verbose = True else: self.verbose = False if 'use_first_plan' in self.policyparams: self.use_first_plan = self.policyparams['use_first_plan'] else: self.use_first_plan = True if 'iterations' in self.policyparams: self.niter = self.policyparams['iterations'] else: self.niter = 10 # number of iterations self.use_net = self.policyparams['usenet'] self.action_list = [] if self.use_net: hyperparams = imp.load_source('hyperparams', self.policyparams['netconf']) self.netconf = hyperparams.configuration self.naction_steps = self.policyparams['nactions'] self.repeat = self.policyparams['repeat'] self.M = self.netconf['batch_size'] assert self.naction_steps * self.repeat == self.netconf[ 'sequence_length'] self.predictor = predictor self.K = 10 # only consider K best samples for refitting self.gtruth_images = [ np.zeros((self.M, 64, 64, 3)) for _ in range(self.naction_steps * self.repeat) ] self.gtruth_states = np.zeros( (self.naction_steps * self.repeat, self.M, 4)) # the full horizon is actions*repeat # self.action_cost_mult = 0.00005 self.action_cost_mult = 0 self.adim = 4 # action dimensions: deltax, delty, close_nstep, hold_nstep self.initial_std = policyparams['initial_std'] gofast = True self.viewer = mujoco_py.MjViewer(visible=True, init_width=480, init_height=480, go_fast=gofast) self.viewer.start() self.viewer.set_model(self.model) self.viewer.cam.camid = 0 self.small_viewer = mujoco_py.MjViewer(visible=True, init_width=64, init_height=64, go_fast=gofast) self.small_viewer.start() self.small_viewer.set_model(self.model) self.small_viewer.cam.camid = 0 self.init_model = [] # predicted positions self.pred_pos = np.zeros( (self.M, self.niter, self.repeat * self.naction_steps, 2)) self.rec_target_pos = np.zeros( (self.M, self.niter, self.repeat * self.naction_steps, 2)) self.bestindices_of_iter = np.zeros((self.niter, self.K)) self.indices = [] self.target = np.zeros(2) self.mean = None self.sigma = None self.goal_image = None
import mujoco_py import numpy as np #from scipy.misc import imsave import time import os from os import path as osp #DATA_DIR = osp.join(os.getenv('HOME'), 'code', 'gps', 'mjc_models', 'reacher.xml') DATA_DIR = osp.join('/work4/pulkitag-code/pkgs', 'gps', 'mjc_models', 'particle2d.xml') fullpath = './example.xml' print (DATA_DIR) print (osp.exists(DATA_DIR)) model = mujoco_py.MjModel(DATA_DIR) width = 1000 height = 1000 viewer = mujoco_py.MjViewer(visible=True, init_width=width, init_height=height) viewer.start() viewer.set_model(model) steps = 100 skip = 10 start = time.time() for i in xrange(steps): viewer.render() viewer.loop_once() data, width, height = viewer.get_image() img = np.fromstring(data, dtype='uint8').reshape(height, width, 3)[::-1,:,:] #imsave('imgs/out_' + str(i) + '.png', img) #model.data.ctrl = np.random.randn(1,2) model.data.ctrl = np.array([1., 1.])
x, y, z = viewer.cam.lookat d = viewer.cam.distance e = viewer.cam.elevation a = viewer.cam.azimuth print('x:%d,y:%d,z:%d,d:%d,e:%d,a:%d\n' % (x, y, z, d, e, a)) if __name__ == "__main__": fullpath = 'mjc/baxter/baxter.xml' if len(argv) > 1: [fullpath] = argv[1:] print('file:' + fullpath) model = mujoco_py.MjModel(fullpath) viewer = mujoco_py.MjViewer(visible=True) viewer.start() viewer.set_model(model) cam_pos = np.array([1.0, 0.0, 0.7, 0.5, -45, 180]) # cam_pos = np.array([0.1, 0.0, 0.7, 0.01, -45., 0.]) set_cam_position(viewer, cam_pos) ctrl = model.data.ctrl.copy() # ctrl[-1] = 10.0 model.data.mocap_pos = model.data.site_xpos[3] #np.array([[0.6, 0., 0.]]) model.data.mocap_quat = quaternion_from_matrix( model.data.site_xmat[3].reshape(3, 3))