Example #1
0
    def __init__(self, animate=False, sim=None):
        if sim is not None:
            self.sim = sim
            self.model = self.sim.model
        else:
            self.modelpath = TestEnv.MODELPATH
            self.model = mujoco_py.load_model_from_path(self.modelpath)
            self.sim = mujoco_py.MjSim(self.model)

        self.model.opt.timestep = 0.02
        self.max_steps = 400

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = self.q_dim + self.qvel_dim
        self.act_dim = self.sim.data.actuator_length.shape[0]

        # Environent inner parameters
        self.viewer = None
        self.step_ctr = 0

        # Initial methods
        if animate:
            self.setupcam()

        self.reset()
Example #2
0
    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.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(self.model)
        self.data = self.sim.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.sim.data.qpos.ravel().copy()
        self.init_qvel = self.sim.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, num_substeps=75):
        if model_path.startswith("/"):
            fullpath = model_path
        else:
            fullpath = osp.join(osp.dirname(__file__), "assets", model_path)
        if not osp.exists(fullpath):
            raise IOError("File %s does not exist" % fullpath)
        else:
            print("Loading model %s" % osp.basename(model_path))

        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model, nsubsteps=num_substeps)
        self.data = self.sim.data
        self.viewer = None

        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.seed()

        # limits of motion with some safety limits
        self.target_range_min = np.array([0.3, 0.0, 0.08]) + 0.05
        self.target_range_max = np.array([0.8, 0.6, 0.25]) - 0.05

        self.initial_qpos = {
            'left_s0': -0.08,
            'left_s1': -1.0,
            'left_e0': -1.19,
            'left_e1': 1.94,
            'left_w0': 0.67,
            'left_w1': 1.03,
            'left_w2': -0.5
        }
