Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
quadprog.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Olivier Kermorgant
36  *
37  *****************************************************************************/
50 #include <iostream>
51 #include <visp3/core/vpConfig.h>
52 
53 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_LAPACK)
54 
55 #include <visp3/core/vpQuadProg.h>
56 #include <visp3/core/vpTime.h>
57 
58 #include "qp_plot.h"
59 
60 int main (int argc, char **argv)
61 {
62  const int n = 20; // x dim
63  const int m = 10; // equality m < n
64  const int p = 30; // inequality
65  const int o = 16; // cost function
66 #ifdef VISP_HAVE_DISPLAY
67  bool opt_display = true;
68  bool opt_click_allowed = true;
69 #endif
70 
71  for (int i = 0; i < argc; i++) {
72 #ifdef VISP_HAVE_DISPLAY
73  if (std::string(argv[i]) == "-d")
74  opt_display = false;
75  else if (std::string(argv[i]) == "-c")
76  opt_click_allowed = false;
77  else
78 #endif
79  if (std::string(argv[i]) == "-h" || std::string(argv[i]) == "--help") {
80  std::cout << "\nUsage: " << argv[0] << " [-d] [-c] [-h] [--help]" << std::endl;
81  std::cout << "\nOptions: \n"
82 #ifdef VISP_HAVE_DISPLAY
83  " -d \n"
84  " Disable the image display. This can be useful \n"
85  " for automatic tests using crontab under Unix or \n"
86  " using the task manager under Windows.\n"
87  "\n"
88  " -c \n"
89  " Disable the mouse click. Useful to automate the \n"
90  " execution of this program without humain intervention.\n"
91  "\n"
92 #endif
93  " -h, --help\n"
94  " Print the help.\n"<< std::endl;
95 
96  return EXIT_SUCCESS;
97  }
98  }
99  std::srand((long) vpTime::measureTimeMs());
100 
101  vpMatrix A, Q, C;
102  vpColVector b, d, r;
103 
104  A = randM(m,n)*5;
105  b = randV(m)*5;
106  Q = randM(o,n)*5;
107  r = randV(o)*5;
108  C = randM(p,n)*5;
109 
110  // make sure Cx <= d has a solution within Ax = b
111  vpColVector x = A.solveBySVD(b);
112  d = C*x;
113  for(int i = 0; i < p; ++i)
114  d[i] += (5.*rand())/RAND_MAX;
115 
116  // solver with warm start
117  vpQuadProg qp_WS;
118 
119  // timing
120  int total = 100;
121  double t_WS(0), t_noWS(0);
122  const double eps = 1e-2;
123 
124 #ifdef VISP_HAVE_DISPLAY
125  QPlot *plot = NULL;
126  if (opt_display)
127  plot = new QPlot(1, total, {"time to solveQP", "warm start"});
128 #endif
129 
130  for(int k = 0; k < total; ++k)
131  {
132  // reset active set at some point
133  if(k == total/2)
134  qp_WS.resetActiveSet();
135 
136  // small change on QP data
137  Q += eps * randM(o,n);
138  r += eps * randV(o);
139  A += eps * randM(m,n);
140  b += eps * randV(m);
141  C += eps * randM(p,n);
142  d += eps * randV(p);
143 
144  // solver without warm start
145  vpQuadProg qp;
146  x = 0;
147  double t = vpTime::measureTimeMs();
148  qp.solveQP(Q, r, A, b, C, d, x);
149 
150  t_noWS += vpTime::measureTimeMs() - t;
151 #ifdef VISP_HAVE_DISPLAY
152  if (opt_display)
153  plot->plot(0,0,k,t);
154 #endif
155 
156  // with warm start
157  x = 0;
158  t = vpTime::measureTimeMs();
159  qp_WS.solveQP(Q, r, A, b, C, d, x);
160 
161  t_WS += vpTime::measureTimeMs() - t;
162 #ifdef VISP_HAVE_DISPLAY
163  if (opt_display)
164  plot->plot(0, 1, k, t);
165 #endif
166  }
167 
168  std::cout.precision(3);
169  std::cout << "Warm start: t = " << t_WS << " ms (for 1 QP = " << t_WS/total << " ms)\n";
170  std::cout << "No warm start: t = " << t_noWS << " ms (for 1 QP = " << t_noWS/total << " ms)" << std::endl;
171 
172 #ifdef VISP_HAVE_DISPLAY
173  if (opt_display) {
174  if (opt_click_allowed) {
175  std::cout << "Click in the graph to exit..." << std::endl;
176  plot->wait();
177  }
178  delete plot;
179  }
180 #endif
181  return EXIT_SUCCESS;
182 }
183 #elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
184 int main()
185 {
186  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
187  std::cout << "Tip:" << std::endl;
188  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
189  return EXIT_SUCCESS;
190 }
191 #else
192 int main()
193 {
194  std::cout << "You did not build ViSP with Lapack support" << std::endl;
195  return EXIT_SUCCESS;
196 }
197 #endif
void solveBySVD(const vpColVector &B, vpColVector &x) const
Definition: vpMatrix.cpp:1908
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:74
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void resetActiveSet()
Definition: vpQuadProg.h:101
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:373