updates
This commit is contained in:
@@ -73,7 +73,7 @@ class AlrMujocoEnv(gym.Env):
|
||||
# observation, _reward, done, _info = self.step(action)
|
||||
# assert not done
|
||||
|
||||
observation = self.reset()
|
||||
observation = self._get_obs() # TODO: is calling get_obs enough? should we call reset, or even step?
|
||||
|
||||
self._set_observation_space(observation)
|
||||
|
||||
@@ -82,14 +82,14 @@ class AlrMujocoEnv(gym.Env):
|
||||
@property
|
||||
def current_pos(self):
|
||||
"""
|
||||
By default returns the joint positions of all simulated objects. May be overriden in subclass.
|
||||
By default returns the joint positions of all simulated objects. May be overridden in subclass.
|
||||
"""
|
||||
return self.sim.data.qpos
|
||||
|
||||
@property
|
||||
def current_vel(self):
|
||||
"""
|
||||
By default returns the joint velocities of all simulated objects. May be overriden in subclass.
|
||||
By default returns the joint velocities of all simulated objects. May be overridden in subclass.
|
||||
"""
|
||||
return self.sim.data.qvel
|
||||
|
||||
@@ -125,10 +125,15 @@ class AlrMujocoEnv(gym.Env):
|
||||
# methods to override:
|
||||
# ----------------------------
|
||||
|
||||
def _get_obs(self):
|
||||
"""Returns the observation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
"""
|
||||
Helper method to set certain environment properties such as contexts in contextual environments since reset()
|
||||
doesn't take arguments. Should be called before/after reset(). TODO: before or after?
|
||||
doesn't take arguments. Should be called before reset().
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from gym.envs.mujoco import mujoco_env
|
||||
from gym import utils
|
||||
import os
|
||||
import numpy as np
|
||||
from alr_envs.mujoco.ball_in_a_cup.ball_in_a_cup_reward import BallInACupReward
|
||||
from alr_envs.mujoco import alr_mujoco_env
|
||||
from alr_envs.mujoco.ball_in_a_cup.ball_in_a_cup_reward_simple import BallInACupReward
|
||||
import mujoco_py
|
||||
|
||||
|
||||
class ALRBallInACupEnv(mujoco_env.MujocoEnv, utils.EzPickle):
|
||||
class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
def __init__(self, ):
|
||||
self._steps = 0
|
||||
|
||||
@@ -21,8 +21,12 @@ class ALRBallInACupEnv(mujoco_env.MujocoEnv, utils.EzPickle):
|
||||
self._q_pos = []
|
||||
|
||||
utils.EzPickle.__init__(self)
|
||||
mujoco_env.MujocoEnv.__init__(self, os.path.join(os.path.dirname(__file__), "assets", "ball-in-a-cup_base.xml"),
|
||||
frame_skip=4)
|
||||
alr_mujoco_env.AlrMujocoEnv.__init__(self, os.path.join(os.path.dirname(__file__), "assets", "ball-in-a-cup_base.xml"),
|
||||
n_substeps=4)
|
||||
|
||||
def configure(self, context):
|
||||
self.context = context
|
||||
self.reward_function.reset(context)
|
||||
|
||||
def reset_model(self):
|
||||
start_pos = self.init_qpos.copy()
|
||||
@@ -30,24 +34,8 @@ class ALRBallInACupEnv(mujoco_env.MujocoEnv, utils.EzPickle):
|
||||
start_vel = np.zeros_like(start_pos)
|
||||
self.set_state(start_pos, start_vel)
|
||||
self._steps = 0
|
||||
self.reward_function.reset()
|
||||
self._q_pos = []
|
||||
|
||||
def do_simulation(self, ctrl, n_frames):
|
||||
self.sim.data.ctrl[:] = ctrl
|
||||
for _ in range(n_frames):
|
||||
try:
|
||||
self.sim.step()
|
||||
except mujoco_py.builder.MujocoException as e:
|
||||
# print("Error in simulation: " + str(e))
|
||||
# error = True
|
||||
# Copy the current torque as if it would have been applied until the end of the trajectory
|
||||
# for i in range(k + 1, sim_time):
|
||||
# torques.append(trq)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def step(self, a):
|
||||
# Apply gravity compensation
|
||||
if not np.all(self.sim.data.qfrc_applied[:7] == self.sim.data.qfrc_bias[:7]):
|
||||
@@ -98,6 +86,7 @@ class ALRBallInACupEnv(mujoco_env.MujocoEnv, utils.EzPickle):
|
||||
|
||||
if __name__ == "__main__":
|
||||
env = ALRBallInACupEnv()
|
||||
env.configure(None)
|
||||
env.reset()
|
||||
for i in range(2000):
|
||||
# objective.load_result("/tmp/cma")
|
||||
|
||||
+6
-5
@@ -26,9 +26,9 @@ class BallInACupReward(alr_reward_fct.AlrReward):
|
||||
self.dists_final = None
|
||||
self.costs = None
|
||||
|
||||
self.reset()
|
||||
self.reset(None)
|
||||
|
||||
def reset(self):
|
||||
def reset(self, context):
|
||||
self.ball_traj = np.zeros(shape=(self.sim_time, 3))
|
||||
self.dists = []
|
||||
self.dists_final = []
|
||||
@@ -51,11 +51,12 @@ class BallInACupReward(alr_reward_fct.AlrReward):
|
||||
self.dists_final.append(np.linalg.norm(goal_final_pos - ball_pos))
|
||||
self.ball_traj[step, :] = ball_pos
|
||||
|
||||
if self.check_collision(sim):
|
||||
return -1000, False, True
|
||||
|
||||
action_cost = np.sum(np.square(action))
|
||||
|
||||
if self.check_collision(sim):
|
||||
reward = - 1e-5 * action_cost - 1000
|
||||
return reward, False, True
|
||||
|
||||
if step == self.sim_time - 1:
|
||||
min_dist = np.min(self.dists)
|
||||
dist_final = self.dists_final[-1]
|
||||
@@ -1,21 +1,16 @@
|
||||
from alr_envs.mujoco import alr_mujoco_env
|
||||
from gym import utils, spaces
|
||||
from gym import utils
|
||||
import os
|
||||
import numpy as np
|
||||
from alr_envs.mujoco.ball_in_a_cup.ball_in_a_cup_reward import BallInACupReward
|
||||
|
||||
|
||||
class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
def __init__(self, reward_function=None):
|
||||
def __init__(self, n_substeps=4, apply_gravity_comp=True, reward_function=None):
|
||||
self._steps = 0
|
||||
|
||||
self.xml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets",
|
||||
"biac_base" + ".xml")
|
||||
|
||||
self.sim_time = 8 # seconds
|
||||
self.sim_steps = int(self.sim_time / (0.0005 * 4)) # circular dependency.. sim.dt <-> mujocoenv init <-> reward fct
|
||||
self.reward_function = reward_function(self.sim_steps)
|
||||
|
||||
self.start_pos = np.array([0.0, 0.58760536, 0.0, 1.36004913, 0.0, -0.32072943, -1.57])
|
||||
self.start_vel = np.zeros(7)
|
||||
|
||||
@@ -34,8 +29,15 @@ class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
utils.EzPickle.__init__(self)
|
||||
alr_mujoco_env.AlrMujocoEnv.__init__(self,
|
||||
self.xml_path,
|
||||
apply_gravity_comp=True,
|
||||
n_substeps=4)
|
||||
apply_gravity_comp=apply_gravity_comp,
|
||||
n_substeps=n_substeps)
|
||||
|
||||
self.sim_time = 8 # seconds
|
||||
self.sim_steps = int(self.sim_time / self.dt)
|
||||
if reward_function is None:
|
||||
from alr_envs.mujoco.ball_in_a_cup.ball_in_a_cup_reward_simple import BallInACupReward
|
||||
reward_function = BallInACupReward
|
||||
self.reward_function = reward_function(self.sim_steps)
|
||||
|
||||
@property
|
||||
def current_pos(self):
|
||||
@@ -47,6 +49,7 @@ class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
|
||||
def configure(self, context):
|
||||
self.context = context
|
||||
self.reward_function.reset(context)
|
||||
|
||||
def reset_model(self):
|
||||
init_pos_all = self.init_qpos.copy()
|
||||
@@ -56,7 +59,6 @@ class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
goal_final_id = self.sim.model._site_name2id["cup_goal_final"]
|
||||
|
||||
self._steps = 0
|
||||
self.reward_function.reset()
|
||||
self._q_pos = []
|
||||
self._q_vel = []
|
||||
|
||||
@@ -65,38 +67,6 @@ class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
|
||||
self.set_state(start_pos, init_vel)
|
||||
|
||||
# Reset the system
|
||||
# self.sim.data.qpos[:] = init_pos_all
|
||||
# self.sim.data.qvel[:] = init_vel
|
||||
# self.sim.data.qpos[0:7] = init_pos_robot
|
||||
#
|
||||
# self.sim.step()
|
||||
#
|
||||
# self.sim.data.qpos[:] = init_pos_all
|
||||
# self.sim.data.qvel[:] = init_vel
|
||||
# self.sim.data.qpos[0:7] = init_pos_robot
|
||||
# self.sim.data.body_xpos[ball_id, :] = np.copy(self.sim.data.site_xpos[goal_final_id, :]) - np.array([0., 0., 0.329])
|
||||
#
|
||||
# # Stabilize the system around the initial position
|
||||
# for i in range(0, 500):
|
||||
# self.sim.data.qpos[7:] = 0.
|
||||
# self.sim.data.qvel[7:] = 0.
|
||||
# # self.sim.data.qpos[7] = -0.2
|
||||
# cur_pos = self.sim.data.qpos[0:7].copy()
|
||||
# cur_vel = self.sim.data.qvel[0:7].copy()
|
||||
# trq = self.p_gains * (init_pos_robot - cur_pos) + self.d_gains * (np.zeros_like(init_pos_robot) - cur_vel)
|
||||
# self.sim.data.qfrc_applied[0:7] = trq + self.sim.data.qfrc_bias[:7].copy()
|
||||
# self.sim.step()
|
||||
# self.render()
|
||||
#
|
||||
# for i in range(0, 500):
|
||||
# cur_pos = self.sim.data.qpos[0:7].copy()
|
||||
# cur_vel = self.sim.data.qvel[0:7].copy()
|
||||
# trq = self.p_gains * (init_pos_robot - cur_pos) + self.d_gains * (np.zeros_like(init_pos_robot) - cur_vel)
|
||||
# self.sim.data.qfrc_applied[0:7] = trq + self.sim.data.qfrc_bias[:7].copy()
|
||||
# self.sim.step()
|
||||
# self.render()
|
||||
|
||||
return self._get_obs()
|
||||
|
||||
def step(self, a):
|
||||
@@ -154,7 +124,10 @@ class ALRBallInACupEnv(alr_mujoco_env.AlrMujocoEnv, utils.EzPickle):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from alr_envs.mujoco.ball_in_a_cup.ball_in_a_cup_reward_simple import BallInACupReward
|
||||
|
||||
env = ALRBallInACupEnv(reward_function=BallInACupReward)
|
||||
env.configure(None)
|
||||
env.reset()
|
||||
env.render()
|
||||
for i in range(4000):
|
||||
|
||||
Reference in New Issue
Block a user