Example #4
0
    def __init__(self, model_path, initial_qpos, n_actions, n_substeps):
        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.model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(self.model, n_substeps=n_substeps)
        self.data = self.sim.data
        self.viewer = None
        self._viewers = {}

        self.metadata = {
            'render.modes': ['human', 'rgb_array', 'depth_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.seed()
        self._env_setup(initial_qpos=initial_qpos)
        self.initial_state = copy.deepcopy(self.sim.get_state())

        obs = self._get_obs()
        self.action_space = spaces.MultiDiscrete(
            list(np.zeros(n_actions) + 11))

        self._set_action_space()

        action = self.action_space.sample()
        observation, _reward, done, _info = self.step(action)
        assert not done

        self._set_observation_space(observation)
Example #5
0
    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.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(self.model)
        self.data = self.sim.data
        self.viewer = None
        self._viewers = {}

        self.metadata = {
            'render.modes': ['human', 'rgb_array', 'depth_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.init_qpos = self.sim.data.qpos.ravel().copy()
        self.init_qvel = self.sim.data.qvel.ravel().copy()

        self._set_action_space()

        action = self.action_space.sample()
        observation, _reward, done, _info = self.step(action)
        assert not done

        self._set_observation_space(observation)

        self.seed()
Example #6
0
    def __init__(self, animate=False, sim=None):
        if sim is not None:
            self.sim = sim
            self.model = self.sim.model
        else:
            self.modelpath = QuadFeelersMjc.MODELPATH
            self.model = mujoco_py.load_model_from_path(self.modelpath)
            self.sim = mujoco_py.MjSim(self.model)

        self.model.opt.timestep = 0.04
        self.N_boxes = 4

        self.max_steps = 400

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = self.q_dim + self.qvel_dim - 7 * self.N_boxes - 6 * self.N_boxes + 7 - 2
        self.act_dim = self.sim.data.actuator_length.shape[0]

        # Environent inner parameters
        self.viewer = None
        self.step_ctr = 0

        self.joints_rads_low = np.array([-0.3, -1.] * 4 + [-1, -1, -1, -1])
        self.joints_rads_high = np.array([0.3, 0] * 4 + [1, 1, 1, 1])
        self.joints_rads_diff = self.joints_rads_high - self.joints_rads_low

        # Initial methods
        if animate:
            self.setupcam()

        self.reset()
Example #7
0
    def __init__(self, env_list=None, max_n_envs=3):
        print("Trossen hexapod envs: {}".format(env_list))

        self.modelpath = Hexapod.MODELPATH
        self.max_steps = 250

        self.episode_reward = 0
        self.max_episode_reward = 0
        self.episodes = 0

        self.joints_rads_low = np.array([-0.3, -1.0, -1.0] * 6)
        self.joints_rads_high = np.array([0.3, 0.2, 0.4] * 6)
        self.joints_rads_diff = self.joints_rads_high - self.joints_rads_low

        self.viewer = None

        path = Hexapod.MODELPATH + "gait.xml"

        self.model = mujoco_py.load_model_from_path(path)
        self.sim = mujoco_py.MjSim(self.model)

        self.model.opt.timestep = 0.02

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = 18 * 2 + 6 + 4 + 6
        self.act_dim = self.sim.data.actuator_length.shape[0]

        self.reset()
Example #8
0
    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.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(self.model)
        self.data = self.sim.data
        self.viewer = None
        self._viewers = {}

        self.metadata = {
            'render.modes': ['human', 'rgb_array', 'depth_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        # self.seed()
    
        self.init_qpos = self.sim.data.qpos.ravel().copy()
        self.init_qvel = self.sim.data.qvel.ravel().copy()

        # self.goal = self._sample_goal()
        
        self._set_action_space()

        ### because of this, we don't get our defined initial condn so we'll have to enforce them at last in __init__ of env itself
        action = self.action_space.sample()
        observation, _reward, done, _info = self.step(action)
        assert not done
        self.set_state(self.init_qpos,self.init_qvel) ### enforcing initial state

        self._set_observation_space(observation)
Example #9
0
    def __init__(self, animate=False):
        self.modelpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "assets/inverted_double_pendulum.xml")
        self.max_steps = 150
        self.mem_dim = 0
        self.cumulative_environment_reward = None

        self.model = mujoco_py.load_model_from_path(self.modelpath)
        self.sim = mujoco_py.MjSim(self.model)

        self.model.opt.timestep = 0.02

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = self.q_dim + self.qvel_dim
        self.act_dim = 1

        # Environent inner parameters
        self.viewer = None

        # Reset env variables
        self.step_ctr = 0
        self.episodes = 0

        self.reset()
Example #10
0
    def try_action(self, an_action):
        xml = XML(self.asset_path)
        # Note: We need to recreate the entire scene
        for ind, prev_action in enumerate(self.xml_actions_taken):  # Adding previous actions
            prev_action['pos'][-1] = ind * 2
            xml.add_mesh(**prev_action)

        xml_action = self.model_action_to_xml_action(an_action)
        new_name = xml.add_mesh(**xml_action)  # Note name is name of action (name of block dropped)
        new_names = self.names + [new_name]

        xml_str = xml.instantiate()
        model = mjc.load_model_from_xml(xml_str)
        sim = mjc.MjSim(model)

        logger = Logger(xml, sim, steps=self.max_num_objects_dropped + 1, img_dim=self.img_dim)
        for a_block in self.names:
            self.set_block_info(sim, a_block, self.get_block_info(a_block))
        logger.hold_drop_execute([], new_name, 1)

        # for act_ind, act in enumerate(new_names[:-1]):
        #     logger.hold_drop_execute(new_names[act_ind+1:], new_names[act_ind], self.settle_steps)
        logger.log(0)

        original_logger = self.logger
        self.logger = logger
        obs = self.get_observation()
        self.logger = original_logger
        return obs
Example #11
0
    def __init__(self, max_num_objects_dropped):
        self.asset_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'mujoco_data/stl/')
        self.img_dim = 64
        self.polygons = ['cube', 'horizontal_rectangle', 'tetrahedron']
        self.settle_bounds = {
            'pos':   [ [-.5, .5], [-.5, 0], [1, 2] ],
            'hsv': [ [0, 1], [0.5, 1], [0.5, 1] ],
            'scale': [ [0.4, 0.4] ],
            'force': [ [0, 0], [0, 0], [0, 0] ]
          }

        self.drop_bounds = {
            'pos':   [ [-1.75, 1.75], [-.5, 0], [0, 3] ],
          }

        self.xml = XML(self.asset_path)

        xml_str = self.xml.instantiate()
        model = mjc.load_model_from_xml(xml_str)
        sim = mjc.MjSim(model)

        self.max_num_objects_dropped = max_num_objects_dropped
        self.logger = Logger(self.xml, sim, steps=max_num_objects_dropped + 1, img_dim=self.img_dim)
        self.logger.log(0)

        self._blank_observation = self.get_observation()  # This can be / is used in the mpc goal acquisition step

        self.xml_actions_taken = []
        self.names = []
        self.env_step = 0
        self.settle_steps = 2000
Example #12
0
    def step(self, an_action):
        xml = XML(self.asset_path)
        # Note: We need to recreate the entire scene
        for ind, prev_action in enumerate(self.xml_actions_taken):  # Adding previous actions
            prev_action['pos'][-1] = ind * 2
            xml.add_mesh(**prev_action)

        xml_action = self.model_action_to_xml_action(an_action)
        # print("Action to take: ", xml_action)
        new_name = xml.add_mesh(**xml_action)  # Note name is name of action (name of block dropped)

        self.names.append(new_name)

        xml_str = xml.instantiate()
        model = mjc.load_model_from_xml(xml_str)
        sim = mjc.MjSim(model)

        logger = Logger(xml, sim, steps=self.max_num_objects_dropped + 1, img_dim=self.img_dim)

        #Set previous block states
        for a_block in self.names[:-1]:
            self.set_block_info(sim, a_block, self.get_block_info(a_block))

        logger.hold_drop_execute([], self.names[-1], self.settle_steps)
        self.logger = logger
        self.logger.log(0)

        ##Update state information
        self.xml_actions_taken.append(xml_action)
        self.sim = self.logger.sim

        return self.get_observation()
Example #13
0
def render_mujoco(model_path):
    model = mujoco_py.load_model_from_path(model_path)
    sim = mujoco_py.MjSim(model)
    viewer = mujoco_py.MjViewer(sim)
    while True:
        viewer.render()
        time.sleep(.01)
Example #14
0
    def __init__(self, model_path, initial_qpos, n_actions, n_substeps):
        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 {} does not exist'.format(fullpath))

        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model, nsubsteps=n_substeps)
        self.viewer = None
        self.obs_viewer = None

        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.seed()
        self._env_setup(initial_qpos=initial_qpos)
        self.initial_state = copy.deepcopy(self.sim.get_state())

        self.goal = self._sample_goal()
        self.visual_goal = self.get_image_obs()  # Not the actual visual goal
        obs = self._get_obs()
        self.action_space = spaces.Box(-1., 1., shape=(n_actions,), dtype='float32')
        self.observation_space = spaces.Dict(dict(
            desired_goal=spaces.Box(-np.inf, np.inf, shape=obs['achieved_goal'].shape, dtype='float32'),
            achieved_goal=spaces.Box(-np.inf, np.inf, shape=obs['achieved_goal'].shape, dtype='float32'),
            observation=spaces.Box(-np.inf, np.inf, shape=obs['observation'].shape, dtype='float32'),
        ))
        self.visual_goal = None
Example #15
0
    def __init__(self, model_path, initial_qpos, n_actions, n_substeps,
                 training_range, lift_angle_range, flex_angle_range,
                 time_offset_range, imi_flag, demonstr_idxs,
                 interaction_t_correction):
        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 {} does not exist'.format(fullpath))

        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model, nsubsteps=n_substeps)
        self.viewer = None
        self.step_counter = 0
        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        qposs_list = []
        for name in DATA_JOINT_NAMES:
            qposs_list.append(self.sim.data.get_joint_qpos(name))

        self.seed()
        self._env_setup(initial_qpos=initial_qpos)
        self.initial_state = copy.deepcopy(self.sim.get_state())
        self.initial_goal_pos = np.array([
            self.sim.data.get_joint_qpos('robot1:HMJX'),
            self.sim.data.get_joint_qpos('robot1:HMJY'),
            self.sim.data.get_joint_qpos('robot1:HMJZ')
        ])

        ctrlrange = self.sim.model.actuator_ctrlrange

        # self.qpos_array = np.clip(np.array(qpos_list,'Float64')[:,:], ctrlrange[-20:, 0], ctrlrange[-20:, 1])
        self.goal = self._sample_goal()

        obs = self._get_obs()
        self.action_space = spaces.Box(-1.,
                                       1.,
                                       shape=(n_actions, ),
                                       dtype='float32')
        self.observation_space = spaces.Dict(
            dict(
                desired_goal=spaces.Box(-np.inf,
                                        np.inf,
                                        shape=obs['achieved_goal'].shape,
                                        dtype='float32'),
                achieved_goal=spaces.Box(-np.inf,
                                         np.inf,
                                         shape=obs['achieved_goal'].shape,
                                         dtype='float32'),
                observation=spaces.Box(-np.inf,
                                       np.inf,
                                       shape=obs['observation'].shape,
                                       dtype='float32'),
            ))
Example #16
0
 def __init__(self, model_path):
     # self.model = mujoco_py.load_model_from_xml(model_path)
     self.model = dy.make_model(model_path)
     self.sim = mujoco_py.MjSim(self.model)
     self.viewer = mujoco_py.MjViewer(self.sim)
     self.qdim = self.model.nq
     self.udim = self.model.nu
     self.pdim = 2 * self.qdim + self.udim
Example #17
0
 def init_sim(self):
     self.model = self.load_model(self.path)
     self.sim = mujoco_py.MjSim(self.model)
     self.init_qpos = self.sim.data.qpos.ravel().copy()
     self.init_qvel = self.sim.data.qvel.ravel().copy()
     self.data = self.sim.data
     self._viewers = {}
     self.viewer = None
Example #18
0
def _setup_render_rgb(sim: mujoco_py.MjSim) -> mujoco_py.MjSim:
  # create copy of simulation to customize rendering context
  # flags defined in mjvisualize.h
  render_sim = mujoco_py.MjSim(sim.model)
  render_sim.set_state(sim.get_state())
  render_ctx = mujoco_py.MjRenderContextOffscreen(render_sim)
  render_ctx.scn.stereo = 2 # side-by-side rendering
  return render_sim
 def __init__(self, modelName, qPosInit, qVelInit, numAgent, qPosInitNoise=0, qVelInitNoise=0):
     model = mujoco.load_model_from_path('../env/xmls/' + modelName + '.xml')
     self.simulation = mujoco.MjSim(model)
     self.qPosInit = qPosInit
     self.qVelInit = qVelInit
     self.numAgent = numAgent
     self.qPosInitNoise = qPosInitNoise
     self.qVelInitNoise = qVelInitNoise
Example #20
0
    def reset(self, test=False):
        if self.episodes % 1000 == 0 and self.episodes > 0:
            self.envgen.save()

        if not test:
            self.envgen.feedback(self.cumulative_environment_reward)
            self.envgen.generate()
        else:
            self.envgen.test_generate()

        self.cumulative_environment_reward = 0

        try:
            self.model = mujoco_py.load_model_from_path(self.modelpath)
        except:
            print(
                "Error, file was not found, backup loaded instead for this episode"
            )
            self.model = mujoco_py.load_model_from_path(self.backuppath)

        self.sim = mujoco_py.MjSim(self.model)

        self.model.opt.timestep = 0.02

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = self.q_dim + self.qvel_dim - 2 + 6 + self.mem_dim
        self.act_dim = self.sim.data.actuator_length.shape[0] + self.mem_dim

        # Environent inner parameters
        self.viewer = None

        # Reset env variables
        self.step_ctr = 0
        self.episodes += 1
        self.ctrl_vecs = []
        self.dead_joint_idx = np.random.randint(0, self.act_dim)
        self.dead_leg_idx = np.random.randint(0, self.act_dim / 3)

        # Sample initial configuration
        init_q = np.zeros(self.q_dim, dtype=np.float32)
        init_q[0] = np.random.randn() * 0.1
        init_q[1] = np.random.randn() * 0.1
        init_q[2] = 1.00 + np.random.rand() * 0.1
        init_qvel = np.random.randn(self.qvel_dim).astype(np.float32) * 0.1

        obs = np.concatenate((init_q[2:], init_qvel)).astype(np.float32)

        # Set environment state
        self.set_state(init_q, init_qvel)

        obs_dict = self.get_obs_dict()
        obs = np.concatenate(
            (obs, obs_dict["contacts"], np.zeros(self.mem_dim)))

        return obs
    def __init__(self, model_path, frame_skip, rgb_rendering_tracking=True):
        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 {} does not exist'.format(fullpath))

        self.frame_skip = frame_skip
        self.model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(self.model)
        self.data = self.sim.data
        self.viewer = None
        self.rgb_rendering_tracking = rgb_rendering_tracking
        self._viewers = {}

        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.goal = self._sample_goal()
        obs = self._get_obs()
        self.action_space = spaces.Box(-1.,
                                       1.,
                                       shape=(n_actions, ),
                                       dtype='float32')
        self.observation_space = spaces.Dict(
            dict(
                desired_goal=spaces.Box(-np.inf,
                                        np.inf,
                                        shape=obs['achieved_goal'].shape,
                                        dtype='float32'),
                achieved_goal=spaces.Box(-np.inf,
                                         np.inf,
                                         shape=obs['achieved_goal'].shape,
                                         dtype='float32'),
                observation=spaces.Box(-np.inf,
                                       np.inf,
                                       shape=obs['observation'].shape,
                                       dtype='float32'),
            ))

        self.init_qpos = self.sim.data.qpos.ravel().copy()
        self.init_qvel = self.sim.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, dtype=np.float32)

        high = np.inf * np.ones(self.obs_dim)
        low = -high
        self.seed()
Example #22
0
    def __init__(self, width, height, frame_skip, rewarding_distance):
        self.frame_skip = frame_skip
        self.width = width
        self.height = height

        # Instantiate Mujoco model
        model_path = "jaco.xml"
        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)
        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model)

        self.init_state = self.sim.get_state()
        self.init_qpos = self.sim.data.qpos.ravel().copy()
        self.init_qvel = self.sim.data.qvel.ravel().copy()

        # Setup actuators
        self.actuator_bounds = self.sim.model.actuator_ctrlrange
        self.actuator_low = self.actuator_bounds[:, 0]
        self.actuator_high = self.actuator_bounds[:, 1]
        self.actuator_ctrlrange = self.actuator_high - self.actuator_low
        self.num_actuators = len(self.actuator_low)

        self.observation_space = spaces.Box(low=-10, high=10, shape=(21, ))
        self.action_space = spaces.Box(low=-3, high=3, shape=(9, ))

        # init model_data_ctrl
        self.null_action = np.zeros(self.num_actuators)
        self.sim.data.ctrl[:] = self.null_action

        self.seed()

        self.sum_reward = 0
        self.rewarding_distance = rewarding_distance

        # Target position bounds
        self.target_bounds = np.array(((0.3, 0.4), (-0.5, 0.5), (0.05, 0.05)))
        self.target_reset_distance = 0.2

        #self.reset_target()
        self.goal_list = [[0.6, -0.3, 0.3], [0.6, 0.3, 0.3], [0.6, 0., 0.3]]
        #self.goal_list = [[0.6, -0.4, 0.5]]
        self.episode = 0

        # Setup discrete action space
        #self.control_values = self.actuator_ctrlrange * control_magnitude

        #self.num_actions = 5
        # self.action_space = [list(range(self.num_actions))
        #                      ] * self.num_actuators
        # self.observation_space = ((0, ), (height, width, 3),
        #                           (height, width, 3))

        #self.reset_target()
        self.lifted = False
        self.reset()
