Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
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 #endif
69 
70  for (int i = 0; i < argc; i++) {
71 #ifdef VISP_HAVE_DISPLAY
72  if (std::string(argv[i]) == "-d")
73  opt_display = false;
74  else
75 #endif
76  if (std::string(argv[i]) == "-h") {
77  std::cout << "\nUsage: " << argv[0] << " [-d] [-h]" << 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 #endif
86  " -h\n"
87  " Print the help.\n"<< std::endl;
88 
89  return EXIT_SUCCESS;
90  }
91  }
92  std::srand((long) vpTime::measureTimeMs());
93 
94  vpMatrix A, Q, C;
95  vpColVector b, d, r;
96 
97  A = randM(m,n)*5;
98  b = randV(m)*5;
99  Q = randM(o,n)*5;
100  r = randV(o)*5;
101  C = randM(p,n)*5;
102 
103  // make sure Cx <= d has a solution within Ax = b
104  vpColVector x = A.solveBySVD(b);
105  d = C*x;
106  for(int i = 0; i < p; ++i)
107  d[i] += (5.*rand())/RAND_MAX;
108 
109  // solver with warm start
110  vpQuadProg qp_WS;
111 
112  // timing
113  int total = 100;
114  double t_WS(0), t_noWS(0);
115  const double eps = 1e-2;
116 
117 #ifdef VISP_HAVE_DISPLAY
118  QPlot *plot = NULL;
119  if (opt_display)
120  plot = new QPlot(1, total, {"time to solveQP", "warm start"});
121 #endif
122 
123  for(int k = 0; k < total; ++k)
124  {
125  // reset active set at some point
126  if(k == total/2)
127  qp_WS.resetActiveSet();
128 
129  // small change on QP data
130  Q += eps * randM(o,n);
131  r += eps * randV(o);
132  A += eps * randM(m,n);
133  b += eps * randV(m);
134  C += eps * randM(p,n);
135  d += eps * randV(p);
136 
137  // solver without warm start
138  vpQuadProg qp;
139  x = 0;
140  double t = vpTime::measureTimeMs();
141  qp.solveQP(Q, r, A, b, C, d, x);
142 
143  t_noWS += vpTime::measureTimeMs() - t;
144 #ifdef VISP_HAVE_DISPLAY
145  if (opt_display)
146  plot->plot(0,0,k,t);
147 #endif
148 
149  // with warm start
150  x = 0;
151  t = vpTime::measureTimeMs();
152  qp_WS.solveQP(Q, r, A, b, C, d, x);
153 
154  t_WS += vpTime::measureTimeMs() - t;
155 #ifdef VISP_HAVE_DISPLAY
156  if (opt_display)
157  plot->plot(0, 1, k, t);
158 #endif
159  }
160 
161  std::cout.precision(3);
162  std::cout << "Warm start: t = " << t_WS << " ms (for 1 QP = " << t_WS/total << " ms)\n";
163  std::cout << "No warm start: t = " << t_noWS << " ms (for 1 QP = " << t_noWS/total << " ms)" << std::endl;
164 
165 #ifdef VISP_HAVE_DISPLAY
166  if (opt_display) {
167  plot->wait();
168  delete plot;
169  }
170 #endif
171  return EXIT_SUCCESS;
172 }
173 #else
174 int main()
175 {
176  std::cout << "You did not build ViSP with C++11 compiler flag" << std::endl;
177  std::cout << "Tip:" << std::endl;
178  std::cout << "- Configure ViSP again using cmake -DUSE_CPP11=ON, and build again this example" << std::endl;
179  return EXIT_SUCCESS;
180 }
181 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
void wait()
Definition: qp_plot.h:93
Definition: qp_plot.h:66
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
void solveBySVD(const vpColVector &B, vpColVector &x) const
Definition: vpMatrix.cpp:1669
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:73
void plot(int g, int c, int i, double t)
Definition: qp_plot.h:88
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void resetActiveSet()
Definition: vpQuadProg.h:100
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:389