TExcelTAD
Home › Optimization › Convex Sets

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 C⊆RnC \subseteq \mathbb{R}^n is convex if the segment between any two of its points lies in it:

x,y∈C, θ∈[0,1]  ⟹  θx+(1−θ)y∈C\mathbf{x}, \mathbf{y} \in C, \ \theta \in [0, 1] \implies \theta\mathbf{x} + (1 - \theta)\mathbf{y} \in C

A convex combination of points x1,…,xk\mathbf{x}_1, \dots, \mathbf{x}_k is ∑iθixi\sum_i \theta_i\mathbf{x}_i with θi≥0\theta_i \ge 0 and ∑iθi=1\sum_i \theta_i = 1. A convex set contains all convex combinations of its points. The convex hull conv⁡S\operatorname{conv} S is the smallest convex set containing SS: all convex combinations of points of SS.

The important examples

Set Description Convex?
hyperplane {x:aTx=b}\{\mathbf{x} : \mathbf{a}^T\mathbf{x} = b\} yes
halfspace {x:aTx≤b}\{\mathbf{x} : \mathbf{a}^T\mathbf{x} \le b\} yes
polyhedron {x:Ax≤b,Cx=d}\{\mathbf{x} : A\mathbf{x} \le \mathbf{b}, C\mathbf{x} = \mathbf{d}\} yes (intersection of halfspaces)
norm ball {x:∥x−c∥≤r}\{\mathbf{x} : \lVert \mathbf{x} - \mathbf{c} \rVert \le r\} yes, for every norm
ellipsoid {x:(x−c)TP−1(x−c)≤1}\{\mathbf{x} : (\mathbf{x} - \mathbf{c})^T P^{-1}(\mathbf{x} - \mathbf{c}) \le 1\}, P≻0P \succ 0 yes
PSD cone symmetric matrices with all eigenvalues ≥0\ge 0 yes
sphere {x:∥x∥=1}\{\mathbf{x} : \lVert \mathbf{x} \rVert = 1\} no
set of integers Zn\mathbb{Z}^n 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.

python · try it yourself
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.

python · try it yourself
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 KK is a cone if x∈K\mathbf{x} \in K implies tx∈Kt\mathbf{x} \in K for every t≥0t \ge 0; a convex cone is also closed under addition. Examples: the non-negative orthant R+n\mathbb{R}^n_+, the second-order cone {(x,t):∥x∥2≤t}\{(\mathbf{x}, t) : \lVert \mathbf{x} \rVert_2 \le t\} 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) {(x,y):y≥x2}\{(x, y) : y \ge x^2\}; (b) {(x,y):y≤x2}\{(x, y) : y \le x^2\}; (c) {(x,y):x+y=3}\{(x, y) : x + y = 3\}; (d) {(x,y):xy≥1,x>0}\{(x, y) : xy \ge 1, x > 0\}.

Answer

(a), (c) and (d) are convex; (b) is not.

Solution

(a) is the region above a convex parabola (its epigraph). (b): (−1,1)(-1, 1) and (1,1)(1, 1) are in it, but the midpoint (0,1)(0, 1) has 1>021 > 0^2. (c) is a line. (d) is the region above the hyperbola y=1/xy = 1/x for x>0x > 0, the epigraph of a convex function.

Exercise 2: Convex combination

Is (2,2)(2, 2) in the convex hull of (0,0)(0, 0), (4,0)(4, 0) and (0,4)(0, 4)? Find the weights.

Answer

Yes: θ=(0,12,12)\theta = (0, \tfrac{1}{2}, \tfrac{1}{2}).

Solution

Solve θ2(4,0)+θ3(0,4)=(2,2)\theta_2(4, 0) + \theta_3(0, 4) = (2, 2), giving θ2=θ3=12\theta_2 = \theta_3 = \tfrac{1}{2} and θ1=1−1=0\theta_1 = 1 - 1 = 0. 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 x+y≥0.5x + y \ge 0.5. What do you expect?

Answer

No violation: an intersection of convex sets is convex.

Solution
python · try it yourself
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 x\mathbf{x} and y\mathbf{y} lie in both sets, the segment lies in each set, so it lies in the intersection.

Test yourself

Which set is NOT convex?

  1. A halfspace
  2. An ellipsoid
  3. The unit circle {x:∥x∥2=1}\{\mathbf{x} : \lVert \mathbf{x} \rVert_2 = 1\}
  4. A polyhedron

A convex combination ∑iθixi\sum_i \theta_i\mathbf{x}_i requires…

  1. θi≥0\theta_i \ge 0 and ∑iθi=1\sum_i \theta_i = 1
  2. ∑iθi=0\sum_i \theta_i = 0
  3. θi∈{0,1}\theta_i \in \{0, 1\}
  4. any real θi\theta_i

The feasible set of a linear program is…

  1. a sphere
  2. a polyhedron
  3. a finite set of points
  4. never bounded