Repository navigation
Expand file tree
/
Copy pathoptimization.py
More file actions
666 lines (523 loc) · 19.7 KB
/
Copy pathoptimization.py
File metadata and controls
666 lines (523 loc) · 19.7 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 06:16:46 2019
@author: ckielasjensen
"""
import matplotlib.pyplot as plt
import numba
from numba import njit, jit
import numpy as np
import bezier as bez
DEG_ELEV = 0
class BezOptimization:
def __init__(self,
numVeh=1,
dimension=1,
degree=5,
minimizeGoal='Euclidean',
maxSep=0.9,
minSpeed=0,
maxSpeed=1e6,
maxAngRate=1e6,
initPoints=None,
finalPoints=None,
initSpeeds=None,
finalSpeeds=None,
initAngs=None,
finalAngs=None,
tf=1.0,
pointObstacles=None,
shapeObstacles=None):
self.pointObstacles = pointObstacles
self.shapeObstacles = shapeObstacles
self._numCols = degree+1
if initPoints is not None:
self._numCols -= 2
if initSpeeds is not None:
self._numCols -= 2
self.model = {'numVeh': numVeh,
'dim': dimension,
'deg': degree,
'minGoal': minimizeGoal,
'maxSep': maxSep,
'minSpeed': minSpeed,
'maxSpeed': maxSpeed,
'maxAngRate': maxAngRate,
'initPoints': np.atleast_2d(initPoints),
'finalPoints': np.atleast_2d(finalPoints),
'initSpeeds': np.atleast_1d(initSpeeds),
'finalSpeeds': np.atleast_1d(finalSpeeds),
'initAngs': np.atleast_1d(initAngs),
'finalAngs': np.atleast_1d(finalAngs),
'tf': tf}
@property
def objectiveFunction(self):
minGoal = self.model['minGoal'].lower()
objectivesDict = {'euclidean': self.euclideanObjective,
'timeopt': lambda x: x[-1],
'accel': self.accelObjective,
'jerk': self.jerkObjective,
}
try:
return objectivesDict[minGoal]
except KeyError:
err = ('The provided minimize goal, {}, is not a valid goal. '
'The available minimize goals are:\n{}'
).format(minGoal, objectivesDict.keys())
raise ValueError(err)
@property
def temporalSeparationConstraints(self):
if self.pointObstacles is not None:
numObs = self.model['numVeh'] + len(self.pointObstacles)
obstacleList = []
for obstacle in self.pointObstacles:
for d in range(self.model['dim']):
obstacleList.append([obstacle[d]]*(self.model['deg']+1))
def wrapper(x):
y = np.vstack((self.reshapeVector(x), obstacleList))
return _temporalSeparationConstraints(y,
numObs,
self.model['dim'],
self.model['maxSep'])
else:
def wrapper(x):
y = self.reshapeVector(x)
return _temporalSeparationConstraints(y,
self.model['numVeh'],
self.model['dim'],
self.model['maxSep'])
return wrapper
def spatialSeparationConstraints(self, x):
"""
"""
numVeh = self.model['numVeh']
dim = self.model['dim']
maxSep = self.model['maxSep']
numObs = numVeh + len(self.shapeObstacles)
distances = []
vehList = []
y = self.reshapeVector(x)
for i in range(numVeh):
vehList.append(bez.Bezier(y[i*dim:(i+1)*dim, :]))
for obstacle in self.shapeObstacles:
vehList.append(obstacle)
for i in range(numObs):
for j in range(i, numObs):
if j > i:
distances.append(vehList[i].minDist(vehList[j]))
return np.array(distances)-maxSep
@property
def minSpeedConstraints(self):
"""
"""
def wrapper(x):
if self.model['minGoal'].lower() == 'timeopt':
tf = x[-1]
else:
tf = self.model['tf']
y = self.reshapeVector(x)
return _minSpeedConstraints(y,
self.model['numVeh'],
self.model['dim'],
tf,
self.model['minSpeed']
)
return wrapper
@property
def maxSpeedConstraints(self):
"""
"""
def wrapper(x):
if self.model['minGoal'].lower() == 'timeopt':
tf = x[-1]
else:
tf = self.model['tf']
y = self.reshapeVector(x)
return _maxSpeedConstraints(y,
self.model['numVeh'],
self.model['dim'],
tf,
self.model['maxSpeed']
)
return wrapper
@property
def maxAngularRateConstraints(self):
"""
"""
def wrapper(x):
if self.model['minGoal'].lower() == 'timeopt':
tf = x[-1]
else:
tf = self.model['tf']
y = self.reshapeVector(x)
return _maxAngularRateConstraints(y,
self.model['numVeh'],
self.model['dim'],
tf,
self.model['maxAngRate']
)
return wrapper
def generateGuess(self, std=0, seed=None):
"""
"""
dim = self.model['dim']
deg = self.model['deg']
numVeh = self.model['numVeh']
tf = self.model['tf']
initPoints = self.model['initPoints']
finalPoints = self.model['finalPoints']
initSpeeds = self.model['initSpeeds']
finalSpeeds = self.model['finalSpeeds']
initAngs = self.model['initAngs']
finalAngs = self.model['finalAngs']
np.random.seed(seed)
xGuess = []
for i in range(numVeh):
for j in range(dim):
if initSpeeds[0] is None:
line = np.linspace(initPoints[i, j],
finalPoints[i, j],
deg+1)
line += np.random.randn(deg+1)*std
else:
if dim != 2:
err = ('The dimension must be 2 for initial and final '
'speeds and angles.')
raise ValueError(err)
initMag = initSpeeds[i]*tf/deg
finalMag = finalSpeeds[i]*tf/deg
if j % 2 == 0:
initPt = initPoints[i, j] + initMag*np.cos(initAngs[i])
finalPt = (finalPoints[i, j] -
finalMag*np.cos(finalAngs[i]))
else:
initPt = initPoints[i, j] + initMag*np.sin(initAngs[i])
finalPt = (finalPoints[i, j] -
finalMag*np.sin(finalAngs[i]))
line = np.linspace(initPt, finalPt, deg+1-2)
line += np.random.randn(deg+1-2)*std
xGuess.append(line[1:-1])
if self.model['minGoal'].lower() == 'timeopt':
xGuess.append([tf])
return np.concatenate(xGuess)
def reshapeVector(self, x):
"""
"""
dim = self.model['dim']
deg = self.model['deg']
numVeh = self.model['numVeh']
tf = self.model['tf']
initPoints = self.model['initPoints']
finalPoints = self.model['finalPoints']
initSpeeds = self.model['initSpeeds']
finalSpeeds = self.model['finalSpeeds']
initAngs = self.model['initAngs']
finalAngs = self.model['finalAngs']
numCols = self._numCols
numRows = dim*self.model['numVeh']
# If we are optimizing time, grab the last element which is tf
if self.model['minGoal'].lower() == 'timeopt':
tf = x[-1]
x = x[:-1]
y = np.empty((dim*numVeh, deg+1))
offset = 0
if initPoints is not None:
offset += 1
for i in range(initPoints.shape[0]):
y[i*dim:(i+1)*dim, 0] = initPoints[i]
y[i*dim:(i+1)*dim, -1] = finalPoints[i]
if initSpeeds[0] is not None:
offset += 1
initMag = initSpeeds*tf/deg
finalMag = finalSpeeds*tf/deg
y[::2, 1] = initPoints[:, 0] + initMag*np.cos(initAngs) # X
y[1::2, 1] = initPoints[:, 1] + initMag*np.sin(initAngs) # Y
y[::2, -2] = finalPoints[:, 0] - finalMag*np.cos(finalAngs) # X
y[1::2, -2] = finalPoints[:, 1] - finalMag*np.sin(finalAngs) # Y
y[:, offset:-offset] = x.reshape((numRows, numCols))
return y
def euclideanObjective(self, x):
"""
"""
return _euclideanObjective(self.reshapeVector(x),
self.model['numVeh'],
self.model['dim'])
def accelObjective(self, x):
"""
"""
return _minAccelObjective(self.reshapeVector(x),
self.model['numVeh'],
self.model['dim'],
self.model['tf'])
def jerkObjective(self, x):
"""
"""
return _minJerkObjective(self.reshapeVector(x),
self.model['numVeh'],
self.model['dim'],
self.model['tf'])
def _temporalSeparationConstraints(y, nVeh, dim, maxSep):
"""Calculate the separation between vehicles.
The maximum separation is found by degree elevation.
NOTE: This only works for 2 dimensions.
:param x: Optimization vector
:type x: numpy.ndarray
:param nVeh: Number of vehicles
:type nVeh: int
:param dim: Dimension of the vehicles. Currently only works for 2D
:type dim: int
:param maxSep: Maximum separation between vehicles.
:type maxSep: float
"""
if nVeh > 1:
distVeh = []
vehList = []
for i in range(nVeh):
vehList.append(bez.Bezier(y[i*dim:(i+1)*dim, :]))
for i in range(nVeh-1):
for j in range(i+1, nVeh):
dv = vehList[i] - vehList[j]
distVeh.append(dv.normSquare().elev(DEG_ELEV))
# distVeh.append(dv.normSquare().min())
distances = np.concatenate([i.cpts.squeeze() for i in distVeh])
# distances = np.array(distVeh)
return (distances - maxSep**2).squeeze()
else:
return None
def _minSpeedConstraints(y, nVeh, dim, tf, minSpeed):
"""Creates the minimum velocity constraints.
Useful in systems such as aircraft who may not fall below a certain speed.
:param x: Optimization vector
:type x: numpy.ndarray
:param nVeh: Number of vehicles
:type nVeh: int
:param dim: Dimension of the vehicles. Currently only works for 2D.
:type dim: int
:param model: See model description in BezOptimization class description.
:type model: dict
:param minSpeed: Minimum speed of the vehicle.
:type minSpeed: float
:return: Inequality constraint for the minimum speed.
:rtype: float
"""
# y = reshapeVector(x, nVeh, dim, model)
# if model['type'].lower() == 'timeopt':
# tf = x[-1]
# else:
# tf = model['tf']
speeds = []
for i in range(nVeh):
pos = bez.Bezier(y[i*dim:i*dim+dim, :], tf=tf)
speed = pos.diff()
speeds.append(speed)
speedSqr = [curve.normSquare().elev(DEG_ELEV) for curve in speeds]
speeds = np.concatenate([i.cpts.squeeze() for i in speedSqr])
return (speeds - minSpeed**2).squeeze()
def _maxSpeedConstraints(y, nVeh, dim, tf, maxSpeed):
"""Creates the maximum velocity constraints.
Useful for limiting the maximum speed of a vehicle.
:param x: Optimization vector
:type x: numpy.ndarray
:param nVeh: Number of vehicles
:type nVeh: int
:param dim: Dimension of the vehicles. Currently only works for 2D
:type dim: int
:param model: See model description in BezOptimization class description
:type model: dict
:param maxSpeed: Maximum speed of the vehicle.
:type maxSpeed: float
:return: Inequality constraint for the maximum speed
:rtype: float
"""
# y = reshapeVector(x, nVeh, dim, model)
# if model['type'].lower() == 'timeopt':
# tf = x[-1]
# else:
# tf = model['tf']
speeds = []
for i in range(nVeh):
pos = bez.Bezier(y[i*dim:i*dim+dim, :], tf=tf)
speed = pos.diff()
speeds.append(speed)
speedSqr = [curve.normSquare().elev(DEG_ELEV) for curve in speeds]
speeds = np.concatenate([i.cpts.squeeze() for i in speedSqr])
return (maxSpeed**2 - speeds).squeeze()
def _maxAngularRateConstraints(y, nVeh, dim, tf, maxAngRate):
"""Creates the maximum angular rate constraint.
This is useful for a dubins car model that has a constraint on the maximum
angular rate. The dimension is assumed to be 2.
:param x: Optimization vector
:type x: numpy.ndarray
:param nVeh: Number of vehicles
:type nVeh: int
:param dim: Dimension of the vehicles. Currently only works for 2D
:type dim: int
:param model: See model description in BezOptimization class description
:type model: dict
:param maxAngRate: Maximum angular rate of the vehicle (in radians).
:type maxAngRate: float
:return: Inequality constraint for the maximum angular rate
:rtype: float
"""
# y = reshapeVector(x, nVeh, dim, model)
# if model['type'].lower() == 'timeopt':
# tf = x[-1]
# else:
# tf = model['tf']
angularRates = []
for i in range(nVeh):
pos = bez.Bezier(y[i*dim:i*dim+dim, :], tf=tf)
angRate = _angularRateSqr(pos.elev(DEG_ELEV))
angularRates.append(angRate)
angularRateCpts = np.concatenate(
[i.cpts.squeeze() for i in angularRates])
return (maxAngRate**2 - angularRateCpts).squeeze()
@njit(cache=True)
def _euclideanObjective(y, nVeh, dim):
"""Sums the Euclidean distance between control points.
The Euclidean difference between each neighboring pair of control points is
summed for each vehicle.
:param y: Optimized vector that has been reshaped using the reshapeVector
function.
:type y: numpy.ndarray
:param nVeh: Number of vehicles
:type nVeh: int
:param dim: Dimension of the vehicles. Currently only works for 2D
:type dim: int
:return: Sum of the Euclidean distances
:rtype: float
"""
summation = 0.0
temp = np.empty(3)
length = y.shape[1]
for veh in range(nVeh):
for i in range(length-1):
for j in range(dim):
temp[j] = y[veh*dim+j, i+1] - y[veh*dim+j, i]
summation += _norm(temp) # np.linalg.norm(temp)
return summation
@njit(cache=True)
def _norm(x):
"""
"""
summation = 0.
for val in x:
summation += val*val
return np.sqrt(summation)
def _minAccelObjective(y, nVeh, dim, tf):
"""
"""
curves = []
for i in range(nVeh):
pos = bez.Bezier(y[i*dim:i*dim+dim, :], tf=tf)
vel = pos.diff()
accel = vel.diff()
curves.append(accel)
summation = 0.0
for curve in curves:
temp = curve.normSquare().elev(DEG_ELEV)
summation = summation + temp.cpts.sum()
return summation
@njit(cache=True)
def _minJerkObjective(y, nVeh, dim, tf):
"""
"""
curves = []
for i in range(nVeh):
pos = bez.Bezier(y[i*dim:i*dim+dim, :], tf=tf)
vel = pos.diff()
jerk = vel.diff().diff()
curves.append(jerk)
summation = 0.0
for curve in curves:
temp = curve.normSquare().elev(DEG_ELEV)
summation = summation + temp.cpts.sum()
return summation
@jit(cache=True)
def _angularRate(bezTraj):
"""
Finds the angular rate of the 2D Bezier Curve.
The equation for the angular rate is as follows:
psiDot = (yDdot*xDot - xDdot*yDot) / (xDot^2 + yDot^2)
Note the second derivative (Ddot) vs the first (Dot)
RETURNS:
RationalBezier - This function returns a rational Bezier curve because
we must divide two Bezier curves.
"""
if bezTraj.dim != 2:
msg = ('The input curve must be two dimensional,\n'
'instead it is {} dimensional'.format(bezTraj.dim))
raise ValueError(msg)
x = bezTraj.x
xDot = x.diff()
xDdot = xDot.diff()
y = bezTraj.y
yDot = y.diff()
yDdot = yDot.diff()
numerator = yDdot*xDot - xDdot*yDot
denominator = xDot*xDot + yDot*yDot
cpts = numerator.cpts / (denominator.cpts)
weights = denominator.cpts
return bez.RationalBezier(cpts, weights)
#@jit(cache=True)
def _angularRateSqr(bezTraj):
"""
Finds the squared angular rate of the 2D Bezier Curve.
The equation for the angular rate is as follows:
psiDot = ((yDdot*xDot - xDdot*yDot))^2 / (xDot^2 + yDot^2)^2
Note the second derivative (Ddot) vs the first (Dot)
RETURNS:
RationalBezier - This function returns a rational Bezier curve because
we must divide two Bezier curves.
"""
if bezTraj.dim != 2:
msg = ('The input curve must be two dimensional,\n'
'instead it is {} dimensional'.format(bezTraj.dim))
raise ValueError(msg)
x = bezTraj.x
xDot = x.diff()
xDdot = xDot.diff()
y = bezTraj.y
yDot = y.diff()
yDdot = yDot.diff()
numerator = yDdot*xDot - xDdot*yDot
numerator = numerator*numerator
denominator = xDot*xDot + yDot*yDot
denominator = denominator*denominator
cpts = numerator.cpts / (denominator.cpts)
weights = denominator.cpts
return bez.RationalBezier(cpts, weights)
if __name__ == '__main__':
numVeh = 2
dim = 2
deg = 5
xLen = numVeh*dim*(deg+1-4)
params = {'numVeh': numVeh, 'dimension': dim, 'degree': deg,
'initPoints': np.array([[1, 2], [3, 4]]),
'finalPoints': np.array([[5, 6], [7, 8]]),
'initSpeeds': np.array([3, 3]),
'finalSpeeds': np.array([10, 10]),
'initAngs': np.array([np.pi/2, np.pi/2]),
'finalAngs': np.array([0, 0]),
'pointObstacles': [[1, 2], [3, 4]]
}
bezopt = BezOptimization(**params)
xGuess = bezopt.generateGuess()
x = np.random.randint(0, 10, xLen)
cpts1 = bezopt.reshapeVector(x)[:2, :]
cpts2 = bezopt.reshapeVector(x)[2:, :]
print('cpts1:\n{},\ncpts2:\n{}'.format(cpts1, cpts2))
print('X Guess:\n{}'.format(xGuess))
c1guess = bez.Bezier(bezopt.reshapeVector(xGuess)[:2, :])
c2guess = bez.Bezier(bezopt.reshapeVector(xGuess)[2:, :])
plt.close('all')
ax1 = c1guess.plot()
c2guess.plot(ax1)
plt.title('X Guess')
c1 = bez.Bezier(cpts1)
c2 = bez.Bezier(cpts2)
ax2 = c1.plot()
c2.plot(ax2)
plt.title('Reshape Vector')
plt.show()