- 3.0.2 optimal control module.
CostFunctionQuadraticSimple-impl.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 namespace ct {
9 namespace optcon {
10 
11 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
13 {
14  x_nominal_.setZero();
15  Q_.setZero();
16  u_nominal_.setZero();
17  R_.setZero();
18  x_final_.setZero();
19  Q_final_.setZero();
20  x_deviation_.setZero();
21  u_deviation_.setZero();
22 }
23 
24 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
26  const control_matrix_t& R,
27  const state_vector_t& x_nominal,
28  const control_vector_t& u_nominal,
29  const state_vector_t& x_final,
30  const state_matrix_t& Q_final)
31  : x_nominal_(x_nominal), Q_(Q), u_nominal_(u_nominal), R_(R), x_final_(x_final), Q_final_(Q_final)
32 {
33  x_deviation_.setZero();
34  u_deviation_.setZero();
35 }
36 
37 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
39 {
40 }
41 
42 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
44  const CostFunctionQuadraticSimple& arg)
47  Q_(arg.Q_),
50  R_(arg.R_),
51  x_final_(arg.x_final_),
52  Q_final_(arg.Q_final_)
53 {
54 }
55 
56 
57 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
60 {
62 }
63 
64 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
66  const control_vector_t& u,
67  const SCALAR& t)
68 {
69  this->x_ = x;
70  this->u_ = u;
71  this->t_ = t;
72 
73  this->x_deviation_ = x - x_nominal_;
74  this->u_deviation_ = u - u_nominal_;
75 }
76 
77 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
79 {
80  SCALAR costQ = SCALAR(0.5) * (x_deviation_.transpose() * Q_ * x_deviation_)(0);
81  SCALAR costR = SCALAR(0.5) * (u_deviation_.transpose() * R_ * u_deviation_)(0);
82  return costQ + costR;
83 }
84 
85 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
88 {
89  return Q_ * x_deviation_;
90 }
91 
92 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
95 {
96  return Q_;
97 }
98 
99 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
102 {
103  return R_ * u_deviation_;
104 }
105 
106 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
109 {
110  return R_;
111 }
112 
113 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
116 {
117  return control_state_matrix_t::Zero();
118 }
119 
120 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
122 {
123  state_vector_t x_deviation_final = this->x_ - x_final_;
124  return SCALAR(0.5) * x_deviation_final.transpose() * Q_final_ * x_deviation_final;
125 }
126 
127 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
130 {
131  state_vector_t x_deviation_final = this->x_ - x_final_;
132  return Q_final_ * x_deviation_final;
133 }
134 
135 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
138 {
139  return Q_final_;
140 }
141 
142 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
144 {
145  x_nominal_ = x_ref;
146 }
147 
148 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
150 {
151  x_final_ = x_final;
152 }
153 
154 
155 } // namespace optcon
156 } // namespace ct
virtual SCALAR evaluateTerminal() override
evaluate terminal costs
Definition: CostFunctionQuadraticSimple-impl.hpp:121
state_vector_t x_
Definition: CostFunction.hpp:103
state_matrix_t Q_
Definition: CostFunctionQuadraticSimple.hpp:94
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
virtual void updateReferenceState(const state_vector_t &x_ref) override
update the reference state for intermediate cost terms
Definition: CostFunctionQuadraticSimple-impl.hpp:143
virtual void setCurrentStateAndControl(const state_vector_t &x, const control_vector_t &u, const SCALAR &t) override
Definition: CostFunctionQuadraticSimple-impl.hpp:65
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: CostFunctionQuadraticSimple.hpp:30
CostFunctionQuadraticSimple< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
Definition: CostFunctionQuadraticSimple-impl.hpp:59
SCALAR t_
Definition: CostFunction.hpp:105
virtual state_vector_t stateDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to state.
Definition: CostFunctionQuadraticSimple-impl.hpp:87
A simple quadratic cost function.
Definition: CostFunctionQuadraticSimple.hpp:23
virtual control_matrix_t controlSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to input.
Definition: CostFunctionQuadraticSimple-impl.hpp:108
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
control_vector_t u_nominal_
Definition: CostFunctionQuadraticSimple.hpp:97
state_matrix_t Q_final_
Definition: CostFunctionQuadraticSimple.hpp:101
state_vector_t x_deviation_
Definition: CostFunctionQuadraticSimple.hpp:92
virtual state_vector_t stateDerivativeTerminal() override
Definition: CostFunctionQuadraticSimple-impl.hpp:129
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual state_matrix_t stateSecondDerivativeTerminal() override
Computes final-cost second-order derivative with respect to state.
Definition: CostFunctionQuadraticSimple-impl.hpp:137
virtual void updateFinalState(const state_vector_t &x_final) override
update the reference state for final cost terms
Definition: CostFunctionQuadraticSimple-impl.hpp:149
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CostFunctionQuadraticSimple.hpp:29
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
control_matrix_t R_
Definition: CostFunctionQuadraticSimple.hpp:98
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CostFunctionQuadratic.hpp:35
virtual SCALAR evaluateIntermediate() override
evaluate intermediate costs
Definition: CostFunctionQuadraticSimple-impl.hpp:78
control_vector_t u_
Definition: CostFunction.hpp:104
virtual ~CostFunctionQuadraticSimple()
Definition: CostFunctionQuadraticSimple-impl.hpp:38
Eigen::Matrix< double, nStates, nStates > state_matrix_t
control_vector_t u_deviation_
Definition: CostFunctionQuadraticSimple.hpp:96
state_vector_t x_final_
Definition: CostFunctionQuadraticSimple.hpp:100
virtual state_matrix_t stateSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to state.
Definition: CostFunctionQuadraticSimple-impl.hpp:94
virtual control_vector_t controlDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to control.
Definition: CostFunctionQuadraticSimple-impl.hpp:101
virtual control_state_matrix_t stateControlDerivativeIntermediate() override
Computes intermediate-cost derivative with respect to state and control.
Definition: CostFunctionQuadraticSimple-impl.hpp:115
CostFunctionQuadraticSimple()
Definition: CostFunctionQuadraticSimple-impl.hpp:12
state_vector_t x_nominal_
Definition: CostFunctionQuadraticSimple.hpp:93