Skip to content

Commit 3eea924

Browse files
author
ArnaudDeza
committed
Add Pluto notebooks for class02 chapter
1 parent 91981e9 commit 3eea924

File tree

6 files changed

+1938
-0
lines changed

6 files changed

+1938
-0
lines changed

class02/class02_constrained_min.jl

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
### A Pluto.jl notebook ###
2+
# v0.20.17
3+
4+
using Markdown
5+
using InteractiveUtils
6+
7+
# ╔═╡ a509f9d8-9c6d-11f0-3db9-cb5fe2e85d64
8+
md"""# Constrained Optimization (Equality & Inequality KKT)
9+
10+
[⬅ Back to Class 02 Overview](class02_overview.jl)
11+
12+
[⬅ Previous: Unconstrained Minimization](class02_unconstrained_min.jl)
13+
14+
[➡ Next: Methods (Penalty/ALM/IPM)](class02_methods_barrier_alm.jl)
15+
16+
**In this section you will:**
17+
18+
* Build the **geometry** of equality constraints and the **KKT** conditions.
19+
* See the **Newton-on-KKT** linear system (saddle point) and when it’s well-posed.
20+
* Contrast **full Newton** vs. **Gauss–Newton** on the KKT system.
21+
* Extend to **inequality constraints** and understand **complementarity**.
22+
23+
"""
24+
25+
# ╔═╡ 57cd6d88-ea7e-4f5c-bc17-7fc65fb78e95
26+
md"""## Equality-constrained minimization: geometry and conditions
27+
28+
**Problem:**
29+
[
30+
\min_{x\in\mathbb{R}^n} f(x) \quad \text{s.t.}\quad C(x)=0,\ \ C:\mathbb{R}^n\to\mathbb{R}^m.
31+
]
32+
33+
**Geometric picture.** At an optimum on the manifold (C(x)=0), the negative gradient must lie in the tangent space:
34+
[
35+
\nabla f(x^\star)\ \perp\ \mathcal{T}_{x^\star}={p:\ J_C(x^\star)p=0}.
36+
]
37+
Equivalently, the gradient is a linear combination of the constraint normals:
38+
[
39+
\nabla f(x^\star)+J_C(x^\star)^{!T}\lambda^\star=0,\qquad C(x^\star)=0\quad(\lambda^\star\in\mathbb{R}^m).
40+
]
41+
42+
**Lagrangian.** (L(x,\lambda)=f(x)+\lambda^{!T}C(x)).
43+
"""
44+
45+
# ╔═╡ b7763163-fc68-4c56-bac9-c37de527858f
46+
md"""
47+
## Equality constraints: picture first
48+
49+
**Goal.** Minimize (f(x)) while staying on the surface (C(x)=0).
50+
51+
* **Feasible set as a surface.** Think of (C(x)=0) as a smooth surface embedded in (\mathbb{R}^n) (a manifold).
52+
* **Move without breaking the constraint.** Tangent directions are the “along-the-surface” moves keeping (C(x)) unchanged to first order.
53+
* **What must be true at the best point.** At (x^\star), there’s no downhill direction within the tangent space.
54+
* **Normals enter the story.** If the gradient can’t point along the surface, it must be balanced by the normals ({J_C(x^\star)_{i:}^{!T}}), producing multipliers (\lambda^\star).
55+
"""
56+
57+
# ╔═╡ 8a08b045-3a1b-4601-a081-27a6a22d05e6
58+
md"""
59+
## From the picture to KKT (equality only)
60+
61+
For a regular local minimum:
62+
63+
1. **Feasibility:** (C(x^\star)=0).
64+
2. **Stationarity:** (\nabla f(x^\star) + J_C(x^\star)^{!T}\lambda^\star = 0).
65+
66+
**Lagrangian viewpoint.** Define (L(x,\lambda)=f(x)+\lambda^{!T}C(x)). At a solution, (x^\star) is stationary for (L) w.r.t. (x), while (C(x^\star)=0) ensures feasibility.
67+
68+
**Interpreting (\lambda^\star).** Each (\lambda_i^\star) reflects how strongly the (i)-th constraint “pushes back”; it’s also a sensitivity of the optimal value to perturbations in (C_i).
69+
"""
70+
71+
# ╔═╡ 907459d1-4a09-441c-a989-71ff687da873
72+
md"""
73+
## KKT system for equalities (first order) & Newton on KKT
74+
75+
**KKT (FOC):**
76+
[
77+
\nabla_x L(x,\lambda)=\nabla f(x)+J_C(x)^{!T}\lambda=0,\qquad C(x)=0.
78+
]
79+
80+
**Newton on KKT (linearize both blocks):**
81+
[
82+
\begin{bmatrix}
83+
\nabla^2 f(x) + \sum_{i=1}^{m}\lambda_i,\nabla^2 C_i(x) & ; J_C(x)^{!T}[2pt]
84+
J_C(x) & ; 0
85+
\end{bmatrix}
86+
\begin{bmatrix}\Delta x\ \Delta\lambda\end{bmatrix}
87+
=-
88+
\begin{bmatrix}
89+
\nabla f(x)+J_C(x)^{!T}\lambda[2pt] C(x)
90+
\end{bmatrix}.
91+
]
92+
93+
**Notes.** This is a symmetric **saddle-point** system. Practical solves use block elimination (Schur complement) and sparse factorizations.
94+
"""
95+
96+
# ╔═╡ aec59d16-c254-43b6-aee2-6261823fb7c3
97+
md"""
98+
## Newton on KKT: practice & safeguards
99+
100+
**Works best when:**
101+
102+
* (J_C(x^\star)) has **full row rank** (regularity).
103+
* The **reduced Hessian** is **positive definite**.
104+
* A **globalization** (e.g., merit/penalty line search) and mild **regularization** are present.
105+
106+
**Common safeguards:**
107+
108+
* **Regularize** the ((1,1)) block (e.g., (+\beta I)) to ensure a good search direction.
109+
* **Merit/penalty line search** balancing feasibility vs. optimality.
110+
* **Scaling** constraints to improve conditioning of the KKT system.
111+
"""
112+
113+
# ╔═╡ a14a100c-5c92-42cc-899d-8c6eb0619368
114+
md"""
115+
## Gauss–Newton vs. full Newton (equality case)
116+
117+
* **Full Newton Lagrangian Hessian:**
118+
[
119+
\nabla_{xx}^2 L(x,\lambda)=\nabla^2 f(x)+\sum_{i=1}^m \lambda_i,\nabla^2 C_i(x).
120+
]
121+
* **Gauss–Newton approximation:** drop the constraint-curvature term:
122+
[
123+
H_{\text{GN}}(x)\approx \nabla^2 f(x).
124+
]
125+
126+
**Trade-offs.**
127+
128+
* **Full Newton:** fewer iterations near the solution; costlier steps; less robust far away.
129+
* **Gauss–Newton:** cheaper per step and often more stable; may need more iterations but competitive in wall-clock on many problems.
130+
"""
131+
132+
# ╔═╡ 300c917e-e61c-4881-82d0-bc79ace66795
133+
md"""
134+
## Solving the KKT system: Schur complement (intuition)
135+
136+
Given
137+
[
138+
\begin{bmatrix} H & A^{!T}\ A & 0\end{bmatrix}
139+
\begin{bmatrix}\Delta x\ \Delta\lambda\end{bmatrix}
140+
=-
141+
\begin{bmatrix} g\ c\end{bmatrix},
142+
]
143+
with (H\approx \nabla_{xx}^2 L), (A=J_C(x)), (g=\nabla f+J_C^{!T}\lambda), (c=C(x)).
144+
145+
* Eliminate (\Delta x): (\Delta x = -H^{-1}(g + A^{!T}\Delta\lambda)).
146+
* Schur system in (\Delta\lambda):
147+
[
148+
(A H^{-1} A^{!T}),\Delta\lambda = c + A H^{-1} g.
149+
]
150+
* Then recover (\Delta x).
151+
Exploit **sparsity**: factor (H) once per iteration; reuse structure across iterations.
152+
153+
"""
154+
155+
# ╔═╡ 28a43bca-618a-4fec-a7eb-ad7a734ceac5
156+
md"""
157+
## Inequality-constrained minimization and KKT
158+
159+
**Problem:** (\min f(x)\ \text{s.t.}\ c(x)\ge 0,\ \ c:\mathbb{R}^n\to\mathbb{R}^p).
160+
161+
**KKT (FOC):**
162+
[
163+
\begin{aligned}
164+
&\text{Stationarity:} && \nabla f(x)-J_c(x)^{!T}\lambda=0,\
165+
&\text{Primal feasibility:} && c(x)\ge 0,\
166+
&\text{Dual feasibility:} && \lambda\ge 0,\
167+
&\text{Complementarity:} && \lambda^{!T}c(x)=0\quad(\lambda_i c_i(x)=0,\ \forall i).
168+
\end{aligned}
169+
]
170+
171+
**Interpretation.**
172+
173+
* **Active** constraints: (c_i(x)=0\Rightarrow \lambda_i) can be nonzero (acts like an equality).
174+
* **Inactive** constraints: (c_i(x)>0\Rightarrow \lambda_i=0) (no influence on stationarity).
175+
"""
176+
177+
# ╔═╡ 3b6300c0-d69f-4004-9b91-41116d0ce832
178+
md"""
179+
## Complementarity: intuition & Newton’s challenge
180+
181+
**What (\lambda_i c_i(x)=0) means.**
182+
183+
* Tight constraint ((c_i=0)) → can press back ((\lambda_i\ge 0)).
184+
* Loose constraint ((c_i>0)) → no force ((\lambda_i=0)).
185+
186+
**Why naïve Newton struggles.**
187+
188+
* Complementarity brings **nonsmoothness** and **inequalities** ((\lambda\ge 0), (c(x)\ge 0)).
189+
* Equality-style Newton can violate nonnegativity or bounce across the boundary.
190+
191+
**Two main strategies (preview).**
192+
193+
* **Active-set:** guess actives → solve equality-constrained subproblem → update the set.
194+
* **Barrier / PDIP / ALM:** smooth or relax complementarity, use damped Newton, and drive the relaxation to zero.
195+
196+
"""
197+
198+
# ╔═╡ ab782dbb-5f3c-404f-87b7-091b4382b0aa
199+
md"""
200+
## Globalization with constraints: merit functions
201+
202+
To balance feasibility and optimality during updates ((x,\lambda)\to(x+\alpha\Delta x,\lambda+\alpha\Delta\lambda)), use a **merit/penalty** function, e.g.
203+
[
204+
\Phi_\mu(x) = f(x) + \mu,|C(x)|*1 \quad \text{(equality case)},
205+
]
206+
or for inequalities, a penalty on **violation** (v(x)=\sum_i \max(0,-c_i(x))).
207+
Do a **backtracking line search** on (\Phi*\mu) to ensure robust progress.
208+
209+
*(You’ll see barrier and ALM variants in the next section.)*
210+
211+
"""
212+
213+
# ╔═╡ 0f722888-d66c-47ae-a1ee-1e2b7c9b4a58
214+
md"""
215+
## Conditioning & scaling
216+
217+
* **Scale constraints** so rows of (J_C) have comparable norms → better KKT conditioning.
218+
* **Regularize** (H) when indefinite/ill-conditioned (modified Cholesky or (+\beta I)).
219+
* **Exploit structure:** block-banded, sparse patterns common in trajectory problems.
220+
* **Warm-starts** from previous solves (e.g., along continuation or time steps) improve robustness.
221+
"""
222+
223+
# ╔═╡ f4d85409-9095-4bc0-b515-ae283e43f344
224+
md"""
225+
## Where to next
226+
227+
* Proceed to **Methods: Penalty vs. Augmented Lagrangian vs. Interior-Point** to see practical algorithms that *enforce* the KKT conditions reliably, including complementarity handling for inequalities.
228+
* Later, we’ll assemble these pieces into **SQP**.
229+
230+
[➡ Methods (Penalty/ALM/IPM) (next)](class02_methods_barrier_alm.jl) · [⬅ Back to overview](class02_overview.jl)
231+
"""
232+
233+
# ╔═╡ 00000000-0000-0000-0000-000000000001
234+
PLUTO_PROJECT_TOML_CONTENTS = """
235+
[deps]
236+
"""
237+
238+
# ╔═╡ 00000000-0000-0000-0000-000000000002
239+
PLUTO_MANIFEST_TOML_CONTENTS = """
240+
# This file is machine-generated - editing it directly is not advised
241+
242+
julia_version = "1.10.0"
243+
manifest_format = "2.0"
244+
project_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
245+
246+
[deps]
247+
"""
248+
249+
# ╔═╡ Cell order:
250+
# ╠═a509f9d8-9c6d-11f0-3db9-cb5fe2e85d64
251+
# ╠═57cd6d88-ea7e-4f5c-bc17-7fc65fb78e95
252+
# ╠═b7763163-fc68-4c56-bac9-c37de527858f
253+
# ╠═8a08b045-3a1b-4601-a081-27a6a22d05e6
254+
# ╠═907459d1-4a09-441c-a989-71ff687da873
255+
# ╠═aec59d16-c254-43b6-aee2-6261823fb7c3
256+
# ╠═a14a100c-5c92-42cc-899d-8c6eb0619368
257+
# ╠═300c917e-e61c-4881-82d0-bc79ace66795
258+
# ╠═28a43bca-618a-4fec-a7eb-ad7a734ceac5
259+
# ╠═3b6300c0-d69f-4004-9b91-41116d0ce832
260+
# ╠═ab782dbb-5f3c-404f-87b7-091b4382b0aa
261+
# ╠═0f722888-d66c-47ae-a1ee-1e2b7c9b4a58
262+
# ╠═f4d85409-9095-4bc0-b515-ae283e43f344
263+
# ╟─00000000-0000-0000-0000-000000000001
264+
# ╟─00000000-0000-0000-0000-000000000002

0 commit comments

Comments
 (0)