-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmjc_yumi_peg_exp.py
189 lines (157 loc) · 4.61 KB
/
mjc_yumi_peg_exp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'''
mujo agent for model learnig research
'''
import numpy as np
import matplotlib.pyplot as plt
import logging
import imp
import os
import os.path
import sys
import pickle
from mjc_exp_policy import Policy
from YumiKinematics import YumiKinematics
from mjc_exp_policy import exp_params_rob, kin_params
# Add gps/python to path so that imports work.
sys.path.append('/home/shahbaz/Research/Software/Spyder_ws/gps/python')
# from gps.gui.gps_training_gui import GPSTrainingGUI
# from gps.utility.data_logger import DataLogger
from gps.utility.data_logger import DataLogger
from gps.agent.mjc.agent_mjc import AgentMuJoCo
from gps.sample.sample_list import SampleList
from gps.proto.gps_pb2 import JOINT_ANGLES, JOINT_VELOCITIES, \
END_EFFECTOR_POINTS, END_EFFECTOR_POINT_VELOCITIES, ACTION
SENSOR_DIMS = {
JOINT_ANGLES: 7,
JOINT_VELOCITIES: 7,
END_EFFECTOR_POINTS: 3,
END_EFFECTOR_POINT_VELOCITIES: 3,
ACTION: 7,
}
common = {
'conditions': 1,
}
yumiKin = YumiKinematics(kin_params)
agent_hyperparams = {
'type': AgentMuJoCo,
# 'filename': '/home/shahbaz/Research/Software/Spyder_ws/gps/mjc_models/yumi_mjcf_l_peg_model_learning.xml',
# 'filename': '/home/shahbaz/Research/Software/Spyder_ws/gps/mjc_models/yumi_model_learning_blocks_2.xml',
'filename': '/home/shahbaz/Research/Software/Spyder_ws/gps/mjc_models/yumi_model_learning_blocks.xml',
'x0': exp_params_rob['x0'],
'x0var': exp_params_rob['x0var'],
'dt': exp_params_rob['dt'],
'substeps': 5,
'conditions': common['conditions'],
'pos_body_idx': np.array([1]),
'pos_body_offset': [[np.array([0, 0, 0])]],
'T': exp_params_rob['T'],
'sensor_dims': SENSOR_DIMS,
'state_include': [JOINT_ANGLES, JOINT_VELOCITIES],
'obs_include': [],
'camera_pos': np.array([5.0, 0.5, 3., 0., 0., 0.]),
'smooth_noise': False,
'smooth_noise_var': 1.,
'smooth_noise_renormalize': False,
}
num_samples = exp_params_rob['num_samples']
cond = 0
dU = SENSOR_DIMS[ACTION]
noise_gain = exp_params_rob['noise_gain']
noise_mean = np.zeros(dU)
noise_std = np.ones(dU)*noise_gain
mean_action = exp_params_rob['mean_action']
T = exp_params_rob['T']
dt = exp_params_rob['dt']
dP = exp_params_rob['dP']
dV = exp_params_rob['dV']
dU = exp_params_rob['dU']
mjc_agent = AgentMuJoCo(agent_hyperparams)
for i in range(num_samples):
ref_x_traj = []
curr_x_traj = []
ref_q_traj = []
pol = Policy(agent_hyperparams, exp_params_rob)
mjc_agent.sample(
pol, cond,
verbose=True,
noisy=True
)
ref_x_traj, curr_x_traj, ref_q_traj = pol.get_traj_data()
del pol
SampleList = mjc_agent.get_samples(cond, -num_samples)
mjc_agent.clear_samples()
ref_x_traj = np.array(ref_x_traj)
curr_x_traj = np.array(curr_x_traj)
ref_q_traj = np.array(ref_q_traj)
Xs = SampleList.get_X()
Us = SampleList.get_U()
# plot samples for sample 0
sample = SampleList[-1]
Js = sample.get(JOINT_ANGLES)
J_dots = sample.get(JOINT_VELOCITIES)
# Es = sample.get(END_EFFECTOR_POINTS)
# E_dots = sample.get(END_EFFECTOR_POINT_VELOCITIES)
Tqs = sample.get(ACTION)
plt.figure()
tm = np.linspace(0,T*dt,T)
#jPos
for j in range(7):
plt.subplot(5,7,1+j)
plt.title('j%dPos' %(j+1))
plt.plot(tm,Js[:,j],color='g')
plt.plot(tm,ref_q_traj[:,j])
#jVel
for j in range(7):
plt.subplot(5,7,8+j)
plt.title('j%dVel' %(j+1))
plt.plot(tm,J_dots[:,j],color='b')
#jTrq
for j in range(7):
plt.subplot(5,7,15+j)
plt.title('j%dTrq' %(j+1))
plt.plot(tm,Tqs[:,j],color='r')
# EE
for j in range(6):
plt.subplot(5,7,22+j)
plt.title('EE%dPos' %(j+1))
plt.plot(tm,ref_x_traj[:,j])
plt.plot(tm,curr_x_traj[:,j])
plt.show()
Xs = np.array(Xs)
Us = np.array(Us)
# dP = 7
# dV = 7
# dU = 7
N, T, dX = Xs.shape
#
# assert(Xs.shape[2]==(dP+dV))
# assert(dP==dV)
# Qts = Xs[:, :, :dP]
# Qts_d = Xs[:, :, dP:dP+dV]
# Uts = Us
# Ets = np.zeros((N, T, 6))
# Ets_d = np.zeros((N, T, 6))
# Fts = np.zeros((N, T, 6))
# for n in range(N):
# for i in range(T):
# Ets[n, i] = yumiKin.fwd_pose(Qts[n,i])
# J_A = yumiKin.get_analytical_jacobian(Qts[n,i])
# Ets_d[n,i] = J_A.dot(Qts_d[n,i])
# Fts[n,i] = np.linalg.pinv(J_A.T).dot(Uts[n,i])
#
# EXts = np.concatenate((Ets,Ets_d),axis=2)
exp_params = {}
exp_params['dP'] = 7
exp_params['dV'] = 7
exp_params['dU'] = 7
exp_params['dt'] = 0.05
exp_params['T'] = T
exp_params['num_samples'] = N
exp_data={}
exp_data['exp_params'] = exp_params
exp_data['X'] = Xs
exp_data['U'] = Us
# exp_data['EX'] = EXts
# exp_data['F'] = Fts
# raw_input()
# pickle.dump( exp_data, open( "./Results/mjc_exp_2_sec_raw.p", "wb" ) )