-
Notifications
You must be signed in to change notification settings - Fork 12
/
builder.py
656 lines (467 loc) · 18.7 KB
/
builder.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
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
655
656
import casadi as cs
from .sx_container import SXContainer
from .spatialmath import arrayify_args
from .optimization import *
class OptimizationBuilder:
"""
OptimizationBuilder allows you to build/specify an optimization problem.
"""
def __init__(self, T, robots=[], tasks=[], optimize_time=False, derivs_align=False):
"""OptimizationBuilder constructor.
Syntax
------
builder = optas.OptimizationBuilder(T, robots, tasks, optimize_time, derivs_align)
Parameters
----------
T (int):
Number of time steps for the trajectory.
robots (list):
A list of robot models.
tasks (list):
A list of task models.
optimize_time (bool):
When true, a trajectory of dt variables are included in the
decision variables. Default is False.
derivs_align (bool):
When true, the time derivatives align for each time
step. Default is False.
"""
# Input check
assert T > 0, f"T must be strictly positive"
if not isinstance(robots, list):
robots = [robots] # allow user to pass a single robot
if not isinstance(tasks, list):
tasks = [tasks] # all user to pass a single task
# Class attributes
self.T = T
self._models = robots + tasks
self.optimize_time = optimize_time
self.derivs_align = derivs_align
# Ensure T is sufficiently large
if not derivs_align:
# Get max time deriv
all_time_derivs = []
for m in self._models:
all_time_derivs += m.time_derivs
max_time_deriv = max(all_time_derivs)
# Check T is large enough
Tmin = max_time_deriv+1
assert T >= Tmin, f"{T=} is too low, it should be at least {Tmin}"
model_names = [m.get_name() for m in self._models]
is_unique_names = len(model_names) == len(set(model_names))
assert is_unique_names, "each model should have a unique name"
# Setup decision variables
self._decision_variables = SXContainer()
for model in self._models:
for d in model.time_derivs:
n = model.state_name(d)
if model.T is None:
t = T-d if not derivs_align else T
else:
t = model.T
self.add_decision_variables(n, model.dim, t)
if optimize_time:
self.add_decision_variables('dt', T-1)
# Setup containers for parameters, cost terms, ineq/eq constraints
self._parameters = SXContainer()
self._cost_terms = SXContainer()
self._lin_eq_constraints = SXContainer()
self._lin_ineq_constraints = SXContainer()
self._ineq_constraints = SXContainer()
self._eq_constraints = SXContainer()
def get_model_names(self):
"""Return the names of each model."""
return [model.name for model in self._models]
def get_model_index(self, name):
"""Return the index of the model in the list of models.
Syntax
------
idx = builder.get_model_index(name)
Parameters
----------
name (string)
Name of the model.
Returns
-------
idx (int)
Index of the model in the list of models.
"""
return self.get_model_names().index(name)
def get_model(self, name):
"""Return the model with given name.
Syntax
------
model = builder.get_model(name)
Parameters
----------
name (string)
Name of the model.
Returns
-------
model (optas.models.Model)
A task or robot model.
"""
return self._models[self.get_model_index(name)]
def get_model_state(self, name, t, time_deriv=0):
"""Get the model state at a given time.
Syntax
------
state = builder.get_model_state(name, t, time_deriv=0)
Parameters
----------
name (string)
Name of the model.
t (int)
Index of the desired state.
time_deriv (int)
The time-deriviative required (i.e. position is 0, velocity is 1, etc.)
Returns
-------
state (casadi.SX, with shape dim-by-1)
The state vector where dim is the model dimension.
"""
states = self.get_model_states(name, time_deriv)
return states[:, t]
def get_model_states(self, name, time_deriv=0):
"""Get the full state trajectory for a given model.
Syntax
------
states = builder.get_model_states(name, time_deriv=0)
Parameters
----------
name (string)
Name of the model.
time_deriv (int)
The time-deriviative required (i.e. position is 0, velocity is 1, etc.)
Returns
-------
states (casadi.SX, with shape dim-by-T)
The state vector where dim is the model dimension, and T is the number of time-steps in the trajectory.
"""
model = self.get_model(name)
assert time_deriv in model.time_derivs, f"model '{name}', was not specified with time derivative to order {time_deriv}"
name = model.state_name(time_deriv)
return self._decision_variables[name]
def get_dt(self):
"""When optimizing time, then this method returns the trajectory of dt variables."""
assert self.optimize_time, "to call get_dt(..), optimize_time should be True in the OptimizationBuilder interface"
return self._decision_variables['dt']
def _x(self):
"""Return the decision variables as a casadi.SX vector."""
return self._decision_variables.vec()
def _p(self):
"""Return the parameters as a casadi.SX vector."""
return self._parameters.vec()
def _is_linear(self, y):
"""Returns true if y is a linear function of the decision variables."""
return cs.is_linear(y, self._x())
def _cost(self):
"""Returns the cost function."""
return cs.sum1(self._cost_terms.vec())
def is_cost_quadratic(self):
"""True when cost function is quadratic in the decision variables, False otherwise."""
return cs.is_quadratic(self._cost(), self._x())
#
# Upate optimization problem
#
def add_decision_variables(self, name, m=1, n=1, is_discrete=False):
"""Add decision variables to the optimization problem.
Syntax
------
d = builder.add_decision_variables(name, m=1, n=1, is_discrete=False)
Parameters
----------
name (string)
Name of decision variable array.
m (int)
Number of rows in decision variable array.
n (int)
Number of columns in decision variable array.
is_discret (bool)
If true, then the decision variables are treated as discrete variables.
Return
------
d (casadi.SX)
Array of the decision variables.
"""
x = cs.SX.sym(name, m, n)
self._decision_variables[name] = x
if is_discrete:
self._decision_variables.variable_is_discrete(name)
return x
def add_parameter(self, name, m=1, n=1):
"""Add a parameter to the optimization problem.
Syntax
------
p = builder.add_parameter(name, m=1, n=1)
Parameters
----------
name (string)
Name of parameter array.
m (int)
Number of rows in parameter array.
n (int)
Number of columns in parameter array.
Return
------
p (casadi.SX)
Array of the parameters.
"""
p = cs.SX.sym(name, m, n)
self._parameters[name] = p
return p
@arrayify_args
def add_cost_term(self, name, cost_term):
"""Add cost term to the optimization problem.
Syntax
------
builder.add_cost_term(name, cost_term)
Parameters
----------
name (string)
Name for cost function.
cost_term (casadi.SX)
Cost term, must be an array with shape 1-by-1.
"""
cost_term = cs.vec(cost_term)
m, n = cost_term.shape
assert m==1 and n==1, "cost term must be scalar"
self._cost_terms[name] = cost_term
@arrayify_args
def add_geq_inequality_constraint(self, name, lhs, rhs=None):
"""Add the inequality constraint lhs >= rhs to the optimization problem.
Syntax
------
builder.add_geq_inequality_constraint(name, lhs, rhs=None)
Parameters
----------
name (string)
Name for the constraint.
lhs (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Left-hand side for the inequality constraint.
rhs (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Right-hand side for the inequality constraint. If None, then it is replaced with the zero array with the same shape as lhs.
"""
if rhs is None:
rhs = cs.DM.zeros(*lhs.shape)
self.add_leq_inequality_constraint(name, rhs, lhs)
@arrayify_args
def add_leq_inequality_constraint(self, name, lhs, rhs=None):
"""Add the inequality constraint lhs <= rhs to the optimization problem.
Syntax
------
builder.add_leq_inequality_constraint(name, lhs, rhs=None)
Parameters
----------
name (string)
Name for the constraint.
lhs (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Left-hand side for the inequality constraint.
rhs (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Right-hand side for the inequality constraint. If None, then it is replaced with the zero array with the same shape as lhs.
"""
if rhs is None:
rhs = cs.DM.zeros(*lhs.shape)
diff = rhs - lhs # diff >= 0
if self._is_linear(diff):
self._lin_ineq_constraints[name] = diff
else:
self._ineq_constraints[name] = diff
@arrayify_args
def add_bound_inequality_constraint(self, name, lhs, mid, rhs):
"""Add the inequality constraint lhs <= mid <= rhs to the optimization problem.
Syntax
------
builder.add_bound_inequality_constraint(name, lhs, mid, rhs)
Parameters
----------
name (string)
Name for the constraint.
lhs (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Left-hand side for the inequality constraint.
mid (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Middle part of the inequality constraint.
rhs (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Right-hand side for the inequality constraint.
"""
self.add_leq_inequality_constraint(name+'_l', lhs, mid)
self.add_leq_inequality_constraint(name+'_r', mid, rhs)
@arrayify_args
def add_equality_constraint(self, name, lhs, rhs=None):
"""Add the equality constraint lhs == rhs to the optimization problem.
Syntax
------
builder.add_equality_constraint(name, lhs, rhs=None)
Parameters
----------
name (string)
Name for the constraint.
lhs (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Left-hand side for the inequality constraint.
rhs (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Right-hand side for the inequality constraint. If None, then it is replaced with the zero array with the same shape as lhs.
"""
if rhs is None:
rhs = cs.DM.zeros(*lhs.shape)
diff = rhs - lhs # diff == 0
if self._is_linear(diff):
self._lin_eq_constraints[name] = diff
else:
self._eq_constraints[name] = diff
#
# Common constraints
#
def ensure_positive_dt(self):
"""Specifies the constraint dt >= 0 when optimize_time=True."""
assert self.optimize_time, "optimize_time should be True in the OptimizationBuilder interface"
self.add_geq_inequality_constraint('__ensure_positive_dt__', self.get_dt())
def _integr(self, m, n):
"""Returns an integration function where m is the state dimension, and n is the number of trajectory points."""
xd = cs.SX.sym('xd', m)
x0 = cs.SX.sym('x0', m)
x1 = cs.SX.sym('x1', m)
dt = cs.SX.sym('dt')
integr = cs.Function('integr', [x0, x1, xd, dt], [x0 + dt*xd - x1])
return integr.map(n)
def integrate_model_states(self, name, time_deriv, dt=None):
"""Integrates the model states over time.
Syntax
------
builder.integrate_model_states(name, time_deriv, dt=None)
Parameters
----------
name (string)
Name of the model.
time_deriv (int)
The time-deriviative required (i.e. position is 0, velocity is 1, etc.).
dt (None, float, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Integration time step.
"""
if self.optimize_time and dt is not None:
raise ValueError("dt is given but user specified optimize_time as True")
if not self.optimize_time and dt is None:
raise ValueError("dt is not given")
model = self.get_model(name)
xd = self.get_model_states(name, time_deriv)
x = self.get_model_states(name, time_deriv-1)
n = x.shape[1]
if self.derivs_align:
xd = xd[:, :-1]
if self.optimize_time:
dt = self.get_dt()[:n]
else:
dt = cs.vec(dt)
assert dt.shape[0] in {1, n-1}, f"dt should be scalar or have {n-1} elements"
if dt.shape[0] == 1:
dt = dt*cs.DM.ones(n-1)
dt = cs.vec(dt).T # ensure dt is 1-by-(n-1) array
integr = self._integr(model.dim, n-1)
name = f'__integrate_model_states_{name}_{time_deriv}__'
self.add_equality_constraint(name, integr(x[:, :-1], x[:, 1:], xd, dt))
def enforce_model_limits(self, name, time_deriv=0, lo=None, up=None):
"""Enforce model limits.
Syntax
------
builder.enforce_model_limits(name, time_deriv=0)
Parameters
----------
name (string)
Name of model.
time_deriv (int)
The time-deriviative required (i.e. position is 0, velocity is 1, etc.)
lo (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Lower limits, if None then model limits specified in the model class are used.
up (None, or array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Upper limits, if None then model limits specified in the model class are used.
"""
x = self.get_model_states(name, time_deriv)
xlo = lo
xup = up
if (xlo is None) or (xup is None):
mlo, mup = self.get_model(name).get_limits(time_deriv)
if xlo is None:
xlo = mlo
if xup is None:
xup = mup
n = f'__{name}_model_limit_{time_deriv}__'
self.add_bound_inequality_constraint(n, xlo, x, xup)
def initial_configuration(self, name, init=None, time_deriv=0, t0=0):
"""Set initial configuration.
Syntax
------
builder.initial_configuration(name, init=None, time_deriv=0, t0=0)
Parameters
----------
name (string)
Name of model.
init (array-like: casadi.SX, casadi.DM, or list or numpy.ndarray)
Initial configuration.
time_deriv (int)
The time-deriviative required (i.e. position is 0, velocity is 1, etc.)
t0 (int)
Index for the initial configuration in trajectory (typically this will be the first element but it could also be the last for example in moving horizon estimation).
"""
x0 = self.get_model_state(name, t0, time_deriv=time_deriv)
n = f'__{name}_initial_configuration_{time_deriv}_{t0}__'
self.add_equality_constraint(n, lhs=x0, rhs=init) # init will be zero when None
#
# Main build method
#
def build(self):
"""Build the optimization problem."""
# Setup optimization
nlin = self._lin_ineq_constraints.numel()+self._lin_eq_constraints.numel() # total no. linear constraints
nnlin = self._ineq_constraints.numel()+self._eq_constraints.numel() # total no. nonlinear constraints
if self.is_cost_quadratic():
# True -> use QP formulation
if nnlin > 0:
opt = QuadraticCostNonlinearConstraints(
self._decision_variables,
self._parameters,
self._cost_terms,
self._lin_eq_constraints,
self._lin_ineq_constraints,
self._eq_constraints,
self._ineq_constraints,
)
elif nlin > 0:
opt = QuadraticCostLinearConstraints(
self._decision_variables,
self._parameters,
self._cost_terms,
self._lin_eq_constraints,
self._lin_ineq_constraints,
)
else:
opt = QuadraticCostUnconstrained(
self._decision_variables,
self._parameters,
self._cost_terms,
)
else:
# False -> use (nonlinear) Optimization formulation
if nnlin > 0:
opt = NonlinearCostNonlinearConstraints(
self._decision_variables,
self._parameters,
self._cost_terms,
self._lin_eq_constraints,
self._lin_ineq_constraints,
self._eq_constraints,
self._ineq_constraints,
)
elif nlin > 0:
opt = NonlinearCostLinearConstraints(
self._decision_variables,
self._parameters,
self._cost_terms,
self._lin_eq_constraints,
self._lin_ineq_constraints,
)
else:
opt = NonlinearCostUnconstrained(
self._decision_variables,
self._parameters,
self._cost_terms,
)
return opt