Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
quadprog.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Example of sequential calls to QP solver
33  *
34 *****************************************************************************/
47 #include <iostream>
48 #include <visp3/core/vpConfig.h>
49 
50 #if defined(VISP_HAVE_LAPACK) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
51 
52 #include <visp3/core/vpQuadProg.h>
53 #include <visp3/core/vpTime.h>
54 
55 #include "qp_plot.h"
56 
57 int main(int argc, char **argv)
58 {
59  const int n = 20; // x dim
60  const int m = 10; // equality m < n
61  const int p = 30; // inequality
62  const int o = 16; // cost function
63 #ifdef VISP_HAVE_DISPLAY
64  bool opt_display = true;
65  bool opt_click_allowed = true;
66 #endif
67 
68  for (int i = 0; i < argc; i++) {
69 #ifdef VISP_HAVE_DISPLAY
70  if (std::string(argv[i]) == "-d")
71  opt_display = false;
72  else if (std::string(argv[i]) == "-c")
73  opt_click_allowed = false;
74  else
75 #endif
76  if (std::string(argv[i]) == "-h" || std::string(argv[i]) == "--help") {
77  std::cout << "\nUsage: " << argv[0] << " [-d] [-c] [-h] [--help]" << std::endl;
78  std::cout << "\nOptions: \n"
79 #ifdef VISP_HAVE_DISPLAY
80  " -d \n"
81  " Disable the image display. This can be useful \n"
82  " for automatic tests using crontab under Unix or \n"
83  " using the task manager under Windows.\n"
84  "\n"
85  " -c \n"
86  " Disable the mouse click. Useful to automate the \n"
87  " execution of this program without human intervention.\n"
88  "\n"
89 #endif
90  " -h, --help\n"
91  " Print the help.\n"
92  << std::endl;
93 
94  return EXIT_SUCCESS;
95  }
96  }
97  std::srand((long)vpTime::measureTimeMs());
98 
99  vpMatrix A, Q, C;
100  vpColVector b, d, r;
101 
102  A = randM(m, n) * 5;
103  b = randV(m) * 5;
104  Q = randM(o, n) * 5;
105  r = randV(o) * 5;
106  C = randM(p, n) * 5;
107 
108  // make sure Cx <= d has a solution within Ax = b
109  vpColVector x = A.solveBySVD(b);
110  d = C * x;
111  for (int i = 0; i < p; ++i)
112  d[i] += (5. * rand()) / RAND_MAX;
113 
114  // solver with warm start
115  vpQuadProg qp_WS;
116 
117  // timing
118  int total = 100;
119  double t_WS(0), t_noWS(0);
120  const double eps = 1e-2;
121 
122 #ifdef VISP_HAVE_DISPLAY
123  QPlot *plot = nullptr;
124  if (opt_display)
125  plot = new QPlot(1, total, { "time to solveQP", "warm start" });
126 #endif
127 
128  for (int k = 0; k < total; ++k) {
129  // reset active set at some point
130  if (k == total / 2)
131  qp_WS.resetActiveSet();
132 
133  // small change on QP data
134  Q += eps * randM(o, n);
135  r += eps * randV(o);
136  A += eps * randM(m, n);
137  b += eps * randV(m);
138  C += eps * randM(p, n);
139  d += eps * randV(p);
140 
141  // solver without warm start
142  vpQuadProg qp;
143  x = 0;
144  double t = vpTime::measureTimeMs();
145  qp.solveQP(Q, r, A, b, C, d, x);
146 
147  t_noWS += vpTime::measureTimeMs() - t;
148 #ifdef VISP_HAVE_DISPLAY
149  if (opt_display)
150  plot->plot(0, 0, k, t);
151 #endif
152 
153  // with warm start
154  x = 0;
155  t = vpTime::measureTimeMs();
156  qp_WS.solveQP(Q, r, A, b, C, d, x);
157 
158  t_WS += vpTime::measureTimeMs() - t;
159 #ifdef VISP_HAVE_DISPLAY
160  if (opt_display)
161  plot->plot(0, 1, k, t);
162 #endif
163  }
164 
165  std::cout.precision(3);
166  std::cout << "Warm start: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
167  std::cout << "No warm start: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)" << std::endl;
168 
169 #ifdef VISP_HAVE_DISPLAY
170  if (opt_display) {
171  if (opt_click_allowed) {
172  std::cout << "Click in the graph to exit..." << std::endl;
173  plot->wait();
174  }
175  delete plot;
176  }
177 #endif
178  return EXIT_SUCCESS;
179 }
180 #elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
181 int main()
182 {
183  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
184  std::cout << "Tip:" << std::endl;
185  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
186  return EXIT_SUCCESS;
187 }
188 #else
189 int main()
190 {
191  std::cout << "You did not build ViSP with Lapack support" << std::endl;
192  return EXIT_SUCCESS;
193 }
194 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
void solveBySVD(const vpColVector &B, vpColVector &x) const
Definition: vpMatrix.cpp:2010
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:70
bool solveQP(const vpMatrix &Q, const vpColVector &r, vpMatrix A, vpColVector b, const vpMatrix &C, const vpColVector &d, vpColVector &x, const double &tol=1e-6)
Definition: vpQuadProg.cpp:375
void resetActiveSet()
Definition: vpQuadProg.h:91
VISP_EXPORT double measureTimeMs()