- 3.0.2 optimal control module.
SwitchedLinearConstraintContainer-impl.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 namespace ct {
9 namespace optcon {
10 
11 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
13  const SwitchedLinearConstraintContainers& switchedLinearConstraintContainers,
14  const ModeSequence_t& continuousModeSequence)
15  : switchedLinearConstraintContainers_(switchedLinearConstraintContainers),
16  continuousModeSequence_(continuousModeSequence),
17  activeLinearConstraintContainer_(switchedLinearConstraintContainers.front()),
18  terminalLinearConstraintContainer_(switchedLinearConstraintContainers.at(continuousModeSequence.getFinalPhase()))
19 {
20 }
21 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
25  : LinearConstraintContainer<STATE_DIM, CONTROL_DIM, SCALAR>(arg),
26  continuousModeSequence_(arg.continuousModeSequence_)
27 {
28  // Clone individual constraints
29  switchedLinearConstraintContainers_.clear();
30  for (auto& linearConstraintContainer : arg.switchedLinearConstraintContainers_)
31  {
32  switchedLinearConstraintContainers_.emplace_back(linearConstraintContainer->clone());
33  }
34 
35  activeLinearConstraintContainer_ = switchedLinearConstraintContainers_.front();
36  terminalLinearConstraintContainer_ =
37  switchedLinearConstraintContainers_.at(continuousModeSequence_.getFinalPhase());
38 }
39 
40 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
42 {
43 }
44 
45 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
48 {
49  return new SwitchedLinearConstraintContainer(*this);
50 }
51 
52 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
55 {
56  return activeLinearConstraintContainer_->evaluateIntermediate();
57 }
58 
59 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
62 {
63  return terminalLinearConstraintContainer_->evaluateTerminal();
64 }
65 
66 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
68 {
69  return activeLinearConstraintContainer_->getIntermediateConstraintsCount();
70 }
71 
72 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
74 {
75  return terminalLinearConstraintContainer_->getTerminalConstraintsCount();
76 }
77 
78 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
81 {
82  return activeLinearConstraintContainer_->jacobianStateSparseIntermediate();
83 }
84 
85 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
88 {
89  return activeLinearConstraintContainer_->jacobianStateIntermediate();
90 }
91 
92 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
95 {
96  return terminalLinearConstraintContainer_->jacobianStateSparseTerminal();
97 }
98 
99 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
102 {
103  return terminalLinearConstraintContainer_->jacobianStateTerminal();
104 }
105 
106 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
109 {
110  return activeLinearConstraintContainer_->jacobianInputSparseIntermediate();
111 }
112 
113 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
116 {
117  return activeLinearConstraintContainer_->jacobianInputIntermediate();
118 }
119 
120 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
123 {
124  return terminalLinearConstraintContainer_->jacobianInputSparseTerminal();
125 }
126 
127 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
130 {
131  return terminalLinearConstraintContainer_->jacobianInputTerminal();
132 }
133 
134 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
136  Eigen::VectorXi& iRows,
137  Eigen::VectorXi& jCols)
138 {
139  return activeLinearConstraintContainer_->sparsityPatternStateIntermediate(iRows, jCols);
140 }
141 
142 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
144  Eigen::VectorXi& iRows,
145  Eigen::VectorXi& jCols)
146 {
147  return terminalLinearConstraintContainer_->sparsityPatternStateTerminal(iRows, jCols);
148 }
149 
150 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
152  Eigen::VectorXi& iRows,
153  Eigen::VectorXi& jCols)
154 {
155  return activeLinearConstraintContainer_->sparsityPatternInputIntermediate(iRows, jCols);
156 }
157 
158 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
160  Eigen::VectorXi& iRows,
161  Eigen::VectorXi& jCols)
162 {
163  return terminalLinearConstraintContainer_->sparsityPatternInputTerminal(iRows, jCols);
164 }
165 
166 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
168 {
169  return activeLinearConstraintContainer_->getJacobianStateNonZeroCountIntermediate();
170 }
171 
172 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
174 {
175  return terminalLinearConstraintContainer_->getJacobianStateNonZeroCountTerminal();
176 }
177 
178 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
180 {
181  return activeLinearConstraintContainer_->getJacobianInputNonZeroCountIntermediate();
182 }
183 
184 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
186 {
187  return terminalLinearConstraintContainer_->getJacobianInputNonZeroCountTerminal();
188 }
189 
190 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
192 {
193  for (auto linearConstraintContainer : switchedLinearConstraintContainers_)
194  {
195  linearConstraintContainer->initialize();
196  if (!linearConstraintContainer->isInitialized())
197  {
198  std::cout << "SwitchedLinearConstraintContainer::initializeIntermediate(): "
199  << "one of the constraints failed to initialize" << std::endl;
200  return false;
201  }
202  }
203  return true;
204 }
205 
206 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
208 {
209  // All in switchedLinearConstraintContainers_ already initialized in initializeIntermediate()
210  this->lowerBoundsTerminal_ = terminalLinearConstraintContainer_->getLowerBoundsTerminal();
211  this->upperBoundsTerminal_ = terminalLinearConstraintContainer_->getUpperBoundsTerminal();
212  return true;
213 }
214 
215 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
217 {
218  auto mode = continuousModeSequence_.getPhaseFromTime(this->t_);
219  activeLinearConstraintContainer_ = switchedLinearConstraintContainers_.at(mode);
220  activeLinearConstraintContainer_->setCurrentStateAndControl(this->x_, this->u_, this->t_);
221 
222  this->lowerBoundsIntermediate_ = activeLinearConstraintContainer_->getLowerBoundsIntermediate();
223  this->upperBoundsIntermediate_ = activeLinearConstraintContainer_->getUpperBoundsIntermediate();
224 }
225 
226 } // namespace optcon
227 } // namespace ct
virtual SwitchedLinearConstraintContainer_Raw_Ptr_t clone() const override
Definition: SwitchedLinearConstraintContainer-impl.h:47
virtual MatrixXs jacobianInputTerminal() override
Evaluates the constraint jacobian wrt the control input.
Definition: SwitchedLinearConstraintContainer-impl.h:129
virtual VectorXs jacobianInputSparseIntermediate() override
Evaluates the constraint jacobian wrt the control input using sparse representation.
Definition: SwitchedLinearConstraintContainer-impl.h:108
virtual void sparsityPatternStateTerminal(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt state.
Definition: SwitchedLinearConstraintContainer-impl.h:143
virtual size_t getTerminalConstraintsCount() override
Retrieves the number of final constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:73
A base function for linear constraint functions which have a first derivative.
Definition: LinearConstraintContainer.h:27
input_vector_t u_
Definition: ConstraintContainerBase.h:198
A container for switching linear constraint containers.
Definition: SwitchedLinearConstraintContainer.h:27
VectorXs lowerBoundsTerminal_
Definition: ConstraintContainerBase.h:202
virtual VectorXs jacobianInputSparseTerminal() override
Evaluates the constraint jacobian wrt the control input using sparse representation.
Definition: SwitchedLinearConstraintContainer-impl.h:122
virtual VectorXs jacobianStateSparseIntermediate() override
Evaluates the constraint jacobian wrt the state using sparse representation.
Definition: SwitchedLinearConstraintContainer-impl.h:80
virtual MatrixXs jacobianStateTerminal() override
Evaluates the constraint jacobian wrt the state.
Definition: SwitchedLinearConstraintContainer-impl.h:101
VectorXs upperBoundsTerminal_
Definition: ConstraintContainerBase.h:204
virtual size_t getJacobianStateNonZeroCountTerminal() override
Returns the number of non zero elements in the constraint jacobian wrt state.
Definition: SwitchedLinearConstraintContainer-impl.h:173
VectorXs lowerBoundsIntermediate_
Definition: ConstraintContainerBase.h:201
virtual bool initializeTerminal() override
Initializes the terminal constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:207
virtual size_t getIntermediateConstraintsCount() override
Retrieves the number of intermediate constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:67
core::Switched< LinearConstraintContainer_Ptr_t > SwitchedLinearConstraintContainers
Definition: SwitchedLinearConstraintContainer.h:38
virtual void sparsityPatternInputTerminal(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt control.
Definition: SwitchedLinearConstraintContainer-impl.h:159
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual MatrixXs jacobianInputIntermediate() override
Evaluates the constraint jacobian wrt the control input.
Definition: SwitchedLinearConstraintContainer-impl.h:115
Phase getFinalPhase() const
virtual VectorXs evaluateTerminal() override
Evaluates the terminal constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:61
SCALAR t_
Definition: ConstraintContainerBase.h:199
virtual size_t getJacobianStateNonZeroCountIntermediate() override
Returns the number of non zero elements in the constraint jacobian wrt state.
Definition: SwitchedLinearConstraintContainer-impl.h:167
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: SwitchedLinearConstraintContainer.h:40
SwitchedLinearConstraintContainer(const SwitchedLinearConstraintContainers &switchedLinearConstraintContainers, const ModeSequence_t &continuousModeSequence)
Default constructor.
Definition: SwitchedLinearConstraintContainer-impl.h:12
VectorXs upperBoundsIntermediate_
Definition: ConstraintContainerBase.h:203
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: SwitchedLinearConstraintContainer.h:39
Phase getPhaseFromTime(Time time) const
virtual void sparsityPatternInputIntermediate(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt control.
Definition: SwitchedLinearConstraintContainer-impl.h:151
state_vector_t x_
Definition: ConstraintContainerBase.h:197
virtual VectorXs evaluateIntermediate() override
Evaluates the intermediate constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:54
virtual VectorXs jacobianStateSparseTerminal() override
Evaluates the constraint jacobian wrt the state using sparse representation.
Definition: SwitchedLinearConstraintContainer-impl.h:94
virtual ~SwitchedLinearConstraintContainer()
Destructor.
Definition: SwitchedLinearConstraintContainer-impl.h:41
virtual size_t getJacobianInputNonZeroCountTerminal() override
Returns the number of non zero elements in the constraint jacobian wrt input.
Definition: SwitchedLinearConstraintContainer-impl.h:185
virtual MatrixXs jacobianStateIntermediate() override
Evaluates the constraint jacobian wrt the state.
Definition: SwitchedLinearConstraintContainer-impl.h:87
virtual void sparsityPatternStateIntermediate(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt state.
Definition: SwitchedLinearConstraintContainer-impl.h:135
virtual size_t getJacobianInputNonZeroCountIntermediate() override
Returns the number of non zero elements in the constraint jacobian wrt input.
Definition: SwitchedLinearConstraintContainer-impl.h:179
virtual bool initializeIntermediate() override
Initializes the intermediate constraints.
Definition: SwitchedLinearConstraintContainer-impl.h:191