KKT explorer: the fence, the force, and the multiplier

Minimise f(z) = ½‖z − zt‖² (be as close as possible to the target) while staying on the allowed side of a fence. Drag the red target. Watch the solution z*, the downhill pull of the cost −∇f (red), and the push of the fence −λ∇g (blue) cancel each other. The multiplier λ switches on exactly when the target crosses the fence.

−∇f(z*) = zt − z* (the cost pulls downhill, toward the target) −λ∇g(z*) (the fence pushes back, normal to the boundary, into the feasible side) level sets of f
the constraint

Numbers

The four KKT conditions, checked live

Closed forms used here (no solver): round fence z* = zt if ‖zt‖ ≤ 1, else zt/‖zt‖ with λ = ‖zt‖ − 1; straight fence z* = zt − max(0, g(zt)) n with λ = max(0, g(zt)); rail z* = zt − h(zt) n with κ = h(zt) of either sign. Companion notes: modules/kkt-explorer/notes.md; lecture: Deck 05, session 4.1.