def __init__(self, action_noise=0.0, file_path=None, template_args=None):
# compile template
if file_path is None:
if self.__class__.FILE is None:
raise "Mujoco file not specified"
file_path = osp.join(MODEL_DIR, self.__class__.FILE)
if file_path.endswith(".mako"):
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_args if template_args is not None else {},
)
tmp_f, file_path = tempfile.mkstemp(text=True)
with open(file_path, 'w') as f:
f.write(content)
self.model = MjModel(file_path)
os.close(tmp_f)
else:
self.model = MjModel(file_path)
self.data = self.model.data
self.viewer = None
self.init_qpos = self.model.data.qpos
self.init_qvel = self.model.data.qvel
self.init_qacc = self.model.data.qacc
self.init_ctrl = self.model.data.ctrl
self.qpos_dim = self.init_qpos.size
self.qvel_dim = self.init_qvel.size
self.ctrl_dim = self.init_ctrl.size
self.action_noise = action_noise
if "frame_skip" in self.model.numeric_names:
frame_skip_id = self.model.numeric_names.index("frame_skip")
addr = self.model.numeric_adr.flat[frame_skip_id]
self.frame_skip = int(self.model.numeric_data.flat[addr])
else:
self.frame_skip = 1
if "init_qpos" in self.model.numeric_names:
init_qpos_id = self.model.numeric_names.index("init_qpos")
addr = self.model.numeric_adr.flat[init_qpos_id]
size = self.model.numeric_size.flat[init_qpos_id]
init_qpos = self.model.numeric_data.flat[addr:addr + size]
self.init_qpos = init_qpos
self.dcom = None
self.current_com = None
self.reset()
super(MujocoEnv, self).__init__()
python类render()的实例源码
def __init__(self,
difficulty=1.0,
texturedir='/tmp/mujoco_textures',
hfield_dir='/tmp/mujoco_terrains',
regen_terrain=True,
*args, **kwargs):
Serializable.quick_init(self, locals())
self.difficulty = max(difficulty, self.MIN_DIFFICULTY)
self.texturedir = texturedir
self.hfield_dir = hfield_dir
model_cls = self.__class__.MODEL_CLASS
if model_cls is None:
raise "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(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
ProxyEnv.__init__(self, inner_env) # here is where the robot env will be initialized
os.close(tmp_f)