Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
quadprog.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
32 */
38
44
45#include <iostream>
46#include <visp3/core/vpConfig.h>
47
48#if defined(VISP_HAVE_LAPACK) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
49
50#include <visp3/core/vpQuadProg.h>
51#include <visp3/core/vpTime.h>
52
53#include "qp_plot.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" || std::string(argv[i]) == "--help") {
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 vpColVector x = A.solveBySVD(b);
114 d = C * x;
115 for (int i = 0; i < p; ++i)
116 d[i] += (5. * rand()) / RAND_MAX;
117
118 // solver with warm start
119 vpQuadProg qp_WS;
120
121 // timing
122 int total = 100;
123 double t_WS(0), t_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(1, total, { "time to solveQP", "warm start" });
130#endif
131
132 for (int k = 0; k < total; ++k) {
133 // reset active set at some point
134 if (k == total / 2)
135 qp_WS.resetActiveSet();
136
137 // small change on QP data
138 Q += eps * randM(o, n);
139 r += eps * randV(o);
140 A += eps * randM(m, n);
141 b += eps * randV(m);
142 C += eps * randM(p, n);
143 d += eps * randV(p);
144
145 // solver without warm start
146 vpQuadProg qp;
147 x = 0;
148 double t = vpTime::measureTimeMs();
149 qp.solveQP(Q, r, A, b, C, d, 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 warm start
158 x = 0;
160 qp_WS.solveQP(Q, r, A, b, C, d, 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
169 std::cout.precision(3);
170 std::cout << "Warm start: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
171 std::cout << "No warm start: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)" << std::endl;
172
173#ifdef VISP_HAVE_DISPLAY
174 if (opt_display) {
175 if (opt_click_allowed) {
176 std::cout << "Click in the graph to exit..." << std::endl;
177 plot->wait();
178 }
179 delete plot;
180 }
181#endif
182 return EXIT_SUCCESS;
183}
184#elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
185int main()
186{
187 std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
188 std::cout << "Tip:" << std::endl;
189 std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
190 return EXIT_SUCCESS;
191}
192#else
193int main()
194{
195 std::cout << "You did not build ViSP with Lapack support" << std::endl;
196 return EXIT_SUCCESS;
197}
198#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)
void resetActiveSet()
Definition vpQuadProg.h:92
VISP_EXPORT double measureTimeMs()