Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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  bool opt_click_allowed = 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 if (std::string(argv[i]) == "-c")
75  opt_click_allowed = false;
76  else
77 #endif
78  if (std::string(argv[i]) == "-h") {
79  std::cout << "\nUsage: " << argv[0] << " [-d] [-c] [-h] [--help]" << std::endl;
80  std::cout << "\nOptions: \n"
81 #ifdef VISP_HAVE_DISPLAY
82  " -d \n"
83  " Disable the image display. This can be useful \n"
84  " for automatic tests using crontab under Unix or \n"
85  " using the task manager under Windows.\n"
86  "\n"
87  " -c \n"
88  " Disable the mouse click. Useful to automate the \n"
89  " execution of this program without humain intervention.\n"
90  "\n"
91 #endif
92  " -h, --help\n"
93  " Print the help.\n"<< std::endl;
94 
95  return EXIT_SUCCESS;
96  }
97  }
98  std::srand((long) vpTime::measureTimeMs());
99 
100  vpMatrix A, Q, C;
101  vpColVector b, d, r;
102 
103  A = randM(m,n)*5;
104  b = randV(m)*5;
105  Q = randM(o,n)*5;
106  r = randV(o)*5;
107  C = randM(p,n)*5;
108 
109  // make sure Cx <= d has a solution within Ax = b
110  vpColVector x = A.solveBySVD(b);
111  d = C*x;
112  for(int i = 0; i < p; ++i)
113  d[i] += (5.*rand())/RAND_MAX;
114 
115  // solver with stored equality and warm start
116  vpQuadProg qp_WS;
117  qp_WS.setEqualityConstraint(A, b);
118 
119  vpQuadProg qp_ineq_WS;
120  qp_ineq_WS.setEqualityConstraint(A, b);
121 
122  // timing
123  int total = 100;
124  double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
125  const double eps = 1e-2;
126 
127 #ifdef VISP_HAVE_DISPLAY
128  QPlot *plot = NULL;
129  if (opt_display)
130  plot = new QPlot(2, total, {"only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start"});
131 #endif
132 
133  for(int k = 0; k < total; ++k)
134  {
135  // small change on QP data (A and b are constant)
136  Q += eps * randM(o,n);
137  r += eps * randV(o);
138  C += eps * randM(p,n);
139  d += eps * randV(p);
140 
141  // solve only equalities
142  // without warm start
143  x = 0;
144  double t = vpTime::measureTimeMs();
145  vpQuadProg::solveQPe(Q, r, A, b, 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 pre-solved Ax = b
154  x = 0;
155  t = vpTime::measureTimeMs();
156  qp_WS.solveQPe(Q, r, 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  // with inequalities
165  // without warm start
166  x = 0;
167  vpQuadProg qp;
168  t = vpTime::measureTimeMs();
169  qp.solveQP(Q, r, A, b, C, d, x);
170 
171  t_ineq_noWS += vpTime::measureTimeMs() - t;
172 #ifdef VISP_HAVE_DISPLAY
173  if (opt_display)
174  plot->plot(1, 0, k, t);
175 #endif
176 
177  // with warm start + pre-solving
178  x = 0;
179  t = vpTime::measureTimeMs();
180  qp_ineq_WS.solveQPi(Q, r, C, d, x, true);
181 
182  t_ineq_WS += vpTime::measureTimeMs() - t;
183 #ifdef VISP_HAVE_DISPLAY
184  if (opt_display)
185  plot->plot(1, 1, k, t);
186 #endif
187  }
188 
189  std::cout.precision(3);
190  std::cout << "With only equality constraints\n";
191  std::cout << " pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS/total << " ms)\n";
192  std::cout << " no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS/total << " ms)\n\n";
193 
194  std::cout << "With inequality constraints\n";
195  std::cout << " Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS/total << " ms)\n";
196  std::cout << " No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS/total << " ms)" << std::endl;
197 
198 #ifdef VISP_HAVE_DISPLAY
199  if (opt_display) {
200  if (opt_click_allowed) {
201  std::cout << "Click in the graph to exit..." << std::endl;
202  plot->wait();
203  }
204  delete plot;
205  }
206 #endif
207 }
208 #elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
209 int main()
210 {
211  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
212  std::cout << "Tip:" << std::endl;
213  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
214  return EXIT_SUCCESS;
215 }
216 #else
217 int main()
218 {
219  std::cout << "You did not build ViSP with Lapack support" << std::endl;
220  return EXIT_SUCCESS;
221 }
222 #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
bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol=1e-6) const
Definition: vpQuadProg.cpp:244
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
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 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