凸性Convexity
优化里最重要的一个"好性质":碗状的地形里只有一个谷底,沿着坡往下走,一定走得到。数学上把这种碗叫凸函数。一个问题是凸的,意味着局部最小就是全局最小,算法有保证、有速度、结果可复现;不是凸的,就要靠运气和经验。认清哪些机器人问题凸、哪些不凸,是选对工具的第一步。 The single most valuable "nice property" in optimization: in a bowl-shaped landscape there is only one valley floor, and walking downhill is sure to reach it. Mathematically such a bowl is a convex function. If a problem is convex, a local minimum is the global minimum, and algorithms come with guarantees, speed and reproducible results; if not, you rely on luck and experience. Telling which robot problems are convex and which are not is the first step in choosing the right tool.
1为什么机器人需要它Why robots need this
机器人领域有一条几乎通用的工程经验:能写成凸问题的,就写成凸问题。全身控制每毫秒要解一个二次规划 (QP),模型预测控制 (MPC) 在线性模型下是一个凸 QP,线性最小二乘标定一步到位。它们都是凸的,所以求解器几毫秒内给出可靠、唯一、可复现的答案,不用担心卡在局部最小、不用调初值。
反过来,机械臂的逆运动学、视觉里程计的位姿优化、神经网络训练都不是凸的,我们只能找"一个够好的谷底"。这一节要建立的是一副"眼镜":给你一个问题,你能判断它的地形是碗还是山脉,从而知道可以对结果有多大的信心。
Robotics has an almost universal engineering rule of thumb: if you can pose it as a convex problem, do. Whole-body control solves a quadratic program (QP) every millisecond; model predictive control (MPC) with a linear model is a convex QP; linear least-squares calibration is one-shot. All are convex, so solvers return a reliable, unique, reproducible answer in milliseconds, with no worry about local minima and no need to tune the initial guess.
Conversely, inverse kinematics of an arm, pose optimization in visual odometry and neural-network training are not convex, and all we can find is "a good enough valley". This lesson builds a pair of glasses: given a problem, you can tell whether its landscape is a bowl or a mountain range, and therefore how much to trust the result.
2直觉:弦在曲线之上Intuition: the chord stays above the curve
取函数曲线上任意两点,连一条线段(弦)。如果无论你取哪两点,弦都不会钻到曲线下面,这个函数就是凸的:它像一个碗,向上"兜住"每一条弦。只要有一对点,弦钻到了曲线下面,就说明那里有个"凸起",凸性被破坏。
这个看似平淡的性质威力巨大。假设碗里有两个"谷底",它们之间的弦必须在曲线之上(或重合);可两个谷底都是最低值 \(m\),弦的高度恰好是 \(m\),而曲线又不可能低于 \(m\),于是两点之间的曲线被夹住,处处等于 \(m\):两个谷底其实是同一片平地。如果两个谷底一高一低,弦从高的那个谷底出发就向下倾斜,紧挨着它的曲线也被压到它的高度之下,它根本不是局部最小(第 7 节给出严格证明)。凸函数不可能有彼此孤立的两个局部最小值。先亲手试试弦测试。
Pick any two points on a function's curve and join them with a segment (the chord). If, whichever two points you pick, the chord never dips below the curve, the function is convex: it behaves like a bowl that holds up every chord. If a single pair has a chord that dips under the curve, there is a "bump" there and convexity fails.
This humble property is powerful. Suppose a bowl had two separate "valley floors". The chord between them must lie on or above the curve; but both floors are at the lowest value \(m\), so the chord sits at height exactly \(m\), and the curve cannot go below \(m\), so between them it is squeezed to equal \(m\) everywhere: the two floors are really one flat stretch. If one floor were higher than the other, the chord from the higher one would slope downward, pushing the curve right next to it below its height, so it would not be a local minimum at all (Section 7 proves this properly). A convex function cannot have two isolated local minima. First, try the chord test by hand.
3动手玩:弦测试Play: the chord test
选一个函数,拖动曲线上的两个点 A、B。弦在曲线之上的部分填绿色,钻到曲线之下的部分填红色。滑块 \(\theta\) 在弦上取一点,对比 \(f(\theta a+(1-\theta)b)\) 与弦的高度 \(\theta f(a)+(1-\theta)f(b)\)。"全面扫描"会测试 780 对点(区间上 40 个点两两配对),告诉你有多大比例的弦违反了凸性。
Choose a function and drag the two points A and B along the curve. Where the chord lies above the curve it is filled green; where it dips below, red. The slider \(\theta\) picks a point on the chord so you can compare \(f(\theta a+(1-\theta)b)\) with the chord height \(\theta f(a)+(1-\theta)f(b)\). "Full scan" tests 780 pairs (every pair of 40 points across the interval) and tells you what fraction of chords violate convexity.
- 在"双谷"或"波浪"上找到一对点,让弦钻到曲线之下。On the "double well" or the "wave", find a pair whose chord dips below the curve.
- 在 \(0.04x^4\) 上做"全面扫描":它在 \(x=0\) 处几乎是平的,却仍然是凸的,违反比例是 0。Run a "full scan" on \(0.04x^4\): it is almost flat at \(x=0\) yet still convex; the violation rate is 0.
- 在凹函数 \(3-0.3x^2\) 上扫描:违反比例是多少?为什么凹函数的"弦"总在曲线下面?Scan the concave function \(3-0.3x^2\): what is the violation rate, and why does its chord always sit below the curve?
凸性是一个"全局"性质,却能用"两点之间"来检验。你不需要知道整个地形,只要任意两点之间的弦不下沉,就知道没有多余的谷底。这也是为什么它能给出全局保证:局部信息(梯度为零)加上凸性,就等于全局信息。
Convexity is a "global" property, but you test it "between two points". You do not need the whole landscape; if no chord between any two points sinks, you know there are no spare valleys. That is why it yields global guarantees: local information (zero gradient) plus convexity equals global information.
4凸集:约束的形状Convex sets: the shape of constraints
同样的"弦测试"用在集合上:一个集合是凸的,当且仅当其中任意两点之间的线段完全留在集合里。球、半空间、盒子是凸的;有凹口的"月牙"、带洞的区域、几块分离的区域都不是。
它为什么重要?因为可行集(6.1 节)就是一个集合。如果约束是凸的,从可行点出发朝另一个可行点走,永远不会"撞墙"。反之,一个障碍物就能把可行集变成非凸的:从左边绕还是从右边绕,是两条不同的路,这正是路径规划难的原因。
The same "chord test" applies to sets: a set is convex if the segment between any two of its points stays entirely inside. Balls, half-spaces and boxes are convex; a "crescent" with a dent, a region with a hole, or several separate blobs are not.
Why does it matter? Because the feasible set (Lesson 6.1) is a set. With convex constraints, walking from one feasible point towards another never hits a wall. A single obstacle, by contrast, makes the feasible set non-convex: going around on the left or on the right are two different routes, which is exactly why path planning is hard.
- 在"月牙"或"星形"里,把 A、B 都放在集合内,却让弦跑出集合。In the "crescent" or the "star", put both A and B inside the set yet make the chord leave it.
- 选"透镜"(圆盘 ∩ 半平面)并做随机扫描:两个凸集的交仍然是凸集,违反数为 0。Pick the "lens" (disk ∩ half-plane) and run a random scan: the intersection of two convex sets is again convex, so zero violations.
5第二个视角:切线在下,曲率向上A second view: tangents below, curvature up
弦测试需要同时看两个点,不方便。对可导函数,有两个"单点"检验:
- 一阶条件:凸函数整条曲线都在它任何一条切线之上。选一点 \(x_0\),画出切线,曲线不能钻到切线下面。
- 二阶条件:曲率处处非负,即 \(f''(x)\ge0\)(多维时是 Hessian \(\nabla^2 f\succeq0\),3.5 节)。曲线始终"向上弯"。
下面拖动 \(x_0\):上图画切线,红色表示曲线跑到了切线之下;下图画 \(f''\),红色表示曲率为负。你会发现这两件事严格同步:只要某处 \(f''<0\),就一定能找到一条切线被曲线穿透;反之,\(f''\ge0\) 处处成立则切线永远在下面。
The chord test must look at two points at once, which is awkward. For differentiable functions there are two "single-point" tests:
- First-order condition: a convex function lies above every one of its tangent lines. Pick a point \(x_0\), draw its tangent; the curve may not dip below it.
- Second-order condition: curvature is everywhere non-negative, \(f''(x)\ge0\) (in several dimensions, the Hessian \(\nabla^2 f\succeq0\), Lesson 3.5). The curve always bends upward.
Drag \(x_0\) below: the top panel draws the tangent, red where the curve dips below it; the bottom panel draws \(f''\), red where curvature is negative. The two stay in lockstep: wherever \(f''<0\) there is a tangent the curve pierces; if \(f''\ge0\) everywhere, every tangent stays below.
- 在"双谷"上拖动 \(x_0\),找到一个位置使得切线被曲线穿透。此时 \(f''\) 在 \(x_0\) 处一定为负吗?(不一定:试试 \(x_0=1\)。)On the "double well" drag \(x_0\) until the tangent is pierced by the curve. Must \(f''\) be negative at \(x_0\) itself? (Not necessarily: try \(x_0=1\).)
- 在"指数"上拖遍整条曲线:切线是否永远在曲线之下?On "exp", drag over the whole curve: does the tangent ever rise above the curve?
6二维地形:弦、Hessian 与"负曲率区"2D landscapes: chords, Hessians and "negative-curvature zones"
到了二维,把 \(x_0\) 换成平面上的点,曲率换成 Hessian 的两个特征值 \(\lambda_1\ge\lambda_2\)(1.11 节)。\(\lambda_2\ge0\) 说明各个方向都向上弯,是碗;\(\lambda_2<0\le\lambda_1\) 说明有的方向向下弯,是鞍;两个都为负是山顶。凸函数要求处处是碗。
下图在平面上拖动 A、B,下方的小图画出 \(f\) 沿这条线段的剖面(实线)和弦(虚线)。红色背景标出 Hessian 有负特征值的地方,那是"非凸区"。试试四种地形:只有第一个是凸的。
In two dimensions the point \(x_0\) becomes a point in the plane and curvature becomes the two Hessian eigenvalues \(\lambda_1\ge\lambda_2\) (Lesson 1.11). \(\lambda_2\ge0\) means it curves upward in every direction: a bowl. \(\lambda_2<0\le\lambda_1\) means some direction curves downward: a saddle; both negative is a hilltop. A convex function must be a bowl everywhere.
In the figure drag A and B in the plane; the small plot below shows the profile of \(f\) along the segment (solid) and the chord (dashed). The red tint marks places where the Hessian has a negative eigenvalue: the "non-convex zone". Try the four terrains: only the first is convex.
- 在 Rosenbrock 上放 A、B,使剖面鼓到弦之上(它只有一个谷底,却不是凸的)。On Rosenbrock place A and B so the profile pokes above the chord (one valley floor, yet not convex).
- 在 log-sum-exp 上随便拖拽 A、B:剖面永远在弦下面,红色区域也不出现。On log-sum-exp drag A and B anywhere: the profile always stays below the chord and no red zone appears.
7再看数学The math
提示:悬停在带底色的符号上,FIG 1、3、4 里对应的元素(弦、切线、非凸区)会被强调。Tip: hover the highlighted symbols; the matching elements in FIG 1, 3 and 4 (chord, tangent, non-convex zone) are emphasized.
定义Definitions
集合 \(C\) 是凸集,如果对任意 \(\x,\y\in C\) 与 \(\theta\in[0,1]\),都有 \(\theta\x+(1-\theta)\y\in C\)。函数 \(f\) 是凸函数,如果定义域是凸集,且
A set \(C\) is convex if \(\theta\x+(1-\theta)\y\in C\) for all \(\x,\y\in C\) and \(\theta\in[0,1]\). A function \(f\) is convex if its domain is convex and
左边是弦下方曲线上的点的高度,右边是弦的高度(FIG 1 的滑块 \(\theta\))。把 \(\le\) 换成 \(<\)(\(\x\ne\y\),\(\theta\in(0,1)\))就是严格凸:碗底没有平的部分,最小点唯一。\(-f\) 凸则称 \(f\) 为凹。
The left side is the height of the curve under the chord; the right side is the chord's height (the slider \(\theta\) of FIG 1). Replacing \(\le\) by \(<\) (for \(\x\ne\y\), \(\theta\in(0,1)\)) gives strict convexity: no flat part at the bottom, hence a unique minimizer. If \(-f\) is convex, \(f\) is concave.
一阶与二阶条件First- and second-order conditions
若 \(f\) 可微,则 \(f\) 凸 \(\iff\) 对所有 \(\x,\y\):
If \(f\) is differentiable, \(f\) is convex \(\iff\) for all \(\x,\y\):
右边正是 \(f\) 在 \(\x\) 处的一阶泰勒展开(切线 / 切平面,FIG 3 的紫线)。若 \(f\) 二阶可微,则 \(f\) 凸 \(\iff\)
The right-hand side is exactly the first-order Taylor expansion of \(f\) at \(\x\) (the tangent line / plane, the purple line of FIG 3). If \(f\) is twice differentiable, \(f\) is convex \(\iff\)
为什么凸问题"容易"Why convex problems are "easy"
1. 局部最小就是全局最小。设 \(\x^\star\) 是局部最小,却有 \(\y\) 使 \(f(\y)<f(\x^\star)\)。沿线段取点 \(\x_t=(1-t)\x^\star+t\y\),由凸性 \(f(\x_t)\le(1-t)f(\x^\star)+tf(\y)<f(\x^\star)\),对任意小的 \(t>0\) 都成立,而 \(\x_t\) 可以任意接近 \(\x^\star\),与"局部最小"矛盾。
2. 梯度为零就够了。在一阶条件里令 \(\nabla f(\x^\star)=\mathbf0\),得 \(f(\y)\ge f(\x^\star)\ \forall\y\):驻点就是全局最小。6.1 节说"梯度为零只是必要条件",对凸函数它也是充分条件。
3. 算法有保证。强凸(\(m I\preceq\nabla^2f\preceq L I\))时,梯度下降的误差按 \(\big(\tfrac{\kappa-1}{\kappa+1}\big)^k\) 衰减,\(\kappa=L/m\) 就是 6.3 节的条件数;Newton 法(6.6 节)在最优点附近二次收敛。
1. A local minimum is the global minimum. Let \(\x^\star\) be a local minimizer and suppose some \(\y\) has \(f(\y)<f(\x^\star)\). Take \(\x_t=(1-t)\x^\star+t\y\) on the segment; convexity gives \(f(\x_t)\le(1-t)f(\x^\star)+tf(\y)<f(\x^\star)\) for every small \(t>0\), while \(\x_t\) comes arbitrarily close to \(\x^\star\), contradicting local minimality.
2. Zero gradient is enough. Put \(\nabla f(\x^\star)=\mathbf0\) in the first-order condition: \(f(\y)\ge f(\x^\star)\ \forall\y\). A stationary point is a global minimizer. Lesson 6.1 said "zero gradient is only necessary"; for convex functions it is also sufficient.
3. Algorithms come with guarantees. For strongly convex \(f\) (\(m I\preceq\nabla^2f\preceq L I\)), gradient descent's error decays like \(\big(\tfrac{\kappa-1}{\kappa+1}\big)^k\), where \(\kappa=L/m\) is the condition number of Lesson 6.3; Newton's method (Lesson 6.6) converges quadratically near the optimum.
怎样得到凸函数:保凸运算Building convex functions: operations that preserve convexity
| 运算Operation | 结论Result | 机器人里的例子Robotics example |
|---|---|---|
| 非负加权和non-negative weighted sum | \(\sum_i w_if_i,\ w_i\ge0\) | 误差 + \(\lambda\)·能耗error + \(\lambda\)·energy |
| 与仿射映射复合composition with an affine map | \(f(A\x+\mathbf b)\) | \(\|A\x-\mathbf b\|^2\):线性最小二乘(1.13 节)linear least squares (Lesson 1.13) |
| 逐点取最大pointwise maximum | \(\max_i f_i\) | 最大关节误差;\(\ell_\infty\) 范数worst joint error; the \(\ell_\infty\) norm |
| 范数、二次型 (PSD)norms, PSD quadratics | \(\|\x\|,\ \x^\top Q\x\) | 马氏距离、力矩平方Mahalanobis distance, squared torques |
| 凸集的交intersection of convex sets | \(C_1\cap C_2\) | 限位 ∩ 速度上限 ∩ 摩擦锥position limits ∩ velocity limits ∩ friction cone |
注意非线性复合会破坏凸性:\(\|A\x-\mathbf b\|^2\) 凸,但 \(\|\mathrm{FK}(\mathbf q)-\mathbf t\|^2\) 里的 \(\mathrm{FK}\) 不是仿射的,所以逆运动学的代价对关节角 \(\mathbf q\) 不凸,见下一节。
Note that nonlinear composition breaks convexity: \(\|A\x-\mathbf b\|^2\) is convex, but \(\mathrm{FK}\) inside \(\|\mathrm{FK}(\mathbf q)-\mathbf t\|^2\) is not affine, so the IK cost is not convex in the joint angles \(\mathbf q\), as the next section shows.
8机器人中的它:逆运动学为什么不凸In robotics: why inverse kinematics is not convex
对于一个目标位置 \(\mathbf t\),最小化 \(f(\mathbf q)=\tfrac12\|\mathrm{FK}(\mathbf q)-\mathbf t\|^2\)。如果把末端位置 \(\mathbf p\) 当变量,\(\tfrac12\|\mathbf p-\mathbf t\|^2\) 是一个完美的碗;可变量是关节角 \(\mathbf q\),\(\mathbf p=\mathrm{FK}(\mathbf q)\) 里全是 \(\sin,\cos\),碗被"揉皱"了。最直接的证据:肘朝上和肘朝下两个解的代价都是 0,而在关节空间里把两个解连成一条线段,中点的代价却大于 0,弦在曲线下面。
拖动下图右侧的 A、B(初始在两个解上,拖动目标会自动吸附到新解),滑块 \(t\) 让手臂沿着这条线段在关节空间里"插值"。注意中途手爪明显偏离了目标,代价鼓起一个包。红色区域是 Hessian 有负特征值的地方。
For a target \(\mathbf t\), minimize \(f(\mathbf q)=\tfrac12\|\mathrm{FK}(\mathbf q)-\mathbf t\|^2\). Take the end-effector position \(\mathbf p\) as the variable and \(\tfrac12\|\mathbf p-\mathbf t\|^2\) is a perfect bowl; but the variable is the joint angle \(\mathbf q\), and \(\mathbf p=\mathrm{FK}(\mathbf q)\) is full of \(\sin,\cos\): the bowl gets crumpled. The cleanest evidence: elbow up and elbow down both have cost 0, yet the midpoint of the segment joining the two solutions in joint space has cost above 0: the chord lies below the curve.
Drag A and B on the right (they start on the two solutions; moving the target snaps them to the new solutions) and use the slider \(t\) to "interpolate" the arm along that segment in joint space. Mid-way the gripper is visibly off target and the cost bulges. The red zone is where the Hessian has a negative eigenvalue.
- 把滑块 \(t\) 拖到中间 (0.4–0.6),读出中点代价:它是否大于两端代价的平均值?Slide \(t\) to the middle (0.4–0.6) and read the midpoint cost: is it larger than the average of the endpoint costs?
这解释了机器人领域的一个普遍现象:"把不凸的部分藏到凸的外面"。解 IK 时通常先在关节空间做一次非凸迭代,再在每一步里解一个凸的线性化子问题(雅可比 \(J\) 把问题局部变成线性最小二乘,6.6、6.9 节)。全身控制则更进一步:在加速度层面,动力学对力矩 \(\boldsymbol\tau\) 与加速度 \(\ddot{\mathbf q}\) 是线性的,于是整个问题是一个凸 QP(6.8 节、第 IV 部分):只要约束可行,求解器就能在 1 kHz 下可靠地给出全局最优解。凸与不凸,常常取决于你选了哪个变量。
This explains a recurring pattern in robotics: "hide the non-convex part outside the convex part". IK is usually solved with a non-convex outer iteration in joint space, but every step solves a convex linearized subproblem (the Jacobian \(J\) turns the problem locally into a linear least-squares one; Lessons 6.6 and 6.9). Whole-body control goes further: at the acceleration level, dynamics are linear in torques \(\boldsymbol\tau\) and accelerations \(\ddot{\mathbf q}\), so the entire problem is a convex QP (Lesson 6.8, Part IV): as long as the constraints are feasible, the solver reliably returns the global optimum at 1 kHz. Whether a problem is convex often depends on which variable you chose.
9常见误区Common pitfalls
反例就是 Rosenbrock(FIG 4):只有一个最小点,却不是凸的,弦能钻到曲线下面。"单峰"(unimodal)比"凸"弱得多。不凸但单谷的函数,局部算法通常也能找到最小点,只是没有凸性那样的速度保证(沿弯曲山谷爬得很慢)。
Rosenbrock (FIG 4) is the counterexample: a single minimizer, yet not convex, with chords that dip below the curve. "Unimodal" is far weaker than "convex". For non-convex single-valley functions local algorithms usually still find the minimum, just without convexity's speed guarantees (they crawl along the curved valley).
二阶条件必须在整个定义域上成立。一个点的 Hessian 正定只说明局部像碗(它是局部最小的二阶充分条件)。FIG 4 里的双谷在每个谷底都是正定的,整体却不凸。
The second-order condition must hold on the whole domain. A positive-definite Hessian at one point only says the function looks like a bowl locally (a second-order sufficient condition for a local minimum). The double well of FIG 4 has a positive-definite Hessian at each valley floor yet is not convex overall.
单位球(\(\le\))是凸集,单位球面(\(=\))不是:球面上两点连线穿过球内部。这就是为什么"四元数模长为 1"、"旋转矩阵正交"这类约束让问题变得不凸,也是第 6.10 节要在流形上直接优化的原因之一。非线性等式约束一般都会破坏凸性;凸问题里的等式约束必须是仿射的。
The unit ball (\(\le\)) is convex; the unit sphere (\(=\)) is not: a chord between two points on the sphere passes through the interior. That is why constraints such as "quaternion has unit norm" or "rotation matrix is orthogonal" make a problem non-convex, and one of the reasons Lesson 6.10 optimizes directly on manifolds. Nonlinear equality constraints generally destroy convexity; equality constraints in a convex problem must be affine.
凸性保证"没有坏的局部最小",但不保证"快"。条件数 \(\kappa\) 很大的凸问题,梯度下降仍然龟速(6.3 节);规模巨大的凸问题仍然需要好的算法和实现。反过来,也有很多不凸的问题(如神经网络训练)在实践中被解得很好。
Convexity guarantees "no bad local minima", not "fast". A convex problem with a huge condition number \(\kappa\) still crawls under gradient descent (Lesson 6.3), and enormous convex problems still need good algorithms and implementations. Conversely, many non-convex problems (such as neural-network training) are solved very well in practice.
10小练习Check yourself
11延伸阅读Further reading
- S. Boyd & L. Vandenberghe, Convex Optimization, Ch. 2–3 — 凸集、凸函数、保凸运算的完整讲解,含大量图示(可免费下载)The definitive treatment of convex sets, convex functions and operations preserving convexity, richly illustrated (free to download)
- J. Nocedal & S. Wright, Numerical Optimization, 2nd ed., Ch. 1–2 & App. A — 凸性在局部最优性条件中的作用The role of convexity in local optimality conditions
- G. Strang, Linear Algebra and Learning from Data, Ch. VI — 凸函数、正定性与深度学习中的优化Convex functions, positive definiteness and optimization for deep learning