blob: 8e6ea726c9652e838da3175ee88b49d72d52de3d [file] [log] [blame]
Austin Schuh9049e202022-02-20 17:34:16 -08001Setup and solve
2===============
3
4
5Consider the following QP
6
7
8.. math::
9 \begin{array}{ll}
10 \mbox{minimize} & \frac{1}{2} x^T \begin{bmatrix}4 & 1\\ 1 & 2 \end{bmatrix} x + \begin{bmatrix}1 \\ 1\end{bmatrix}^T x \\
11 \mbox{subject to} & \begin{bmatrix}1 \\ 0 \\ 0\end{bmatrix} \leq \begin{bmatrix} 1 & 1\\ 1 & 0\\ 0 & 1\end{bmatrix} x \leq \begin{bmatrix}1 \\ 0.7 \\ 0.7\end{bmatrix}
12 \end{array}
13
14
15
16We show below how to solve the problem in Python, Matlab, Julia and C.
17
18
19
20Python
21------
22
23.. code:: python
24
25 import osqp
26 import numpy as np
27 from scipy import sparse
28
29 # Define problem data
30 P = sparse.csc_matrix([[4, 1], [1, 2]])
31 q = np.array([1, 1])
32 A = sparse.csc_matrix([[1, 1], [1, 0], [0, 1]])
33 l = np.array([1, 0, 0])
34 u = np.array([1, 0.7, 0.7])
35
36 # Create an OSQP object
37 prob = osqp.OSQP()
38
39 # Setup workspace and change alpha parameter
40 prob.setup(P, q, A, l, u, alpha=1.0)
41
42 # Solve problem
43 res = prob.solve()
44
45
46
47Matlab
48------
49
50.. code:: matlab
51
52 % Define problem data
53 P = sparse([4, 1; 1, 2]);
54 q = [1; 1];
55 A = sparse([1, 1; 1, 0; 0, 1]);
56 l = [1; 0; 0];
57 u = [1; 0.7; 0.7];
58
59 % Create an OSQP object
60 prob = osqp;
61
62 % Setup workspace and change alpha parameter
63 prob.setup(P, q, A, l, u, 'alpha', 1);
64
65 % Solve problem
66 res = prob.solve();
67
68
69
70Julia
71------
72
73.. code:: julia
74
75 using OSQP
76 using Compat.SparseArrays
77
78 # Define problem data
79 P = sparse([4. 1.; 1. 2.])
80 q = [1.; 1.]
81 A = sparse([1. 1.; 1. 0.; 0. 1.])
82 l = [1.; 0.; 0.]
83 u = [1.; 0.7; 0.7]
84
85 # Crate OSQP object
86 prob = OSQP.Model()
87
88 # Setup workspace and change alpha parameter
89 OSQP.setup!(prob; P=P, q=q, A=A, l=l, u=u, alpha=1)
90
91 # Solve problem
92 results = OSQP.solve!(prob)
93
94
95
96R
97-
98
99.. code:: r
100
101 library(osqp)
102 library(Matrix)
103
104 # Define problem data
105 P <- Matrix(c(4., 1.,
106 1., 2.), 2, 2, sparse = TRUE)
107 q <- c(1., 1.)
108 A <- Matrix(c(1., 1., 0.,
109 1., 0., 1.), 3, 2, sparse = TRUE)
110 l <- c(1., 0., 0.)
111 u <- c(1., 0.7, 0.7)
112
113 # Change alpha parameter and setup workspace
114 settings <- osqpSettings(alpha = 1.0)
115 model <- osqp(P, q, A, l, u, settings)
116
117 # Solve problem
118 res <- model$Solve()
119
120
121
122C
123-
124
125.. code:: c
126
127 #include "osqp.h"
128
129 int main(int argc, char **argv) {
130 // Load problem data
131 c_float P_x[3] = {4.0, 1.0, 2.0, };
132 c_int P_nnz = 3;
133 c_int P_i[3] = {0, 0, 1, };
134 c_int P_p[3] = {0, 1, 3, };
135 c_float q[2] = {1.0, 1.0, };
136 c_float A_x[4] = {1.0, 1.0, 1.0, 1.0, };
137 c_int A_nnz = 4;
138 c_int A_i[4] = {0, 1, 0, 2, };
139 c_int A_p[3] = {0, 2, 4, };
140 c_float l[3] = {1.0, 0.0, 0.0, };
141 c_float u[3] = {1.0, 0.7, 0.7, };
142 c_int n = 2;
143 c_int m = 3;
144
145 // Exitflag
146 c_int exitflag = 0;
147
148 // Workspace structures
149 OSQPWorkspace *work;
150 OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
151 OSQPData *data = (OSQPData *)c_malloc(sizeof(OSQPData));
152
153 // Populate data
154 if (data) {
155 data->n = n;
156 data->m = m;
157 data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
158 data->q = q;
159 data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
160 data->l = l;
161 data->u = u;
162 }
163
164 // Define solver settings as default
165 if (settings) {
166 osqp_set_default_settings(settings);
167 settings->alpha = 1.0; // Change alpha parameter
168 }
169
170 // Setup workspace
171 exitflag = osqp_setup(&work, data, settings);
172
173 // Solve Problem
174 osqp_solve(work);
175
176 // Cleanup
177 osqp_cleanup(work);
178 if (data) {
179 if (data->A) c_free(data->A);
180 if (data->P) c_free(data->P);
181 c_free(data);
182 }
183 if (settings) c_free(settings);
184
185 return exitflag;
186 };