Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
quadprog_eq.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 with constant equality constraint
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 "qp_plot.h"
53 #include <visp3/core/vpQuadProg.h>
54 #include <visp3/core/vpTime.h>
55 
56 int main(int argc, char **argv)
57 {
58  const int n = 20; // x dim
59  const int m = 10; // equality m < n
60  const int p = 30; // inequality
61  const int o = 16; // cost function
62 #ifdef VISP_HAVE_DISPLAY
63  bool opt_display = true;
64  bool opt_click_allowed = true;
65 #endif
66 
67  for (int i = 0; i < argc; i++) {
68 #ifdef VISP_HAVE_DISPLAY
69  if (std::string(argv[i]) == "-d")
70  opt_display = false;
71  else if (std::string(argv[i]) == "-c")
72  opt_click_allowed = false;
73  else
74 #endif
75  if (std::string(argv[i]) == "-h") {
76  std::cout << "\nUsage: " << argv[0] << " [-d] [-c] [-h] [--help]" << 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  " -c \n"
85  " Disable the mouse click. Useful to automate the \n"
86  " execution of this program without human intervention.\n"
87  "\n"
88 #endif
89  " -h, --help\n"
90  " Print the help.\n"
91  << std::endl;
92 
93  return EXIT_SUCCESS;
94  }
95  }
96  std::srand((long)vpTime::measureTimeMs());
97 
98  vpMatrix A, Q, C;
99  vpColVector b, d, r;
100 
101  A = randM(m, n) * 5;
102  b = randV(m) * 5;
103  Q = randM(o, n) * 5;
104  r = randV(o) * 5;
105  C = randM(p, n) * 5;
106 
107  // make sure Cx <= d has a solution within Ax = b
108 
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 stored equality and warm start
115  vpQuadProg qp_WS;
116  qp_WS.setEqualityConstraint(A, b);
117 
118  vpQuadProg qp_ineq_WS;
119  qp_ineq_WS.setEqualityConstraint(A, b);
120 
121  // timing
122  int total = 100;
123  double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
124  const double eps = 1e-2;
125 
126 #ifdef VISP_HAVE_DISPLAY
127  QPlot *plot = nullptr;
128  if (opt_display)
129  plot = new QPlot(2, total,
130  { "only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start" });
131 #endif
132 
133  for (int k = 0; k < total; ++k) {
134  // small change on QP data (A and b are constant)
135  Q += eps * randM(o, n);
136  r += eps * randV(o);
137  C += eps * randM(p, n);
138  d += eps * randV(p);
139 
140  // solve only equalities
141  // without warm start
142  x = 0;
143  double t = vpTime::measureTimeMs();
144  vpQuadProg::solveQPe(Q, r, A, b, x);
145 
146  t_noWS += vpTime::measureTimeMs() - t;
147 #ifdef VISP_HAVE_DISPLAY
148  if (opt_display)
149  plot->plot(0, 0, k, t);
150 #endif
151 
152  // with pre-solved Ax = b
153  x = 0;
154  t = vpTime::measureTimeMs();
155  qp_WS.solveQPe(Q, r, x);
156 
157  t_WS += vpTime::measureTimeMs() - t;
158 #ifdef VISP_HAVE_DISPLAY
159  if (opt_display)
160  plot->plot(0, 1, k, t);
161 #endif
162 
163  // with inequalities
164  // without warm start
165  x = 0;
166  vpQuadProg qp;
167  t = vpTime::measureTimeMs();
168  qp.solveQP(Q, r, A, b, C, d, x);
169 
170  t_ineq_noWS += vpTime::measureTimeMs() - t;
171 #ifdef VISP_HAVE_DISPLAY
172  if (opt_display)
173  plot->plot(1, 0, k, t);
174 #endif
175 
176  // with warm start + pre-solving
177  x = 0;
178  t = vpTime::measureTimeMs();
179  qp_ineq_WS.solveQPi(Q, r, C, d, x, true);
180 
181  t_ineq_WS += vpTime::measureTimeMs() - t;
182 #ifdef VISP_HAVE_DISPLAY
183  if (opt_display)
184  plot->plot(1, 1, k, t);
185 #endif
186  }
187 
188  std::cout.precision(3);
189  std::cout << "With only equality constraints\n";
190  std::cout << " pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
191  std::cout << " no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)\n\n";
192 
193  std::cout << "With inequality constraints\n";
194  std::cout << " Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS / total << " ms)\n";
195  std::cout << " No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS / total << " ms)"
196  << 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
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
bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol=1e-6) const
Definition: vpQuadProg.cpp:245
bool setEqualityConstraint(const vpMatrix &A, const vpColVector &b, const double &tol=1e-6)
Definition: vpQuadProg.cpp:147
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:446
VISP_EXPORT double measureTimeMs()