Visual Servoing Platform  version 3.6.1 under development (2024-12-04)
quadprog_eq.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of sequential calls to QP solver with constant equality constraint
32  */
33 
46 #include <iostream>
47 #include <visp3/core/vpConfig.h>
48 
49 #if defined(VISP_HAVE_LAPACK) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
50 
51 #include "qp_plot.h"
52 #include <visp3/core/vpQuadProg.h>
53 #include <visp3/core/vpTime.h>
54 
55 int main(int argc, char **argv)
56 {
57 #ifdef ENABLE_VISP_NAMESPACE
58  using namespace VISP_NAMESPACE_NAME;
59 #endif
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 human intervention.\n"
90  "\n"
91 #endif
92  " -h, --help\n"
93  " Print the help.\n"
94  << 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 
112  vpColVector x = A.solveBySVD(b);
113  d = C * x;
114  for (int i = 0; i < p; ++i)
115  d[i] += (5. * rand()) / RAND_MAX;
116 
117  // solver with stored equality and warm start
118  vpQuadProg qp_WS;
119  qp_WS.setEqualityConstraint(A, b);
120 
121  vpQuadProg qp_ineq_WS;
122  qp_ineq_WS.setEqualityConstraint(A, b);
123 
124  // timing
125  int total = 100;
126  double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
127  const double eps = 1e-2;
128 
129 #ifdef VISP_HAVE_DISPLAY
130  QPlot *plot = nullptr;
131  if (opt_display)
132  plot = new QPlot(2, total,
133  { "only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start" });
134 #endif
135 
136  for (int k = 0; k < total; ++k) {
137  // small change on QP data (A and b are constant)
138  Q += eps * randM(o, n);
139  r += eps * randV(o);
140  C += eps * randM(p, n);
141  d += eps * randV(p);
142 
143  // solve only equalities
144  // without warm start
145  x = 0;
146  double t = vpTime::measureTimeMs();
147  vpQuadProg::solveQPe(Q, r, A, b, x);
148 
149  t_noWS += vpTime::measureTimeMs() - t;
150 #ifdef VISP_HAVE_DISPLAY
151  if (opt_display)
152  plot->plot(0, 0, k, t);
153 #endif
154 
155  // with pre-solved Ax = b
156  x = 0;
157  t = vpTime::measureTimeMs();
158  qp_WS.solveQPe(Q, r, x);
159 
160  t_WS += vpTime::measureTimeMs() - t;
161 #ifdef VISP_HAVE_DISPLAY
162  if (opt_display)
163  plot->plot(0, 1, k, t);
164 #endif
165 
166  // with inequalities
167  // without warm start
168  x = 0;
169  vpQuadProg qp;
170  t = vpTime::measureTimeMs();
171  qp.solveQP(Q, r, A, b, C, d, x);
172 
173  t_ineq_noWS += vpTime::measureTimeMs() - t;
174 #ifdef VISP_HAVE_DISPLAY
175  if (opt_display)
176  plot->plot(1, 0, k, t);
177 #endif
178 
179  // with warm start + pre-solving
180  x = 0;
181  t = vpTime::measureTimeMs();
182  qp_ineq_WS.solveQPi(Q, r, C, d, x, true);
183 
184  t_ineq_WS += vpTime::measureTimeMs() - t;
185 #ifdef VISP_HAVE_DISPLAY
186  if (opt_display)
187  plot->plot(1, 1, k, t);
188 #endif
189  }
190 
191  std::cout.precision(3);
192  std::cout << "With only equality constraints\n";
193  std::cout << " pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
194  std::cout << " no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)\n\n";
195 
196  std::cout << "With inequality constraints\n";
197  std::cout << " Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS / total << " ms)\n";
198  std::cout << " No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS / total << " ms)"
199  << std::endl;
200 
201 #ifdef VISP_HAVE_DISPLAY
202  if (opt_display) {
203  if (opt_click_allowed) {
204  std::cout << "Click in the graph to exit..." << std::endl;
205  plot->wait();
206  }
207  delete plot;
208  }
209 #endif
210 }
211 #elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
212 int main()
213 {
214  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
215  std::cout << "Tip:" << std::endl;
216  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
217  return EXIT_SUCCESS;
218 }
219 #else
220 int main()
221 {
222  std::cout << "You did not build ViSP with Lapack support" << std::endl;
223  return EXIT_SUCCESS;
224 }
225 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void solveBySVD(const vpColVector &B, vpColVector &x) const
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:71
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:384
bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol=1e-6) const
Definition: vpQuadProg.cpp:248
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:458
VISP_EXPORT double measureTimeMs()