Skip to content

Commit 3ab8662

Browse files
authored
update readme with toy example
1 parent 723acf2 commit 3ab8662

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# DNLP — Disciplined Nonlinear Programming
22
The DNLP package is an extension of [CVXPY](https://www.cvxpy.org/) to general nonlinear programming (NLP).
33
DNLP allows smooth functions to be freely mixed with nonsmooth convex and concave functions,
4-
with some rules governing how nonsmooth convex and concave functions can appear. For details, see our paper [Disciplined Nonlinear Programming](XXX).
4+
with some rules governing how the nonsmooth functions can be used. For details, see our paper [Disciplined Nonlinear Programming](XXX).
55

66
---
77
## Installation
@@ -24,10 +24,32 @@ pip install .
2424

2525
---
2626
## Example
27-
Below we give a toy example. Many more examples, including the ones in the paper, can be found at [DNLP-examples](https://github.com/cvxgrp/dnlp-examples).
27+
Below we give a toy example where we maximize a convex quadratic function subject to a nonlinear equality constraint. Many more examples, including the ones in the paper, can be found at [DNLP-examples](https://github.com/cvxgrp/dnlp-examples).
2828
```python
2929
import cvxpy as cp
30+
import numpy as np
31+
import cvxpy as cp
32+
33+
# problem data
34+
np.random.seed(0)
35+
n = 3
36+
A = np.random.randn(n, n)
37+
A = A.T @ A
38+
39+
# formulate optimization problem
40+
x = cp.Variable(n)
41+
obj = cp.Maximize(cp.quad_form(x, A))
42+
constraints = [cp.sum_squares(x) == 1]
43+
44+
# initialize and solve
45+
x.value = np.ones(n)
46+
prob = cp.Problem(obj, constraints)
47+
prob.solve(nlp=True, verbose=True)
48+
print("Optimal value from DNLP: ", prob.value)
3049

50+
# the optimal value for this toy problem can also be found by computing the maximum eigenvalue of A
51+
eigenvalues = np.linalg.eigvalsh(A)
52+
print("Maximum eigenvalue: " , np.max(eigenvalues))
3153
```
3254

3355
---

0 commit comments

Comments
 (0)