Real-Time Embedded Convex Optimization
Real-Time Embedded Convex Optimization
Stephen Boyd
joint work with Michael Grant, Jacob Mattingley, Yang Wang
Electrical Engineering Department, Stanford University
ISMP 2009
Outline
• Examples
ISMP 2009 1
Embedded optimization
ISMP 2009 2
What’s new
embedded optimization at millisecond/microsecond time-scales
millions
traditional problems →
problem size −→
thousands
← our focus
tens
microseconds seconds hours days
solve time −→
ISMP 2009 3
Applications
ISMP 2009 4
Outline
• Examples
ISMP 2009 5
Grasp force optimization
• choose K grasping forces on object to
– resist external wrench (force and torque)
– respect friction cone constraints
– minimize maximum grasp force
• convex problem (second-order cone program or SOCP):
ISMP 2009 6
Example
ISMP 2009 7
Grasp force optimization solve times
ISMP 2009 8
Robust Kalman filtering
• estimate state of a linear dynamical system driven by IID noise
ISMP 2009 9
Measurement update via optimization
ISMP 2009 10
Example
• 50 states, 15 measurements
• so, get a flawed measurement (i.e., zt 6= 0) every other step (or so)
ISMP 2009 11
State estimation error
kx − x̂t|tk2 for KF (red); robust KF (blue); KF with z = 0 (gray)
0.5
ISMP 2009 12
Robust Kalman filter solve time
ISMP 2009 13
Linearizing pre-equalizer
y
∗h
u v y
equalizer ∗h
ISMP 2009 14
Linearizing pre-equalizer
∗h
u e
equalizer v ∗h
ISMP 2009 15
• system: xt+1 = Axt + B sat(vt), yt = Cxt
• (linear) reference system: xref
t+1 = Axref
t + But, ytref = Cxref
t
• et = Cxref
t − Cxt
ISMP 2009 16
Example
ISMP 2009 17
Outputs
desired (black), no compensation (red), equalized (blue)
2.5
−2.5
ISMP 2009 18
Errors
no compensation (red), with equalization (blue)
0.75
−0.75
ISMP 2009 19
Inputs
no compensation (red), with equalization (blue)
2.5
−2.5
ISMP 2009 20
Linearizing pre-equalizer solve time
ISMP 2009 21
Constrained linear quadratic stochastic control
T −1
1 X T T
J = lim E xt Qxt + ut Rut
T →∞ T
t=0
ISMP 2009 22
Constrained linear quadratic stochastic control
ISMP 2009 23
Control-Lyapunov policy
ISMP 2009 24
Quadratic control-Lyapunov policy
• assume
– polyhedral constraint set: U = {v | F v ≤ g}, g ∈ Rk
– quadratic control-Lyapunov function: Vclf (z) = z T P z
ISMP 2009 25
Control-Lyapunov policy evaluation times
ISMP 2009 26
Outline
• Examples
ISMP 2009 27
Parser/solvers for convex optimization
• problem is convex-by-construction
ISMP 2009 28
Example: cvx
• parser/solver written in Matlab
• cvx specification:
cvx begin
variable x(n) % declare vector variable
minimize (norm(A*x-b,2) + lambda*norm(x,1))
subject to F*x <= g
cvx end
ISMP 2009 29
when cvx processes this specification, it
• verifies convexity of problem
• generates equivalent cone problem (here, an SOCP)
• solves it using SDPT3 or SeDuMi
• transforms solution back to original problem
ISMP 2009 30
The same example, transformed by ‘hand’
transform problem to SOCP, call SeDuMi, reconstruct solution:
ISMP 2009 31
Outline
• Examples
ISMP 2009 32
General vs. embedded solvers
• embedded solver
– solves many instances of the same problem family (dimensions,
sparsity pattern) with different data
– solves small or smallish problems
– can deliver lower (application dependent) accuracy
– often must satisfy hard real-time deadline
ISMP 2009 33
Embedded solvers
ISMP 2009 34
Convex optimization solver generation
• specify convex problem family in natural form, via disciplined convex
programming
– declare optimization variables, parameters
– form convex objective and constraints using a specific set of atoms
and calculus rules
• code generator
– analyzes problem structure (dimensions, sparsity, . . . )
– chooses elimination ordering
– generates solver code for specific problem family
• idea:
– spend (perhaps much) time generating code
– save (hopefully much) time solving problem instances
ISMP 2009 35
placements
Problem Parser/solver
x⋆
instance
ISMP 2009 36
Example: cvxmod
• written in Python
• QP family, with variable x ∈ Rn, parameters P , q, g, h
minimize xT P x + q T x
subject to Gx ≤ h, Ax = b
• cvxmod specification:
A = matrix(...); b = matrix(...)
P = param(‘P’, n, n, psd=True); q = param(‘q’, n)
G = param(‘G’, m, n); h = param(‘h’, m)
x = optvar(‘x’, n)
qpfam = problem(minimize(quadform(x, P) + tp(q)*x),
[G*x <= h, A*x == b])
ISMP 2009 37
cvxmod code generation
qpfam.codegen()
ISMP 2009 38
Using cvxmod generated code
#include "solver.h"
int main(int argc, char **argv) {
// Initialize structures at application start-up.
Params params = init params();
Vars vars = init vars();
Workspace work = init work(vars);
// Enter real-time loop.
for (;;) {
update params(params);
status = solve(params, vars, work);
export vars(vars);
}}
ISMP 2009 39
cvxmod code generator
(preliminary implementation)
• handles problems transformable to QP
ISMP 2009 40
Sample solve times for cvxmod generated code
ISMP 2009 41
Conclusions
ISMP 2009 42
References
• Automatic Code Generation for Real-Time Convex Optimization
(Mattingley, Boyd)
ISMP 2009 43