File: osqp_quadratic_program.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (65 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download | duplicates (3)
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
/*
* This example shows how to formulate and solve the following QP problem:
* https://osqp.org/docs/examples/setup-and-solve.html
*
*   Minimize
*   Objective:
*     1/2(4x1^2 + 2x1x2 + 2x2^2) + (x1 + x2) + 0 or in the matrix form
*     1/2 x^T|4 1|x + |1|^Tx + 0
*            |1 2|    |1|
*   Subject to
*     1 <= x1 + x2 <= 1
*     0 <= x1      <= 0.7
*     0 <=      x2 <= 0.7 or in the matrix form
*     |1|    |1 1|     |1.0|
*     |0| <= |1 0|x <= |0.7|
*     |0|    |0 1|     |0.7|
*
*   Expected results: x1 = 0.3; x2 = 0.7;
*/

#include <vector>
#include <iostream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/OSQP_quadratic_program_traits.h>

using Kernel = CGAL::Simple_cartesian<double>;
using FT = typename Kernel::FT;

int main(void) {

  const std::size_t n = 2; // number of variables
  const std::size_t m = 3; // number of constraints
  CGAL::OSQP_quadratic_program_traits<FT> osqp(n, m);

  osqp.set_P(0, 0, 4);
  osqp.set_P(0, 1, 1);
  osqp.set_P(1, 1, 2);

  osqp.set_q(0, 1);
  osqp.set_q(1, 1);

  osqp.set_r(0);

  osqp.set_A(0, 0, 1);
  osqp.set_A(0, 1, 1);
  osqp.set_A(1, 0, 1);
  osqp.set_A(2, 1, 1);

  osqp.set_l(0, 1);
  osqp.set_l(1, 0);
  osqp.set_l(2, 0);

  osqp.set_u(0, 1);
  osqp.set_u(1, 0.7);
  osqp.set_u(2, 0.7);

  std::vector<FT> x; x.reserve(2);
  osqp.solve(std::back_inserter(x));

  std::cout << "solution (x1 x2): ";
  for (const FT value : x) {
    std::cout << value << "; ";
  }
  std::cout << std::endl;
}