osqp 1.0.1

The OSQP (Operator Splitting Quadratic Program) solver.
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
//!
//! The OSQP (Operator Splitting Quadratic Program) solver is a numerical optimization package for
//! solving convex quadratic programs in the form
//!
//! <div class="math">
//! \[\begin{split}\begin{array}{ll}
//!   \mbox{minimize} &amp; \frac{1}{2} x^T P x + q^T x \\
//!   \mbox{subject to} &amp; l \leq A x \leq u
//! \end{array}\end{split}\]
//! </div>
//!
//! <p>
//! where \(x\) is the optimization variable and \(P \in \mathbf{S}^{n}_{+}\) a positive
//! semidefinite matrix.
//! </p>
//!
//! Further information about the solver is available at
//! [osqp.org](https://osqp.org/).
//!
//! # Example
//!
//! Consider the following QP
//!
//! <div class="math">
//! \[\begin{split}\begin{array}{ll}
//!   \mbox{minimize} &amp; \frac{1}{2} x^T \begin{bmatrix}4 &amp; 1\\ 1 &amp; 2 \end{bmatrix} x + \begin{bmatrix}1 \\ 1\end{bmatrix}^T x \\
//!   \mbox{subject to} &amp; \begin{bmatrix}1 \\ 0 \\ 0\end{bmatrix} \leq \begin{bmatrix} 1 &amp; 1\\ 1 &amp; 0\\ 0 &amp; 1\end{bmatrix} x \leq  \begin{bmatrix}1 \\ 0.7 \\ 0.7\end{bmatrix}
//! \end{array}\end{split}\]
//! </div>
//!
//! ```rust
//! use osqp::{CscMatrix, Problem, Settings};
//!
//! // Define problem data
//! let P = &[[4.0, 1.0],
//!           [1.0, 2.0]];
//! let q = &[1.0, 1.0];
//! let A = &[[1.0, 1.0],
//!           [1.0, 0.0],
//!           [0.0, 1.0]];
//! let l = &[1.0, 0.0, 0.0];
//! let u = &[1.0, 0.7, 0.7];
//!
//! // Extract the upper triangular elements of `P`
//! let P = CscMatrix::from(P).into_upper_tri();
//!
//! // Disable verbose output
//! let settings = Settings::default()
//!     .verbose(false);
//! # let settings = settings.adaptive_rho(false);
//!
//! // Create an OSQP problem
//! let mut prob = Problem::new(P, q, A, l, u, &settings).expect("failed to setup problem");
//!
//! // Solve problem
//! let result = prob.solve();
//!
//! // Print the solution
//! println!("{:?}", result.x().expect("failed to solve problem"));
//! #
//! # // Check the solution
//! # let expected = &[0.30137570387082474, 0.6983956863817343];
//! # let x = result.solution().unwrap().x();
//! # assert_eq!(expected.len(), x.len());
//! # assert!(expected.iter().zip(x).all(|(&a, &b)| (a - b).abs() < 1e-9));
//! ```

extern crate osqp_sys;

use osqp_sys as ffi;
use std::error::Error;
use std::fmt;
use std::ptr;

mod csc;
pub use csc::CscMatrix;

mod settings;
pub use settings::{LinsysSolver, Settings};

mod status;

pub use status::{
    DualInfeasibilityCertificate, Failure, PolishStatus, PrimalInfeasibilityCertificate, Solution,
    Status,
};

#[allow(non_camel_case_types)]
type float = f64;

// Ensure osqp_int is the same size as usize/isize.
#[allow(dead_code)]
fn assert_osqp_int_size() {
    let _osqp_int_must_be_usize = ::std::mem::transmute::<ffi::osqp_int, usize>;
}

macro_rules! check {
    ($fun:ident, $ret:expr) => {
        assert!(
            $ret == 0,
            "osqp_{} failed with exit code {}",
            stringify!($fun),
            $ret
        );
    };
}

