-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpiecewise_jerk_optimize.py
More file actions
262 lines (208 loc) · 7.81 KB
/
piecewise_jerk_optimize.py
File metadata and controls
262 lines (208 loc) · 7.81 KB
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
import math
import numpy
import osqp
from scipy import sparse
from matplotlib import pyplot as plt
class PieceJerkOptimize:
def __init__(self, num_of_point):
self.num_of_point = num_of_point
self.num_of_variable = 3 * num_of_point
self.w_x = 0.0
self.w_dx = 0.0
self.w_ddx = 0.0
self.w_dddx = 0.0
self.w_ref_x = 0.0
self.step = []
self.step_square = []
self.ref_x = []
self.init_state = []
self.end_state = []
self.x_upper_bound = []
self.x_lower_bound = []
self.dx_upper_bound = []
self.dx_lower_bound = []
self.ddx_upper_bound = []
self.ddx_lower_bound = []
self.dddx_upper_bound = []
self.dddx_lower_bound = []
self.solution_x = []
self.solution_dx = []
self.solution_ddx = []
self.solution_dddx = []
pass
def SetWeight(self, w_x, w_dx,w_ddx, w_dddx, w_ref_x):
self.w_x = w_x
self.w_dx = w_dx
self.w_ddx = w_ddx
self.w_dddx = w_dddx
self.w_ref_x = w_ref_x
def SetReferenceX(self, ref_x):
self.ref_x = ref_x
def SetStep(self, step):
self.step = step
self.step_square = [x for x in self.step]
def SetXBound(self, upper_bound, lower_bound):
self.x_lower_bound = lower_bound
self.x_upper_bound = upper_bound
def SetDXBound(self, upper_bound, lower_bound):
self.dx_lower_bound = lower_bound
self.dx_upper_bound = upper_bound
def SetDDXBound(self, upper_bound, lower_bound):
self.ddx_lower_bound = lower_bound
self.ddx_upper_bound = upper_bound
def SetDDDXBound(self, upper_bound, lower_bound):
self.dddx_lower_bound = lower_bound
self.dddx_upper_bound = upper_bound
def SetInitState(self, init_state):
self.init_state = init_state
def SetEndState(self, end_state):
self.end_state = end_state
def CalculateQ(self):
row = []
col = []
data = []
return sparse.csc_matrix((data, (row, col)), shape=(
self.num_of_variable, self.num_of_variable))
def CalculateP(self):
P = []
for i in range(self.num_of_point):
P.append(-2.0 * self.ref_x[i] * self.w_ref_x)
for i in range(2 * self.num_of_point):
P.append(0.0)
P = numpy.array(P)
return P
def CalculateAffineConstraint(self):
row = []
col = []
data = []
# x constrait.
for i in range(self.num_of_point):
row.append(i)
col.append(i)
data.append(1.0)
# dx constraint.
for i in range(self.num_of_point):
row.append(self.num_of_point + i)
col.append(self.num_of_point + i)
data.append(1)
# ddx constraint.
for i in range(self.num_of_point):
row.append(2 * self.num_of_point + i)
col.append(2 * self.num_of_point + i)
data.append(1)
# dddx constraint.
for i in range(self.num_of_point - 1):
row.append(3 * self.num_of_point + i)
col.append(2 * self.num_of_point + i)
data.append(-1.0)
row.append(3 * self.num_of_point + i)
col.append(2 * self.num_of_point + i + 1)
data.append(1.0)
# start/end x limit
row.append(4 * self.num_of_point - 1)
col.append(0)
data.append(1)
row.append(4 * self.num_of_point)
col.append(self.num_of_point - 1)
data.append(1)
# start dx limit
row.append(4 * self.num_of_point + 1)
col.append(self.num_of_point)
data.append(1)
# start ddx limit
row.append(4 * self.num_of_point + 2)
col.append(2 * self.num_of_point)
data.append(1)
# ddx consistency
for i in range(self.num_of_point - 1):
row.append(4 * self.num_of_point + 3 + i)
col.append(self.num_of_point + i)
data.append(1)
row.append(4 * self.num_of_point + 3 + i)
col.append(self.num_of_point + i + 1)
data.append(-1)
row.append(4 * self.num_of_point + 3 + i)
col.append(2 * self.num_of_point + i)
data.append(0.5 * self.step[i])
row.append(4 * self.num_of_point + 3 + i)
col.append(2 * self.num_of_point + i + 1)
data.append(0.5 * self.step[i])
# dx consistency
for i in range(self.num_of_point - 1):
row.append(5 * self.num_of_point + 2 + i)
col.append(i)
data.append(1)
row.append(5 * self.num_of_point + 2 + i)
col.append(i + 1)
data.append(-1)
row.append(5 * self.num_of_point + 2 + i)
col.append(self.num_of_point + i)
data.append(self.step[i])
row.append(5 * self.num_of_point + 2 + i)
col.append(self.num_of_point + i + 1)
data.append(self.step[i])
row.append(5 * self.num_of_point + 2 + i)
col.append(2 * self.num_of_point + i)
data.append(1.0/3.0 * self.step_square[i])
row.append(5 * self.num_of_point + 2 + i)
col.append(2 * self.num_of_point + i + 1)
data.append(1.0/6.0 * self.step_square[i])
A = sparse.csc_matrix((data, (row, col)), shape=(
6 * self.num_of_point + 1, self.num_of_variable))
lb = []
ub = []
# x bound
for i in range(self.num_of_point):
lb.append(self.x_lower_bound[i])
ub.append(self.x_upper_bound[i])
# dx bound
for i in range(self.num_of_point):
lb.append(self.dx_lower_bound[i])
ub.append(self.dx_upper_bound[i])
# ddx bound
for i in range(self.num_of_point):
lb.append(self.ddx_lower_bound[i])
ub.append(self.ddx_upper_bound[i])
# dddx bound
for i in range(self.num_of_point - 1):
lb.append(self.dddx_lower_bound[i] * self.step[i])
ub.append(self.dddx_upper_bound[i] * self.step[i])
# start/end x
lb.append(self.init_state[0] - 1e-2)
ub.append(self.init_state[0] + 1e-2)
lb.append(self.end_state[0] - 1e-2)
ub.append(self.end_state[0] + 1e-2)
# start dx
lb.append(self.init_state[1] - 1e-2)
ub.append(self.init_state[1] + 1e-2)
# start ddx
lb.append(self.init_state[2] - 1e-2)
ub.append(self.init_state[2] + 1e-2)
## dx consistency
for i in range(self.num_of_point - 1):
lb.append(-10e-5)
ub.append(10e-5)
## x consistency
for i in range(self.num_of_point - 1):
lb.append(-10e-5)
ub.append(10e-5)
numpy.set_printoptions(linewidth=numpy.inf)
return A,lb,ub
def Optimize(self):
Q = self.CalculateQ()
P = self.CalculateP()
A, lb, ub = self.CalculateAffineConstraint()
prob = osqp.OSQP()
prob.setup(Q, P, A, lb, ub, polish=True, eps_abs=1e-5, eps_rel=1e-5,
eps_prim_inf=1e-5, eps_dual_inf=1e-5, verbose=True)
var_warm_start = numpy.array(self.ref_x + [0.0 for n in range(2 * self.num_of_point)])
prob.warm_start(x = var_warm_start)
res = prob.solve()
self.solution_x = res.x[0:self.num_of_point]
self.solution_dx = res.x[self.num_of_point:2 *self.num_of_point]
self.solution_ddx = res.x[2 *self.num_of_point:3 * self.num_of_point]
for i in range(self.num_of_point - 1):
self.solution_dddx.append((self.solution_ddx[i+1] - self.solution_ddx[i])/self.step[i])
self.solution_dddx.append(0.0)
def VizResult(self):
pass