Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, goal_direction=None):
self.goal_direction = goal_direction if goal_direction else 1.0
MujocoEnv.__init__(self, 'ant.xml', 5)
gym.utils.EzPickle.__init__(self)
for x in [-X_RANGE + i * X_RANGE / 5. for i in range(10)]:
for y in [-Y_RANGE + i * 0.12 for i in range(10)]:
discrete_action_set.append([[x, y], d])
return discrete_action_set
DISCRETE_ACTION_SET = _create_discrete_action_set()
# cardinal vectors
# TODO: ideally this should be packaged into scene struct
four_cardinal_vectors = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0]]
four_cardinal_vectors = np.array(four_cardinal_vectors, dtype=np.float32)
four_cardinal_vectors_names = ['front', 'behind', 'left', 'right']
class ClevrEnv(mujoco_env.MujocoEnv, utils.EzPickle):
"""ClevrEnv."""
def __init__(self,
maximum_episode_steps=100,
xml_path=None,
metadata_path=None,
template_path=None,
num_object=5,
agent_type='pm',
random_start=False,
fixed_objective=True,
description_num=15,
action_type='continuous',
obs_type='direct',
use_movement_bonus=False,
direct_obs=False,
# else:
# self.nn = 8
# self.all_joints = self.nn + 2
# self.gripper_action = np.zeros(2)
# self.unity = unity
# if self.visionnet_input:
# if self.unity:
# self.b = bytearray(3*self.imgsize_h*self.imgsize_w)
# self.no_viewer = False
# else:
# self.no_viewer = True
# else:
# self.no_viewer = False
# frame_skip = 20
utils.EzPickle.__init__(self)
# mujoco_env.MujocoEnv.__init__(self, self.xml_path, frame_skip)
import numpy as np
from gym import utils
from gym.envs.mujoco import mujoco_env
from gym.utils import seeding
class ReacherGoalEnv(mujoco_env.MujocoEnv, utils.EzPickle):
def __init__(self, seed=0, start_variance=0.1,
goal_state_pos='variable', goal_state_access=True):
'''
Multi-task (population) version of Gym Reacher environment.
Args:
seed: for RNG determining goal position.
start_variance: variance of the starting position for the arm.
goal_state_pos: if 'fixed', goal position is static across reset().
goal_state_access: is goal position included in the state?
'''
self._start_variance = start_variance
self._goal_state_pos = goal_state_pos
self._goal_state_access = goal_state_access
utils.EzPickle.__init__(self)
mujoco_env.MujocoEnv.__init__(self, 'reacher.xml', 2)
import numpy as np
from gym import utils
from gym.envs.mujoco import mujoco_env
class HalfCheetahEnv(mujoco_env.MujocoEnv, utils.EzPickle):
def __init__(self):
mujoco_env.MujocoEnv.__init__(self, 'half_cheetah.xml', 5)
utils.EzPickle.__init__(self)
def _step(self, action):
xposbefore = self.model.data.qpos[0, 0]
self.do_simulation(action, self.frame_skip)
xposafter = self.model.data.qpos[0, 0]
ob = self._get_obs()
reward_ctrl = - 0.1 * np.square(action).sum()
reward_run = (xposafter - xposbefore)/self.dt
reward = reward_ctrl + reward_run
done = False
return ob, reward, done, dict(reward_run=reward_run, reward_ctrl=reward_ctrl)
def _get_obs(self):
import numpy as np
from gym.envs.adversarial.mujoco import mujoco_env
from gym import utils
def mass_center(model):
mass = model.body_mass
xpos = model.data.xipos
return (np.sum(mass * xpos, 0) / np.sum(mass))[0]
class HumanoidStandupEnv(mujoco_env.MujocoEnv, utils.EzPickle):
def __init__(self):
mujoco_env.MujocoEnv.__init__(self, 'humanoidstandup.xml', 5)
utils.EzPickle.__init__(self)
def _get_obs(self):
data = self.model.data
return np.concatenate([data.qpos.flat[2:],
data.qvel.flat,
data.cinert.flat,
data.cvel.flat,
data.qfrc_actuator.flat,
data.cfrc_ext.flat])
def _step(self, a):
self.do_simulation(a, self.frame_skip)
pos_after = self.model.data.qpos[2][0]
def __init__(self, **kwargs):
EzPickle.__init__(self)
self.seed()
self.viewer = None
self.world = Box2D.b2World()
self.moon = None
self.lander = None
self.particles = []
self.prev_reward = None
# useful range is -1 .. +1, but spikes can be higher
self.observation_space = spaces.Box(-np.inf, np.inf, shape=(8,), dtype=np.float32)
if self.continuous:
# Action is two floats [main engine, left-right engines].
# Main engine: -1..0 off, 0..+1 throttle from 50% to 100% power. Engine can't work with less than 50% power.
import numpy as np
from gym import utils
from gym.envs.mujoco import mujoco_env
class AntBanditsEnv(mujoco_env.MujocoEnv, utils.EzPickle):
def __init__(self):
utils.EzPickle.__init__(self)
mujoco_env.MujocoEnv.__init__(self, 'ant_bandits.xml', 5)
# self.realgoal = self.np_random.uniform(low=0, high=5, size=2)
self.realgoal = np.array([5, 0]) if np.random.uniform() < 0.5 else np.array([0, 5])
# self.realgoal = np.array([5, 0])
# self.realgoal = np.array([3, 3])
def viewer_setup(self):
self.viewer.cam.trackbodyid = 0
def _step(self, a):
self.do_simulation(a, self.frame_skip)
vec = self.get_body_com("torso")-self.get_body_com("target")
reward_dist = -np.sqrt(np.linalg.norm(vec)) / 3000
# reward_dist = -np.linalg.norm(vec)
def __init__(self, model_path=os.path.dirname(gym.envs.mujoco.__file__) + "/assets/striker.xml", **kwargs):
self._striked = False
self._min_strike_dist = np.inf
self.strike_threshold = 0.1
mujoco_env.MujocoEnv.__init__(self, model_path, 5)
utils.EzPickle.__init__(self)
def __init__(self, reward_type='sparse'):
initial_qpos = {
'robot0:slide0': 0.4049,
'robot0:slide1': 0.48,
'robot0:slide2': 0.0,
}
fetch_env.FetchEnv.__init__(
self, MODEL_XML_PATH, has_object=False, block_gripper=True, n_substeps=20,
gripper_extra_height=0.2, target_in_the_air=True, target_offset=0.0,
obj_range=0.15, target_range=0.15, distance_threshold=0.05,
initial_qpos=initial_qpos, reward_type=reward_type)
utils.EzPickle.__init__(self)