-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from keenon/keenon/acc-minimizer
Adding native AccelerationMinimizer support
- Loading branch information
Showing
12 changed files
with
419 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.10.44 | ||
0.10.45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include "AccelerationMinimizer.hpp" | ||
|
||
#include <iostream> | ||
|
||
// #include <Eigen/Core> | ||
// #include <Eigen/Dense> | ||
#include <Eigen/IterativeLinearSolvers> | ||
// #include <unsupported/Eigen/IterativeSolvers> | ||
|
||
#include "dart/math/MathTypes.hpp" | ||
|
||
namespace dart { | ||
namespace utils { | ||
|
||
/** | ||
* Create (and pre-factor) a smoother that can remove the "jerk" from a time | ||
* series of data. | ||
* | ||
* The alpha value will determine how much smoothing to apply. A value of 0 | ||
* corresponds to no smoothing. | ||
*/ | ||
AccelerationMinimizer::AccelerationMinimizer( | ||
int timesteps, | ||
s_t smoothingWeight, | ||
s_t regularizationWeight, | ||
s_t startPositionZeroWeight, | ||
s_t endPositionZeroWeight, | ||
s_t startVelocityZeroWeight, | ||
s_t endVelocityZeroWeight, | ||
int numIterations) | ||
: mTimesteps(timesteps), | ||
mSmoothingWeight(smoothingWeight), | ||
mRegularizationWeight(regularizationWeight), | ||
mStartPositionZeroWeight(startPositionZeroWeight * timesteps), | ||
mEndPositionZeroWeight(endPositionZeroWeight * timesteps), | ||
mStartVelocityZeroWeight(startVelocityZeroWeight * timesteps), | ||
mEndVelocityZeroWeight(endVelocityZeroWeight * timesteps), | ||
mNumIterations(numIterations), | ||
mNumIterationsBackoff(6), | ||
mDebugIterationBackoff(false), | ||
mConvergenceTolerance(1e-10) | ||
{ | ||
Eigen::Vector3s stamp; | ||
stamp << -1, 2, -1; | ||
stamp *= mSmoothingWeight; | ||
|
||
typedef Eigen::Triplet<s_t> T; | ||
std::vector<T> tripletList; | ||
const int accTimesteps = mTimesteps - 2; | ||
for (int i = 0; i < accTimesteps; i++) | ||
{ | ||
for (int j = 0; j < 3; j++) | ||
{ | ||
tripletList.push_back(T(i, i + j, stamp(j))); | ||
} | ||
} | ||
// Add start velocity zero constraint | ||
tripletList.push_back(T(accTimesteps, 0, mStartVelocityZeroWeight)); | ||
tripletList.push_back(T(accTimesteps, 1, -mStartVelocityZeroWeight)); | ||
// Add end velocity zero constraint | ||
tripletList.push_back( | ||
T(accTimesteps + 1, mTimesteps - 2, mEndVelocityZeroWeight)); | ||
tripletList.push_back( | ||
T(accTimesteps + 1, mTimesteps - 1, -mEndVelocityZeroWeight)); | ||
// Add start position zero constraint | ||
tripletList.push_back(T(accTimesteps + 2, 0, mStartPositionZeroWeight)); | ||
// Add end position zero constraint | ||
tripletList.push_back( | ||
T(accTimesteps + 3, mTimesteps - 1, mEndPositionZeroWeight)); | ||
for (int i = 0; i < mTimesteps; i++) | ||
{ | ||
tripletList.push_back(T(accTimesteps + 4 + i, i, mRegularizationWeight)); | ||
} | ||
mB_sparse | ||
= Eigen::SparseMatrix<s_t>(accTimesteps + 4 + mTimesteps, mTimesteps); | ||
mB_sparse.setFromTriplets(tripletList.begin(), tripletList.end()); | ||
mB_sparse.makeCompressed(); | ||
mB_sparseSolver.analyzePattern(mB_sparse); | ||
mB_sparseSolver.factorize(mB_sparse); | ||
if (mB_sparseSolver.info() != Eigen::Success) | ||
{ | ||
std::cout << "mB_sparseSolver.factorize(mB_sparse) error: " | ||
<< mB_sparseSolver.lastErrorMessage() << std::endl; | ||
} | ||
assert(mB_sparseSolver.info() == Eigen::Success); | ||
} | ||
|
||
Eigen::VectorXs AccelerationMinimizer::minimize(Eigen::VectorXs series) | ||
{ | ||
const int accTimesteps = mTimesteps - 2; | ||
Eigen::VectorXs b = Eigen::VectorXs(accTimesteps + 4 + mTimesteps); | ||
b.segment(0, accTimesteps + 4).setZero(); | ||
b.segment(accTimesteps + 4, mTimesteps) = series * mRegularizationWeight; | ||
|
||
Eigen::VectorXs x = series; | ||
|
||
int iterations = mNumIterations; | ||
for (int i = 0; i < mNumIterationsBackoff; i++) | ||
{ | ||
Eigen::LeastSquaresConjugateGradient<Eigen::SparseMatrix<s_t>> solver; | ||
solver.compute(mB_sparse); | ||
solver.setTolerance(mConvergenceTolerance); | ||
solver.setMaxIterations(iterations); | ||
x = solver.solveWithGuess(b, series); | ||
// Check convergence | ||
if (solver.info() == Eigen::Success) | ||
{ | ||
// Converged | ||
break; | ||
} | ||
else | ||
{ | ||
if (mDebugIterationBackoff) | ||
{ | ||
std::cout | ||
<< "[AccelerationMinimizer] LeastSquaresConjugateGradient did " | ||
"not converge in " | ||
<< iterations << ", with error " << solver.error() | ||
<< " so doubling iteration count and trying again." << std::endl; | ||
} | ||
iterations *= 2; | ||
} | ||
} | ||
|
||
return x; | ||
} | ||
|
||
void AccelerationMinimizer::setDebugIterationBackoff(bool debug) | ||
{ | ||
mDebugIterationBackoff = debug; | ||
} | ||
|
||
void AccelerationMinimizer::setNumIterationsBackoff(int numIterations) | ||
{ | ||
mNumIterationsBackoff = numIterations; | ||
} | ||
|
||
void AccelerationMinimizer::setConvergenceTolerance(s_t tolerance) | ||
{ | ||
mConvergenceTolerance = tolerance; | ||
} | ||
|
||
} // namespace utils | ||
} // namespace dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef UTILS_ACC_MINIMIZER | ||
#define UTILS_ACC_MINIMIZER | ||
|
||
#include <Eigen/Sparse> | ||
#include <Eigen/SparseQR> | ||
|
||
#include "dart/math/MathTypes.hpp" | ||
|
||
namespace dart { | ||
namespace utils { | ||
|
||
class AccelerationMinimizer | ||
{ | ||
public: | ||
AccelerationMinimizer( | ||
int numTimesteps, | ||
s_t smoothingWeight = 1.0, | ||
s_t regularizationWeight = 0.01, | ||
s_t startPositionZeroWeight = 0.0, | ||
s_t endPositionZeroWeight = 0.0, | ||
s_t startVelocityZeroWeight = 0.0, | ||
s_t endVelocityZeroWeight = 0.0, | ||
int numIterations = 10000); | ||
|
||
Eigen::VectorXs minimize(Eigen::VectorXs series); | ||
|
||
void setDebugIterationBackoff(bool debug); | ||
|
||
void setNumIterationsBackoff(int numIterations); | ||
|
||
void setConvergenceTolerance(s_t tolerance); | ||
|
||
protected: | ||
int mTimesteps; | ||
s_t mSmoothingWeight; | ||
s_t mRegularizationWeight; | ||
s_t mStartPositionZeroWeight; | ||
s_t mEndPositionZeroWeight; | ||
s_t mStartVelocityZeroWeight; | ||
s_t mEndVelocityZeroWeight; | ||
int mNumIterations; | ||
int mNumIterationsBackoff; | ||
bool mDebugIterationBackoff; | ||
s_t mConvergenceTolerance; | ||
Eigen::SparseMatrix<s_t> mB_sparse; | ||
Eigen::SparseQR<Eigen::SparseMatrix<s_t>, Eigen::NaturalOrdering<int>> | ||
mB_sparseSolver; | ||
}; | ||
|
||
} // namespace utils | ||
} // namespace dart | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2011-2019, The DART development contributors | ||
* All rights reserved. | ||
* | ||
* The list of contributors can be found at: | ||
* https://github.com/dartsim/dart/blob/master/LICENSE | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <dart/utils/AccelerationMinimizer.hpp> | ||
#include <pybind11/eigen.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace dart { | ||
namespace python { | ||
|
||
void AccelerationMinimizer(py::module& m) | ||
{ | ||
::py::class_<dart::utils::AccelerationMinimizer>(m, "AccelerationMinimizer") | ||
.def( | ||
::py::init<int, s_t, s_t, s_t, s_t, s_t, s_t, int>(), | ||
::py::arg("numTimesteps"), | ||
::py::arg("smoothingWeight") = 1.0, | ||
::py::arg("regularizationWeight") = 0.01, | ||
::py::arg("startPositionZeroWeight") = 0.0, | ||
::py::arg("endPositionZeroWeight") = 0.0, | ||
::py::arg("startVelocityZeroWeight") = 0.0, | ||
::py::arg("endVelocityZeroWeight") = 0.0, | ||
::py::arg("numIterations") = 10000) | ||
.def( | ||
"minimize", | ||
&dart::utils::AccelerationMinimizer::minimize, | ||
::py::arg("series")) | ||
.def( | ||
"setDebugIterationBackoff", | ||
&dart::utils::AccelerationMinimizer::setDebugIterationBackoff, | ||
::py::arg("iterations")) | ||
.def( | ||
"setNumIterationsBackoff", | ||
&dart::utils::AccelerationMinimizer::setNumIterationsBackoff, | ||
::py::arg("series")) | ||
.def( | ||
"setConvergenceTolerance", | ||
&dart::utils::AccelerationMinimizer::setConvergenceTolerance, | ||
::py::arg("tolerance")); | ||
} | ||
|
||
} // namespace python | ||
} // namespace dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.