-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstrained.py
More file actions
310 lines (261 loc) · 9.17 KB
/
constrained.py
File metadata and controls
310 lines (261 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
constrained_optimizers.py — Minimal equality & inequality constrained solvers
============================================================================
Implements simple 2-D Lagrange and primal–dual gradient methods for constrained
optimization. These solvers use finite-difference gradients, fixed step sizes,
and lightweight multiplier updates suitable for demos and visualization.
Classes:
- EqualityConstrainedOptimizer
Solves min f(x) s.t. h_i(x)=0 via Lagrange-multiplier gradient steps.
- InequalityConstrainedOptimizer
Solves min f(x) s.t. g_i(x)<=0 using projected primal–dual updates.
Features:
- central finite-difference gradients
- KKT-style multiplier updates
- built-in 3D and contour plotting of optimization paths
- simple API for small educational examples
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable, List, Optional, Sequence, Tuple
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
ConstraintFn = Callable[[np.ndarray], float]
ObjectiveFn = Callable[[np.ndarray], float]
__all__ = [
"EqualityConstrainedOptimizer",
"InequalityConstrainedOptimizer",
]
@dataclass
class OptimizationResult:
"""Lightweight container for the optimization trajectory."""
path: np.ndarray
multipliers: np.ndarray
stop_iter: int
class _BaseConstrainedOptimizer:
"""
Shared utilities for the equality/inequality constrained optimizers.
The optimizers follow the "call -> optimize -> optional plot"
"""
def __init__(
self,
objective: ObjectiveFn,
constraints: Optional[Sequence[ConstraintFn]] = None,
*,
num_vars: int = 2,
tol: float = 1e-4,
delta_fd: float = 1e-4,
step_size_x: float = 5e-3,
step_size_lambda: float = 5e-3,
n_epochs: int = 2000,
random_state: Optional[int] = None,
) -> None:
self.objective = objective
self.constraints = list(constraints or [])
self.num_vars = num_vars
self.tol = tol
self.delta_fd = delta_fd
self.step_size_x = step_size_x
self.step_size_lambda = step_size_lambda
self.n_epochs = n_epochs
if random_state is not None:
np.random.seed(random_state)
def _finite_diff_grad(self, func: Callable[[np.ndarray], float]):
delta = self.delta_fd
def grad_fn(x: np.ndarray) -> np.ndarray:
x = np.asarray(x, dtype=float).ravel()
grad = np.zeros(self.num_vars, dtype=float)
for i in range(self.num_vars):
e = np.zeros(self.num_vars, dtype=float)
e[i] = delta
grad[i] = (func(x + e) - func(x - e)) / (2.0 * delta)
return grad
return grad_fn
@staticmethod
def _stack_constraint_grads(
grad_list: Sequence[Callable[[np.ndarray], np.ndarray]], x: np.ndarray
) -> np.ndarray:
if not grad_list:
return np.zeros((0, x.size), dtype=float)
return np.stack([grad_fn(x) for grad_fn in grad_list])
def plot_descent_path(
self,
points: np.ndarray,
X: np.ndarray,
Y: np.ndarray,
Z: np.ndarray,
*,
elev: float = 30,
azim: float = 60,
title: str = "Gradient Descent on Objective",
):
points = np.asarray(points, dtype=float)
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection="3d")
ax.plot_surface(X, Y, Z, cmap="viridis", alpha=0.7)
z_vals = np.array([self.objective(pt) for pt in points])
ax.plot(
points[:, 0],
points[:, 1],
z_vals,
color="b",
marker="o",
markersize=3,
label="Optimization Path",
)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("f(x, y)")
ax.set_title(title)
ax.view_init(elev=elev, azim=azim)
ax.legend()
fig.tight_layout()
return fig, ax
def plot_contours(
self,
points: np.ndarray,
X: np.ndarray,
Y: np.ndarray,
Z: np.ndarray,
*,
title: str = "Optimization Path (Contours)",
):
points = np.asarray(points, dtype=float)
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111)
contour = ax.contour(X, Y, Z, 50, cmap="viridis")
ax.clabel(contour, inline=True, fontsize=8)
ax.plot(
points[:, 0],
points[:, 1],
color="b",
marker="o",
markersize=3,
label="Optimization Path",
)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title(title)
ax.legend()
fig.tight_layout()
return fig, ax
def plot_path(
self,
path: np.ndarray,
*,
x_bounds: Optional[Tuple[float, float]] = None,
y_bounds: Optional[Tuple[float, float]] = None,
grid_res: int = 200,
elev: float = 30,
azim: float = 60,
title: str = "Constrained Optimization",
show_contours: bool = False,
):
"""
Visualize the objective surface f(x, y) alongside the optimization path.
"""
path = np.asarray(path, dtype=float)
assert path.ndim == 2 and path.shape[1] == 2, "Expect path with shape (k, 2)"
def _auto_bounds(coords, default=(-3.0, 3.0)):
if coords.size == 0:
return default
lo = np.min(coords) - 0.5
hi = np.max(coords) + 0.5
if np.isclose(lo, hi):
lo -= 1.0
hi += 1.0
return float(lo), float(hi)
x_bounds = x_bounds or _auto_bounds(path[:, 0])
y_bounds = y_bounds or _auto_bounds(path[:, 1])
x = np.linspace(*x_bounds, grid_res)
y = np.linspace(*y_bounds, grid_res)
X, Y = np.meshgrid(x, y)
grid = np.array([X, Y])
Z = self.objective(grid)
fig3d = self.plot_descent_path(path, X, Y, Z, elev=elev, azim=azim, title=title)
fig2d = None
if show_contours:
fig2d = self.plot_contours(
path,
X,
Y,
Z,
title=f"{title} (Contours)",
)
plt.show()
class EqualityConstrainedOptimizer(_BaseConstrainedOptimizer):
"""
Lagrange multiplier based solver for equality-constrained problems:
min f(x)
s.t. h_i(x) = 0
"""
def optimize(
self,
*,
seed_pos: Optional[np.ndarray] = None,
) -> OptimizationResult:
x = (
np.asarray(seed_pos, dtype=float).ravel()
if seed_pos is not None
else np.random.uniform(-1.0, 1.0, size=self.num_vars)
)
m = len(self.constraints)
lam = np.zeros(m, dtype=float)
steps: List[np.ndarray] = [x.copy()]
grad_f = self._finite_diff_grad(self.objective)
grad_h_list = [self._finite_diff_grad(h) for h in self.constraints]
for k in range(self.n_epochs):
g_f = grad_f(x)
h_vals = np.array([h(x) for h in self.constraints], dtype=float)
h_grads = self._stack_constraint_grads(grad_h_list, x)
grad_L_x = g_f + (lam @ h_grads if m else 0.0)
if (
np.linalg.norm(grad_L_x) < self.tol
and np.linalg.norm(h_vals) < self.tol
):
break
x = x - self.step_size_x * grad_L_x
lam = lam + self.step_size_lambda * h_vals
steps.append(x.copy())
return OptimizationResult(
path=np.asarray(steps, dtype=float),
multipliers=lam.copy(),
stop_iter=len(steps) - 1,
)
class InequalityConstrainedOptimizer(_BaseConstrainedOptimizer):
"""
Primal–dual gradient method for inequality constraints (KKT conditions):
min f(x)
s.t. g_i(x) <= 0
"""
def optimize(
self,
*,
seed_pos: Optional[np.ndarray] = None,
) -> OptimizationResult:
x = (
np.asarray(seed_pos, dtype=float).ravel()
if seed_pos is not None
else np.random.uniform(-1.0, 1.0, size=self.num_vars)
)
m = len(self.constraints)
lam = np.zeros(m, dtype=float)
steps: List[np.ndarray] = [x.copy()]
grad_f = self._finite_diff_grad(self.objective)
grad_g_list = [self._finite_diff_grad(g) for g in self.constraints]
for k in range(self.n_epochs):
g_f = grad_f(x)
g_vals = np.array([g(x) for g in self.constraints], dtype=float)
g_grads = self._stack_constraint_grads(grad_g_list, x)
grad_L_x = g_f + (g_grads.T @ lam if m else 0.0)
if np.linalg.norm(grad_L_x) < self.tol and np.all(g_vals <= self.tol):
break
x = x - self.step_size_x * grad_L_x
lam = np.maximum(lam + self.step_size_lambda * g_vals, 0.0)
steps.append(x.copy())
return OptimizationResult(
path=np.asarray(steps, dtype=float),
multipliers=lam.copy(),
stop_iter=len(steps) - 1,
)