Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame^] | 1 | Least-squares |
| 2 | ============= |
| 3 | |
| 4 | Consider the following constrained least-squares problem |
| 5 | |
| 6 | .. math:: |
| 7 | \begin{array}{ll} |
| 8 | \mbox{minimize} & \frac{1}{2} \|Ax - b\|_2^2 \\ |
| 9 | \mbox{subject to} & 0 \leq x \leq 1 |
| 10 | \end{array} |
| 11 | |
| 12 | The problem has the following equivalent form |
| 13 | |
| 14 | .. math:: |
| 15 | \begin{array}{ll} |
| 16 | \mbox{minimize} & \frac{1}{2} y^T y \\ |
| 17 | \mbox{subject to} & y = A x - b \\ |
| 18 | & 0 \le x \le 1 |
| 19 | \end{array} |
| 20 | |
| 21 | |
| 22 | |
| 23 | Python |
| 24 | ------ |
| 25 | |
| 26 | .. code:: python |
| 27 | |
| 28 | import osqp |
| 29 | import numpy as np |
| 30 | import scipy as sp |
| 31 | from scipy import sparse |
| 32 | |
| 33 | # Generate problem data |
| 34 | sp.random.seed(1) |
| 35 | m = 30 |
| 36 | n = 20 |
| 37 | Ad = sparse.random(m, n, density=0.7, format='csc') |
| 38 | b = np.random.randn(m) |
| 39 | |
| 40 | # OSQP data |
| 41 | P = sparse.block_diag([sparse.csc_matrix((n, n)), sparse.eye(m)], format='csc') |
| 42 | q = np.zeros(n+m) |
| 43 | A = sparse.vstack([ |
| 44 | sparse.hstack([Ad, -sparse.eye(m)]), |
| 45 | sparse.hstack([sparse.eye(n), sparse.csc_matrix((n, m))])], format='csc') |
| 46 | l = np.hstack([b, np.zeros(n)]) |
| 47 | u = np.hstack([b, np.ones(n)]) |
| 48 | |
| 49 | # Create an OSQP object |
| 50 | prob = osqp.OSQP() |
| 51 | |
| 52 | # Setup workspace |
| 53 | prob.setup(P, q, A, l, u) |
| 54 | |
| 55 | # Solve problem |
| 56 | res = prob.solve() |
| 57 | |
| 58 | |
| 59 | |
| 60 | Matlab |
| 61 | ------ |
| 62 | |
| 63 | .. code:: matlab |
| 64 | |
| 65 | % Generate problem data |
| 66 | rng(1) |
| 67 | m = 30; |
| 68 | n = 20; |
| 69 | Ad = sprandn(m, n, 0.7); |
| 70 | b = randn(m, 1); |
| 71 | |
| 72 | % OSQP data |
| 73 | P = blkdiag(sparse(n, n), speye(m)); |
| 74 | q = zeros(n+m, 1); |
| 75 | A = [Ad, -speye(m); |
| 76 | speye(n), sparse(n, m)]; |
| 77 | l = [b; zeros(n, 1)]; |
| 78 | u = [b; ones(n, 1)]; |
| 79 | |
| 80 | % Create an OSQP object |
| 81 | prob = osqp; |
| 82 | |
| 83 | % Setup workspace |
| 84 | prob.setup(P, q, A, l, u); |
| 85 | |
| 86 | % Solve problem |
| 87 | res = prob.solve(); |
| 88 | |
| 89 | |
| 90 | |
| 91 | CVXPY |
| 92 | ----- |
| 93 | |
| 94 | .. code:: python |
| 95 | |
| 96 | from cvxpy import * |
| 97 | import numpy as np |
| 98 | import scipy as sp |
| 99 | from scipy import sparse |
| 100 | |
| 101 | # Generate problem data |
| 102 | sp.random.seed(1) |
| 103 | m = 30 |
| 104 | n = 20 |
| 105 | A = sparse.random(m, n, density=0.7, format='csc') |
| 106 | b = np.random.randn(m) |
| 107 | |
| 108 | # Define problem |
| 109 | x = Variable(n) |
| 110 | objective = 0.5*sum_squares(A*x - b) |
| 111 | constraints = [x >= 0, x <= 1] |
| 112 | |
| 113 | # Solve with OSQP |
| 114 | Problem(Minimize(objective), constraints).solve(solver=OSQP) |
| 115 | |
| 116 | |
| 117 | |
| 118 | YALMIP |
| 119 | ------ |
| 120 | |
| 121 | .. code:: matlab |
| 122 | |
| 123 | % Generate data |
| 124 | rng(1) |
| 125 | m = 30; |
| 126 | n = 20; |
| 127 | A = sprandn(m, n, 0.7); |
| 128 | b = randn(m, 1); |
| 129 | |
| 130 | % Define problem |
| 131 | x = sdpvar(n, 1); |
| 132 | objective = 0.5*norm(A*x - b)^2; |
| 133 | constraints = [ 0 <= x <= 1]; |
| 134 | |
| 135 | % Solve with OSQP |
| 136 | options = sdpsettings('solver','osqp'); |
| 137 | optimize(constraints, objective, options); |
| 138 | |