1 #ifndef _OCRA_SOLVER_UTILITIES_HXX_ 2 #define _OCRA_SOLVER_UTILITIES_HXX_ 12 template<
class Derived,
class VectorBase>
13 inline void printLinearEquation(
const MatrixBase<Derived>& A,
const VectorBase& b,
int space,
int precision)
21 for (
int r=0; r<A.rows(); ++r)
25 for (
int c=0; c<A.cols(); ++c)
28 std::cout << (c/10)%10 <<
"-";
31 for (
int i=0; i<space; ++i)
34 std::cout << std::endl;
37 for (
int c=0; c<A.cols(); ++c)
41 if (std::abs(A(r,c)) < eps)
44 std::cout << std::setw(space) <<
"+eps";
45 else if (A(r,c)<-zero)
46 std::cout << std::setw(space) <<
"-eps";
48 std::cout << std::setw(space) <<
"0";
52 if (std::abs(A(r,c)) < 0.01)
53 std::cout << std::setw(space) << std::setprecision(precision-1) << A(r,c);
55 std::cout << std::setw(space) << std::setprecision(precision) << A(r,c);
62 if (std::abs(b[r]) < eps)
65 std::cout << std::setw(space) <<
"+eps" << std::endl;
67 std::cout << std::setw(space) <<
"-eps" << std::endl;
69 std::cout << std::setw(space) <<
"0" << std::endl;
72 std::cout << std::setw(space) << std::setprecision(precision) << b[r] << std::endl;
75 std::cout << std::endl;
82 template<
class Derived1,
class Derived2>
83 inline void uncompressByCol(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
84 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
86 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
95 template<
class Derived1,
class Derived2>
97 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
98 std::vector<int>& mapping,
double scale)
105 if (mapping.size() != rel.
getSize())
106 throw std::runtime_error(
"[ocra::utils::uncompressByCol] base variable does not contains relative variable" );
108 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
112 template<
class Derived1,
class Derived2>
113 inline void uncompressByRow(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
114 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
116 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
123 template<
class Derived1,
class Derived2>
125 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
126 std::vector<int>& mapping,
double scale)
133 if (mapping.size() != rel.
getSize())
134 throw std::runtime_error(
"[ocra::utils::uncompressByRow] base variable does not contains relative variable" );
136 uncompressbyRow(in, out, mapping, scale);
139 template<
class Derived1,
class Derived2>
140 inline void uncompress2d(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
141 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
143 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
150 template<
class Derived1,
class Derived2>
152 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
153 std::vector<int>& mapping,
double scale)
159 if (mapping.size() != rel.
getSize())
160 throw std::runtime_error(
"[ocra::utils::uncompress2d] base variable does not contains relative variable" );
171 template<
class Derived1,
class Derived2>
173 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
175 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
182 template<
class Derived1,
class Derived2>
184 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
185 std::vector<int>& mapping,
double scale)
192 if (mapping.size() != rel.
getSize())
193 throw std::runtime_error(
"[ocra::utils::addCompressByCol] base variable does not contains relative variable" );
195 addCompressByCol(in, out, mapping, scale);
198 template<
class Derived1,
class Derived2>
200 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
202 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
209 template<
class Derived1,
class Derived2>
211 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
212 std::vector<int>& mapping,
double scale)
219 if (mapping.size() != rel.
getSize())
220 throw std::runtime_error(
"[ocra::utils::addCompressedByRow] base variable does not contains relative variable" );
225 template<
class Derived1,
class Derived2>
226 inline void addCompressed2d(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
227 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
229 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
236 template<
class Derived1,
class Derived2>
238 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
239 std::vector<int>& mapping,
double scale)
241 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
246 if (mapping.size() != rel.
getSize())
247 throw std::runtime_error(
"[ocra::utils::addCompressed2d] base variable does not contains relative variable" );
258 template<
class Derived1,
class Derived2>
260 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
262 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
269 template<
class Derived1,
class Derived2>
271 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
272 std::vector<int>& mapping,
double scale)
279 if (mapping.size() != rel.
getSize())
280 throw std::runtime_error(
"[ocra::utils::minCompressedByCol] base variable does not contains relative variable" );
285 template<
class Derived1,
class Derived2>
287 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
289 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
296 template<
class Derived1,
class Derived2>
298 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
299 std::vector<int>& mapping,
double scale)
306 if (mapping.size() != rel.
getSize())
307 throw std::runtime_error(
"[ocra::utils::minCompressedByRow] base variable does not contains relative variable" );
312 template<
class Derived1,
class Derived2>
313 inline void minCompressed2d(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
314 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
316 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
323 template<
class Derived1,
class Derived2>
325 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
326 std::vector<int>& mapping,
double scale)
332 if (mapping.size() != rel.
getSize())
333 throw std::runtime_error(
"[ocra::utils::minCompressed2d] base variable does not contains relative variable" );
343 template<
class Derived1,
class Derived2>
345 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
347 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
354 template<
class Derived1,
class Derived2>
356 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
357 std::vector<int>& mapping,
double scale)
364 if (mapping.size() != rel.
getSize())
365 throw std::runtime_error(
"[ocra::utils::maxCompressedByCol] base variable does not contains relative variable" );
370 template<
class Derived1,
class Derived2>
372 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
374 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
381 template<
class Derived1,
class Derived2>
383 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
384 std::vector<int>& mapping,
double scale)
391 if (mapping.size() != rel.
getSize())
392 throw std::runtime_error(
"[ocra::utils::maxCompressedByRow] base variable does not contains relative variable" );
397 template<
class Derived1,
class Derived2>
398 inline void maxCompressed2d(
const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& _out,
399 const std::vector<int>& mapping,
double scale,
bool reverseMapping)
401 MatrixBase<Derived2>& out =
const_cast<MatrixBase<Derived2>&
>(_out);
408 template<
class Derived1,
class Derived2>
410 const MatrixBase<Derived1>& in, MatrixBase<Derived2>
const& out,
411 std::vector<int>& mapping,
double scale)
417 if (mapping.size() != rel.
getSize())
418 throw std::runtime_error(
"[ocra::utils::maxCompressed2d] base variable does not contains relative variable" );
427 template<
class Derived,
class VectorDerived>
429 MatrixBase<Derived>& A, MatrixBase<VectorDerived>
const & _b)
433 switch (apparentType)
442 MatrixBase<VectorDerived>& b =
const_cast<MatrixBase<VectorDerived>&
>(_b);
447 template<
class DerivedIn,
class VectorBaseIn1,
class VectorBaseIn2,
class DerivedOut,
class VectorBaseOut>
448 inline void convert(
const MatrixBase<DerivedIn>& M,
const VectorBaseIn1& v1,
const VectorBaseIn2& v2,
449 MatrixBase<DerivedOut>& A, VectorBaseOut& b,
450 const std::vector<int>& mapping,
457 ocra_assert(M.rows()== A.rows() &&
"Input and output matrices do not have the same row size");
458 ocra_assert(v1.size()== b.size() &&
"Input and output vectors do not have the same size");
477 template<
class Derived,
class VectorBase1,
class VectorBase2>
479 MatrixBase<Derived>& A, VectorBase1& b, VectorBase2& l,
double infinity)
494 ocra_assert(infinity > 0 &&
"if you use this type of output (CSTR_DOUBLE_BOUNDS), please specify its value (last argument of convert())");
495 switch (cstr.getType())
518 int dim = cstr.getDimension();
519 ocra_assert(A.rows() == 2*dim &&
"Output matrix A does not have the correct number of rows");
520 ocra_assert(b.size() == 2*dim &&
"Output vector b does not have the correct dimension");
521 Eigen::Block<Derived> A_block_0 = A.block(0, 0, dim, A.cols());
522 Eigen::Block<Derived> A_block_dim = A.block(dim, 0, dim, A.cols());
555 template<
class VectorBase1,
class VectorBase2>
562 ocra_assert((dynamic_cast<const BoundConstraint*>(&bounds) || dynamic_cast<const IdentityConstraint*>(&bounds)) &&
"bounds can only be of type BoundConstraint or IdentityConstraint");
567 if (bounds.getDimension() == 0)
571 switch(bounds.getType())
580 ocra_assert(
false &&
"should never happen, the constraint should be a bound constraint, hence an inequality");
585 switch(bounds.getType())
594 ocra_assert(
false &&
"should never happen, the constraint should be a bound constraint, hence an inequality");
601 #endif // _OCRA_SOLVER_UTILITIES_HXX_ void minCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void addCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
const details::eConvertCase conversion_cases[7][7]
void addCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void minCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void convert_(const LinearConstraint &cstr, eConstraintType apparentType, const std::vector< int > &mapping, eConvertCase convertCase, MatrixBase< Derived > &A, MatrixBase< VectorDerived > const &_b)
void getRelativeMappingOf(const Variable &subVariable, std::vector< int > &mapping) const
Returns the indexes of a subvariable in the variable.
static void uncompress2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mappingRow, const std::vector< int > &mappingCol, double scale)
void uncompressByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void uncompressByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
static void uncompressByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mapping, double scale)
void uncompress2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
This class represents a variable in a mathematical sense.
void addCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void maxCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
virtual T & getFunction(void)
void intersectBounds(const DiagonalLinearConstraint &bounds, const std::vector< int > &mapping, VectorBase1 &bl, VectorBase2 &bu)
void convert(const MatrixBase< DerivedIn > &M, const VectorBaseIn1 &v1, const VectorBaseIn2 &v2, MatrixBase< DerivedOut > &A, VectorBaseOut &b, const std::vector< int > &mapping, details::eConvertCase convertCase)
void maxCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void maxCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
#define ocra_assert(ocra_expression)
static void uncompressByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mapping, double scale)
#define OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(TYPE)
void printLinearEquation(const MatrixBase< Derived > &A, const VectorBase &b, int space=9, int precision=3)
void minCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)