-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_failed.py
270 lines (224 loc) · 10.3 KB
/
test_failed.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import numpy as np
import casadi as cs
import adam_model
import parser
import copy
import random
import pickle
import datetime
import os
import tqdm
class NaiveOCP:
""" Define OCP problem and solver (IpOpt) """
def __init__(self, model, n_steps,obstacles=None):
self.params = model.params
self.model = model
self.nq = model.nq
self.obstacles = obstacles
N = self.params.N
opti = cs.Opti()
x_init = opti.parameter(model.nx)
cost = 0
# Define decision variables
X, U = [], []
X += [opti.variable(model.nx)]
for k in range(n_steps):
X += [opti.variable(model.nx)]
opti.subject_to(opti.bounded(model.x_min, X[-1], model.x_max))
U += [opti.variable(model.nu)]
opti.subject_to(X[0] == x_init)
Q = 1e2 * np.eye(3)
R = 5e-3 * np.eye(self.model.nu)
ee_ref = model.ee_ref
for k in range(n_steps+1):
ee_pos = model.ee_fun(X[k])
cost += (ee_pos - ee_ref).T @ Q @ (ee_pos - ee_ref)
if k < n_steps:
cost += U[k].T @ R @ U[k]
# Dynamics constraint
opti.subject_to(X[k + 1] == model.f_fun(X[k], U[k]))
# Torque constraints
opti.subject_to(opti.bounded(model.tau_min, model.tau_fun(X[k], U[k]), model.tau_max))
# if obstacles != None:
# for i in obstacles:
# opti.subject_to(opti.bounded(i['lb'],ee_pos[i['axis']],i['ub']))
if obstacles != None:
if len(obstacles['walls']):
for i in obstacles['walls']:
opti.subject_to(opti.bounded(i['lb'],ee_pos[i['axis']],i['ub']))
if len(obstacles['objects']):
for i in obstacles['objects']:
opti.subject_to(cs.dot((ee_pos-i['position']),(ee_pos-i['position']))>i['radius']**2)
self.opti = opti
self.X = X
self.U = U
self.x_init = x_init
self.cost = cost
#self.dist_b = dist_b
self.additionalSetting()
opti.minimize(cost)
def additionalSetting(self):
pass
def checkCollision(self, x):
if self.obstacles is not None and self.params.obs_flag:
t_glob = self.model.jointToEE(x)
for obs in self.obstacles:
if obs['name'] == 'floor':
if t_glob[2] + self.params.tol_obs < obs['bounds'][0]:
return False
elif obs['name'] == 'ball':
dist_b = np.sum((t_glob.flatten() - obs['position']) ** 2)
if dist_b + self.params.tol_obs < obs['bounds'][0]:
return False
return True
def instantiateProblem(self):
opti = self.opti
opts = {
'ipopt.print_level': 0,
'print_time': 0,
'ipopt.tol': 1e-6,
'ipopt.constr_viol_tol': 1e-6,
'ipopt.compl_inf_tol': 1e-6,
#'ipopt.hessian_approximation': 'limited-memory',
# 'detect_simple_bounds': 'yes',
'ipopt.max_iter': 1000,
#'ipopt.linear_solver': 'ma57',
'ipopt.sb': 'no'
}
opti.solver('ipopt', opts)
return opti
class AccBoundsOCP(NaiveOCP):
def __init__(self, model, n_steps, ddq_max, obstacles=None):
self.ddq_max = ddq_max
super().__init__(model, n_steps, obstacles)
def additionalSetting(self):
nq = self.model.nq
# dq_min = - self.X[-1][nq:] ** 2 / ddq_max + self.X[-1][:nq]
# dq_max = self.X[-1][nq:] ** 2 / ddq_max + self.X[-1][:nq]
dq_min = -cs.sqrt(2*ddq_max[:nq]*(self.X[-1][:nq]-self.model.x_min[:nq])+regularization_sqrt)
dq_max = cs.sqrt(2*ddq_max[:nq]*(self.model.x_max[:nq]-self.X[-1][:nq])+regularization_sqrt)
self.opti.set_initial(self.X[-1],x0)
# self.opti.subject_to(cs.fmax(dq_min,cs.MX.ones(nq,1)*regularization_sqrt) <= self.X[-1][nq:])
# self.opti.subject_to(cs.fmax(dq_max,cs.MX.ones(nq,1)*regularization_sqrt) >= self.X[-1][nq:])
self.opti.subject_to(dq_min <= self.X[-1][nq:])
self.opti.subject_to(dq_max >= self.X[-1][nq:])
if self.obstacles != None:
if len(self.obstacles['walls'])>0:
for i in self.obstacles['walls']:
distance= i['pos'] - robot.ee_fun(self.X[-1])[i['axis']]
dx_max = cs.sqrt(2*ddx_max[i['axis']]*cs.fabs(distance)) # [i['axis']]
self.opti.subject_to(((robot.jac(np.eye(4),self.X[-1][:nq])[:3,6:]@self.X[-1][nq:])[i['axis']])*cs.sign(distance) <= dx_max)
#dx_max = cs.sqrt(2*ddx_max[i['axis']]*cs.fabs(robot.ee_fun(self.X[-1])[i['axis']]- i['pos'])) # [i['axis']]
#self.opti.subject_to(self.opti.bounded(-dx_max, (robot.jac(np.eye(4),self.X[-1][:nq])[:3,6:]@self.X[-1][nq:]) [i['axis']], dx_max))
if len(self.obstacles['objects'])>0:
for i in self.obstacles['objects']:
dist_vec_end = (i['position'])-robot.ee_fun(self.X[-1])
dx_max = cs.sqrt(2*ddx_max*cs.fabs(dist_vec_end))
self.opti.subject_to(cs.dot((robot.jac(np.eye(4),self.X[-1][:nq])[:3,6:]@self.X[-1][nq:]),dist_vec_end/cs.norm_2(dist_vec_end))<=cs.norm_2(dx_max))
#self.opti.subject_to(self.opti.bounded(-dx_max,robot.jac(np.eye(4),self.X[-1][:robot.nq])[:3,6:]@self.X[-1][robot.nq:], dx_max))
self.opti.cost = 0
def check_cartesian_constraint(state, obstacles):
if obstacles != None:
check = True
if len(obstacles['walls'])>0:
for i in obstacles['walls']:
check = check and (i['lb'] <= robot.ee_fun(state)[i['axis']] <= i['ub'])
distance = i['pos'] -robot.ee_fun(state)[i['axis']]
dx_max = np.sqrt(2*ddx_max[i['axis']]*np.abs(distance))
check = check and (((robot.jac(np.eye(4),state[:robot.nq])[:3,6:]@state[robot.nq:])[i['axis']])*np.sign(distance) <= dx_max)
if len(obstacles['objects'])>0:
for i in obstacles['objects']:
dist_vec = np.array(i['position'])-robot.ee_fun(state)
check = check and (np.linalg.norm(dist_vec)>i['radius'])
dx_max = np.sqrt(2*np.multiply(ddx_max,cs.fabs(dist_vec)))
check = check and cs.dot((robot.jac(np.eye(4),state[:robot.nq])[:3,6:]@state[robot.nq:]),dist_vec/cs.norm_2(dist_vec))<= np.linalg.norm(dx_max)
#check = check and np.array(- dx_max <= robot.jac(np.eye(4),state[:robot.nq])[:3,6:]@state[robot.nq:]).all() \
#and np.array(robot.jac(np.eye(4),state[:robot.nq])[:3,6:]@state[robot.nq:]<= dx_max).all()
return check
else:
return True
def load(filename):
with open(filename, 'rb') as file:
loaded_data = pickle.load(file)
return loaded_data
def perturbate_init(state,perturbation):
state[:robot.nq] = state[:robot.nq] + perturbation*np.sign(np.random.rand(robot.nq))
return state
if __name__ == "__main__":
now = datetime.datetime.now()
saving_date = str(datetime.datetime.now())
np.random.seed(now.microsecond*now.second+now.minute)
params = parser.Parameters('fr3')
robot = adam_model.AdamModel(params,n_dofs=6)
if robot.params.urdf_name == 'fr3':
robot.tau_max = np.array([17*1.5,87,8.7*2.2,34.8,4.8,4.8])
robot.tau_min = -robot.tau_max
# robot.tau_max = robot.tau_max/5
# robot.tau_min = robot.tau_min/5
robot.tau_max = robot.tau_max[:robot.nq]
robot.tau_min = robot.tau_min[:robot.nq]
# ddq_max = np.array([0.3,3,5,7,7,7])/3
ddq_max = np.array([0.17226047, 0.3566412 , 0.27797771, 0.48120222, 1.15786895,
2.47659424])
ddq_max = ddq_max[:robot.nq]
ddx_max = np.array([0.1, 0.1, 0.05])
regularization_sqrt = 1e-4*0
ee_radius = 0.075
walls = [
{'axis':2, 'lb':0+ee_radius, 'ub':1e6, 'pos':0+ee_radius},
# {'axis':2, 'lb':-1e6, 'ub':0.5, 'pos':0.5},
# {'axis':0, 'lb':-1e6, 'ub':0.5, 'pos':0.5},
# {'axis':0, 'lb':-0.5, 'ub':1e5, 'pos':-0.5}
]
#walls = None
# objects modeled as spheres
objects = [
{'position':[0.6, 0., 0.12], 'radius':0.12+ee_radius}
# {'position':[0.,0.3,0.15],'radius':0.05},
# {'position':[0.3,-0.35,0.25], 'radius':0.1}
]
obstacles = {'walls':walls,'objects':objects}
#obstacles = None
n_samples=10000
max_n_steps = 300*3
x0_successes = []
x0_failed = []
print(f'joint bounds = {robot.x_min} , {robot.x_max} ')
data_folder = os.path.join(os.getcwd(),'N-steps results')
x0_s = load(os.path.join(data_folder,'x0_failed2025-01-13 09:00:07.809522.pkl'))
progress_bar = tqdm.tqdm(total=len(x0_s), desc='Progress')
for k in range(0,len(x0_s)):
for j in range(1):
x0=perturbate_init(x0_s[k],1e-4*j)
print(f'{k} : {x0_s[k]}')
horizon = 100
while horizon <= max_n_steps:
ocp_form= AccBoundsOCP(robot,horizon,ddq_max,obstacles)
ocp = ocp_form.instantiateProblem()
ocp.set_value(ocp_form.x_init, x0)
try:
sol = ocp.solve()
x0_successes.append([x0,horizon])
print(f"Returned in {horizon} steps")
break
except:
print(robot.ee_fun(x0))
#ocp.debug.g_describe(0)
#ocp.debug.show_infeasibilities()
print(f"Failed in {horizon} steps")
if horizon >= max_n_steps:
x0_failed.append(x0)
print(x0)
if horizon >= 100: horizon +=300
else: horizon += 25
progress_bar.update(1)
progress_bar.close()
print(len(x0_successes))
folder = os.getcwd()+'/N-steps-failed-tested'
if not os.path.exists(folder):
os.makedirs(folder)
print(f'Failures: {len(x0_failed)}/{len(x0_s)}')
with open(folder + '/x0_solved' + saving_date + '.pkl', 'wb') as file:
pickle.dump(x0_successes, file)
with open(folder + '/x0_failed' + saving_date + '.pkl', 'wb') as file:
pickle.dump(x0_failed, file)