Example #23
0
    def reset(self):
        # Generate environment
        hm_fun = np.random.choice(self.hm_fun_list)
        hm, info = hm_fun(*self.hm_args)
        cv2.imwrite(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         "hm.png"), hm)

        # Load simulator
        while True:
            try:
                self.model = mujoco_py.load_model_from_path(Quad.MODELPATH)
                break
            except Exception:
                pass

        # Set appropriate height according to height map
        self.model.hfield_size[0][2] = info["height"]

        self.sim = mujoco_py.MjSim(self.model)
        self.model.opt.timestep = 0.02
        self.viewer = None

        # Height field
        self.hf_data = self.model.hfield_data
        self.hf_ncol = self.model.hfield_ncol[0]
        self.hf_nrow = self.model.hfield_nrow[0]
        self.hf_column_meters = self.model.hfield_size[0][0] * 2
        self.hf_row_meters = self.model.hfield_size[0][1] * 2
        self.hf_height_meters = self.model.hfield_size[0][2]
        self.pixels_per_column = self.hf_ncol / float(self.hf_column_meters)
        self.pixels_per_row = self.hf_nrow / float(self.hf_row_meters)
        self.hf_grid = self.hf_data.reshape((self.hf_nrow, self.hf_ncol))

        local_grid = self.hf_grid[45:55, 5:15]
        max_height = np.max(local_grid) * self.hf_height_meters

        # Environment dimensions
        self.q_dim = self.sim.get_state().qpos.shape[0]
        self.qvel_dim = self.sim.get_state().qvel.shape[0]

        self.obs_dim = 12 + 12 + 4 + 6 + 4  # j, jd, quat, pose_velocity, contacts
        self.act_dim = self.sim.data.actuator_length.shape[0]

        # Set initial position
        init_q = np.zeros(self.q_dim, dtype=np.float32)
        init_q[0] = 0.0
        init_q[1] = 0.0
        init_q[2] = max_height + 0.05
        init_qvel = np.random.randn(self.qvel_dim).astype(np.float32) * 0.1

        # Set environment state
        self.set_state(init_q, init_qvel)
        self.step_ctr = 0

        obs, _, _, _ = self.step(np.zeros(self.act_dim))

        return obs
