Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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 (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_LAPACK)
51
52#include "qp_plot.h"
53#include <visp3/core/vpQuadProg.h>
54#include <visp3/core/vpTime.h>
55
56int 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 vpColVector x = A.solveBySVD(b);
109 d = C * x;
110 for (int i = 0; i < p; ++i)
111 d[i] += (5. * rand()) / RAND_MAX;
112
113 // solver with stored equality and warm start
114 vpQuadProg qp_WS;
115 qp_WS.setEqualityConstraint(A, b);
116
117 vpQuadProg qp_ineq_WS;
118 qp_ineq_WS.setEqualityConstraint(A, b);
119
120 // timing
121 int total = 100;
122 double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
123 const double eps = 1e-2;
124
125#ifdef VISP_HAVE_DISPLAY
126 QPlot *plot = NULL;
127 if (opt_display)
128 plot = new QPlot(2, total,
129 { "only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start" });
130#endif
131
132 for (int k = 0; k < total; ++k) {
133 // small change on QP data (A and b are constant)
134 Q += eps * randM(o, n);
135 r += eps * randV(o);
136 C += eps * randM(p, n);
137 d += eps * randV(p);
138
139 // solve only equalities
140 // without warm start
141 x = 0;
142 double t = vpTime::measureTimeMs();
143 vpQuadProg::solveQPe(Q, r, A, b, x);
144
145 t_noWS += vpTime::measureTimeMs() - t;
146#ifdef VISP_HAVE_DISPLAY
147 if (opt_display)
148 plot->plot(0, 0, k, t);
149#endif
150
151 // with pre-solved Ax = b
152 x = 0;
154 qp_WS.solveQPe(Q, r, x);
155
156 t_WS += vpTime::measureTimeMs() - t;
157#ifdef VISP_HAVE_DISPLAY
158 if (opt_display)
159 plot->plot(0, 1, k, t);
160#endif
161
162 // with inequalities
163 // without warm start
164 x = 0;
165 vpQuadProg qp;
167 qp.solveQP(Q, r, A, b, C, d, x);
168
169 t_ineq_noWS += vpTime::measureTimeMs() - t;
170#ifdef VISP_HAVE_DISPLAY
171 if (opt_display)
172 plot->plot(1, 0, k, t);
173#endif
174
175 // with warm start + pre-solving
176 x = 0;
178 qp_ineq_WS.solveQPi(Q, r, C, d, x, true);
179
180 t_ineq_WS += vpTime::measureTimeMs() - t;
181#ifdef VISP_HAVE_DISPLAY
182 if (opt_display)
183 plot->plot(1, 1, k, t);
184#endif
185 }
186
187 std::cout.precision(3);
188 std::cout << "With only equality constraints\n";
189 std::cout << " pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
190 std::cout << " no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)\n\n";
191
192 std::cout << "With inequality constraints\n";
193 std::cout << " Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS / total << " ms)\n";
194 std::cout << " No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS / total << " ms)"
195 << std::endl;
196
197#ifdef VISP_HAVE_DISPLAY
198 if (opt_display) {
199 if (opt_click_allowed) {
200 std::cout << "Click in the graph to exit..." << std::endl;
201 plot->wait();
202 }
203 delete plot;
204 }
205#endif
206}
207#elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
208int main()
209{
210 std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
211 std::cout << "Tip:" << std::endl;
212 std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
213 return EXIT_SUCCESS;
214}
215#else
216int main()
217{
218 std::cout << "You did not build ViSP with Lapack support" << std::endl;
219 return EXIT_SUCCESS;
220}
221#endif
Implementation of column vector and the associated operations.
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:152
void solveBySVD(const vpColVector &B, vpColVector &x) const
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)
bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol=1e-6) const
bool setEqualityConstraint(const vpMatrix &A, const vpColVector &b, const double &tol=1e-6)
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)
VISP_EXPORT double measureTimeMs()