线性规划与二次规划Linear & Quadratic Programming
目标是线性的、约束也是线性的,最优解一定躲在多面体的某个角上,这就是线性规划。把目标换成二次函数,最优解就可以离开角,停在棱上甚至内部:这就是二次规划。机器人每个控制周期里解的 MPC、全身控制和抓取力分配,几乎都是二次规划,因为它又快又可靠。 Linear objective, linear constraints: the optimum must hide in a corner of a polytope. That is linear programming. Swap the objective for a quadratic and the optimum may leave the corner and sit on an edge or in the interior: quadratic programming. The MPC, whole-body control and grasp-force allocation that robots solve every control cycle are almost all QPs, because QPs are fast and reliable.
1为什么机器人需要它Why robots need this
机器人的世界里到处是"不等式":关节力矩不能超过电机上限,足底接触力必须落在摩擦锥里,夹爪的手指只能推、不能拉,末端不能撞到桌面。6.3 节的梯度下降对这些硬性界限无能为力,它只会在无约束的山坡上往下走。
好消息是,当目标是二次函数、约束是线性不等式时,问题就是一个二次规划 (QP)。它是凸的(6.2 节),没有局部最小值的烦恼,而且有成熟的求解器,规模几十到几百个变量时,几毫秒就能解完。于是一台人形机器人可以在 1 kHz 的控制回路里,每个周期解一次 QP 来决定所有关节的力矩;一个机械臂可以每 20 ms 解一次 MPC,让自己在速度、加速度和障碍的限制下贴着参考轨迹走。
线性规划 (LP) 是 QP 去掉二次项后的特例。它的几何最干净,是理解"约束到底怎样决定最优解"的最好起点:先看 LP,再看 QP 多出来的那一点自由度。
A robot's world is full of inequalities: joint torques cannot exceed the motor limit, foot contact forces must lie in a friction cone, a gripper finger can only push and never pull, the end effector must not hit the table. The gradient descent of Lesson 6.3 knows nothing about such hard limits; it just walks downhill on an unconstrained slope.
The good news: when the objective is a quadratic and the constraints are linear inequalities, the problem is a quadratic program (QP). It is convex (Lesson 6.2), so there are no local minima to fear, and mature solvers handle tens to hundreds of variables in milliseconds. A humanoid can therefore solve a QP every cycle of a 1 kHz control loop to pick all its joint torques; an arm can solve an MPC problem every 20 ms to track a reference while respecting velocity, acceleration and obstacle limits.
A linear program (LP) is the QP with the quadratic term removed. Its geometry is the cleanest there is, so it is the best place to learn how constraints decide the optimum: LP first, then the one extra degree of freedom a QP adds.
2直觉:多面体与顶点Intuition: polytopes and vertices
每个线性不等式 \(\mathbf a^\top\x \le b\) 把空间切成两半,只保留一侧。几个不等式同时成立的点,构成一个凸的多面体(二维里是凸多边形),叫作可行域。线性目标 \(\mathbf c^\top\x\) 的等值线是一族平行直线(高维里是平行超平面)。把这族直线沿"代价下降"的方向推过去,直到再推就离开可行域为止,最后接触到的位置,就是最优解。
一个凸多边形被一条直线最后碰到的,只可能是一个顶点,或者恰好平行时的一整条边,不会是边的中间某个孤立点。所以线性规划的最优解(如果存在)总能在某个顶点取到。拖动下图的箭头,亲眼看一遍。
Each linear inequality \(\mathbf a^\top\x \le b\) cuts space in half and keeps one side. The points satisfying several inequalities at once form a convex polytope (a convex polygon in 2D), the feasible set. The level sets of a linear objective \(\mathbf c^\top\x\) are parallel lines (parallel hyperplanes in higher dimensions). Slide that family of lines in the direction of decreasing cost until pushing any further would leave the feasible set; where it touches last is the optimum.
The last thing a line touches on a convex polygon can only be a vertex, or a whole edge if the line happens to be parallel to it, never an isolated point in the middle of an edge. So a linear program, if it has an optimum, always has one at a vertex. Drag the arrow below and see it for yourself.
- 转动箭头,让最优解从一个顶点跳到另一个顶点。注意:目标方向连续变化,最优解却是跳跃的。Rotate the arrow so the optimum jumps from one vertex to another. The objective changes continuously but the optimum jumps.
- 让箭头吸附到某条边的法线上,得到一整条边都是最优解(无穷多个最优解)。Snap the arrow to an edge normal so that a whole edge is optimal (infinitely many optima).
- 按"单纯形行走",看它沿边走几步到达最优顶点。Press "Simplex walk" and count how many edge steps it takes to reach the optimal vertex.
- 切换到"不可行":两个约束互相矛盾,可行域为空,问题无解。Switch to "Infeasible": two constraints contradict each other, the feasible set is empty and there is no solution.
LP 把一个连续的优化问题变成了组合问题:只需要在有限多个顶点里挑一个。单纯形法 (simplex) 沿着边走顶点;内点法 (interior-point) 则从内部穿过去逼近最优点。两者都靠同一件事:凸,所以"局部最优 = 全局最优"。
An LP turns a continuous optimization problem into a combinatorial one: you only need to pick among finitely many vertices. The simplex method walks from vertex to vertex along edges; interior-point methods cut through the inside to approach the optimum. Both rely on convexity: a local optimum is the global one.
3二次规划:最优解离开了角Quadratic programming: the optimum leaves the corner
把线性目标换成凸二次函数,等值线从平行直线变成椭圆,中心 \(\mathbf p\) 是不考虑约束时的最优点。如果 \(\mathbf p\) 恰好在可行域内部,约束全部"休眠",答案就是 \(\mathbf p\) 本身;如果 \(\mathbf p\) 在域外,最优解是最先碰到可行域的那个椭圆与可行域的接触点,它可以落在边的中间,也可以落在顶点上。
6.7 节的 KKT 条件在这里有一幅很具体的"受力图":在接触点 \(\x^\star\),目标想把你拉向 \(\mathbf p\)(橙色,\(-\nabla f\)),被碰到的那几面"墙"各自把你推回来(紫色,大小 \(\lambda_i\)),平衡时两者之和为零。推力不能为负(墙只能推不能拉):\(\lambda_i \ge 0\)。没碰到的墙不出力:\(\lambda_i = 0\)。这些碰到的约束叫作有效约束 (active constraints)。
Replace the linear objective by a convex quadratic and the level sets turn from parallel lines into ellipses centered at \(\mathbf p\), the unconstrained optimum. If \(\mathbf p\) happens to be inside the feasible set, every constraint is "asleep" and the answer is \(\mathbf p\) itself. If \(\mathbf p\) is outside, the solution is the point where the smallest ellipse first touches the feasible set; it may sit in the middle of an edge or at a vertex.
The KKT conditions of Lesson 6.7 have a very concrete "free-body diagram" here. At the touching point \(\x^\star\) the objective pulls you toward \(\mathbf p\) (amber, \(-\nabla f\)), and each wall you touch pushes back (purple, magnitude \(\lambda_i\)); at balance they sum to zero. A wall can only push, never pull, so \(\lambda_i \ge 0\); a wall you do not touch exerts nothing, \(\lambda_i = 0\). The constraints you touch are the active constraints.
- 把 \(\mathbf p\) 拖进可行域内部:没有任何有效约束,解就是 \(\mathbf p\)。Drag \(\mathbf p\) inside the feasible set: no active constraint, the solution is \(\mathbf p\).
- 让解停在某条边的中间(一个有效约束)。观察拉力与推力共线。Make the solution sit in the middle of an edge (one active constraint). Notice pull and push are collinear.
- 让解停在一个顶点(两个有效约束,两个 \(\lambda\) 都大于 0)。此时推力是两面墙的合力。Make the solution sit at a vertex (two active constraints, both \(\lambda\) positive). The push is the resultant of two walls.
- 固定 \(\mathbf p\) 在域外,转动椭圆的方向:解沿着边滑动,直到换到另一条边。With \(\mathbf p\) outside, rotate the ellipse: the solution slides along the edge until it switches to another edge.
几何视角:解是椭圆与可行域的"首次接触点",也是 \(\mathbf p\) 在 \(Q\) 度量下到可行域的投影(\(Q=I\) 时就是欧氏投影,1.9 节)。力学视角:解是各种力的平衡点,\(\lambda_i\) 是墙 \(i\) 出的力,也等于"把这面墙挪动一点,最优代价改变多少"(影子价格)。LP 只是其中的极端情形:椭圆被拉成无限宽,只剩下一个方向有区别。
Geometric view: the solution is the "first contact" of the ellipse and the feasible set; equivalently it is the projection of \(\mathbf p\) onto the feasible set in the \(Q\)-metric (the Euclidean projection of Lesson 1.9 when \(Q=I\)). Mechanical view: the solution is where the forces balance, and \(\lambda_i\) is the force of wall \(i\), which is also how much the optimal cost changes if you move that wall slightly (a shadow price). An LP is the extreme case: the ellipse stretched infinitely wide so only one direction matters.
4再看数学The math
提示:把鼠标悬停在公式中带底色的符号上,FIG 1、FIG 2 中对应的元素会被高亮。Tip: hover the highlighted symbols in the formulas; the matching elements in FIG 1 and FIG 2 light up.
标准形式Standard forms
线性规划:
Linear program:
二次规划:多一个二次项,其中 \(H\) 是对称半正定矩阵(\(H\succeq 0\),问题才是凸的,6.2 节):
Quadratic program: one extra quadratic term, where \(H\) is symmetric positive semidefinite (\(H\succeq 0\) makes the problem convex, Lesson 6.2):
FIG 2 中 \(f=\tfrac12(\x-\mathbf p)^\top Q(\x-\mathbf p)\) 展开后正是 \(H=Q,\ \mathbf g=-Q\mathbf p\)(加一个不影响解的常数)。最常见的来源是最小二乘(1.13 节):\(\tfrac12\|M\x-\mathbf y\|^2\) 展开得 \(H=M^\top M,\ \mathbf g=-M^\top\mathbf y\)。所以 QP 就是"带不等式约束的最小二乘"。
The \(f=\tfrac12(\x-\mathbf p)^\top Q(\x-\mathbf p)\) of FIG 2 expands to exactly \(H=Q,\ \mathbf g=-Q\mathbf p\) (plus a constant that does not change the minimizer). The most common source is least squares (Lesson 1.13): \(\tfrac12\|M\x-\mathbf y\|^2\) expands to \(H=M^\top M,\ \mathbf g=-M^\top\mathbf y\). A QP is least squares with inequality constraints.
KKT 条件与有效集KKT conditions and the active set
把 6.7 节的 KKT 条件用在 QP 上(只写不等式部分,记 \(\mathbf a_i^\top\) 为 \(A\) 的第 \(i\) 行):
Apply the KKT conditions of Lesson 6.7 to a QP (inequality part only; \(\mathbf a_i^\top\) is row \(i\) of \(A\)):
第一式就是 FIG 2 的受力平衡:\(-\nabla f=\sum_i\lambda_i\mathbf a_i\)(拉力恰好被墙的推力 \(-\lambda_i\mathbf a_i\) 抵消);最后一式叫互补松弛:墙要么没碰到(\(\mathbf a_i^\top\x<b_i\),则 \(\lambda_i=0\)),要么碰到了(等号成立,\(\lambda_i\) 可以大于 0)。
The first equation is the force balance of FIG 2: \(-\nabla f=\sum_i\lambda_i\mathbf a_i\) (the pull is exactly cancelled by the wall pushes \(-\lambda_i\mathbf a_i\)). The last one is complementary slackness: a wall is either untouched (\(\mathbf a_i^\top\x<b_i\), so \(\lambda_i=0\)) or touched (equality holds and \(\lambda_i\) may be positive).
有效集法的想法:如果我们猜对了哪些约束有效(集合 \(W\)),问题就退化成只带等式约束的 QP,只需解一个线性方程组:
The idea of the active-set method: if we guess right which constraints are active (the set \(W\)), the problem collapses to an equality-constrained QP, and that needs just one linear solve:
检查两件事:解是否满足所有没选入 \(W\) 的约束(可行性)?\(\boldsymbol\lambda_W\) 是否都 \(\ge 0\)?若某个 \(\lambda_i<0\),说明这面墙在"拉",应当把它从 \(W\) 里放掉;若违反了某个没选入的约束,就把它加进 \(W\)。实用算法(primal active-set、Goldfarb–Idnani 等)就是这样一边加一边减地迭代。FIG 2 因为只有 5 个约束、\(n=2\),直接枚举了全部 16 种可能的 \(W\)。
Then check two things: does the solution satisfy every constraint left out of \(W\) (feasibility)? Are all \(\boldsymbol\lambda_W\ge 0\)? If some \(\lambda_i<0\), that wall is "pulling" and must be released from \(W\); if some unlisted constraint is violated, add it to \(W\). Practical algorithms (primal active-set, Goldfarb–Idnani, ...) iterate by adding and dropping in exactly this way. FIG 2 has only 5 constraints and \(n=2\), so it simply enumerates all 16 possible sets \(W\).
三类求解器Three families of solvers
| 做法Idea | 特点Character | |
|---|---|---|
| Active set / simplex | 沿着有效约束的"面"走,每步解线性方程组Walk along faces of active constraints, one linear solve per step | 热启动极好(MPC 里下个周期的解和这个周期很像),精确到顶点;最坏情况步数可能很多Excellent warm starts (next MPC cycle looks like this one) and exact at vertices; worst-case step count can be large |
| Interior point | 用对数障碍把约束"软化",从内部走牛顿步逼近边界Soften constraints with a log barrier and take Newton steps from the interior toward the boundary | 迭代次数稳定(十几次),热启动差;大规模问题的首选A steady dozen or so iterations, poor warm starts; the choice for big problems |
| ADMM (OSQP) | 交替做一次线性方程组求解和一次"投影到约束区间"Alternate a linear solve with a "clip to the constraint interval" step | 实现简单、矩阵只分解一次,精度中等;机器人里很常用。FIG 3、FIG 4 用的就是它Simple, factorizes the matrix once, medium accuracy; popular in robotics. FIG 3 and FIG 4 use it |
5机器人中的它:抓取力分配与 MPCIn robotics: grasp-force allocation and MPC
抓取:用最小的力握住物体Grasping: hold the object with the least force
圆盘形物体受重力(或任意外力)\(\mathbf w\),由 \(k\) 个手指接触。每个手指 \(i\) 能产生法向力 \(f_{n,i}\ge 0\)(只能推)和切向力 \(f_{t,i}\)(摩擦),而且必须在摩擦锥内:\(|f_{t,i}|\le\mu f_{n,i}\)。这是两个线性不等式。物体不动要求受力平衡与力矩平衡(线性等式)。夹爪还有最大握力 \(f_{n,i}\le f_{\max}\)。在所有能平衡的力分配里,我们挑总力平方最小的那组,以免捏坏物体:
A disc-shaped object under gravity (or any external force) \(\mathbf w\) is held by \(k\) fingers. Finger \(i\) can produce a normal force \(f_{n,i}\ge 0\) (push only) and a tangential friction force \(f_{t,i}\), which must lie in the friction cone: \(|f_{t,i}|\le\mu f_{n,i}\). These are two linear inequalities. For the object not to move we need force and torque balance (linear equalities). The gripper also has a maximum squeeze \(f_{n,i}\le f_{\max}\). Among all balanced force allocations, pick the one with the smallest squared total force, so we do not crush the object:
其中 \(\mathbf F_i = f_{n,i}\mathbf n_i + f_{t,i}\mathbf t_i\) 是第 \(i\) 个接触点施加给物体的力。目标是 \(H=I\) 的二次函数,所有约束都是线性的:这是个 QP,下面用 ADMM 实时求解。
Here \(\mathbf F_i = f_{n,i}\mathbf n_i + f_{t,i}\mathbf t_i\) is the force finger \(i\) applies to the object. The objective is a quadratic with \(H=I\) and all constraints are linear: a QP, solved live below with ADMM.
- 用两个手指(默认对置),把摩擦系数 \(\mu\) 调小,直到抓不住。临界值大约是多少?(提示:外力大小为 1,默认 \(f_{\max}=6\);每个手指的摩擦力要承担一半的重量,而法向力最多 \(f_{\max}\)。)With two fingers (opposed by default), lower the friction coefficient \(\mu\) until the grasp fails. What is the critical value? (Hint: the load has magnitude 1 and \(f_{\max}=6\) by default; each finger's friction carries half the weight, while its normal force is at most \(f_{\max}\).)
- 切换成三指,把 \(\mu\) 调到 0.1:现在抓得住了。哪个手指几乎没有出力?Switch to three fingers with \(\mu=0.1\): the grasp holds now. Which finger is almost idle?
- 用两指,把外力方向转成水平:外力所推向的那个手指只用法向力就能直接抵抗它,不再需要摩擦(另一个手指几乎不出力)。With two fingers, turn the load horizontal: the finger the load pushes against resists it with a pure normal force, and friction is no longer needed (the other finger is almost idle).
MPC:每个周期解一个 QPMPC: one QP per control cycle
模型预测控制 (MPC,见第 IV 部分) 的想法是:在每个控制周期,预测未来 \(N\) 步,在满足约束的前提下选一串最好的控制量,只执行第一个,下个周期再重新规划。当动力学是线性的 \(\mathbf x_{k+1}=A\mathbf x_k+B u_k\)、代价是二次的、约束是线性的,整个预测问题就是一个 QP,决策变量是 \(u_0,\dots,u_{N-1}\)。下图是一个一维小车:质点位置 \(p\)、速度 \(v\),控制量是加速度 \(u\)。目标是让位置跟上参考 \(p_{\rm ref}\),约束是加速度有界、位置不能越过前方的"墙" \(p_{\max}\)。
Model predictive control (MPC, Part IV) works like this: at each control cycle, predict \(N\) steps ahead, choose the best control sequence that respects the constraints, apply only the first, and re-plan next cycle. When the dynamics are linear, \(\mathbf x_{k+1}=A\mathbf x_k+B u_k\), the cost is quadratic and the constraints are linear, the whole prediction problem is a QP in \(u_0,\dots,u_{N-1}\). Below is a 1D cart: position \(p\), velocity \(v\), control input acceleration \(u\). The goal is to follow a reference \(p_{\rm ref}\); the constraints are bounded acceleration and a "wall" \(p_{\max}\) ahead that must not be crossed.
- 把加速度上限 \(u_{\max}\) 调小,让预测里饱和的柱子越来越多,直到整段起步加速都顶在上限上。Lower the acceleration limit \(u_{\max}\) and watch more bars saturate until the whole start-up acceleration sits on the limit.
- 把参考拖到墙的后面:小车为什么最终停在墙前,而不是穿过去?Drag the reference behind the wall: why does the cart end up in front of the wall rather than going through?
- 按"运行 MPC",观察闭环轨迹。Press "Run MPC" and watch the closed-loop trajectory.
全身控制 (whole-body control) 把"让质心加速度、末端加速度、姿态都符合期望"写成关于关节加速度和接触力的最小二乘,再加上力矩上限、摩擦锥、关节限位等不等式,每个 1 kHz 周期解一个 QP(第 IV 部分)。足式机器人 MPC(如 MIT Cheetah 的凸 MPC)把足底力当作决策变量,摩擦锥线性化为金字塔,也是一个 QP。常用求解器有 OSQP、qpOASES、ProxQP、HPIPM;它们都针对"每个周期问题结构不变、只有数字略变"做了热启动优化。
Whole-body control writes "make the center-of-mass acceleration, end-effector acceleration and posture match the desired values" as a least-squares problem in joint accelerations and contact forces, adds torque limits, friction cones and joint limits as inequalities, and solves one QP per 1 kHz cycle (Part IV). Legged-robot MPC (e.g. the convex MPC of MIT Cheetah) treats foot forces as decision variables and linearizes friction cones into pyramids: again a QP. Common solvers are OSQP, qpOASES, ProxQP and HPIPM; all exploit warm starts, since the problem structure is the same every cycle and only the numbers change slightly.
6常见误区Common pitfalls
约束互相矛盾(FIG 1 的"不可行")时求解器会直接返回失败;目标可以无限下降而约束拦不住时问题是"无界"的。MPC 里最常见的不可行原因是硬约束与初始状态冲突:小车已经越过了位置约束,或者速度太大、在加速度上限内根本刹不住。解决办法是把这类约束变成软约束:加一个松弛变量 \(s\ge 0\) 和大惩罚 \(\rho s^2\),让问题永远可行,但违反时有代价。真实系统必须处理求解失败时用什么备用控制量。
When constraints contradict each other (the "Infeasible" mode of FIG 1) a solver simply reports failure; when the cost can fall forever and no constraint stops it, the problem is "unbounded". The most common cause of infeasibility in MPC is a hard constraint conflicting with the initial state: the cart is already past the position limit, or moving so fast it cannot brake within the acceleration bound. The cure is to make such constraints soft: add a slack variable \(s\ge 0\) with a large penalty \(\rho s^2\), so the problem is always feasible but violations cost something. A real system must also decide what control to use when the solver fails.
如果 \(H\) 有负特征值(比如由数值误差、或把 Hessian 近似写错造成),问题是非凸的:可能无界,也可能有多个局部最小。凸 QP 求解器遇到它可能报错、发散,或悄悄给出一个鞍点。排查办法是看 \(H\) 的最小特征值(1.11 节);对最小二乘形式 \(M^\top M\) 则天然半正定,可以放心。
If \(H\) has a negative eigenvalue (from numerical error or a wrongly approximated Hessian) the problem is nonconvex: possibly unbounded, possibly with several local minima. A convex QP solver may error out, diverge, or quietly return a saddle point. Check the smallest eigenvalue of \(H\) (Lesson 1.11); an \(M^\top M\) from a least-squares form is positive semidefinite automatically.
有效约束的严格含义是"等号成立",并且通常\(\lambda>0\)。解离某面墙 0.001 但 \(\lambda=0\),这面墙对解毫无影响。反过来,退化情形(多个约束在同一个顶点相交,比如三面墙交于一点)会让乘子不唯一,也会让有效集法"绕圈"。数值上用容差(如 \(10^{-9}\))判断,而不是 ==。
"Active" strictly means "equality holds", and usually \(\lambda>0\). A solution that sits 0.001 from a wall with \(\lambda=0\) is not influenced by that wall at all. Conversely, degenerate cases (several constraints meeting at one vertex, e.g. three walls through a point) make the multipliers non-unique and can make active-set methods cycle. Test with a tolerance (say \(10^{-9}\)), never with ==.
ADMM 是一阶方法,和 6.3 节的梯度下降一样怕条件数。位置以米计、力以千牛计时,要先对变量和约束行做缩放(OSQP 内置了 Ruiz 平衡)。另外,ADMM 给的是中等精度解,约束违反量可能有 \(10^{-3}\),对安全相关约束要留余量或做后处理。
ADMM is a first-order method and, like the gradient descent of Lesson 6.3, dislikes bad conditioning. If positions are in meters and forces in kilonewtons, scale variables and constraint rows first (OSQP does Ruiz equilibration internally). Also, ADMM returns a medium-accuracy answer whose constraint violation may be around \(10^{-3}\); for safety-critical constraints, leave a margin or polish the result.
7小练习Check yourself
8延伸阅读Further reading
- S. Boyd & L. Vandenberghe, Convex Optimization, Cambridge University Press, 2004, Ch. 4.3–4.4 — LP、QP 的标准形式、对偶与例子,免费在线standard forms, duality and examples of LP and QP; free online
- J. Nocedal & S. J. Wright, Numerical Optimization, 2nd ed., Springer, 2006, Ch. 13–16 — 单纯形法、有效集法与内点法 QP 的权威讲解the standard treatment of the simplex, active-set and interior-point methods for LP and QP
- B. Stellato, G. Banjac, P. Goulart, A. Bemporad & S. Boyd, OSQP: An Operator Splitting Solver for Quadratic Programs, Mathematical Programming Computation, 2020 — FIG 3、FIG 4 所用 ADMM 迭代的出处the source of the ADMM iteration used in FIG 3 and FIG 4
- J. B. Rawlings, D. Q. Mayne & M. M. Diehl, Model Predictive Control: Theory, Computation, and Design, 2nd ed., 2017 — 线性 MPC 如何化为 QPhow linear MPC becomes a QP