Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpQuadProg.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Quadratic Programming
32  */
33 
39 #ifndef _vpQuadProg_h_
40 #define _vpQuadProg_h_
41 
42 #include <stdlib.h>
43 #include <vector>
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpLinProg.h>
46 #include <visp3/core/vpMatrix.h>
47 #include <visp3/core/vpMatrixException.h>
48 
49 BEGIN_VISP_NAMESPACE
70 class VISP_EXPORT vpQuadProg
71 {
72 public:
73 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
76  bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpColVector &x, const double &tol = 1e-6) const;
77 
78  bool solveQPi(const vpMatrix &Q, const vpColVector &r, const vpMatrix &C, const vpColVector &d, vpColVector &x,
79  bool use_equality = false, const double &tol = 1e-6);
80 
81  bool solveQP(const vpMatrix &Q, const vpColVector &r, vpMatrix A, vpColVector b, const vpMatrix &C,
82  const vpColVector &d, vpColVector &x, const double &tol = 1e-6);
84 
87  bool setEqualityConstraint(const vpMatrix &A, const vpColVector &b, const double &tol = 1e-6);
88 
92  void resetActiveSet() { active.clear(); }
94 
95  static void fromCanonicalCost(const vpMatrix &H, const vpColVector &c, vpMatrix &Q, vpColVector &r,
96  const double &tol = 1e-6);
97  static bool solveQPe(const vpMatrix &Q, const vpColVector &r, vpMatrix A, vpColVector b, vpColVector &x,
98  const double &tol = 1e-6);
99 
100 protected:
104  std::vector<unsigned int> active;
105 
109  std::vector<unsigned int> inactive;
110 
115 
120 
121  static vpColVector solveSVDorQR(const vpMatrix &A, const vpColVector &b);
122 
123  static bool solveByProjection(const vpMatrix &Q, const vpColVector &r, vpMatrix &A, vpColVector &b, vpColVector &x,
124  const double &tol = 1e-6);
125 
141  static unsigned int checkDimensions(const vpMatrix &Q, const vpColVector &r, const vpMatrix *A, const vpColVector *b,
142  const vpMatrix *C, const vpColVector *d, const std::string fct)
143  {
144  // check data consistency
145  unsigned int n = Q.getCols();
146  const bool Ab = (A != nullptr && b != nullptr && A->getRows());
147  const bool Cd = (C != nullptr && d != nullptr && C->getRows());
148 
149  if ((Ab && n != A->getCols()) || (Cd && n != C->getCols()) || (Ab && A->getRows() != b->getRows()) ||
150  (Cd && C->getRows() != d->getRows()) || Q.getRows() != r.getRows()) {
151  std::cout << "vpQuadProg::" << fct << ": wrong dimension\n"
152  << "Q: " << Q.getRows() << "x" << Q.getCols() << " - r: " << r.getRows() << std::endl;
153  if (Ab)
154  std::cout << "A: " << A->getRows() << "x" << A->getCols() << " - b: " << b->getRows() << std::endl;
155  if (Cd)
156  std::cout << "C: " << C->getRows() << "x" << C->getCols() << " - d: " << d->getRows() << std::endl;
158  }
159  return n;
160  }
161 #endif
162 };
163 END_VISP_NAMESPACE
164 #endif
unsigned int getCols() const
Definition: vpArray2D.h:337
unsigned int getRows() const
Definition: vpArray2D.h:347
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
This class provides a solver for Quadratic Programs.
Definition: vpQuadProg.h:71
std::vector< unsigned int > inactive
Definition: vpQuadProg.h:109
std::vector< unsigned int > active
Definition: vpQuadProg.h:104
vpMatrix Z
Definition: vpQuadProg.h:119
vpColVector x1
Definition: vpQuadProg.h:114
static unsigned int checkDimensions(const vpMatrix &Q, const vpColVector &r, const vpMatrix *A, const vpColVector *b, const vpMatrix *C, const vpColVector *d, const std::string fct)
Definition: vpQuadProg.h:141
void resetActiveSet()
Definition: vpQuadProg.h:92