CoolFace
Apppublic

marimo-team/marimo-learn

sourceHugging Faceupdated 5mo agoView on Hugging Face
3likes
07_sdp.py128 linesDownload Raw Back to optimization
1# /// script2# requires-python = ">=3.13"3# dependencies = [4#     "clarabel>=0.11.1",5#     "cvxpy>=1.8.2",6#     "marimo",7#     "numpy==2.4.3",8#     "wigglystuff==0.2.37",9# ]10# ///11 12import marimo13 14__generated_with = "0.18.4"15app = marimo.App()16 17 18@app.cell19def _():20    import marimo as mo21    return (mo,)22 23 24@app.cell(hide_code=True)25def _(mo):26    mo.md(r"""27    # Semidefinite Program28    """)29    return30 31 32@app.cell(hide_code=True)33def _(mo):34    mo.md(r"""35    _This notebook introduces an advanced topic._ A semidefinite program (SDP) is an optimization problem of the form36 37    \[38        \begin{array}{ll}39        \text{minimize}   & \mathbf{tr}(CX) \\40        \text{subject to} & \mathbf{tr}(A_iX) = b_i, \quad i=1,\ldots,p \\41                          & X \succeq 0,42        \end{array}43    \]44 45    where $\mathbf{tr}$ is the trace function, $X \in \mathcal{S}^{n}$ is the optimization variable and $C, A_1, \ldots, A_p \in \mathcal{S}^{n}$, and $b_1, \ldots, b_p \in \mathcal{R}$ are problem data, and $X \succeq 0$ is a matrix inequality. Here $\mathcal{S}^{n}$ denotes the set of $n$-by-$n$ symmetric matrices.46 47    **Example.** An example of an SDP is to complete a covariance matrix $\tilde \Sigma \in \mathcal{S}^{n}_+$ with missing entries $M \subset \{1,\ldots,n\} \times \{1,\ldots,n\}$:48 49    \[50        \begin{array}{ll}51        \text{minimize}   & 0 \\52        \text{subject to} & \Sigma_{ij} = \tilde \Sigma_{ij}, \quad (i,j) \notin M \\53                          & \Sigma \succeq 0,54        \end{array}55    \]56    """)57    return58 59 60@app.cell(hide_code=True)61def _(mo):62    mo.md(r"""63    ## Example64 65    In the following code, we show how to specify and solve an SDP with CVXPY.66    """)67    return68 69 70@app.cell71def _():72    import cvxpy as cp73    import numpy as np74    return cp, np75 76 77@app.cell78def _(np):79    # Generate a random SDP.80    n = 381    p = 382    np.random.seed(1)83    C = np.random.randn(n, n)84    A = []85    b = []86    for i in range(p):87        A.append(np.random.randn(n, n))88        b.append(np.random.randn())89    return A, C, b, n, p90 91 92@app.cell93def _(A, C, b, cp, n, p):94    # Create a symmetric matrix variable.95    X = cp.Variable((n, n), symmetric=True)96 97    # The operator >> denotes matrix inequality, with X >> 0 constraining X98    # to be positive semidefinite99    constraints = [X >> 0]100    constraints += [cp.trace(A[i] @ X) == b[i] for i in range(p)]101    prob = cp.Problem(cp.Minimize(cp.trace(C @ X)), constraints)102    _ = prob.solve()103    return X, prob104 105 106@app.cell107def _(X, mo, prob, wigglystuff):108    mo.md(109        f"""110        The optimal value is {prob.value:0.4f}.111 112        A solution for $X$ is (rounded to the nearest decimal) is: 113 114        {mo.ui.anywidget(wigglystuff.Matrix(X.value)).center()}115        """116    )117    return118 119 120@app.cell121def _():122    import wigglystuff123    return (wigglystuff,)124 125 126if __name__ == "__main__":127    app.run()128