fancy_gym/alr_envs/classic_control/hole_reacher.py

326 lines
12 KiB
Python
Raw Normal View History

2021-01-11 16:08:42 +01:00
import gym
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import patches
from alr_envs import DmpWrapper
from alr_envs.utils.wrapper.detpmp_wrapper import DetPMPWrapper
2021-01-11 16:08:42 +01:00
def ccw(A, B, C):
return (C[1] - A[1]) * (B[0] - A[0]) - (B[1] - A[1]) * (C[0] - A[0]) > 1e-12
2021-01-11 16:08:42 +01:00
# Return true if line segments AB and CD intersect
def intersect(A, B, C, D):
return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D)
def holereacher_dmp(**kwargs):
_env = gym.make("alr_envs:HoleReacher-v0")
# _env = HoleReacher(**kwargs)
return DmpWrapper(_env, num_dof=5, num_basis=5, duration=2, dt=_env.dt, learn_goal=True, alpha_phase=3.5,
start_pos=_env.start_pos, policy_type="velocity", weights_scale=100, goal_scale=0.1)
def holereacher_fix_goal_dmp(**kwargs):
_env = gym.make("alr_envs:HoleReacher-v0")
# _env = HoleReacher(**kwargs)
return DmpWrapper(_env, num_dof=5, num_basis=5, duration=2, dt=_env.dt, learn_goal=False, alpha_phase=3.5,
start_pos=_env.start_pos, policy_type="velocity", weights_scale=50, goal_scale=1,
final_pos=np.array([2.02669572, -1.25966385, -1.51618198, -0.80946476, 0.02012344]))
def holereacher_detpmp(**kwargs):
_env = gym.make("alr_envs:HoleReacher-v0")
# _env = HoleReacher(**kwargs)
return DetPMPWrapper(_env, num_dof=5, num_basis=5, width=0.005, policy_type="velocity", start_pos=_env.start_pos,
duration=2, post_traj_time=0, dt=_env.dt, weights_scale=0.25, zero_start=True, zero_goal=False)
2021-01-11 16:08:42 +01:00
class HoleReacher(gym.Env):
def __init__(self, n_links, hole_x, hole_width, hole_depth, allow_self_collision=False,
2021-01-11 16:08:42 +01:00
allow_wall_collision=False, collision_penalty=1000):
self.n_links = n_links
self.link_lengths = np.ones((n_links, 1))
# task
2021-01-11 16:08:42 +01:00
self.hole_x = hole_x # x-position of center of hole
self.hole_width = hole_width # width of hole
self.hole_depth = hole_depth # depth of hole
2021-01-11 16:08:42 +01:00
self.bottom_center_of_hole = np.hstack([hole_x, -hole_depth])
self.top_center_of_hole = np.hstack([hole_x, 0])
self.left_wall_edge = np.hstack([hole_x - self.hole_width / 2, 0])
2021-01-11 16:08:42 +01:00
self.right_wall_edge = np.hstack([hole_x + self.hole_width / 2, 0])
# collision
2021-01-11 16:08:42 +01:00
self.allow_self_collision = allow_self_collision
self.allow_wall_collision = allow_wall_collision
self.collision_penalty = collision_penalty
# state
2021-01-11 16:08:42 +01:00
self._joints = None
self._joint_angles = None
self._angle_velocity = None
self.start_pos = np.hstack([[np.pi / 2], np.zeros(self.n_links - 1)])
self.start_vel = np.zeros(self.n_links)
2021-01-11 16:08:42 +01:00
2021-02-11 12:32:32 +01:00
self.dt = 0.01
# self.time_limit = 2
2021-01-11 16:08:42 +01:00
action_bound = np.pi * np.ones((self.n_links,))
2021-01-11 16:08:42 +01:00
state_bound = np.hstack([
[np.pi] * self.n_links, # cos
[np.pi] * self.n_links, # sin
[np.inf] * self.n_links, # velocity
2021-01-11 16:08:42 +01:00
[np.inf] * 2, # x-y coordinates of target distance
[np.inf] # env steps, because reward start after n steps TODO: Maybe
])
self.action_space = gym.spaces.Box(low=-action_bound, high=action_bound, shape=action_bound.shape)
self.observation_space = gym.spaces.Box(low=-state_bound, high=state_bound, shape=state_bound.shape)
self.fig = None
rect_1 = patches.Rectangle((-self.n_links, -1),
self.n_links + self.hole_x - self.hole_width / 2, 1,
2021-01-11 16:08:42 +01:00
fill=True, edgecolor='k', facecolor='k')
rect_2 = patches.Rectangle((self.hole_x + self.hole_width / 2, -1),
self.n_links - self.hole_x + self.hole_width / 2, 1,
2021-01-11 16:08:42 +01:00
fill=True, edgecolor='k', facecolor='k')
rect_3 = patches.Rectangle((self.hole_x - self.hole_width / 2, -1), self.hole_width,
1 - self.hole_depth,
fill=True, edgecolor='k', facecolor='k')
self.patches = [rect_1, rect_2, rect_3]
@property
def end_effector(self):
return self._joints[self.n_links].T
2021-01-11 16:08:42 +01:00
2021-02-17 17:48:05 +01:00
def configure(self, context):
pass
2021-01-12 10:52:08 +01:00
def reset(self):
self._joint_angles = self.start_pos
self._angle_velocity = self.start_vel
self._joints = np.zeros((self.n_links + 1, 2))
2021-01-12 10:52:08 +01:00
self._update_joints()
self._steps = 0
return self._get_obs().copy()
2021-01-11 16:08:42 +01:00
def step(self, action: np.ndarray):
2021-01-12 10:52:08 +01:00
"""
a single step with an action in joint velocity space
"""
2021-03-19 16:31:46 +01:00
vel = action # + 0.01 * np.random.randn(self.num_links)
2021-02-11 12:32:32 +01:00
acc = (vel - self._angle_velocity) / self.dt
2021-01-11 16:08:42 +01:00
self._angle_velocity = vel
2021-02-11 12:32:32 +01:00
self._joint_angles = self._joint_angles + self.dt * self._angle_velocity
2021-01-11 16:08:42 +01:00
self._update_joints()
2021-03-22 15:28:50 +01:00
reward = 0
if self._is_collided:
reward = -self.collision_penalty
else:
2021-03-19 16:31:46 +01:00
if self._steps == 199:
2021-03-22 15:28:50 +01:00
reward = - np.linalg.norm(self.end_effector - self.bottom_center_of_hole) ** 2
2021-01-11 16:08:42 +01:00
2021-03-22 15:28:50 +01:00
reward -= 5e-8 * np.sum(acc ** 2)
2021-01-14 17:10:03 +01:00
info = {"is_collided": self._is_collided}
2021-01-11 16:08:42 +01:00
self._steps += 1
# done = self._steps * self.dt > self.time_limit or self._is_collided
done = self._is_collided
2021-01-15 17:16:52 +01:00
return self._get_obs().copy(), reward, done, info
2021-01-11 16:08:42 +01:00
def _update_joints(self):
"""
update _joints to get new end effector position. The other links are only required for rendering.
Returns:
"""
line_points_in_taskspace = self.get_forward_kinematics(num_points_per_link=20)
self._joints[1:, 0] = self._joints[0, 0] + line_points_in_taskspace[:, -1, 0]
self._joints[1:, 1] = self._joints[0, 1] + line_points_in_taskspace[:, -1, 1]
self_collision = False
wall_collision = False
if not self.allow_self_collision:
self_collision = self.check_self_collision(line_points_in_taskspace)
if np.any(np.abs(self._joint_angles) > np.pi) and not self.allow_self_collision:
self_collision = True
if not self.allow_wall_collision:
wall_collision = self.check_wall_collision(line_points_in_taskspace)
self._is_collided = self_collision or wall_collision
def _get_obs(self):
theta = self._joint_angles
return np.hstack([
np.cos(theta),
np.sin(theta),
self._angle_velocity,
self.end_effector - self.bottom_center_of_hole,
self._steps
])
def get_forward_kinematics(self, num_points_per_link=1):
theta = self._joint_angles[:, None]
if num_points_per_link > 1:
intermediate_points = np.linspace(0, 1, num_points_per_link)
else:
intermediate_points = 1
accumulated_theta = np.cumsum(theta, axis=0)
endeffector = np.zeros(shape=(self.n_links, num_points_per_link, 2))
2021-01-11 16:08:42 +01:00
x = np.cos(accumulated_theta) * self.link_lengths * intermediate_points
y = np.sin(accumulated_theta) * self.link_lengths * intermediate_points
endeffector[0, :, 0] = x[0, :]
endeffector[0, :, 1] = y[0, :]
for i in range(1, self.n_links):
2021-01-11 16:08:42 +01:00
endeffector[i, :, 0] = x[i, :] + endeffector[i - 1, -1, 0]
endeffector[i, :, 1] = y[i, :] + endeffector[i - 1, -1, 1]
return np.squeeze(endeffector + self._joints[0, :])
def check_self_collision(self, line_points):
for i, line1 in enumerate(line_points):
for line2 in line_points[i + 2:, :, :]:
2021-01-11 16:08:42 +01:00
# if line1 != line2:
if intersect(line1[0], line1[-1], line2[0], line2[-1]):
return True
return False
def check_wall_collision(self, line_points):
# all points that are before the hole in x
r, c = np.where(line_points[:, :, 0] < (self.hole_x - self.hole_width / 2))
# check if any of those points are below surface
nr_line_points_below_surface_before_hole = np.sum(line_points[r, c, 1] < 0)
if nr_line_points_below_surface_before_hole > 0:
return True
# all points that are after the hole in x
r, c = np.where(line_points[:, :, 0] > (self.hole_x + self.hole_width / 2))
# check if any of those points are below surface
nr_line_points_below_surface_after_hole = np.sum(line_points[r, c, 1] < 0)
if nr_line_points_below_surface_after_hole > 0:
return True
# all points that are above the hole
r, c = np.where((line_points[:, :, 0] > (self.hole_x - self.hole_width / 2)) & (
line_points[:, :, 0] < (self.hole_x + self.hole_width / 2)))
2021-01-11 16:08:42 +01:00
# check if any of those points are below surface
nr_line_points_below_surface_in_hole = np.sum(line_points[r, c, 1] < -self.hole_depth)
if nr_line_points_below_surface_in_hole > 0:
return True
return False
def render(self, mode='human'):
if self.fig is None:
self.fig = plt.figure()
2021-01-12 10:52:08 +01:00
# plt.ion()
# plt.pause(0.01)
2021-01-11 16:08:42 +01:00
else:
plt.figure(self.fig.number)
if mode == "human":
plt.cla()
plt.title(f"Iteration: {self._steps}, distance: {self.end_effector - self.bottom_center_of_hole}")
# Arm
plt.plot(self._joints[:, 0], self._joints[:, 1], 'ro-', markerfacecolor='k')
# Add the patch to the Axes
[plt.gca().add_patch(rect) for rect in self.patches]
lim = np.sum(self.link_lengths) + 0.5
plt.xlim([-lim, lim])
plt.ylim([-1.1, lim])
# plt.draw()
plt.pause(1e-4) # pushes window to foreground, which is annoying.
2021-01-11 16:08:42 +01:00
# self.fig.canvas.flush_events()
2021-01-12 10:52:08 +01:00
elif mode == "partial":
if self._steps == 1:
# fig, ax = plt.subplots()
2021-01-11 16:08:42 +01:00
# Add the patch to the Axes
[plt.gca().add_patch(rect) for rect in self.patches]
2021-01-12 10:52:08 +01:00
# plt.pause(0.01)
2021-01-11 16:08:42 +01:00
2021-01-12 10:52:08 +01:00
if self._steps % 20 == 0 or self._steps in [1, 199] or self._is_collided:
# Arm
plt.plot(self._joints[:, 0], self._joints[:, 1], 'ro-', markerfacecolor='k', alpha=self._steps / 200)
# ax.plot(line_points_in_taskspace[:, 0, 0],
# line_points_in_taskspace[:, 0, 1],
# line_points_in_taskspace[:, -1, 0],
# line_points_in_taskspace[:, -1, 1], marker='o', color='k', alpha=t / 200)
lim = np.sum(self.link_lengths) + 0.5
plt.xlim([-lim, lim])
plt.ylim([-1.1, lim])
2021-01-11 16:08:42 +01:00
plt.pause(0.01)
2021-01-12 10:52:08 +01:00
elif mode == "final":
if self._steps == 199 or self._is_collided:
# fig, ax = plt.subplots()
2021-01-11 16:08:42 +01:00
# Add the patch to the Axes
[plt.gca().add_patch(rect) for rect in self.patches]
plt.xlim(-self.n_links, self.n_links), plt.ylim(-1, self.n_links)
2021-01-11 16:08:42 +01:00
# Arm
plt.plot(self._joints[:, 0], self._joints[:, 1], 'ro-', markerfacecolor='k')
plt.pause(0.01)
2021-01-14 17:10:03 +01:00
def close(self):
if self.fig is not None:
plt.close(self.fig)
2021-01-11 16:08:42 +01:00
if __name__ == '__main__':
nl = 5
2021-01-12 10:52:08 +01:00
render_mode = "human" # "human" or "partial" or "final"
env = HoleReacher(n_links=nl, allow_self_collision=False, allow_wall_collision=False, hole_width=0.15,
hole_depth=1, hole_x=1)
2021-01-11 16:08:42 +01:00
env.reset()
2021-01-12 10:52:08 +01:00
# env.render(mode=render_mode)
2021-01-11 16:08:42 +01:00
for i in range(200):
# objective.load_result("/tmp/cma")
2021-01-12 10:52:08 +01:00
# test with random actions
ac = 2 * env.action_space.sample()
2021-01-11 16:08:42 +01:00
# ac[0] += np.pi/2
2021-01-15 17:16:52 +01:00
obs, rew, d, info = env.step(ac)
2021-01-12 10:52:08 +01:00
env.render(mode=render_mode)
2021-01-11 16:08:42 +01:00
print(rew)
2021-01-15 17:16:52 +01:00
if d:
2021-01-11 16:08:42 +01:00
break
2021-01-14 17:10:03 +01:00
env.close()