Visual Servoing Platform  version 3.4.0
quadprog_eq.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 with constant equality constraint
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 #include "qp_plot.h"
58 
59 int main (int argc, char **argv)
60 {
61  const int n = 20; // x dim
62  const int m = 10; // equality m < n
63  const int p = 30; // inequality
64  const int o = 16; // cost function
65 #ifdef VISP_HAVE_DISPLAY
66  bool opt_display = true;
67 #endif
68 
69  for (int i = 0; i < argc; i++) {
70 #ifdef VISP_HAVE_DISPLAY
71  if (std::string(argv[i]) == "-d")
72  opt_display = false;
73  else
74 #endif
75  if (std::string(argv[i]) == "-h") {
76  std::cout << "\nUsage: " << argv[0] << " [-d] [-h]" << std::endl;
77  std::cout << "\nOptions: \n"
78 #ifdef VISP_HAVE_DISPLAY
79  " -d \n"
80  " Disable the image display. This can be useful \n"
81  " for automatic tests using crontab under Unix or \n"
82  " using the task manager under Windows.\n"
83  "\n"
84 #endif
85  " -h\n"
86  " Print the help.\n"<< std::endl;
87 
88  return EXIT_SUCCESS;
89  }
90  }
91  std::srand((long) vpTime::measureTimeMs());
92 
93  vpMatrix A, Q, C;
94  vpColVector b, d, r;
95 
96  A = randM(m,n)*5;
97  b = randV(m)*5;
98  Q = randM(o,n)*5;
99  r = randV(o)*5;
100  C = randM(p,n)*5;
101 
102  // make sure Cx <= d has a solution within Ax = b
103  vpColVector x = A.solveBySVD(b);
104  d = C*x;
105  for(int i = 0; i < p; ++i)
106  d[i] += (5.*rand())/RAND_MAX;
107 
108  // solver with stored equality and warm start
109  vpQuadProg qp_WS;
110  qp_WS.setEqualityConstraint(A, b);
111 
112  vpQuadProg qp_ineq_WS;
113  qp_ineq_WS.setEqualityConstraint(A, b);
114 
115  // timing
116  int total = 100;
117  double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
118  const double eps = 1e-2;
119 
120 #ifdef VISP_HAVE_DISPLAY
121  QPlot *plot = NULL;
122  if (opt_display)
123  plot = new QPlot(2, total, {"only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start"});
124 #endif
125 
126  for(int k = 0; k < total; ++k)
127  {
128  // small change on QP data (A and b are constant)
129  Q += eps * randM(o,n);
130  r += eps * randV(o);
131  C += eps * randM(p,n);
132  d += eps * randV(p);
133 
134  // solve only equalities
135  // without warm start
136  x = 0;
137  double t = vpTime::measureTimeMs();
138  vpQuadProg::solveQPe(Q, r, A, b, x);
139 
140  t_noWS += vpTime::measureTimeMs() - t;
141 #ifdef VISP_HAVE_DISPLAY
142  if (opt_display)
143  plot->plot(0, 0, k, t);
144 #endif
145 
146  // with pre-solved Ax = b
147  x = 0;
148  t = vpTime::measureTimeMs();
149  qp_WS.solveQPe(Q, r, x);
150 
151  t_WS += vpTime::measureTimeMs() - t;
152 #ifdef VISP_HAVE_DISPLAY
153  if (opt_display)
154  plot->plot(0, 1, k, t);
155 #endif
156 
157  // with inequalities
158  // without warm start
159  x = 0;
160  vpQuadProg qp;
161  t = vpTime::measureTimeMs();
162  qp.solveQP(Q, r, A, b, C, d, x);
163 
164  t_ineq_noWS += vpTime::measureTimeMs() - t;
165 #ifdef VISP_HAVE_DISPLAY
166  if (opt_display)
167  plot->plot(1, 0, k, t);
168 #endif
169 
170  // with warm start + pre-solving
171  x = 0;
172  t = vpTime::measureTimeMs();
173  qp_ineq_WS.solveQPi(Q, r, C, d, x, true);
174 
175  t_ineq_WS += vpTime::measureTimeMs() - t;
176 #ifdef VISP_HAVE_DISPLAY
177  if (opt_display)
178  plot->plot(1, 1, k, t);
179 #endif
180  }
181 
182  std::cout.precision(3);
183  std::cout << "With only equality constraints\n";
184  std::cout << " pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS/total << " ms)\n";
185  std::cout << " no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS/total << " ms)\n\n";
186 
187  std::cout << "With inequality constraints\n";
188  std::cout << " Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS/total << " ms)\n";
189  std::cout << " No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS/total << " ms)" << std::endl;
190 
191 #ifdef VISP_HAVE_DISPLAY
192  if (opt_display) {
193  plot->wait();
194  delete plot;
195  }
196 #endif
197 }
198 #elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
199 int main()
200 {
201  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
202  std::cout << "Tip:" << std::endl;
203  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
204  return EXIT_SUCCESS;
205 }
206 #else
207 int main()
208 {
209  std::cout << "You did not build ViSP with Lapack support" << std::endl;
210  return EXIT_SUCCESS;
211 }
212 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
void solveBySVD(const vpColVector &B, vpColVector &x) const
Definition: vpMatrix.cpp:1908
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:74
bool setEqualityConstraint(const vpMatrix &A, const vpColVector &b, const double &tol=1e-6)
Definition: vpQuadProg.cpp:147
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol=1e-6) const
Definition: vpQuadProg.cpp:244
bool solveQPi(const vpMatrix &Q, const vpColVector &r, const vpMatrix &C, const vpColVector &d, vpColVector &x, bool use_equality=false, const double &tol=1e-6)
Definition: vpQuadProg.cpp:443
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