File: quadprog_eq.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (221 lines) | stat: -rw-r--r-- 6,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Example of sequential calls to QP solver with constant equality constraint
 *
*****************************************************************************/
/*!
  \file quadprog_eq.cpp

  \brief Example of sequential calls to QP solver with constant equality constraint
*/

/*!
  \example quadprog_eq.cpp

  Example of sequential calls to QP solver with constant equality constraint
*/

#include <iostream>
#include <visp3/core/vpConfig.h>

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_LAPACK)

#include "qp_plot.h"
#include <visp3/core/vpQuadProg.h>
#include <visp3/core/vpTime.h>

int main(int argc, char **argv)
{
  const int n = 20; // x dim
  const int m = 10; // equality m < n
  const int p = 30; // inequality
  const int o = 16; // cost function
#ifdef VISP_HAVE_DISPLAY
  bool opt_display = true;
  bool opt_click_allowed = true;
#endif

  for (int i = 0; i < argc; i++) {
#ifdef VISP_HAVE_DISPLAY
    if (std::string(argv[i]) == "-d")
      opt_display = false;
    else if (std::string(argv[i]) == "-c")
      opt_click_allowed = false;
    else
#endif
      if (std::string(argv[i]) == "-h") {
        std::cout << "\nUsage: " << argv[0] << " [-d] [-c] [-h] [--help]" << std::endl;
        std::cout << "\nOptions: \n"
#ifdef VISP_HAVE_DISPLAY
          "  -d \n"
          "     Disable the image display. This can be useful \n"
          "     for automatic tests using crontab under Unix or \n"
          "     using the task manager under Windows.\n"
          "\n"
          "  -c \n"
          "     Disable the mouse click. Useful to automate the \n"
          "     execution of this program without human intervention.\n"
          "\n"
#endif
          "  -h, --help\n"
          "     Print the help.\n"
          << std::endl;

        return EXIT_SUCCESS;
      }
  }
  std::srand((long)vpTime::measureTimeMs());

  vpMatrix A, Q, C;
  vpColVector b, d, r;

  A = randM(m, n) * 5;
  b = randV(m) * 5;
  Q = randM(o, n) * 5;
  r = randV(o) * 5;
  C = randM(p, n) * 5;

  // make sure Cx <= d has a solution within Ax = b
  vpColVector x = A.solveBySVD(b);
  d = C * x;
  for (int i = 0; i < p; ++i)
    d[i] += (5. * rand()) / RAND_MAX;

  // solver with stored equality and warm start
  vpQuadProg qp_WS;
  qp_WS.setEqualityConstraint(A, b);

  vpQuadProg qp_ineq_WS;
  qp_ineq_WS.setEqualityConstraint(A, b);

  // timing
  int total = 100;
  double t_WS(0), t_noWS(0), t_ineq_WS(0), t_ineq_noWS(0);
  const double eps = 1e-2;

#ifdef VISP_HAVE_DISPLAY
  QPlot *plot = NULL;
  if (opt_display)
    plot = new QPlot(2, total,
                     { "only equalities", "pre-solving", "equalities + inequalities", "pre-solving / warm start" });
#endif

  for (int k = 0; k < total; ++k) {
    // small change on QP data (A and b are constant)
    Q += eps * randM(o, n);
    r += eps * randV(o);
    C += eps * randM(p, n);
    d += eps * randV(p);

    // solve only equalities
    // without warm start
    x = 0;
    double t = vpTime::measureTimeMs();
    vpQuadProg::solveQPe(Q, r, A, b, x);

    t_noWS += vpTime::measureTimeMs() - t;
#ifdef VISP_HAVE_DISPLAY
    if (opt_display)
      plot->plot(0, 0, k, t);
#endif

    // with pre-solved Ax = b
    x = 0;
    t = vpTime::measureTimeMs();
    qp_WS.solveQPe(Q, r, x);

    t_WS += vpTime::measureTimeMs() - t;
#ifdef VISP_HAVE_DISPLAY
    if (opt_display)
      plot->plot(0, 1, k, t);
#endif

    // with inequalities
    // without warm start
    x = 0;
    vpQuadProg qp;
    t = vpTime::measureTimeMs();
    qp.solveQP(Q, r, A, b, C, d, x);

    t_ineq_noWS += vpTime::measureTimeMs() - t;
#ifdef VISP_HAVE_DISPLAY
    if (opt_display)
      plot->plot(1, 0, k, t);
#endif

    // with warm start + pre-solving
    x = 0;
    t = vpTime::measureTimeMs();
    qp_ineq_WS.solveQPi(Q, r, C, d, x, true);

    t_ineq_WS += vpTime::measureTimeMs() - t;
#ifdef VISP_HAVE_DISPLAY
    if (opt_display)
      plot->plot(1, 1, k, t);
#endif
  }

  std::cout.precision(3);
  std::cout << "With only equality constraints\n";
  std::cout << "   pre-solving: t = " << t_WS << " ms (for 1 QP = " << t_WS / total << " ms)\n";
  std::cout << "   no pre-solving: t = " << t_noWS << " ms (for 1 QP = " << t_noWS / total << " ms)\n\n";

  std::cout << "With inequality constraints\n";
  std::cout << "   Warm start: t = " << t_ineq_WS << " ms (for 1 QP = " << t_ineq_WS / total << " ms)\n";
  std::cout << "   No warm start: t = " << t_ineq_noWS << " ms (for 1 QP = " << t_ineq_noWS / total << " ms)"
    << std::endl;

#ifdef VISP_HAVE_DISPLAY
  if (opt_display) {
    if (opt_click_allowed) {
      std::cout << "Click in the graph to exit..." << std::endl;
      plot->wait();
    }
    delete plot;
  }
#endif
}
#elif !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
int main()
{
  std::cout << "You did not build ViSP with c++11 or higher compiler flag" << std::endl;
  std::cout << "Tip:" << std::endl;
  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
  return EXIT_SUCCESS;
}
#else
int main()
{
  std::cout << "You did not build ViSP with Lapack support" << std::endl;
  return EXIT_SUCCESS;
}
#endif