File size: 9,146 Bytes
9e55022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8646a4e
 
 
 
 
 
9e55022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "cvxpy==1.6.0",
#     "marimo",
#     "matplotlib==3.10.0",
#     "numpy==2.2.2",
#     "wigglystuff==0.1.9",
# ]
# ///

import marimo

__generated_with = "0.11.0"
app = marimo.App()


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        # Linear program

        A linear program is an optimization problem with a linear objective and affine
        inequality constraints. A common standard form is the following:

        \[  
            \begin{array}{ll}
            \text{minimize}   & c^Tx \\
            \text{subject to} & Ax \leq b.
            \end{array}
        \]

        Here $A \in \mathcal{R}^{m \times n}$, $b \in \mathcal{R}^m$, and $c \in \mathcal{R}^n$ are problem data and $x \in \mathcal{R}^{n}$ is the optimization variable. The inequality constraint $Ax \leq b$ is elementwise.

        For example, we might have $n$ different products, each constructed out of $m$ components. Each entry $A_{ij}$ is the amount of component $i$ required to build one unit of product $j$. Each entry $b_i$ is the total amount of component $i$ available. We lose $c_j$ for each unit of product $j$ ($c_j < 0$ indicates profit). Our goal then is to choose how many units of each product $j$ to make, $x_j$, in order to minimize loss without exceeding our budget for any component.

        In addition to a solution $x^\star$, we obtain a dual solution $\lambda^\star$. A positive entry $\lambda^\star_i$ indicates that the constraint $a_i^Tx \leq b_i$ holds with equality for $x^\star$ and suggests that changing $b_i$ would change the optimal value.

        **Why linear programming?** Linear programming is a way to achieve an optimal outcome, such as maximum utility or lowest cost, subject to a linear objective function and affine constraints. Developed in the 20th century, linear programming is widely used today to solve problems in resource allocation, scheduling, transportation, and more. The discovery of polynomial-time algorithms to solve linear programs was of tremendous worldwide importance and entered the public discourse, even making the front page of the New York Times.

        In the late 20th and early 21st century, researchers generalized linear programming to a much wider class of problems called convex optimization problems. Nearly all convex optimization problems can be solved efficiently and reliably, and even more difficult problems are readily solved by a sequence of convex optimization problems. Today, convex optimization is used to fit machine learning models, land rockets in real-time at SpaceX, plan trajectories for self-driving cars at Waymo, execute many billions of dollars of financial trades a day, and much more.

        This marimo learn course uses CVXPY, a modeling language for convex optimization problems developed originally at Stanford, to construct and solve convex programs.
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        f"""
        {mo.image("https://www.debugmind.com/wp-content/uploads/2020/01/spacex-1.jpg")}
        _SpaceX solves convex optimization problems onboard to land its rockets, using CVXGEN, a code generator for quadratic programming developed at Stephen Boyd’s Stanford lab. Photo by SpaceX, licensed CC BY-NC 2.0._
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Example

        Here we use CVXPY to construct and solve a linear program.
        """
    )
    return


@app.cell
def _():
    import cvxpy as cp
    import numpy as np
    return cp, np


@app.cell(hide_code=True)
def _(np):
    A = np.array(
        [
            [0.76103773, 0.12167502],
            [0.44386323, 0.33367433],
            [1.49407907, -0.20515826],
            [0.3130677, -0.85409574],
            [-2.55298982, 0.6536186],
            [0.8644362, -0.74216502],
            [2.26975462, -1.45436567],
            [0.04575852, -0.18718385],
            [1.53277921, 1.46935877],
            [0.15494743, 0.37816252],
        ]
    )

    b = np.array(
        [
            2.05062369,
            0.94934659,
            0.89559424,
            1.04389978,
            2.45035643,
            -0.95479445,
            -0.83801349,
            -0.26562529,
            2.35763652,
            0.98286942,
        ]
    )
    return A, b


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""We've randomly generated problem data $A$ and $B$. The vector for $c$ is shown below. Try playing with the value of $c$ by dragging the components, and see how the level curves change in the visualization below.""")
    return


@app.cell
def _(mo, np):
    from wigglystuff import Matrix

    c_widget = mo.ui.anywidget(Matrix(matrix=np.array([[0.1, -0.2]]), step=0.01))
    c_widget
    return Matrix, c_widget