/// An instance of the OSQP solver.
pub struct Problem {
    solver: *mut ffi::OSQPSolver,
    /// Number of variables
    n: usize,
    /// Number of constraints
    m: usize,
}

impl Problem {
    /// Initialises the solver and validates the problem.
    ///
    /// Returns an error if the problem is non-convex or the solver cannot be initialised.
    ///
    /// Panics if any of the matrix or vector dimensions are incompatible, if `P` or `A` are not
    /// valid CSC matrices, or if `P` is not structurally upper triangular.
    #[allow(non_snake_case)]
    pub fn new<'a, 'b, T: Into<CscMatrix<'a>>, U: Into<CscMatrix<'b>>>(
        P: T,
        q: &[float],
        A: U,
        l: &[float],
        u: &[float],
        settings: &Settings,
    ) -> Result<Problem, SetupError> {
        // Function split to avoid monomorphising the main body of Problem::new.
        Problem::new_inner(P.into(), q, A.into(), l, u, settings)
    }

    #[allow(non_snake_case)]
    fn new_inner(
        P: CscMatrix,
        q: &[float],
        A: CscMatrix,
        l: &[float],
        u: &[float],
        settings: &Settings,
    ) -> Result<Problem, SetupError> {
        let invalid_data = |msg| Err(SetupError::DataInvalid(msg));

        unsafe {
            // Ensure the provided data is valid. While OSQP internally performs some validity
            // checks it can be made to read outside the provided buffers so all the invariants
            // are checked here.

            // Dimensions must be consistent with the number of variables
            let n = P.nrows;
            if P.ncols != n {
                return invalid_data("P must be a square matrix");
            }
            if q.len() != n {
                return invalid_data("q must be the same number of rows as P");
            }
            if A.ncols != n {
                return invalid_data("A must have the same number of columns as P");
            }

            // Dimensions must be consistent with the number of constraints
            let m = A.nrows;
            if l.len() != m {
                return invalid_data("l must have the same number of rows as A");
            }
            if u.len() != m {
                return invalid_data("u must have the same number of rows as A");
            }
            if l.iter().zip(u.iter()).any(|(&l, &u)| !(l <= u)) {
                return invalid_data("all elements of l must be less than or equal to the corresponding element of u");
            }

            // `A` and `P` must be valid CSC matrices and `P` must be structurally upper triangular
            if !P.is_valid() {
                return invalid_data("P must be a valid CSC matrix");
            }
            if !A.is_valid() {
                return invalid_data("A must be a valid CSC matrix");
            }
            if !P.is_structurally_upper_tri() {
                return invalid_data("P must be structurally upper triangular");
            }

            // Calling `to_ffi` is safe as we have ensured that `P` and `A` are valid CSC matrices.
            let P_ffi = P.to_ffi();
            let A_ffi = A.to_ffi();

            let settings = &settings.inner as *const ffi::OSQPSettings as *mut ffi::OSQPSettings;
            let mut solver: *mut ffi::OSQPSolver = ptr::null_mut();

            let status = ffi::osqp_setup(&mut solver, P_ffi, q.as_ptr(), A_ffi, l.as_ptr(), u.as_ptr(), m as ffi::osqp_int, n as ffi::osqp_int, settings);
            let err = match status as ffi::osqp_error_type {
                0 => return Ok(Problem { solver, n, m }),
                ffi::OSQP_DATA_VALIDATION_ERROR => SetupError::DataInvalid(""),
                ffi::OSQP_SETTINGS_VALIDATION_ERROR => SetupError::SettingsInvalid,
                ffi::OSQP_ALGEBRA_LOAD_ERROR => SetupError::LinsysSolverLoadFailed,
                ffi::OSQP_LINSYS_SOLVER_INIT_ERROR => SetupError::LinsysSolverInitFailed,
                ffi::OSQP_NONCVX_ERROR => SetupError::NonConvex,
                ffi::OSQP_MEM_ALLOC_ERROR => SetupError::MemoryAllocationFailed,
                _ => unreachable!(),
            };

            // If the call to `osqp_setup` fails the `OSQPSolver` may be partially allocated
            if !solver.is_null() {
                ffi::osqp_cleanup(solver);
            }
            Err(err)
        }
    }

