forked from cvxpy/cvxpy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic DQCP docs, & quasiconcave curvature.
- Loading branch information
Showing
14 changed files
with
958 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
Fractional optimization | ||
======================= | ||
|
||
This notebook shows how to solve a simple *concave fractional problem*, | ||
in which the objective is to maximize the ratio of a nonnegative concave | ||
function and a positive convex function. Concave fractional problems are | ||
quasiconvex programs (QCPs). They can be specified using disciplined | ||
quasiconvex programming | ||
(`DQCP <https://www.cvxpy.org/tutorial/dqcp/index.html>`__), and hence | ||
can be solved using CVXPY. | ||
|
||
.. code:: | ||
!pip install --upgrade cvxpy | ||
.. code:: | ||
import cvxpy as cp | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
Our goal is to minimize the function | ||
|
||
.. math:: \frac{\sqrt{x}}{\exp(x)}. | ||
|
||
This function is not concave, but it is quasiconcave, as can be seen by | ||
inspecting its graph. | ||
|
||
.. code:: | ||
plt.plot([np.sqrt(y) / np.exp(y) for y in np.linspace(0, 10)]) | ||
plt.show() | ||
.. image:: concave_fractional_function_files/concave_fractional_function_4_0.png | ||
|
||
|
||
The below code specifies and solves the QCP, using DQCP. The concave | ||
fraction function is DQCP-compliant, because the ratio atom is | ||
quasiconcave (actually, quasilinear), increasing in the numerator when | ||
the denominator is positive, and decreasing in the denominator when the | ||
numerator is nonnegative. | ||
|
||
.. code:: | ||
x = cp.Variable() | ||
concave_fractional_fn = cp.sqrt(x) / cp.exp(x) | ||
problem = cp.Problem(cp.Maximize(concave_fractional_fn)) | ||
assert problem.is_dqcp() | ||
problem.solve(qcp=True) | ||
.. parsed-literal:: | ||
0.4288821220397949 | ||
.. code:: | ||
x.value | ||
.. parsed-literal:: | ||
array(0.50000165) | ||
Binary file added
BIN
+9 KB
...ples/dqcp/concave_fractional_function_files/concave_fractional_function_4_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
Minimum-length least squares | ||
============================ | ||
|
||
This notebook shows how to solve a *minimum-length least squares* | ||
problem, which finds a minimum-length vector :math:`x \in \mathbf{R}^n` | ||
achieving small mean-square error (MSE) for a particular least squares | ||
problem: | ||
|
||
.. math:: | ||
\begin{array}{ll} | ||
\mbox{minimize} & \mathrm{len}(x) \\ | ||
\mbox{subject to} & \frac{1}{n}\|Ax - b\|_2^2 \leq \epsilon, | ||
\end{array} | ||
where the variable is :math:`x` and the problem data are :math:`n`, | ||
:math:`A`, :math:`b`, and :math:`\epsilon`. | ||
|
||
This is a quasiconvex program (QCP). It can be specified using | ||
disciplined quasiconvex programming | ||
(`DQCP <https://www.cvxpy.org/tutorial/dqcp/index.html>`__), and it can | ||
therefore be solved using CVXPY. | ||
|
||
.. code:: | ||
!pip install --upgrade cvxpy | ||
.. code:: | ||
import cvxpy as cp | ||
import numpy as np | ||
The below cell constructs the problem data. | ||
|
||
.. code:: | ||
n = 10 | ||
np.random.seed(1) | ||
A = np.random.randn(n, n) | ||
x_star = np.random.randn(n) | ||
b = A @ x_star | ||
epsilon = 1e-2 | ||
And the next cell constructs and solves the QCP. | ||
|
||
.. code:: | ||
x = cp.Variable(n) | ||
mse = cp.sum_squares(A @ x - b)/n | ||
problem = cp.Problem(cp.Minimize(cp.length(x)), [mse <= epsilon]) | ||
print("Is problem DQCP?: ", problem.is_dqcp()) | ||
problem.solve(qcp=True) | ||
print("Found a solution, with length: ", problem.value) | ||
.. parsed-literal:: | ||
Is problem DQCP?: True | ||
Found a solution, with length: 8.0 | ||
.. code:: | ||
print("MSE: ", mse.value) | ||
.. parsed-literal:: | ||
MSE: 0.00926009328813662 | ||
.. code:: | ||
print("x: ", x.value) | ||
.. parsed-literal:: | ||
x: [-2.58366030e-01 1.38434327e+00 2.10714108e-01 9.44811159e-01 | ||
-1.14622208e+00 1.51283929e-01 6.62931941e-01 -1.16358584e+00 | ||
2.78132907e-13 -1.76314786e-13] | ||
.. code:: | ||
print("x_star: ", x_star) | ||
.. parsed-literal:: | ||
x_star: [-0.44712856 1.2245077 0.40349164 0.59357852 -1.09491185 0.16938243 | ||
0.74055645 -0.9537006 -0.26621851 0.03261455] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.