Example #24
0
    def __init__(self, xml_specification, mesh_specification=None):
        ref_angles = {
            0: -np.pi / 2,
            1: 0,
            2: np.pi / 2,
            3: -np.pi / 2,
            4: np.pi / 2,
            5: 0
        }

        self.ref_angles = ref_angles
        self.xml_specification = xml_specification
        self.mesh_specification = mesh_specification

        if not path.isfile(self.xml_specification):
            raise Exception('Missing XML specification at: {}'.format(
                self.xml_specification))

        if mesh_specification is not None:
            if not path.isdir(self.mesh_specification):
                raise Exception('Missing mesh specification at: {}'.format(
                    self.mesh_specification))

        print('Arm model is specified at: {}'.format(self.xml_specification))

        try:
            self.mjc_model = mjc.load_model_from_path(self.xml_specification)
        except:
            raise Exception('Mujoco was unable to load the model')

        # Initializing joint dictionary
        joint_ids, joint_names = self.get_joints_info()
        joint_positions_addr = [
            self.mjc_model.get_joint_qpos_addr(name) for name in joint_names
        ]
        joint_velocity_addr = [
            self.mjc_model.get_joint_qvel_addr(name) for name in joint_names
        ]
        self.joint_dict = {}
        for i, ii in enumerate(joint_ids):
            self.joint_dict[ii] = {
                'name': joint_names[i],
                'position_address': joint_positions_addr[i],
                'velocity_address': joint_velocity_addr[i]
            }

        if not np.all(np.array(self.mjc_model.jnt_type) ==
                      3):  # 3 stands for revolute joint
            raise Exception('Revolute joints are assumed')

        self.n_joints = len(self.joint_dict.items())

        # Initialize simulator
        self.simulation = mjc.MjSim(self.mjc_model)

        self.viewer = mjc.MjViewer(self.simulation)
        '''
Example #25
0
    def __init__(self, model_path):
        self.model = mjp.load_model_from_path(model_path)
        self.sim = mjp.MjSim(self.model)

        self._controllable_joints = self.get_controllable_joints()

        self._all_joints = [
            self.model.joint_name2id(j) for j in self.model.joint_names
        ]
Example #26
0
def create_sim(collision=False):
    if collision:
        model_filename = 'full_kuka_mesh_collision.xml'
    else:
        model_filename = 'full_kuka_no_collision.xml'
    model_path = os.path.join(kuka_asset_dir(), model_filename)
    model = mujoco_py.load_model_from_path(model_path)
    sim = mujoco_py.MjSim(model)
    return sim
 def __init__(self, aliveBonus, deathPenalty, isTerminal):
     self.aliveBonus = aliveBonus
     self.deathPenalty = deathPenalty
     self.isTerminal = isTerminal
     modelName = 'inverted_double_pendulum'
     model = mujoco.load_model_from_path('xmls/' + modelName + '.xml')
     self.simulation = mujoco.MjSim(model)
     self.numQPos = len(self.simulation.data.qpos)
     self.numQVel = len(self.simulation.data.qvel)
    def __init__(self, model_path, initial_qpos, n_actions, n_substeps):
        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 {} does not exist'.format(fullpath))

        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model, nsubsteps=n_substeps)
        #self.viewer = None # comment when using "human"
        self.viewer = mujoco_py.MjViewer(
            self.sim)  #comment when using "rgb_array"
        self._viewers = {}

        self.modder = TextureModder(self.sim)
        self.visual_randomize = True
        self.mat_modder = MaterialModder(self.sim)
        self.light_modder = LightModder(self.sim)

        self.metadata = {
            'render.modes': ['human', 'rgb_array'],
            'video.frames_per_second': int(np.round(1.0 / self.dt))
        }

        self.visual_data_recording = True
        self._index = 0
        self._label_matrix = []

        self.seed()
        self._env_setup(initial_qpos=initial_qpos)
        self.initial_state = copy.deepcopy(self.sim.get_state())

        self.goal = self._sample_goal()
        obs = self._get_obs()
        self.action_space = spaces.Box(-1.,
                                       1.,
                                       shape=(n_actions, ),
                                       dtype='float32')
        self.observation_space = spaces.Dict(
            dict(
                desired_goal=spaces.Box(-np.inf,
                                        np.inf,
                                        shape=obs['achieved_goal'].shape,
                                        dtype='float32'),
                achieved_goal=spaces.Box(-np.inf,
                                         np.inf,
                                         shape=obs['achieved_goal'].shape,
                                         dtype='float32'),
                observation=spaces.Box(-np.inf,
                                       np.inf,
                                       shape=obs['observation'].shape,
                                       dtype='float32'),
            ))
        if self.viewer:
            self._viewer_setup()
Example #29
0
 def load_env(self, num):
     if num < len(self.env_paths):
         new_scene = self.env_paths[num]
         scene = mujoco_py.load_model_from_path(new_scene)
         self.env = mujoco_py.MjSim(scene)
         if self.is_vis:
             self.viewer = mujoco_py.MjViewer(self.env)
     else:
         print("Wrong number,")
    def __init__(self):
        """Initializes a new Fetch environment."""

        initial_qpos = {
            "robot0:slide0": 0.4049,
            "robot0:slide1": 0.48,
            "robot0:slide2": 0.0,
        }
        model_path = MODEL_XML_PATH
        n_substeps = 20

        self.target_min = 0.15
        self.target_max = 0.2
        self.target_range = self.target_max - self.target_min
        self.obj_min = 0.05
        self.obj_max = 0.1
        self.obj_range = self.obj_max - self.obj_min

        self.gripper_extra_height = 0.2
        self.target_offset = 0.0
        self.distance_threshold = 0.05
        self.use_penalty = True

        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 {} does not exist".format(fullpath))

        model = mujoco_py.load_model_from_path(fullpath)
        self.sim = mujoco_py.MjSim(model, nsubsteps=n_substeps)
        self.viewer = None
        self._viewers = {}

        self.metadata = {
            "render.modes": ["human", "rgb_array"],
            "video.frames_per_second": int(np.round(1.0 / self.dt)),
        }

        self.seed()
        self._env_setup(initial_qpos=initial_qpos)
        self.initial_state = copy.deepcopy(self.sim.get_state())

        self.goal = self._sample_goal()
        self.stack_pos = self._sample_stack_pos()

        obs = self._get_obs()
        self.action_space = spaces.Box(-1.0, 1.0, shape=(3, ), dtype="float32")
        self.observation_space = spaces.Box(-np.inf,
                                            np.inf,
                                            shape=obs.shape,
                                            dtype="float32")

        utils.EzPickle.__init__(self)