    /// Sets the linear part of the cost function to `q`.
    ///
    /// Panics if the length of `q` is not the same as the number of problem variables.
    pub fn update_lin_cost(&mut self, q: &[float]) {
        unsafe {
            assert_eq!(self.n, q.len());
            check!(
                update_lin_cost,
                ffi::osqp_update_data_vec(self.solver, q.as_ptr(), ptr::null(), ptr::null())
            );
        }
    }

    /// Sets the lower and upper bounds of the constraints to `l` and `u`.
    ///
    /// Panics if the length of `l` or `u` is not the same as the number of problem constraints.
    pub fn update_bounds(&mut self, l: &[float], u: &[float]) {
        unsafe {
            assert_eq!(self.m, l.len());
            assert_eq!(self.m, u.len());
            check!(
                update_bounds,
                ffi::osqp_update_data_vec(self.solver, ptr::null(), l.as_ptr(), u.as_ptr())
            );
        }
    }

    /// Sets the lower bound of the constraints to `l`.
    ///
    /// Panics if the length of `l` is not the same as the number of problem constraints.
    pub fn update_lower_bound(&mut self, l: &[float]) {
        unsafe {
            assert_eq!(self.m, l.len());
            check!(
                update_lower_bound,
                ffi::osqp_update_data_vec(self.solver, ptr::null(), l.as_ptr(), ptr::null())
            );
        }
    }

    /// Sets the upper bound of the constraints to `u`.
    ///
    /// Panics if the length of `u` is not the same as the number of problem constraints.
    pub fn update_upper_bound(&mut self, u: &[float]) {
        unsafe {
            assert_eq!(self.m, u.len());
            check!(
                update_upper_bound,
                ffi::osqp_update_data_vec(self.solver, ptr::null(), ptr::null(), u.as_ptr())
            );
        }
    }

    /// Warm starts the primal variables at `x` and the dual variables at `y`.
    ///
    /// Panics if the length of `x` is not the same as the number of problem variables or the
    /// length of `y` is not the same as the number of problem constraints.
    pub fn warm_start(&mut self, x: &[float], y: &[float]) {
        unsafe {
            assert_eq!(self.n, x.len());
            assert_eq!(self.m, y.len());
            check!(
                warm_start,
                ffi::osqp_warm_start(self.solver, x.as_ptr(), y.as_ptr())
            );
        }
    }

    /// Warm starts the primal variables at `x`.
    ///
    /// Panics if the length of `x` is not the same as the number of problem variables.
    pub fn warm_start_x(&mut self, x: &[float]) {
        unsafe {
            assert_eq!(self.n, x.len());
            check!(
                warm_start_x,
                ffi::osqp_warm_start(self.solver, x.as_ptr(), ptr::null())
            );
        }
    }

    /// Warms start the dual variables at `y`.
    ///
    /// Panics if the length of `y` is not the same as the number of problem constraints.
    pub fn warm_start_y(&mut self, y: &[float]) {
        unsafe {
            assert_eq!(self.m, y.len());
            check!(
                warm_start_y,
                ffi::osqp_warm_start(self.solver, ptr::null(), y.as_ptr())
            );
        }
    }

    /// Updates the elements of matrix `P` without changing its sparsity structure.
    ///
    /// Panics if the sparsity structure of `P` differs from the sparsity structure of the `P`
    /// matrix provided to `Problem::new`.
    #[allow(non_snake_case)]
    pub fn update_P<'a, T: Into<CscMatrix<'a>>>(&mut self, P: T) {
        self.update_P_inner(P.into());
    }

