Spaces:
Running
Running
Akshay Agrawal
commited on
Commit
·
07062d3
1
Parent(s):
f56200e
qp: level curves
Browse files- optimization/04_quadratic_program.py +140 -11
optimization/04_quadratic_program.py
CHANGED
|
@@ -3,7 +3,9 @@
|
|
| 3 |
# dependencies = [
|
| 4 |
# "cvxpy==1.6.0",
|
| 5 |
# "marimo",
|
|
|
|
| 6 |
# "numpy==2.2.2",
|
|
|
|
| 7 |
# ]
|
| 8 |
# ///
|
| 9 |
|
|
@@ -74,25 +76,45 @@ def _():
|
|
| 74 |
|
| 75 |
@app.cell(hide_code=True)
|
| 76 |
def _(mo):
|
| 77 |
-
mo.md("""First we generate synthetic data.""")
|
| 78 |
return
|
| 79 |
|
| 80 |
|
| 81 |
@app.cell
|
| 82 |
def _(np):
|
| 83 |
-
m =
|
| 84 |
-
n =
|
| 85 |
-
p = 5
|
| 86 |
|
| 87 |
np.random.seed(1)
|
| 88 |
-
P = np.random.randn(n, n)
|
| 89 |
-
P = P.T @ P
|
| 90 |
q = np.random.randn(n)
|
| 91 |
G = np.random.randn(m, n)
|
| 92 |
h = G @ np.random.randn(n)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
@app.cell(hide_code=True)
|
|
@@ -102,12 +124,12 @@ def _(mo):
|
|
| 102 |
|
| 103 |
|
| 104 |
@app.cell
|
| 105 |
-
def _(
|
| 106 |
x = cp.Variable(n)
|
| 107 |
|
| 108 |
problem = cp.Problem(
|
| 109 |
cp.Minimize((1 / 2) * cp.quad_form(x, P) + q.T @ x),
|
| 110 |
-
[G @ x <= h
|
| 111 |
)
|
| 112 |
_ = problem.solve()
|
| 113 |
return problem, x
|
|
@@ -126,6 +148,113 @@ def _(mo, problem, x):
|
|
| 126 |
return
|
| 127 |
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
@app.cell
|
| 130 |
def _():
|
| 131 |
import marimo as mo
|
|
|
|
| 3 |
# dependencies = [
|
| 4 |
# "cvxpy==1.6.0",
|
| 5 |
# "marimo",
|
| 6 |
+
# "matplotlib==3.10.0",
|
| 7 |
# "numpy==2.2.2",
|
| 8 |
+
# "wigglystuff==0.1.9",
|
| 9 |
# ]
|
| 10 |
# ///
|
| 11 |
|
|
|
|
| 76 |
|
| 77 |
@app.cell(hide_code=True)
|
| 78 |
def _(mo):
|
| 79 |
+
mo.md("""First we generate synthetic data. In this problem, we don't include equality constraints, only inequality.""")
|
| 80 |
return
|
| 81 |
|
| 82 |
|
| 83 |
@app.cell
|
| 84 |
def _(np):
|
| 85 |
+
m = 4
|
| 86 |
+
n = 2
|
|
|
|
| 87 |
|
| 88 |
np.random.seed(1)
|
|
|
|
|
|
|
| 89 |
q = np.random.randn(n)
|
| 90 |
G = np.random.randn(m, n)
|
| 91 |
h = G @ np.random.randn(n)
|
| 92 |
+
return G, h, m, n, q
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@app.cell(hide_code=True)
|
| 96 |
+
def _(mo, np):
|
| 97 |
+
import wigglystuff
|
| 98 |
+
|
| 99 |
+
P_widget = mo.ui.anywidget(
|
| 100 |
+
wigglystuff.Matrix(np.array([[4.0, -1.4], [-1.4, 4]]), step=0.1)
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
mo.md(
|
| 104 |
+
f"""
|
| 105 |
+
The quadratic form $P$ is equal to the symmetrized version of this
|
| 106 |
+
matrix:
|
| 107 |
+
|
| 108 |
+
{P_widget.center()}
|
| 109 |
+
"""
|
| 110 |
+
)
|
| 111 |
+
return P_widget, wigglystuff
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@app.cell
|
| 115 |
+
def _(P_widget, np):
|
| 116 |
+
P = 0.5 * (np.array(P_widget.matrix) + np.array(P_widget.matrix).T)
|
| 117 |
+
return (P,)
|
| 118 |
|
| 119 |
|
| 120 |
@app.cell(hide_code=True)
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
@app.cell
|
| 127 |
+
def _(G, P, cp, h, n, q):
|
| 128 |
x = cp.Variable(n)
|
| 129 |
|
| 130 |
problem = cp.Problem(
|
| 131 |
cp.Minimize((1 / 2) * cp.quad_form(x, P) + q.T @ x),
|
| 132 |
+
[G @ x <= h],
|
| 133 |
)
|
| 134 |
_ = problem.solve()
|
| 135 |
return problem, x
|
|
|
|
| 148 |
return
|
| 149 |
|
| 150 |
|
| 151 |
+
@app.cell
|
| 152 |
+
def _(G, P, h, plot_contours, q, x):
|
| 153 |
+
plot_contours(P, G, h, q, x.value)
|
| 154 |
+
return
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
@app.cell(hide_code=True)
|
| 158 |
+
def _(mo):
|
| 159 |
+
mo.md(
|
| 160 |
+
r"""
|
| 161 |
+
In this plot, the gray shaded region is the feasible region (points satisfying the inequality), and the ellipses are level curves of the quadratic form.
|
| 162 |
+
|
| 163 |
+
**🌊 Try it!** Try changing the entries of $P$ above with your mouse. How do the
|
| 164 |
+
level curves and the optimal value of $x$ change? Can you explain what you see?
|
| 165 |
+
"""
|
| 166 |
+
)
|
| 167 |
+
return
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@app.cell(hide_code=True)
|
| 171 |
+
def _(P, mo):
|
| 172 |
+
mo.md(
|
| 173 |
+
rf"""
|
| 174 |
+
The above contour lines were generated with
|
| 175 |
+
|
| 176 |
+
\[
|
| 177 |
+
P= \begin{{bmatrix}}
|
| 178 |
+
{P[0, 0]:.01f} & {P[0, 1]:.01f} \\
|
| 179 |
+
{P[1, 0]:.01f} & {P[1, 1]:.01f} \\
|
| 180 |
+
\end{{bmatrix}}
|
| 181 |
+
\]
|
| 182 |
+
"""
|
| 183 |
+
)
|
| 184 |
+
return
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
@app.cell(hide_code=True)
|
| 188 |
+
def _(np):
|
| 189 |
+
def plot_contours(P, G, h, q, x_star):
|
| 190 |
+
import matplotlib.pyplot as plt
|
| 191 |
+
|
| 192 |
+
# Create a grid of x and y values.
|
| 193 |
+
x = np.linspace(-5, 5, 400)
|
| 194 |
+
y = np.linspace(-5, 5, 400)
|
| 195 |
+
X, Y = np.meshgrid(x, y)
|
| 196 |
+
|
| 197 |
+
# Compute the quadratic form Q(x, y) = a*x^2 + 2*b*x*y + c*y^2.
|
| 198 |
+
# Here, a = P[0,0], b = P[0,1] (and P[1,0]), c = P[1,1]
|
| 199 |
+
Z = (
|
| 200 |
+
0.5 * (P[0, 0] * X**2 + 2 * P[0, 1] * X * Y + P[1, 1] * Y**2)
|
| 201 |
+
+ q[0] * X
|
| 202 |
+
+ q[1] * Y
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# --- Evaluate the constraints on the grid ---
|
| 206 |
+
# We stack X and Y to get a list of (x,y) points.
|
| 207 |
+
points = np.vstack([X.ravel(), Y.ravel()]).T
|
| 208 |
+
|
| 209 |
+
# Start with all points feasible
|
| 210 |
+
feasible = np.ones(points.shape[0], dtype=bool)
|
| 211 |
+
|
| 212 |
+
# Apply the inequality constraints Gx <= h.
|
| 213 |
+
# Each row of G and corresponding h defines a condition.
|
| 214 |
+
for i in range(G.shape[0]):
|
| 215 |
+
# For a given point x, the condition is: G[i,0]*x + G[i,1]*y <= h[i]
|
| 216 |
+
feasible &= points.dot(G[i]) <= h[i] + 1e-8 # small tolerance
|
| 217 |
+
# Reshape the boolean mask back to grid shape.
|
| 218 |
+
feasible_grid = feasible.reshape(X.shape)
|
| 219 |
+
|
| 220 |
+
# --- Plot the feasible region and contour lines---
|
| 221 |
+
plt.figure(figsize=(8, 6))
|
| 222 |
+
|
| 223 |
+
# Use contourf to fill the region where feasible_grid is True.
|
| 224 |
+
# We define two levels, so that points that are True (feasible) get one
|
| 225 |
+
# color.
|
| 226 |
+
plt.contourf(
|
| 227 |
+
X,
|
| 228 |
+
Y,
|
| 229 |
+
feasible_grid,
|
| 230 |
+
levels=[-0.5, 0.5, 1.5],
|
| 231 |
+
colors=["white", "gray"],
|
| 232 |
+
alpha=0.5,
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
contours = plt.contour(X, Y, Z, levels=10, cmap="viridis")
|
| 236 |
+
plt.clabel(contours, inline=True, fontsize=8)
|
| 237 |
+
plt.title("Feasible region and level curves")
|
| 238 |
+
plt.xlabel("$x_1$")
|
| 239 |
+
plt.ylabel("$y_2$")
|
| 240 |
+
# plt.colorbar(contours, label='Q(x, y)')
|
| 241 |
+
|
| 242 |
+
ax = plt.gca()
|
| 243 |
+
# Optionally, mark and label the point x_star.
|
| 244 |
+
ax.plot(x_star[0], x_star[1], "ko", markersize=5)
|
| 245 |
+
ax.text(
|
| 246 |
+
x_star[0],
|
| 247 |
+
x_star[1],
|
| 248 |
+
r"$\mathbf{x}^\star$",
|
| 249 |
+
color="black",
|
| 250 |
+
fontsize=12,
|
| 251 |
+
verticalalignment="bottom",
|
| 252 |
+
horizontalalignment="right",
|
| 253 |
+
)
|
| 254 |
+
return plt.gca()
|
| 255 |
+
return (plot_contours,)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
@app.cell
|
| 259 |
def _():
|
| 260 |
import marimo as mo
|