- 3.0.2 optimal control module.
CostFunctionAnalytical-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 }
15 
16 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
18  : CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>(arg)
19 {
20 }
21 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
24  bool verbose)
25 {
26  loadFromConfigFile(filename, verbose);
27 }
28 
29 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
31  const
32 {
33  return new CostFunctionAnalytical(*this);
34 }
35 
36 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
38 {
39 }
40 
41 
42 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
44  bool verbose)
45 {
46  if (verbose)
47  std::cout << "Starting to load analytical cost function from file " << filename << std::endl;
48 
49  this->intermediateCostAnalytical_.clear();
50  this->finalCostAnalytical_.clear();
51 
52  boost::property_tree::ptree pt;
53  boost::property_tree::read_info(filename, pt);
54  int i = 0;
55  std::string currentTerm;
56  do
57  {
58  std::cout << "=============================================" << std::endl; //indicating new term
59  currentTerm = "term" + std::to_string(i);
60  std::string termKind = pt.get<std::string>(currentTerm + ".kind");
61  boost::algorithm::to_lower(termKind);
62  int currentTermType = pt.get<int>(currentTerm + ".type");
63  std::string termName;
64  try
65  {
66  termName = pt.get<std::string>(currentTerm + ".name");
67  if (verbose)
68  std::cout << "Trying to add " + termName + " as term" << std::endl;
69  } catch (boost::property_tree::ptree_bad_path err)
70  {
71  termName = "Unnamed";
72  if (verbose)
73  {
74  std::cout << "Name field for " + currentTerm + " does not exist" << std::endl;
75  }
76  }
77 
78  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> term;
79 
81 
82  if (!term)
83  {
84  throw std::runtime_error("Term type \"" + termKind + "\" not supported");
85  }
86  else
87  {
88  addTerm(filename, currentTerm, currentTermType, term, this, verbose);
89  }
90  currentTerm = "term" + std::to_string(++i);
91  } while (pt.find(currentTerm) != pt.not_found());
92 }
93 
94 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
96 {
97  return this->evaluateIntermediateBase();
98 }
99 
100 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
102 {
103  return this->evaluateTerminalBase();
104 }
105 
106 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
109 {
110  return this->stateDerivativeIntermediateBase();
111 }
112 
113 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
116 {
117  return this->stateDerivativeTerminalBase();
118 }
119 
120 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
123 {
125 }
126 
127 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
130 {
131  return this->stateSecondDerivativeTerminalBase();
132 }
133 
134 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
137 {
138  return this->controlDerivativeIntermediateBase();
139 }
140 
141 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
144 {
145  return this->controlDerivativeTerminalBase();
146 }
147 
148 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
151 {
153 }
154 
155 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
158 {
160 }
161 
162 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
165 {
167 }
168 
169 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
172 {
173  return this->stateControlDerivativeTerminalBase();
174 }
175 
176 } // namespace optcon
177 } // namespace ct
control_vector_t controlDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to control.
Definition: CostFunctionAnalytical-impl.hpp:136
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
SCALAR evaluateIntermediateBase()
evaluate intermediate analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:310
state_vector_t stateDerivativeTerminal() override
Definition: CostFunctionAnalytical-impl.hpp:115
CostFunctionAnalytical()
Basic constructor.
Definition: CostFunctionAnalytical-impl.hpp:12
state_matrix_t stateSecondDerivativeTerminal() override
Computes final-cost second-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:129
void loadFromConfigFile(const std::string &filename, bool verbose=false) override
Loads cost function from config file.
Definition: CostFunctionAnalytical-impl.hpp:43
A cost function which contains only terms that have analytical derivatives.
Definition: CostFunctionAnalytical.hpp:31
control_state_matrix_t stateControlDerivativeTerminal() override
Computes final-cost derivative with respect to state and control.
Definition: CostFunctionAnalytical-impl.hpp:171
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
state_matrix_t stateSecondDerivativeIntermediateBase()
evaluate intermediate analytical state second derivatives
Definition: CostFunctionQuadratic-impl.hpp:374
control_state_matrix_t stateControlDerivativeIntermediate() override
Computes intermediate-cost derivative with respect to state and control.
Definition: CostFunctionAnalytical-impl.hpp:164
control_vector_t controlDerivativeIntermediateBase()
evaluate intermediate analytical control derivatives
Definition: CostFunctionQuadratic-impl.hpp:407
SCALAR evaluateIntermediate() override
evaluate intermediate costs
Definition: CostFunctionAnalytical-impl.hpp:95
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
void addTerm(const std::string &filename, std::string &currentTerm, int currentTermType, TERM_PTR term, costFuncType *costFunc, bool verbose=false)
Definition: utilities.hpp:81
SCALAR evaluateTerminalBase()
evaluate terminal analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:328
SCALAR evaluateTerminal() override
evaluate terminal costs
Definition: CostFunctionAnalytical-impl.hpp:101
for i
Definition: mpc_unittest_plotting.m:14
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: CostFunctionAnalytical.hpp:38
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CostFunctionAnalytical.hpp:37
state_matrix_t stateSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:122
control_matrix_t controlSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to input.
Definition: CostFunctionAnalytical-impl.hpp:150
CostFunctionAnalytical< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
Definition: CostFunctionAnalytical-impl.hpp:30
control_matrix_t controlSecondDerivativeTerminal() override
Computes final-cost second-order derivative with respect to input.
Definition: CostFunctionAnalytical-impl.hpp:157
~CostFunctionAnalytical()
Definition: CostFunctionAnalytical-impl.hpp:37
const bool verbose
Definition: ConstraintComparison.h:18
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
#define CT_LOADABLE_TERMS(SCALAR_EVAL, SCALAR)
Definition: TermLoadMacros.hpp:18
control_vector_t controlDerivativeTerminal() override
Computes terminal-cost first-order derivative with respect to control.
Definition: CostFunctionAnalytical-impl.hpp:143
state_vector_t stateDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:108