    #[allow(non_snake_case)]
    fn update_P_inner(&mut self, P: CscMatrix) {
        unsafe {
            // TODO: can no longer get current P/q/A/l/u from the solver?
            // let P_ffi = CscMatrix::from_ffi((*(*self.solver).work).P);
            // P.assert_same_sparsity_structure(&P_ffi);

            check!(
                update_P,
                ffi::osqp_update_data_mat(
                    self.solver,
                    P.data.as_ptr(),
                    ptr::null(),
                    P.data.len() as ffi::osqp_int,
                    ptr::null(),
                    ptr::null(),
                    0
                )
            );
        }
    }

    /// Updates the elements of matrix `A` without changing its sparsity structure.
    ///
    /// Panics if the sparsity structure of `A` differs from the sparsity structure of the `A`
    /// matrix provided to `Problem::new`.
    #[allow(non_snake_case)]
    pub fn update_A<'a, T: Into<CscMatrix<'a>>>(&mut self, A: T) {
        self.update_A_inner(A.into());
    }

    #[allow(non_snake_case)]
    fn update_A_inner(&mut self, A: CscMatrix) {
        unsafe {
            // TODO: can no longer get current P/q/A/l/u from the solver?
            // let A_ffi = CscMatrix::from_ffi((*(*self.workspace).data).A);
            // A.assert_same_sparsity_structure(&A_ffi);

            check!(
                update_A,
                ffi::osqp_update_data_mat(
                    self.solver,
                    ptr::null(),
                    ptr::null(),
                    0,
                    A.data.as_ptr(),
                    ptr::null(),
                    A.data.len() as ffi::osqp_int,
                )
            );
        }
    }

    #[allow(non_snake_case)]
    pub fn update_settings(&mut self, settings: &Settings) {
        let settings = &settings.inner as *const ffi::OSQPSettings;
        unsafe {
            check!(
                update_settings,
                ffi::osqp_update_settings(self.solver, settings)
            );
        }
    }

    /// Attempts to solve the quadratic program.
    pub fn solve<'a>(&'a mut self) -> Status<'a> {
        unsafe {
            check!(solve, ffi::osqp_solve(self.solver));
            Status::from_problem(self)
        }
    }
}

impl Drop for Problem {
    fn drop(&mut self) {
        unsafe {
            ffi::osqp_cleanup(self.solver);
        }
    }
}

unsafe impl Send for Problem {}
unsafe impl Sync for Problem {}

/// An error that can occur when setting up the solver.
#[derive(Debug)]
pub enum SetupError {
    DataInvalid(&'static str),
    SettingsInvalid,
    LinsysSolverLoadFailed,
    LinsysSolverInitFailed,
    NonConvex,
    MemoryAllocationFailed,
    // Prevent exhaustive enum matching
    #[doc(hidden)]
    __Nonexhaustive,
}

impl fmt::Display for SetupError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SetupError::DataInvalid(msg) => {
                "problem data invalid".fmt(f)?;
                if !msg.is_empty() {
                    ": ".fmt(f)?;
                    msg.fmt(f)?;
                }
                Ok(())
            }
            SetupError::SettingsInvalid => "problem settings invalid".fmt(f),
            SetupError::LinsysSolverLoadFailed => "linear system solver failed to load".fmt(f),
            SetupError::LinsysSolverInitFailed => {
                "linear system solver failed to initialise".fmt(f)
            }
            SetupError::NonConvex => "problem non-convex".fmt(f),
            SetupError::MemoryAllocationFailed => "memory allocation failed".fmt(f),
            SetupError::__Nonexhaustive => unreachable!(),
        }
    }
}

impl Error for SetupError {}

#[cfg(test)]
mod tests {
    use std::iter;

    use super::*;

    #[test]
    #[allow(non_snake_case)]
    fn update_settings() {
        // Directly update some settings using methods on the Settings object

        let settings = Settings::default().alpha(0.7).verbose(false);
        let settings = settings.adaptive_rho(true);

        assert_eq!(settings.inner.alpha, 0.7);
        assert_eq!(settings.inner.verbose, 0);
        assert_eq!(settings.inner.adaptive_rho, 1);
    }

