Skip to content

Commit

Permalink
Merge pull request #83 from gergondet/topic/ReductionSparse
Browse files Browse the repository at this point in the history
  • Loading branch information
gergondet authored Dec 8, 2022
2 parents 548fdcf + afd38b6 commit b38f2b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/GenQPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ void GenQPSolver::setDependencies(int nrVars, std::vector<std::tuple<int, int, d
fullToReduced_.resize(static_cast<size_t>(nrVars), -1);
reducedToFull_.resize(static_cast<size_t>(nrVars) - dependencies_.size(), -1);
/* Initialize the multipliers and offset variable */
multipliers_ = Eigen::MatrixXd::Zero(nrVars, nrVars - static_cast<int>(dependencies.size()));
multipliers_ = Eigen::SparseMatrix<double>(nrVars, nrVars - static_cast<int>(dependencies.size()));
std::vector<Eigen::Triplet<double>> triplets;
triplets.reserve(static_cast<size_t>(nrVars));
/* Number of removed variables encountered so far */
size_t shift = 0;
for(size_t i = 0; i < fullToReduced_.size(); ++i)
Expand All @@ -67,16 +69,17 @@ void GenQPSolver::setDependencies(int nrVars, std::vector<std::tuple<int, int, d
continue;
}
fullToReduced_[i] = static_cast<int>(i - shift);
multipliers_(static_cast<Eigen::DenseIndex>(i), fullToReduced_[i]) = 1.0;
triplets.push_back({static_cast<Eigen::SparseMatrix<double>::StorageIndex>(i), fullToReduced_[i], 1.0});
reducedToFull_[i - shift] = static_cast<int>(i);
}
for(const auto & d : dependencies_)
{
auto leader_idx = std::get<0>(d);
auto mimic_idx = std::get<1>(d);
auto mult = std::get<2>(d);
multipliers_(mimic_idx, fullToReduced_[static_cast<size_t>(leader_idx)]) = mult;
triplets.push_back({mimic_idx, fullToReduced_[static_cast<size_t>(leader_idx)], mult});
}
multipliers_.setFromTriplets(triplets.begin(), triplets.end());
}

} // namespace qp
Expand Down
7 changes: 4 additions & 3 deletions src/GenQPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// Eigen
#include <Eigen/Core>
#include <Eigen/Sparse>

// Tasks
#include "Tasks/QPSolver.h"
Expand Down Expand Up @@ -81,7 +82,7 @@ inline void reduceQC(const Eigen::MatrixXd & QFull,
const Eigen::VectorXd & CFull,
Eigen::MatrixXd & Q,
Eigen::VectorXd & C,
const Eigen::MatrixXd & M)
const Eigen::SparseMatrix<double> & M)
{
Q.noalias() = M.transpose() * QFull * M;
C.noalias() = M.transpose() * CFull;
Expand Down Expand Up @@ -296,7 +297,7 @@ inline void fillBound(const std::vector<Bound *> & bounds, Eigen::VectorXd & XL,
* L \leq A M y \leq U
* \f}
*/
inline void reduceA(const Eigen::MatrixXd & AFull, Eigen::MatrixXd & A, const Eigen::MatrixXd & M)
inline void reduceA(const Eigen::MatrixXd & AFull, Eigen::MatrixXd & A, const Eigen::SparseMatrix<double> & M)
{
A.noalias() = AFull * M;
}
Expand Down Expand Up @@ -345,7 +346,7 @@ inline void reduceBound(const Eigen::VectorXd & XLFull,
*/
inline void expandResult(const Eigen::VectorXd & result,
Eigen::VectorXd & resultFull,
const Eigen::MatrixXd & multipliers)
const Eigen::SparseMatrix<double> & multipliers)
{
resultFull.noalias() = multipliers * result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/GenQPSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
// std
#include <vector>

// Eigen
// Tasks
#include <tasks/config.hh>

// Eigen
#include <Eigen/Core>
#include <Eigen/Sparse>

// forward declaration
// RBDyn
Expand Down Expand Up @@ -116,7 +118,7 @@ class TASKS_DLLAPI GenQPSolver
* the full variable and the factor and offset in the dependency equation: replica = factor * primary */
std::vector<std::tuple<int, int, double>> dependencies_;
/** Multipliers matrix M of size (nFull, nReduced) such that full = M * reduce */
Eigen::MatrixXd multipliers_;
Eigen::SparseMatrix<double> multipliers_;
};

} // namespace qp
Expand Down

0 comments on commit b38f2b8

Please sign in to comment.