hpp-core 9.3.0
Implement basic classes for canonical path planning for kinematic chains.
Loading...
Searching...
No Matches
linear-constraint.hh
Go to the documentation of this file.
1// Copyright (c) 2017, Joseph Mirabel
2// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3//
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// 1. Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright
13// notice, this list of conditions and the following disclaimer in the
14// documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27// DAMAGE.
28
29#ifndef HPP_CORE_PATH_OPTIMIZATION_SPLINE_GRADIENT_BASED_LINEAR_CONSTRAINT_HH
30#define HPP_CORE_PATH_OPTIMIZATION_SPLINE_GRADIENT_BASED_LINEAR_CONSTRAINT_HH
31
32#include <Eigen/Sparse>
33#include <hpp/core/fwd.hh>
34#include <hpp/util/debug.hh>
35
36namespace hpp {
37namespace core {
38namespace pathOptimization {
40typedef Eigen::SparseMatrix<value_type, Eigen::RowMajor> RowMajorSparseMatrix_t;
41typedef Eigen::SparseMatrix<value_type, Eigen::ColMajor> SparseMatrix_t;
42
48 LinearConstraint(size_type inputSize, size_type outputSize)
49 : J(outputSize, inputSize), b(outputSize), xSol(inputSize) {
50 b.setZero();
51 }
52
54
55 void concatenate(const LinearConstraint& oc) {
56 assert(oc.J.cols() == J.cols());
57 const size_type rows = J.rows();
58 J.conservativeResize(rows + oc.J.rows(), J.cols());
59 J.middleRows(rows, oc.J.rows()) = oc.J;
60 b.conservativeResize(b.rows() + oc.b.rows());
61 b.tail(oc.b.rows()) = oc.b;
62 }
63
66 void setBlock(size_type row, size_type col, const matrix_t& block) {
67 for (size_type i = 0; i < block.rows(); ++i)
68 for (size_type j = 0; j < block.cols(); ++j)
69 if (block(i, j) != 0 || J.coeff(row + i, col + j) != 0)
70 J.coeffRef(row + i, col + j) = block(i, j);
71 }
72
83 bool decompose(bool check = false, bool throwIfNotValid = false);
84
87 void computeRank();
88
97 bool computeRank = true) const {
98 lcr.J = lc.J * PK;
99 lcr.b.noalias() = lc.b - lc.J * xStar;
100
101 // Decompose
102 if (computeRank) {
103 lcr.computeRank();
104 return lcr.rank == std::min(lcr.J.rows(), lcr.J.cols());
105 } else
106 return true;
107 }
108
113 void computeSolution(const vector_t& v) {
114 xSol.noalias() = xStar + PK * v;
115#ifdef HPP_DEBUG
117#endif // HPP_DEBUG
118 }
119
121 bool isSatisfied(const vector_t& x,
122 const value_type& threshold =
123 Eigen::NumTraits<value_type>::dummy_precision()) {
124 vector_t err(J * x - b);
125 if (err.isZero(threshold)) return true;
126 hppDout(error, "constraints could not be satisfied: " << err.norm() << '\n'
127 << err);
128 return false;
129 }
130
131 void addRows(const std::size_t& nbRows) {
132 if (nbRows > 0) {
133 J.conservativeResize(J.rows() + nbRows, J.cols());
134 b.conservativeResize(b.rows() + nbRows);
135 b.tail(nbRows).setZero();
136 }
137 }
138
144
149
152
157
159};
160} // namespace pathOptimization
161} // namespace core
162} // namespace hpp
163
164#endif // HPP_CORE_PATH_OPTIMIZATION_SPLINE_GRADIENT_BASED_LINEAR_CONSTRAINT_HH
#define hppDout(channel, data)
Definition fwd.hh:290
Eigen::SparseMatrix< value_type, Eigen::RowMajor > RowMajorSparseMatrix_t
Sparse matrix types of the spline optimization problems.
Definition linear-constraint.hh:40
Eigen::SparseMatrix< value_type, Eigen::ColMajor > SparseMatrix_t
Definition linear-constraint.hh:41
Definition bi-rrt-planner.hh:36
pinocchio::value_type value_type
Definition fwd.hh:175
pinocchio::vector_t vector_t
Definition fwd.hh:221
pinocchio::size_type size_type
Definition fwd.hh:174
pinocchio::matrix_t matrix_t
Definition fwd.hh:163
bool reduceConstraint(const LinearConstraint &lc, LinearConstraint &lcr, bool computeRank=true) const
Definition linear-constraint.hh:96
LinearConstraint(size_type inputSize, size_type outputSize)
Definition linear-constraint.hh:48
vector_t b
Definition linear-constraint.hh:142
vector_t xSol
Definition linear-constraint.hh:156
SparseMatrix_t PK
Projector onto .
Definition linear-constraint.hh:154
vector_t xStar
is a particular solution.
Definition linear-constraint.hh:156
size_type rank
Rank of J.
Definition linear-constraint.hh:151
bool decompose(bool check=false, bool throwIfNotValid=false)
void concatenate(const LinearConstraint &oc)
Definition linear-constraint.hh:55
bool isSatisfied(const vector_t &x, const value_type &threshold=Eigen::NumTraits< value_type >::dummy_precision())
Returns .
Definition linear-constraint.hh:121
void setBlock(size_type row, size_type col, const matrix_t &block)
Definition linear-constraint.hh:66
void addRows(const std::size_t &nbRows)
Definition linear-constraint.hh:131
RowMajorSparseMatrix_t J
Definition linear-constraint.hh:141
void computeSolution(const vector_t &v)
Definition linear-constraint.hh:113