    #[test]
    #[allow(non_snake_case)]
    fn update_rho() {
        // Update a setting using a method generated by the settings macro

        // Define problem data
        let P = CscMatrix::from(&[[4.0, 1.0], [1.0, 2.0]]).into_upper_tri();
        let q = &[1.0, 1.0];
        let A = &[[1.0, 1.0], [1.0, 0.0], [0.0, 1.0]];
        let l = &[1.0, 0.0, 0.0];
        let u = &[1.0, 0.7, 0.7];

        let settings = Settings::default().verbose(false);
        let mut prob = Problem::new(&P, q, A, l, u, &settings).unwrap();

        prob.update_rho(0.7);
        unsafe {
            let solver_ref = prob.solver.as_ref().unwrap();
            let settings_ref = solver_ref.settings.as_ref().unwrap();

            assert!((*settings_ref).rho == 0.7, "Expected rho to be 0.7 but found {}", (*settings_ref).rho);

        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn update_prob_settings() {
        // Update some settings using update_settings on the Problem object

        // Define problem data
        let P = CscMatrix::from(&[[4.0, 1.0], [1.0, 2.0]]).into_upper_tri();
        let q = &[1.0, 1.0];
        let A = &[[1.0, 1.0], [1.0, 0.0], [0.0, 1.0]];
        let l = &[1.0, 0.0, 0.0];
        let u = &[1.0, 0.7, 0.7];

        let settings = Settings::default().verbose(false);
        let mut prob = Problem::new(&P, q, A, l, u, &settings).unwrap();

        let settings = settings.max_iter(1_000_000);
        prob.update_settings(&settings);
        unsafe {
            let solver_ref = prob.solver.as_ref().unwrap();
            let settings_ref = solver_ref.settings.as_ref().unwrap();

            assert_eq!((*settings_ref).max_iter, 1_000_000);

        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn update_matrices() {
        // Define problem data
        let P_wrong = CscMatrix::from(&[[2.0, 1.0], [1.0, 4.0]]).into_upper_tri();
        let A_wrong = &[[2.0, 3.0], [1.0, 0.0], [0.0, 9.0]];

        let P = CscMatrix::from(&[[4.0, 1.0], [1.0, 2.0]]).into_upper_tri();
        let q = &[1.0, 1.0];
        let A = &[[1.0, 1.0], [1.0, 0.0], [0.0, 1.0]];
        let l = &[1.0, 0.0, 0.0];
        let u = &[1.0, 0.7, 0.7];

        // Change the default alpha and disable verbose output
        let settings = Settings::default().alpha(1.0).verbose(false);
        let settings = settings.adaptive_rho(false);

        // Check updating P and A separately
        let mut prob = Problem::new(&P_wrong, q, A_wrong, l, u, &settings).unwrap();
        prob.update_P(&P);
        prob.update_A(A);
        let result = prob.solve();
        let x = result.solution().unwrap().x();
        let expected = &[0.2987710845986426, 0.701227995544065];
        assert_eq!(expected.len(), x.len());
        assert!(expected.iter().zip(x).all(|(&a, &b)| (a - b).abs() < 1e-9));
    }

    #[test]
    #[allow(non_snake_case)]
    fn empty_A() {
        let P = CscMatrix::from(&[[4.0, 1.0], [1.0, 2.0]]).into_upper_tri();
        let q = &[1.0, 1.0];

        let A = CscMatrix::from_column_iter_dense(0, 2, iter::empty());
        let l = &[];
        let u = &[];
        let mut prob = Problem::new(&P, q, &A, l, u, &Settings::default()).unwrap();
        prob.update_A(&A);

        let A = CscMatrix::from(&[[0.0, 0.0], [0.0, 0.0]]);
        assert_eq!(A.data.len(), 0);
        let l = &[0.0, 0.0];
        let u = &[1.0, 1.0];
        let mut prob = Problem::new(&P, q, &A, l, u, &Settings::default()).unwrap();
        prob.update_A(&A);
    }
}