Karush–Kuhn–Tucker conditions
KKT 条件
Check feasibility, signs, stationarity and slackness without confusing a candidate with a global optimum.
kktAt a boundary, a nonzero gradient can be the right answer
A calibrated scalar should stay close to a target a but must lie between zero and one. If a=3, the best allowed value is 1. The loss gradient there is negative: increasing x would reduce the loss, but the upper bound forbids that move. Declaring failure because the gradient is not zero would reject the correct solution.
KKT conditions combine four checks: the point obeys the constraints, inequality multipliers have the correct signs, the constraint forces balance the objective gradient, and only active constraints can have nonzero multipliers. Together they describe constrained stationarity. Under convexity they can also certify a global solution.
Start with Lagrange multipliers and convexity. We use a minimization problem with inequalities written in the “less than or equal to zero” direction throughout.
Write the signs before writing the conditions
Let f be the objective, gᵢ the inequality functions and hⱼ the equality functions. The Lagrangian and multiplier convention are:
Equality multipliers ν are unrestricted. For differentiable functions, the KKT conditions are:
The duality lecture slides, slides 5.20–5.21 collect these conditions and their convex interpretation. If gᵢ(x*) is strictly negative, complementary slackness forces λᵢ*=0. The converse is weaker: a constraint can be active with zero multiplier. Activity says where the point lies; multiplier size says something about the objective and its local constraint balance.
When does satisfying the equations settle the problem?
For a differentiable convex objective and inequalities, with affine equalities on a convex domain, a feasible KKT point is globally optimal. To see why, fix its multipliers. Their nonnegative signs make the Lagrangian convex in x; stationarity makes x* a global minimizer of that Lagrangian. At x*, complementary slackness and equality feasibility make its Lagrangian value equal f(x*). At any other feasible z, the Lagrangian is at most f(z). Combining these inequalities gives f(x*)≤f(z).
Existence of multipliers at an optimum is a separate question. Suitable constraint qualifications are needed. For convex problems, a standard Slater condition supplies strict inequality feasibility in the relative interior of the domain while satisfying the affine equalities; with the usual finite-optimum hypotheses it yields the strong-duality and multiplier guarantees. For a nonconvex local problem, a qualification such as linear independence of active inequality gradients together with equality gradients gives a local necessary condition. Necessity does not turn it into a global certificate. Convex Optimization §5.5.3 explains the role of convexity and strong duality.
A box-constrained quadratic you can solve by hand
Minimize half the squared distance from a subject to 0≤x≤1. Write the lower constraint as −x≤0 and the upper as x−1≤0. The stationarity and complementary products become:
If a lies inside the interval, choose x=a and both multipliers zero. If a is below zero, choose x=0 and lower multiplier −a. If a exceeds one, choose x=1 and upper multiplier a−1. In compact form:
The objective is strictly convex, so the certified minimizer is unique. At a=1, x*=1 lies on the upper bound but both multipliers are zero. That is a concrete counterexample to “active means a strictly positive multiplier.”
Report all four conditions instead of one norm
Run the complete Python 3.12 standard-library program. The scalar target and certificate inputs must be integers or Fraction values; floats and booleans are rejected. Fractions let us distinguish exact zero from a tolerance policy. conditions validates a proposed certificate for this specific quadratic; it does not solve arbitrary constrained problems.
from fractions import Fraction as F
def rational(value):
if type(value) not in (int,F):
raise ValueError('integer or Fraction required')
return F(value)
def conditions(a,x,lower,upper):
a,x,lower,upper=map(rational,(a,x,lower,upper))
return {'primal':0 <= x <= 1,
'dual':lower >= 0 and upper >= 0,
'stationarity':x-a-lower+upper == 0,
'complementary':lower*x == 0 and upper*(x-1) == 0}
def solve_box(a):
a=rational(a)
x=min(F(1),max(F(0),a))
lower,upper=max(-a,F(0)),max(a-1,F(0))
assert all(conditions(a,x,lower,upper).values())
return x,lower,upper,(x-a)**2/2
if __name__ == '__main__':
for a in [F(-2),F(1,3),F(1),F(3)]:
x,lower,upper,loss=solve_box(a)
print('a='+str(a),'x='+str(x),'lower='+str(lower),
'upper='+str(upper),'loss='+str(loss))
Output:
a=-2 x=0 lower=2 upper=0 loss=2
a=1/3 x=1/3 lower=0 upper=0 loss=0
a=1 x=1 lower=0 upper=0 loss=0
a=3 x=1 lower=0 upper=2 loss=2
The first and last cases lie outside the interval and activate different bounds. The middle cases have zero loss, including the boundary target a=1. Clipping is the closed-form solver here; checking KKT explains why the answer is valid. Both solving and checking use a constant number of arithmetic operations, with variable bit cost for large fractions.
In a floating-point optimizer, report primal violations, multiplier-sign violations, the stationarity residual and complementary products separately. A small gradient-balance residual alone may accompany an infeasible point or a negative multiplier. Tolerances should account for scale; this exact toy example deliberately does not choose a universal numerical tolerance.
Four equations are useful only with the right assumptions
For a nonconvex objective, a KKT point need not be a minimum. Minimize −x² on [−1,1]. At x=0 both bound multipliers can be zero and the derivative vanishes, so the KKT conditions hold. Yet x=1 has smaller objective −1; the interior stationary point is a maximum. Convexity was essential to the global argument above.
A failed constraint qualification can mean an optimum has no multipliers of the stated form. Conversely, sufficient convex KKT conditions can certify a particular point without first proving Slater. Slater is used to obtain the existence/strong-duality direction, not to invalidate an already verified convex certificate.
Nondifferentiable objectives need subgradient versions or reformulation. Integer decisions require different optimality tools; applying smooth KKT equations to a continuous relaxation does not certify an integer optimum. The duality chapter develops bounds that help distinguish these questions.
Try to pass three checks while failing the fourth
- For a=3 and x=1, find valid lower and upper multipliers.
Answer 1: Lower is 0 and upper is 2. Stationarity is 1−3−0+2=0; the upper complementary product is 2×0=0.
- A constraint is active. Must its multiplier be positive?
Answer 2: No. At a=1, x=1, the upper constraint is active with upper multiplier zero. Complementary slackness forces inactive multipliers to zero, not active ones away from zero.
- For a=2, propose x=2 and both multipliers zero. Which check fails?
Answer 3: Primal feasibility fails because x exceeds 1. Stationarity, multiplier signs and complementary products all pass, showing why those checks alone are insufficient.
- Why is x=0 not an optimum of minimizing −x² over [−1,1], despite satisfying KKT with zero multipliers?
Answer 4: The objective is nonconvex, so KKT is not a sufficient global-minimum certificate. Either endpoint has objective −1, below the value zero at the candidate.
Use KKT as a diagnostic language
The book and lecture slides connect multiplier balance to duality and convex optimality. This chapter turns the conditions into individually inspectable checks with a hand-solvable example. It does not substitute a software status message for those conditions.
For a next project, extend the scalar interval to independent box constraints on several coordinates. Then introduce one coupling equality and observe why clipping each coordinate independently may violate the total. That is the point to study a constrained solver, its residuals and its actual convergence criteria.
Make a prediction, then check
Can an active constraint have a zero multiplier?
Learning objectives
- Check each KKT condition under a fixed inequality convention.
- Distinguish convex sufficiency from nonconvex stationarity.
Prerequisites & connections
Explore related knowledge →References
- Convex Optimization
Stephen Boyd; Lieven Vandenberghe · 2004 - Convex Optimization — Duality lecture slides
Stephen Boyd; Lieven Vandenberghe; Parth Nobel
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-06 · Codex single-agent authored technical review: primary-source locations, explicit assumptions, derivations, literal or exact computational checks, and bilingual review; not independent human or second-agent peer review.
- Single-agent authored technical review: source locations, stated domains and assumptions, derivations, literal or exact reference cases and matching bilingual programs/exercises. Not independent human or second-agent peer review.
- Four focused tests passed on CPython 3.12.4. Exact Fraction examples are used except for the explicitly floating-point damped Newton experiment. Strict bilingual MDX/KaTeX rendering gives four equations, six anchors and four answers per language; repository publication checks are recorded separately.
- Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
- Publication navigation points published topics to complete articles and removes corresponding pending wording, retaining directory entries for unwritten targets. Principles, equations, programs and exercises are unchanged; final article SHA values rebound.
Source locations checked:
- Convex Optimization · §5.5.3: differentiable KKT conditions and sufficiency for convex objectives/inequalities with affine equalities.
- Convex Optimization — Duality lecture slides · Slides labeled 5.19–5.21: complementary slackness, four conditions, convex sufficiency and Slater; header credits revised slides to three named authors, no publication date inferred.
Related topics