Convex Sets
Convexity is the property that makes optimization tractable. It starts with sets: the feasible region of a problem is a set, and if that set is convex, local search cannot get trapped on the wrong side of a hole.
Definition
A set is convex if the segment between any two of its points lies in it:
A convex combination of points is with and . A convex set contains all convex combinations of its points. The convex hull is the smallest convex set containing : all convex combinations of points of .
The important examples
| Set | Description | Convex? |
|---|---|---|
| hyperplane | yes | |
| halfspace | yes | |
| polyhedron | yes (intersection of halfspaces) | |
| norm ball | yes, for every norm | |
| ellipsoid | , | yes |
| PSD cone | symmetric matrices with all eigenvalues | yes |
| sphere | no | |
| set of integers | no |
The feasible set of an LP is a polyhedron, so it is convex. The feasible set of an integer program is not, which is why Integer Programming is much harder.
Testing convexity numerically
A numerical test cannot prove convexity, but it can find a counterexample: pick random pairs of points in the set and check midpoints.
import numpy as np
rng = np.random.default_rng(1)
def find_violation(inside, trials=20000):
for _ in range(trials):
x, y = rng.uniform(-2, 2, size=(2, 2))
if inside(x) and inside(y):
t = rng.uniform()
if not inside(t * x + (1 - t) * y):
return np.round(x, 2), np.round(y, 2)
return None
disk = lambda p: p @ p <= 1
l1_ball = lambda p: np.abs(p).sum() <= 1
annulus = lambda p: 0.5 <= p @ p <= 1
cross = lambda p: min(abs(p[0]), abs(p[1])) <= 0.2
for name, s in [("disk", disk), ("l1 ball", l1_ball), ("annulus", annulus), ("cross", cross)]:
v = find_violation(s)
print(f"{name:8s}", "no violation found" if v is None else f"violation between {v[0]} and {v[1]}")disk no violation found
l1 ball no violation found
annulus violation between [-0.08 -0.87] and [-0.83 0.28]
cross violation between [0.11 0.76] and [0.83 0.15]
Convex hulls
The convex hull of a finite point set is a polygon (in 2D) whose corners are some of the points. Every point of the hull is a convex combination of the corners, and a linear function over the hull is maximized at a corner, the key fact behind the simplex method.
import numpy as np
from scipy.spatial import ConvexHull
rng = np.random.default_rng(3)
pts = rng.uniform(0, 10, size=(30, 2))
hull = ConvexHull(pts)
print("corners of the hull:", len(hull.vertices), "of", len(pts), "points")
c = np.array([1.0, 2.0]) # maximize x + 2y over the points
best = pts[np.argmax(pts @ c)]
print("best point is a hull corner:", any(np.allclose(best, pts[v]) for v in hull.vertices))corners of the hull: 7 of 30 points
best point is a hull corner: True
Cones
A set is a cone if implies for every ; a convex cone is also closed under addition. Examples: the non-negative orthant , the second-order cone and the PSD cone. Conic programs (Second-Order Cone and Semidefinite Programs) are built on them.
In software
scipy.spatial.ConvexHull(Qhull) computes hulls; collision detection in games uses convex shapes because overlap tests between them are cheap.- Solvers model feasible sets as intersections of simple convex sets: halfspaces, cones and boxes.
Exercises
Exercise 1: Which are convex?
(a) ; (b) ; (c) ; (d) .
Answer
(a), (c) and (d) are convex; (b) is not.
Solution
(a) is the region above a convex parabola (its epigraph). (b): and are in it, but the midpoint has . (c) is a line. (d) is the region above the hyperbola for , the epigraph of a convex function.
Exercise 2: Convex combination
Is in the convex hull of , and ? Find the weights.
Answer
Yes: .
Solution
Solve , giving and . All weights are non-negative and sum to 1, so the point is on the hull (on the edge between the last two corners).
Exercise 3: Intersections (code)
Use find_violation on the intersection of the unit disk and the halfplane . What do you expect?
Answer
No violation: an intersection of convex sets is convex.
Solution
import numpy as np
rng = np.random.default_rng(5)
inside = lambda p: p @ p <= 1 and p[0] + p[1] >= 0.5
bad = 0
for _ in range(20000):
x, y = rng.uniform(-1, 1, size=(2, 2))
if inside(x) and inside(y) and not inside(0.5 * x + 0.5 * y):
bad += 1
print("violations:", bad)violations: 0
If and lie in both sets, the segment lies in each set, so it lies in the intersection.
Test yourself
Which set is NOT convex?
- A halfspace
- An ellipsoid
- The unit circle
- A polyhedron
A convex combination requires…
- and
- any real
The feasible set of a linear program is…
- a sphere
- a polyhedron
- a finite set of points
- never bounded