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