@app.cell
def _(c_widget, np):
    c = np.array(c_widget.value["matrix"][0])
    return (c,)


@app.cell
def _(A, b, c, cp):
    x = cp.Variable(A.shape[1])
    prob = cp.Problem(cp.Minimize(c.T @ x), [A @ x <= b])
    _ = prob.solve()
    x_star = x.value
    return prob, x, x_star


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""Below, we plot the feasible region of the problem — the intersection of the inequalities — and the level curves of the objective function. The optimal value $x^\star$ is the point farthest in the feasible region in the direction $-c$.""")
    return


@app.cell(hide_code=True)
def _(A, b, c, make_plot, x_star):
    make_plot(A, b, c, x_star)
    return


@app.cell(hide_code=True)
def _(np):
    import matplotlib.pyplot as plt


    def make_plot(A, b, c, x_star):
        # Define a grid over a region that covers the feasible set.
        # You might need to adjust these limits.
        x_vals = np.linspace(-1, 1, 400)
        y_vals = np.linspace(1, 3, 400)
        X, Y = np.meshgrid(x_vals, y_vals)

        # Flatten the grid points into an (N,2) array.
        points = np.vstack([X.ravel(), Y.ravel()]).T

        # For each point, check if it satisfies all the constraints: A @ x <= b.
        # A dot product: shape of A @ x.T will be (m, N). We add a little tolerance.
        feasible = np.all(np.dot(A, points.T) <= (b[:, None] + 1e-8), axis=0)
        feasible = feasible.reshape(X.shape)

        # Create the figure with a white background.
        fig = plt.figure(figsize=(8, 6), facecolor="white")
        ax = fig.add_subplot(111)
        ax.set_facecolor("white")

        # Plot the feasible region.
        # Since "feasible" is a boolean array (False=0, True=1), we set contour levels so that
        # the region with value 1 (feasible) gets filled.
        ax.contourf(
            X,
            Y,
            feasible.astype(float),
            levels=[-0.5, 0.5, 1.5],
            colors=["white", "gray"],
            alpha=0.5,
        )

        # Plot level curves of the objective function c^T x.
        # Compute c^T x over the grid:
        Z = c[0] * X + c[1] * Y
        # Choose several levels for the iso-cost lines.
        levels = np.linspace(np.min(Z), np.max(Z), 20)
        contours = ax.contour(
            X, Y, Z, levels=levels, colors="gray", linestyles="--", linewidths=1
        )

        # Draw the vector -c as an arrow starting at x_star.
        norm_c = np.linalg.norm(c)
        if norm_c > 0:
            head_width = norm_c * 0.1
            head_length = norm_c * 0.1
            # The arrow starts at x_star and points in the -c direction.
            ax.arrow(
                x_star[0],
                x_star[1],
                -c[0],
                -c[1],
                head_width=head_width,
                head_length=head_length,
                fc="black",
                ec="black",
                length_includes_head=True,
            )
            # Label the arrow near its tip.
            ax.text(
                x_star[0] - c[0] * 1.05,
                x_star[1] - c[1] * 1.05,
                r"$-c$",
                color="black",
                fontsize=12,
            )

        # Optionally, mark and label the point x_star.
        ax.plot(x_star[0], x_star[1], "ko", markersize=5)
        ax.text(
            x_star[0],
            x_star[1],
            r"$\mathbf{x}^\star$",
            color="black",
            fontsize=12,
            verticalalignment="bottom",
            horizontalalignment="right",
        )
        # Label the axes and set title.
        ax.set_xlabel("$x_1$")
        ax.set_ylabel("$x_2$")
        ax.set_title("Feasible Region and Level Curves of $c^Tx$")
        ax.set_xlim(np.min(x_vals), np.max(x_vals))
        ax.set_ylim(np.min(y_vals), np.max(y_vals))
        return ax
    return make_plot, plt


@app.cell(hide_code=True)
def _(mo, prob, x):
    mo.md(
        f"""
        The optimal value is {prob.value:.04f}.
        
        A solution $x$ is {mo.as_html(list(x.value))}
        A dual solution is is {mo.as_html(list(prob.constraints[0].dual_value))}
        """
    )
    return


if __name__ == "__main__":
    app.run()