- 3.0.2 optimal control module.
CostFunctionQuadratic.hpp
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 #include "CostFunction.hpp"
9 #include "term/TermBase.hpp"
10 
11 namespace ct {
12 namespace optcon {
13 
14 
28 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
29 class CostFunctionQuadratic : public CostFunction<STATE_DIM, CONTROL_DIM, SCALAR>
30 {
31 public:
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
33 
34  typedef Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM> state_matrix_t;
35  typedef Eigen::Matrix<SCALAR, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
36  typedef Eigen::Matrix<SCALAR, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
37 
40 
42 
47 
53 
59 
63  virtual ~CostFunctionQuadratic();
64 
71  virtual size_t addIntermediateTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> term,
72  bool verbose = false);
73 
80  virtual size_t addFinalTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> term, bool verbose = false);
81 
82 
83 #ifdef CPPADCG
84  virtual void addIntermediateADTerm(
86  bool verbose = false);
87 
88  virtual void addFinalADTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
89  bool verbose = false);
90 #endif
91 
97  virtual void loadFromConfigFile(const std::string& filename, bool verbose = false);
98 
103  virtual state_vector_t stateDerivativeIntermediate() = 0;
104 
109  virtual state_vector_t stateDerivativeTerminal() = 0;
110 
115  virtual state_matrix_t stateSecondDerivativeIntermediate() = 0;
116 
121  virtual state_matrix_t stateSecondDerivativeTerminal() = 0;
122 
127  virtual control_vector_t controlDerivativeIntermediate() = 0;
128 
135  virtual control_vector_t controlDerivativeTerminal();
140  virtual control_matrix_t controlSecondDerivativeIntermediate() = 0;
141 
148  virtual control_matrix_t controlSecondDerivativeTerminal();
149 
154  virtual control_state_matrix_t stateControlDerivativeIntermediate() = 0;
155 
160  virtual control_state_matrix_t stateControlDerivativeTerminal();
161 
163  virtual void updateReferenceState(const state_vector_t& x_ref);
164 
166  virtual void updateFinalState(const state_vector_t& x_final);
167 
169  virtual void updateReferenceControl(const control_vector_t& u_ref);
170 
172  bool stateDerivativeIntermediateTest(bool verbose = false);
173 
175  bool controlDerivativeIntermediateTest(bool verbose = false);
176 
177  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> getIntermediateTermById(const size_t id);
178 
179  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> getFinalTermById(const size_t id);
180 
181  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> getIntermediateTermByName(const std::string& name);
182 
183  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> getFinalTermByName(const std::string& name);
184 
186  virtual void initialize();
187 
188 protected:
191 
194 
196  state_vector_t stateDerivativeIntermediateBase();
197 
199  state_vector_t stateDerivativeTerminalBase();
200 
202  state_matrix_t stateSecondDerivativeIntermediateBase();
203 
205  state_matrix_t stateSecondDerivativeTerminalBase();
206 
208  control_vector_t controlDerivativeIntermediateBase();
209 
211  control_vector_t controlDerivativeTerminalBase();
212 
214  control_matrix_t controlSecondDerivativeIntermediateBase();
215 
217  control_matrix_t controlSecondDerivativeTerminalBase();
218 
220  control_state_matrix_t stateControlDerivativeIntermediateBase();
221 
223  control_state_matrix_t stateControlDerivativeTerminalBase();
224 
226  state_vector_t stateDerivativeIntermediateNumDiff();
227 
229  control_vector_t controlDerivativeIntermediateNumDiff();
230 
233 
236 
238  std::vector<std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>> intermediateCostAnalytical_;
239 
241  std::vector<std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>> finalCostAnalytical_;
242 };
243 
244 
245 } // namespace optcon
246 } // namespace ct
CostFunction< STATE_DIM, CONTROL_DIM, SCALAR > BASE
Definition: CostFunctionQuadratic.hpp:41
std::vector< std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > > intermediateCostAnalytical_
Definition: CostFunctionQuadratic.hpp:238
control_state_matrix_t stateControlDerivativeIntermediateBase()
evaluate intermediate analytical control mixed state control derivatives
Definition: CostFunctionQuadratic-impl.hpp:473
control_matrix_t controlSecondDerivativeIntermediateBase()
evaluate intermediate analytical control second derivatives
Definition: CostFunctionQuadratic-impl.hpp:440
virtual state_vector_t stateDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to state.
SCALAR eps_
stepsize for numerical differentiation
Definition: CostFunctionQuadratic.hpp:232
A base function for cost functions. All cost functions should derive from this.
Definition: CostFunction.hpp:25
SCALAR evaluateIntermediateBase()
evaluate intermediate analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:310
virtual void updateReferenceControl(const control_vector_t &u_ref)
update the reference control for intermediate cost terms
Definition: CostFunctionQuadratic-impl.hpp:105
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: CostFunctionQuadratic.hpp:36
core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: CostFunctionQuadratic.hpp:38
virtual void updateFinalState(const state_vector_t &x_final)
update the reference state for final cost terms
Definition: CostFunctionQuadratic-impl.hpp:98
virtual void updateReferenceState(const state_vector_t &x_ref)
update the reference state for intermediate cost terms
Definition: CostFunctionQuadratic-impl.hpp:91
virtual void initialize()
initialize the cost function (e.g. to be used in CostFunctionAD)
Definition: CostFunctionQuadratic-impl.hpp:269
virtual control_state_matrix_t stateControlDerivativeTerminal()
Computes final-cost derivative with respect to state and control.
Definition: CostFunctionQuadratic-impl.hpp:85
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > state_matrix_t
Definition: CostFunctionQuadratic.hpp:34
virtual control_matrix_t controlSecondDerivativeIntermediate()=0
Computes intermediate-cost second-order derivative with respect to input.
state_vector_t stateDerivativeTerminalBase()
evaluate terminal analytical state derivatives
Definition: CostFunctionQuadratic-impl.hpp:361
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
virtual void loadFromConfigFile(const std::string &filename, bool verbose=false)
Loads cost function from config file.
Definition: CostFunctionQuadratic-impl.hpp:63
state_matrix_t stateSecondDerivativeIntermediateBase()
evaluate intermediate analytical state second derivatives
Definition: CostFunctionQuadratic-impl.hpp:374
control_vector_t controlDerivativeIntermediateBase()
evaluate intermediate analytical control derivatives
Definition: CostFunctionQuadratic-impl.hpp:407
CppAD::AD< CppAD::cg::CG< double > > SCALAR
control_vector_t controlDerivativeTerminalBase()
evaluate terminal analytical control derivatives
Definition: CostFunctionQuadratic-impl.hpp:426
state_matrix_t stateSecondDerivativeTerminalBase()
evaluate terminal analytical state second derivatives
Definition: CostFunctionQuadratic-impl.hpp:393
state_vector_t stateDerivativeIntermediateBase()
evaluate intermediate analytical state derivatives
Definition: CostFunctionQuadratic-impl.hpp:341
SCALAR evaluateTerminalBase()
evaluate terminal analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:328
virtual size_t addIntermediateTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds an intermediate term.
Definition: CostFunctionQuadratic-impl.hpp:279
core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: CostFunctionQuadratic.hpp:39
CostFunctionQuadratic()
Definition: CostFunctionQuadratic-impl.hpp:12
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getIntermediateTermByName(const std::string &name)
Definition: CostFunctionQuadratic-impl.hpp:153
virtual size_t addFinalTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds a final term.
Definition: CostFunctionQuadratic-impl.hpp:294
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getIntermediateTermById(const size_t id)
Definition: CostFunctionQuadratic-impl.hpp:139
bool stateDerivativeIntermediateTest(bool verbose=false)
compare the state derivative against numerical differentiation
Definition: CostFunctionQuadratic-impl.hpp:112
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CostFunctionQuadratic.hpp:35
virtual state_vector_t stateDerivativeTerminal()=0
virtual state_matrix_t stateSecondDerivativeIntermediate()=0
Computes intermediate-cost second-order derivative with respect to state.
bool doubleSidedDerivative_
use double sided derivatives in numerical differentiation
Definition: CostFunctionQuadratic.hpp:235
bool controlDerivativeIntermediateTest(bool verbose=false)
compare the control derivative against numerical differentiation
Definition: CostFunctionQuadratic-impl.hpp:125
virtual control_vector_t controlDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to control.
virtual control_vector_t controlDerivativeTerminal()
Computes terminal-cost first-order derivative with respect to control.
Definition: CostFunctionQuadratic-impl.hpp:71
const bool verbose
Definition: ConstraintComparison.h:18
virtual control_state_matrix_t stateControlDerivativeIntermediate()=0
Computes intermediate-cost derivative with respect to state and control.
control_vector_t controlDerivativeIntermediateNumDiff()
compute the control derivative by numerical differentiation (can be used for testing) ...
Definition: CostFunctionQuadratic-impl.hpp:222
control_matrix_t controlSecondDerivativeTerminalBase()
evaluate terminal analytical control second derivatives
Definition: CostFunctionQuadratic-impl.hpp:459
control_state_matrix_t stateControlDerivativeTerminalBase()
evaluate terminal analytical control mixed state control derivatives
Definition: CostFunctionQuadratic-impl.hpp:492
std::vector< std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > > finalCostAnalytical_
Definition: CostFunctionQuadratic.hpp:241
virtual CostFunctionQuadratic< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const =0
virtual state_matrix_t stateSecondDerivativeTerminal()=0
Computes final-cost second-order derivative with respect to state.
virtual control_matrix_t controlSecondDerivativeTerminal()
Computes final-cost second-order derivative with respect to input.
Definition: CostFunctionQuadratic-impl.hpp:78
state_vector_t stateDerivativeIntermediateNumDiff()
compute the state derivative by numerical differentiation (can be used for testing) ...
Definition: CostFunctionQuadratic-impl.hpp:175
virtual ~CostFunctionQuadratic()
Definition: CostFunctionQuadratic-impl.hpp:40
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getFinalTermById(const size_t id)
Definition: CostFunctionQuadratic-impl.hpp:146
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getFinalTermByName(const std::string &name)
Definition: CostFunctionQuadratic-impl.hpp:164