ConstraintNormal.h
Go to the documentation of this file.
1 /* Copyright 2020 CNRS-AIST JRL */
2 
3 #pragma once
4 
5 #include <iosfwd>
6 
7 #include <Eigen/Core>
8 
9 #include <jrl-qp/api.h>
10 #include <jrl-qp/defs.h>
11 #include <jrl-qp/enums.h>
13 #include <jrl-qp/utils/toMatlab.h>
14 
15 namespace jrl::qp::internal
16 {
24 {
25 public:
27  ConstraintNormal() : p_(-1), status_(ActivationStatus::INACTIVE), C_(Eigen::MatrixXd(0, 0)) {}
38  ConstraintNormal(const MatrixConstRef & C, int p, ActivationStatus status) : p_(p), status_(status), C_(C)
39  {
40  assert((status < ActivationStatus::LOWER_BOUND && p < C_.cols())
41  || (status >= ActivationStatus::LOWER_BOUND && p >= C.cols()));
42  }
43 
46  : ConstraintNormal(C, sc.index(), sc.status())
47  {
48  }
49 
50  ConstraintNormal(const ConstraintNormal & other) : p_(other.p_), status_(other.status_), C_(other.C_) {}
52  : p_(std::move(other.p_)), status_(std::move(other.status_)), C_(std::move(other.C_))
53  {
54  }
56  {
57  p_ = other.p_;
58  status_ = other.status_;
59  new(&C_) MatrixConstRef(other.C_);
60  return *this;
61  }
62 
64  int index() const
65  {
66  return p_;
67  }
69  int bndIndex() const
70  {
71  assert(status_ >= ActivationStatus::LOWER_BOUND);
72  return p_ - static_cast<int>(C_.cols());
73  }
76  {
77  return status_;
78  }
79 
81  void preMultiplyByMt(VectorRef out, const MatrixConstRef & M) const
82  {
83  switch(status_)
84  {
85  case ActivationStatus::EQUALITY: // fallthrough
87  out.noalias() = M.transpose() * C_.col(p_);
88  break;
90  out.noalias() = -M.transpose() * C_.col(p_);
91  break;
92  case ActivationStatus::FIXED: // fallthrough
94  out = M.row(p_ - C_.cols()).transpose();
95  break;
97  out = -M.row(p_ - C_.cols()).transpose();
98  break;
99  default:
100  assert(false);
101  }
102  }
103 
105  double dot(const VectorConstRef & v) const
106  {
107  switch(status_)
108  {
109  case ActivationStatus::EQUALITY: // fallthrough
111  return v.dot(C_.col(p_));
113  return -v.dot(C_.col(p_));
114  case ActivationStatus::FIXED: // fallthrough
116  return v[bndIndex()];
118  return -v[bndIndex()];
119  default:
120  assert(false);
121  return 0;
122  }
123  }
124 
125  friend std::ostream & operator<<(std::ostream & os, const ConstraintNormal & n)
126  {
127  os << "{" << n.p_ << ", " << static_cast<int>(n.status_) << ", ";
128  switch(n.status_)
129  {
130  case ActivationStatus::EQUALITY: // fallthrough
132  os << (utils::toMatlab)n.C_.col(n.p_);
133  break;
135  os << (utils::toMatlab)(-n.C_.col(n.p_));
136  break;
137  case ActivationStatus::FIXED: // fallthrough
139  os << (utils::toMatlab)Eigen::MatrixXd::Identity(n.C_.rows(), n.C_.rows()).col(n.bndIndex());
140  break;
142  os << (utils::toMatlab)(-Eigen::MatrixXd::Identity(n.C_.rows(), n.C_.rows()).col(n.bndIndex()));
143  break;
144  default:
145  assert(false);
146  }
147  os << "}";
148  return os;
149  }
150 
151 private:
152  int p_;
153  ActivationStatus status_;
154  MatrixConstRef C_;
155 };
156 
157 } // namespace jrl::qp::internal
jrl::qp::internal::SelectedConstraint
Definition: SelectedConstraint.h:13
jrl::qp::internal::ConstraintNormal::ConstraintNormal
ConstraintNormal(const MatrixConstRef &C, const SelectedConstraint &sc)
Definition: ConstraintNormal.h:45
jrl::qp::internal::ConstraintNormal::dot
double dot(const VectorConstRef &v) const
Definition: ConstraintNormal.h:105
jrl::qp::internal::ConstraintNormal::operator=
ConstraintNormal & operator=(const ConstraintNormal &other)
Definition: ConstraintNormal.h:55
jrl::qp::VectorConstRef
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
Definition: defs.h:13
jrl::qp::internal
Definition: ActiveSet.h:11
jrl::qp::ActivationStatus
ActivationStatus
Definition: enums.h:14
jrl::qp::internal::ConstraintNormal::ConstraintNormal
ConstraintNormal()
Definition: ConstraintNormal.h:27
jrl::qp::ActivationStatus::EQUALITY
@ EQUALITY
jrl::qp::MatrixConstRef
Eigen::Ref< const Eigen::MatrixXd > MatrixConstRef
Definition: defs.h:11
jrl::qp::internal::ConstraintNormal::ConstraintNormal
ConstraintNormal(const MatrixConstRef &C, int p, ActivationStatus status)
Definition: ConstraintNormal.h:38
jrl::qp::ActivationStatus::INACTIVE
@ INACTIVE
jrl::qp::internal::ConstraintNormal::status
ActivationStatus status() const
Definition: ConstraintNormal.h:75
jrl::qp::ActivationStatus::UPPER_BOUND
@ UPPER_BOUND
enums.h
jrl::qp::ActivationStatus::UPPER
@ UPPER
jrl::qp::internal::ConstraintNormal::bndIndex
int bndIndex() const
Definition: ConstraintNormal.h:69
jrl::qp::ActivationStatus::FIXED
@ FIXED
jrl::qp::internal::ConstraintNormal::index
int index() const
Definition: ConstraintNormal.h:64
jrl::qp::internal::ConstraintNormal::ConstraintNormal
ConstraintNormal(ConstraintNormal &&other) noexcept
Definition: ConstraintNormal.h:51
defs.h
jrl::qp::ActivationStatus::LOWER_BOUND
@ LOWER_BOUND
jrl::qp::internal::ConstraintNormal::preMultiplyByMt
void preMultiplyByMt(VectorRef out, const MatrixConstRef &M) const
Definition: ConstraintNormal.h:81
SelectedConstraint.h
jrl::qp::VectorRef
Eigen::Ref< Eigen::VectorXd > VectorRef
Definition: defs.h:14
jrl::qp::internal::ConstraintNormal::operator<<
friend std::ostream & operator<<(std::ostream &os, const ConstraintNormal &n)
Definition: ConstraintNormal.h:125
toMatlab.h
jrl::qp::utils::toMatlab
Definition: toMatlab.h:23
jrl::qp::internal::ConstraintNormal::ConstraintNormal
ConstraintNormal(const ConstraintNormal &other)
Definition: ConstraintNormal.h:50
api.h
jrl::qp::internal::ConstraintNormal
Definition: ConstraintNormal.h:23
jrl::qp::ActivationStatus::LOWER
@ LOWER