-
Notifications
You must be signed in to change notification settings - Fork 3
/
dcp-analysis.py
196 lines (139 loc) · 3.2 KB
/
dcp-analysis.py
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
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "cvxpy",
# "numpy",
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.7.21-dev18"
app = marimo.App()
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
# DCP analysis
In this exercise, you will learn how to use the DCP ruleset to write convex functions in a DCP-compatible way.
"""
)
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""_This notebook accompanies [Lecture 1, Introduction to Convex Optimization](https://www.cvxgrp.org/nasa/pdf/lecture1.pdf), of the Convex Optimization Short Course, which was held at NASA in the summer of 2024._""")
return
@app.cell
def __():
import cvxpy as cp
import numpy as np
return cp, np
@app.cell
def __(mo):
mo.md(
r"""
### Problem 1:
\[
f(x) = \exp(\sqrt{x})
\]
- Why is this function not DCP?
- Which property would the inner function, here $\sqrt{x}$, need, to make $f$ DCP-compliant?
"""
)
return
@app.cell
def __(cp):
_x = cp.Variable()
_g = cp.sqrt(_x)
_f = cp.exp(_g)
_f.is_dcp()
# TODO replace _g with a different CVXPY function to make _f DCP compliant
return
@app.cell
def __(mo):
mo.md(
r"""
### Problem 2:
\[
f(x) = \sqrt{x^2}
\]
- Why is this function not DCP?
_Hint: This function has a special name._
"""
)
return
@app.cell
def __(cp):
_x = cp.Variable()
_f = cp.sqrt(_x**2)
_f.is_dcp()
# TODO explain why the function isn't DCP and rewrite it to satisfy DCP.
return
@app.cell
def __(mo):
mo.md(
r"""
### Problem 3:
\[
f(x) = \sqrt{x^2 + 1}
\]
- Why is this function not DCP?
_Hint: `Use cp.hstack([a, b])` to create a vector (a, b)_
"""
)
return
@app.cell
def __(cp):
_x = cp.Variable()
_f = cp.sqrt(_x**2 + 1)
_f.is_dcp()
# TODO explain why the function isn't DCP and rewrite it to satisfy DCP.
return
@app.cell
def __(mo):
mo.md(
r"""
### Problem 4:
\[
f(x) = \left( \max(x, 4) -3 \right)^2
\]
- Why is this function not DCP?
_Hint: Can we rewrite the inner expression so CVXPY can infer the sign?_
"""
)
return
@app.cell
def __(cp):
_x = cp.Variable()
_f = (cp.maximum(_x, 4) - 3) ** 2
_f.is_dcp()
# TODO explain why the function isn't DCP and rewrite it to satisfy DCP.
return
@app.cell
def __(mo):
mo.md(
r"""
### Problem 5:
\[
f(x) = \frac{cx}{u - x}
\]
- Why is this function not DCP?
_Hint: Add_ $\text{ }cu - cu\text{ }$_ to the numerator._
"""
)
return
@app.cell
def __(cp):
_x = cp.Variable()
_c = 1
_u = 2
# a / b is written as a * cp.inv_pos(b) in CVXPY
_f = _c * _x * cp.inv_pos(_u - _x)
_f.is_dcp()
# TODO explain why the function isn't DCP and rewrite it to satisfy DCP.
return
@app.cell
def __():
import marimo as mo
return mo,
if __name__ == "__main__":
app.run()