fancy_gym/alr_envs/examples/pd_control_gain_tuning.py

101 lines
2.8 KiB
Python
Raw Normal View History

import numpy as np
from matplotlib import pyplot as plt
2021-08-20 14:23:33 +02:00
from alr_envs import dmc, meta
2022-01-25 15:23:57 +01:00
from alr_envs.alr import mujoco
2021-11-30 16:11:32 +01:00
from alr_envs.utils.make_env_helpers import make_promp_env
2022-01-25 15:23:57 +01:00
def visualize(env):
t = env.t
pos_features = env.mp.basis_generator.basis(t)
plt.plot(t, pos_features)
plt.show()
2022-04-07 14:40:43 +02:00
# This might work for some environments, however, please verify either way the correct trajectory information
# for your environment are extracted below
2022-01-25 15:23:57 +01:00
SEED = 1
# env_id = "ball_in_cup-catch"
env_id = "ALRReacherSparse-v0"
2022-04-07 14:40:43 +02:00
env_id = "button-press-v2"
2022-01-25 15:23:57 +01:00
wrappers = [mujoco.reacher.MPWrapper]
2022-04-07 14:40:43 +02:00
wrappers = [meta.goal_object_change_mp_wrapper.MPWrapper]
mp_kwargs = {
2022-04-07 14:40:43 +02:00
"num_dof": 4,
"num_basis": 5,
"duration": 6.25,
"policy_type": "metaworld",
"weights_scale": 10,
"zero_start": True,
2022-04-07 14:40:43 +02:00
# "policy_kwargs": {
# "p_gains": 1,
# "d_gains": 0.1
# }
}
2022-01-25 15:23:57 +01:00
# kwargs = dict(time_limit=4, episode_length=200)
kwargs = {}
2022-01-25 15:23:57 +01:00
env = make_promp_env(env_id, wrappers, seed=SEED, mp_kwargs=mp_kwargs, **kwargs)
2022-04-07 14:40:43 +02:00
env.action_space.seed(SEED)
# Plot difference between real trajectory and target MP trajectory
env.reset()
2022-04-07 14:40:43 +02:00
w = env.action_space.sample() # N(0,1)
2022-01-25 15:23:57 +01:00
visualize(env)
pos, vel = env.mp_rollout(w)
base_shape = env.full_action_space.shape
actual_pos = np.zeros((len(pos), *base_shape))
actual_vel = np.zeros((len(pos), *base_shape))
act = np.zeros((len(pos), *base_shape))
2022-04-07 14:40:43 +02:00
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
img = ax.imshow(env.env.render("rgb_array"))
fig.show()
for t, pos_vel in enumerate(zip(pos, vel)):
actions = env.policy.get_action(pos_vel[0], pos_vel[1])
actions = np.clip(actions, env.full_action_space.low, env.full_action_space.high)
_, _, _, _ = env.env.step(actions)
2022-04-07 14:40:43 +02:00
if t % 15 == 0:
img.set_data(env.env.render("rgb_array"))
fig.canvas.draw()
fig.canvas.flush_events()
act[t, :] = actions
# TODO verify for your environment
actual_pos[t, :] = env.current_pos
2022-04-07 14:40:43 +02:00
actual_vel[t, :] = 0 # env.current_vel
plt.figure(figsize=(15, 5))
plt.subplot(131)
plt.title("Position")
2022-01-25 15:23:57 +01:00
p1 = plt.plot(actual_pos, c='C0', label="true")
# plt.plot(actual_pos_ball, label="true pos ball")
2022-01-25 15:23:57 +01:00
p2 = plt.plot(pos, c='C1', label="MP") # , label=["MP" if i == 0 else None for i in range(np.prod(base_shape))])
plt.xlabel("Episode steps")
2022-01-25 15:23:57 +01:00
# plt.legend()
handles, labels = plt.gca().get_legend_handles_labels()
from collections import OrderedDict
by_label = OrderedDict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
plt.subplot(132)
plt.title("Velocity")
2022-01-25 15:23:57 +01:00
plt.plot(actual_vel, c='C0', label="true")
plt.plot(vel, c='C1', label="MP")
plt.xlabel("Episode steps")
plt.subplot(133)
2022-04-07 14:40:43 +02:00
plt.title(f"Actions {np.std(act, axis=0)}")
plt.plot(act, c="C0"), # label=[f"actions" if i == 0 else "" for i in range(np.prod(base_action_shape))])
plt.xlabel("Episode steps")
# plt.legend()
plt.show()