- 3.0.2 optimal control module.
CostEvaluatorSimple.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 #include <omp.h>
9 #include <math.h>
10 #include <cmath>
11 
13 
17 
18 
19 namespace ct {
20 namespace optcon {
21 
22 
32 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
34 {
35 public:
36  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
37 
39 
40  typedef typename DIMENSIONS::state_vector_t state_vector_t;
41  typedef typename DIMENSIONS::control_vector_t control_vector_t;
42 
43  CostEvaluatorSimple() = delete;
44 
55  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid,
56  DmsSettings settings)
57  : costFct_(costFct), w_(w), timeGrid_(timeGrid), settings_(settings)
58  {
59  phi_.resize(settings_.N_ + 1);
60  // phi_diff_h_ = Eigen::VectorXd::Ones(settings_.N_+1, 1);
61  updatePhi();
62  }
63 
64  ~CostEvaluatorSimple() override = default;
65  SCALAR eval() override;
66 
67  void evalGradient(size_t grad_length, Eigen::Map<Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>>& grad) override;
68 
69 private:
73  void updatePhi();
74 
75  std::shared_ptr<ct::optcon::CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>> costFct_;
76  std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>> w_;
77  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid_;
78  const DmsSettings settings_;
79  Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> phi_; /* the summation weights */
80  // Eigen::VectorXd phi_diff_h_; /* the summation weights for derivative w.r.t h */
81 };
82 
83 
84 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
86 {
87  // updatePhi();
88  SCALAR cost = SCALAR(0.0);
89 
90  for (size_t i = 0; i < settings_.N_ + 1; ++i)
91  {
92  costFct_->setCurrentStateAndControl(
93  w_->getOptimizedState(i), w_->getOptimizedControl(i), timeGrid_->getShotStartTime(i));
94  cost += phi_(i) * costFct_->evaluateIntermediate();
95  }
96 
97  costFct_->setCurrentStateAndControl(w_->getOptimizedState(settings_.N_), control_vector_t::Zero());
98  cost += costFct_->evaluateTerminal();
99  assert(cost == cost);
100  return cost;
101 }
102 
103 
104 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
106  Eigen::Map<Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>>& grad)
107 {
108  // updatePhi();
109  grad.setZero();
110  for (size_t i = 0; i < settings_.N_ + 1; ++i)
111  {
112  costFct_->setCurrentStateAndControl(
113  w_->getOptimizedState(i), w_->getOptimizedControl(i), timeGrid_->getShotStartTime(i));
114  grad.segment(w_->getStateIndex(i), STATE_DIM) += phi_(i) * costFct_->stateDerivativeIntermediate();
115  grad.segment(w_->getControlIndex(i), CONTROL_DIM) += phi_(i) * costFct_->controlDerivativeIntermediate();
116  }
117 
118  /* gradient of terminal cost */
119  costFct_->setCurrentStateAndControl(w_->getOptimizedState(settings_.N_), control_vector_t::Zero());
120  grad.segment(w_->getStateIndex(settings_.N_), STATE_DIM) += costFct_->stateDerivativeTerminal();
121 }
122 
123 
124 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
126 {
127  switch (settings_.splineType_)
128  {
130  {
131  for (size_t i = 0; i < settings_.N_; i++)
132  phi_(i) = (timeGrid_->getShotDuration(i));
133 
134  phi_(settings_.N_) = SCALAR(0.0);
135  break;
136  }
138  {
139  phi_(0) = SCALAR(0.5) * (timeGrid_->getShotDuration(0));
140 
141  for (size_t i = 1; i < settings_.N_; i++)
142  phi_(i) = SCALAR(0.5) * (timeGrid_->getShotEndTime(i) - timeGrid_->getShotStartTime(i - 1));
143 
144  phi_(settings_.N_) = SCALAR(0.5) * (timeGrid_->getShotDuration(settings_.N_));
145  break;
146  }
147  default:
148  throw(std::runtime_error(" ERROR: Unknown spline-type in CostEvaluatorSimple - exiting."));
149  }
150 }
151 
152 } // namespace optcon
153 } // namespace ct
Implements an abstract base class which evaluates the cost function and its gradient in the NLP...
Definition: DiscreteCostEvaluatorBase.h:22
Definition: DmsSettings.h:26
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DmsDimensions< STATE_DIM, CONTROL_DIM, SCALAR > DIMENSIONS
Definition: CostEvaluatorSimple.h:38
This class is a wrapper around the NLP Optvector. It wraps the Vectors from the NLP solvers into stat...
Definition: OptVectorDms.h:37
SCALAR eval() override
Evaluates the cost function.
Definition: CostEvaluatorSimple.h:85
size_t N_
Definition: DmsSettings.h:51
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
Definition: DmsSettings.h:26
void evalGradient(size_t grad_length, Eigen::Map< Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 >> &grad) override
Evaluates the cost gradient.
Definition: CostEvaluatorSimple.h:105
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Defines basic types used in the DMS algorithm.
Definition: DmsDimensions.h:18
Definition: TimeGrid.h:27
for i
Definition: mpc_unittest_plotting.m:14
~CostEvaluatorSimple() override=default
SplineType_t splineType_
Definition: DmsSettings.h:54
CostEvaluatorSimple(std::shared_ptr< ct::optcon::CostFunctionQuadratic< STATE_DIM, CONTROL_DIM, SCALAR >> costFct, std::shared_ptr< OptVectorDms< STATE_DIM, CONTROL_DIM, SCALAR >> w, std::shared_ptr< tpl::TimeGrid< SCALAR >> timeGrid, DmsSettings settings)
Custom constructor.
Definition: CostEvaluatorSimple.h:53
DIMENSIONS::state_vector_t state_vector_t
Definition: CostEvaluatorSimple.h:40
Defines the DMS settings.
Definition: DmsSettings.h:23
Evaluates the cost at the shots and performs some interpolation in between.
Definition: CostEvaluatorSimple.h:33
DIMENSIONS::control_vector_t control_vector_t
Definition: CostEvaluatorSimple.h:41