-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatter_plotter_paper.py
97 lines (66 loc) · 2.59 KB
/
scatter_plotter_paper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os,sys
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import gym
from gym_foo import gym_foo
import ppo
import pickle
from utils.utils import *
import argparse
if __name__ == "__main__":
np.random.seed(2019)
parser = argparse.ArgumentParser()
parser.add_argument("--gym_env", help="which gym environment to use.", type=str, default='DubinsCarEnv-v0')
parser.add_argument("--reward_type", help="which type of reward to use.", type=str, default='hand_craft')
parser.add_argument("--load_dir", type=str, default=None)
parser.add_argument("--load_iter", type=int, default=1)
args = parser.parse_args()
TRAJ_DIR = os.path.join(os.getcwd(), 'trajs_icra',
(args.load_dir).split('/')[-2] + '_' + 'iter_' + str(args.load_iter))
maybe_mkdir(TRAJ_DIR)
# Initialize env
env = gym.make(args.gym_env)
env.reward_type = args.reward_type
env.set_additional_goal = 'angle'
# Initialize policy
ppo.create_session()
init_policy = ppo.create_policy('pi', env)
ppo.initialize()
pi = init_policy
pi.load_model(args.load_dir, iteration=args.load_iter)
_, _, eval_ep_mean_reward, eval_suc_percent, trajs, dones = ppo.ppo_eval(env, pi, timesteps_per_actorbatch = 128,
max_iters=5, stochastic=False, scatter_collect=True)
# save the traj data to pkl
with open(TRAJ_DIR + '/trajs.pkl', 'wb') as f1:
pickle.dump(trajs, f1)
with open(TRAJ_DIR + '/dones.pkl', 'wb') as f2:
pickle.dump(dones, f2)
print(trajs)
print(dones)
trajs = np.concatenate(trajs, axis=0)
print("total steps:", np.shape(trajs))
fig = plt.figure()
if args.gym_env == 'DubinsCarEnv-v0':
xys = trajs[:, :2]
ax = fig.add_subplot(111)
ax.set_title("2d histagram for simple car task")
ax.set_xlabel('X pos')
ax.set_ylabel('Y pos')
ax.hist2d(xys[:, 0], xys[:, 1], bins=100)
elif args.gym_env == 'PlanarQuadEnv-v0':
xs = trajs[:, 0]
zs = trajs[:, 2]
ts = trajs[:, 4]
print("len of xs:", np.shape(xs))
print("len of zs:", np.shape(zs))
print("len of ts:", np.shape(ts))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, zs, ts, marker="o", color="r")
ax.set_title("3D scatter plot for quadrotor task")
ax.set_xlabel("X pos")
ax.set_ylabel("Z pos")
ax.set_zlabel("Pitch angle")
else:
raise ValueError("invalid env name!!")
plt.show()