2021-02-15 16:31:34 +01:00
|
|
|
from alr_envs.utils.policies import get_policy_class
|
2021-01-11 16:08:42 +01:00
|
|
|
from mp_lib.phase import ExpDecayPhaseGenerator
|
|
|
|
from mp_lib.basis import DMPBasisGenerator
|
|
|
|
from mp_lib import dmps
|
|
|
|
import numpy as np
|
|
|
|
import gym
|
|
|
|
|
|
|
|
|
2021-02-11 10:49:57 +01:00
|
|
|
class DmpEnvWrapper(gym.Wrapper):
|
2021-02-05 17:10:03 +01:00
|
|
|
def __init__(self,
|
|
|
|
env,
|
|
|
|
num_dof,
|
|
|
|
num_basis,
|
|
|
|
start_pos=None,
|
|
|
|
final_pos=None,
|
|
|
|
duration=1,
|
|
|
|
alpha_phase=2,
|
|
|
|
dt=0.01,
|
|
|
|
learn_goal=False,
|
|
|
|
post_traj_time=0.,
|
2021-02-11 10:49:57 +01:00
|
|
|
policy_type=None,
|
2021-03-19 16:31:46 +01:00
|
|
|
weights_scale=1.,
|
|
|
|
goal_scale=1.,
|
|
|
|
):
|
2021-02-11 10:49:57 +01:00
|
|
|
super(DmpEnvWrapper, self).__init__(env)
|
2021-01-11 16:08:42 +01:00
|
|
|
self.num_dof = num_dof
|
|
|
|
self.num_basis = num_basis
|
|
|
|
self.dim = num_dof * num_basis
|
|
|
|
if learn_goal:
|
|
|
|
self.dim += num_dof
|
2021-02-05 17:10:03 +01:00
|
|
|
self.learn_goal = learn_goal
|
2021-01-12 10:52:08 +01:00
|
|
|
self.duration = duration # seconds
|
2021-01-11 16:08:42 +01:00
|
|
|
time_steps = int(duration / dt)
|
|
|
|
self.t = np.linspace(0, duration, time_steps)
|
2021-02-05 17:10:03 +01:00
|
|
|
self.post_traj_steps = int(post_traj_time / dt)
|
2021-01-11 16:08:42 +01:00
|
|
|
|
2021-02-05 17:10:03 +01:00
|
|
|
phase_generator = ExpDecayPhaseGenerator(alpha_phase=alpha_phase, duration=duration)
|
2021-01-11 16:08:42 +01:00
|
|
|
basis_generator = DMPBasisGenerator(phase_generator, duration=duration, num_basis=self.num_basis)
|
|
|
|
|
|
|
|
self.dmp = dmps.DMP(num_dof=num_dof,
|
|
|
|
basis_generator=basis_generator,
|
|
|
|
phase_generator=phase_generator,
|
|
|
|
num_time_steps=time_steps,
|
|
|
|
dt=dt
|
|
|
|
)
|
|
|
|
|
2021-02-05 17:10:03 +01:00
|
|
|
self.dmp.dmp_start_pos = start_pos.reshape((1, num_dof))
|
2021-01-11 16:08:42 +01:00
|
|
|
|
|
|
|
dmp_weights = np.zeros((num_basis, num_dof))
|
2021-02-05 17:10:03 +01:00
|
|
|
if learn_goal:
|
|
|
|
dmp_goal_pos = np.zeros(num_dof)
|
|
|
|
else:
|
|
|
|
dmp_goal_pos = final_pos
|
2021-01-11 16:08:42 +01:00
|
|
|
|
|
|
|
self.dmp.set_weights(dmp_weights, dmp_goal_pos)
|
2021-02-11 10:49:57 +01:00
|
|
|
self.weights_scale = weights_scale
|
2021-03-19 16:31:46 +01:00
|
|
|
self.goal_scale = goal_scale
|
2021-01-11 16:08:42 +01:00
|
|
|
|
2021-02-11 10:49:57 +01:00
|
|
|
policy_class = get_policy_class(policy_type)
|
|
|
|
self.policy = policy_class(env)
|
2021-02-05 17:10:03 +01:00
|
|
|
|
2021-02-11 10:49:57 +01:00
|
|
|
def __call__(self, params, contexts=None):
|
2021-01-12 10:52:08 +01:00
|
|
|
params = np.atleast_2d(params)
|
|
|
|
rewards = []
|
|
|
|
infos = []
|
2021-02-11 10:49:57 +01:00
|
|
|
for p, c in zip(params, contexts):
|
2021-02-11 12:32:32 +01:00
|
|
|
reward, info = self.rollout(p, c)
|
2021-01-12 10:52:08 +01:00
|
|
|
rewards.append(reward)
|
|
|
|
infos.append(info)
|
|
|
|
|
2021-02-05 17:10:03 +01:00
|
|
|
return np.array(rewards), infos
|
2021-01-12 10:52:08 +01:00
|
|
|
|
2021-01-11 16:08:42 +01:00
|
|
|
def goal_and_weights(self, params):
|
|
|
|
if len(params.shape) > 1:
|
|
|
|
assert params.shape[1] == self.dim
|
|
|
|
else:
|
|
|
|
assert len(params) == self.dim
|
|
|
|
params = np.reshape(params, [1, self.dim])
|
|
|
|
|
|
|
|
if self.learn_goal:
|
|
|
|
goal_pos = params[0, -self.num_dof:]
|
|
|
|
weight_matrix = np.reshape(params[:, :-self.num_dof], [self.num_basis, self.num_dof])
|
|
|
|
else:
|
2021-03-19 16:31:46 +01:00
|
|
|
goal_pos = self.dmp.dmp_goal_pos.flatten()
|
|
|
|
assert goal_pos is not None
|
2021-01-11 16:08:42 +01:00
|
|
|
weight_matrix = np.reshape(params, [self.num_basis, self.num_dof])
|
|
|
|
|
2021-03-19 16:31:46 +01:00
|
|
|
return goal_pos * self.goal_scale, weight_matrix * self.weights_scale
|
2021-01-11 16:08:42 +01:00
|
|
|
|
2021-02-11 10:49:57 +01:00
|
|
|
def rollout(self, params, context=None, render=False):
|
2021-01-12 10:52:08 +01:00
|
|
|
""" This function generates a trajectory based on a DMP and then does the usual loop over reset and step"""
|
2021-02-11 10:49:57 +01:00
|
|
|
goal_pos, weight_matrix = self.goal_and_weights(params)
|
2021-02-05 17:10:03 +01:00
|
|
|
self.dmp.set_weights(weight_matrix, goal_pos)
|
|
|
|
trajectory, velocity = self.dmp.reference_trajectory(self.t)
|
|
|
|
|
|
|
|
if self.post_traj_steps > 0:
|
|
|
|
trajectory = np.vstack([trajectory, np.tile(trajectory[-1, :], [self.post_traj_steps, 1])])
|
|
|
|
velocity = np.vstack([velocity, np.zeros(shape=(self.post_traj_steps, self.num_dof))])
|
|
|
|
|
|
|
|
self._trajectory = trajectory
|
|
|
|
self._velocity = velocity
|
|
|
|
|
|
|
|
rews = []
|
|
|
|
infos = []
|
|
|
|
|
2021-02-11 12:32:32 +01:00
|
|
|
self.env.configure(context)
|
2021-02-05 17:10:03 +01:00
|
|
|
self.env.reset()
|
|
|
|
|
|
|
|
for t, pos_vel in enumerate(zip(trajectory, velocity)):
|
2021-02-11 10:49:57 +01:00
|
|
|
ac = self.policy.get_action(pos_vel[0], pos_vel[1])
|
2021-02-05 17:10:03 +01:00
|
|
|
obs, rew, done, info = self.env.step(ac)
|
|
|
|
rews.append(rew)
|
|
|
|
infos.append(info)
|
|
|
|
if render:
|
|
|
|
self.env.render(mode="human")
|
|
|
|
if done:
|
|
|
|
break
|
|
|
|
|
|
|
|
reward = np.sum(rews)
|
|
|
|
|
2021-02-11 12:32:32 +01:00
|
|
|
return reward, info
|