finite_differences_variable_grid_se2.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020, Christoph Rösmann, All rights reserved.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Authors: Christoph Rösmann
21  *********************************************************************/
22 
24 
26 
27 #include <corbo-communication/utilities.h>
28 #include <corbo-core/console.h>
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <memory>
33 
34 namespace mpc_local_planner {
35 
36 void FiniteDifferencesVariableGridSE2::setDtBounds(double dt_lb, double dt_ub)
37 {
38  _dt_lb = dt_lb;
39  _dt_ub = dt_ub;
40 }
41 
42 void FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
43 {
45  _n_max = n_max;
46  _dt_hyst_ratio = dt_hyst_ratio;
47  _adapt_first_iter = adapt_first_iter;
48 }
49 
50 void FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
51 {
53  _n_max = n_max;
54  _dt_hyst_ratio = dt_hyst_ratio;
55  _adapt_first_iter = adapt_first_iter;
56 }
57 
59 {
61  _adapt_first_iter = adapt_first_iter;
62 }
63 
64 bool FiniteDifferencesVariableGridSE2::adaptGrid(bool new_run, NlpFunctions& nlp_fun)
65 {
66  // do not adapt grid in a new run
67  if (new_run && !_adapt_first_iter) return false;
68 
69  bool changed = false;
70  switch (_grid_adapt)
71  {
73  {
74  break;
75  }
77  {
78  changed = adaptGridTimeBasedSingleStep(nlp_fun);
79  break;
80  }
82  {
83  changed = adaptGridTimeBasedAggressiveEstimate(nlp_fun);
84  break;
85  }
87  {
88  changed = adaptGridSimpleShrinkingHorizon(nlp_fun);
89  break;
90  }
91  default:
92  {
93  PRINT_ERROR_NAMED("selected grid adaptation strategy not implemented.");
94  }
95  }
96  return changed;
97 }
98 
100 {
101  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
102 
103  _nlp_fun = &nlp_fun;
104 
105  int n = getN();
106 
107  double dt = getDt();
108  if (dt > _dt_ref * (1.0 + _dt_hyst_ratio) && n < _n_max)
109  {
110  resampleTrajectory(n + 1);
111  _n_adapt = n + 1;
112  return true;
113  }
114  else if (dt < _dt_ref * (1.0 - _dt_hyst_ratio) && n > _n_min)
115  {
116  resampleTrajectory(n - 1);
117  _n_adapt = n - 1;
118  return true;
119  }
120  return false;
121 }
122 
124 {
125  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
126 
127  _nlp_fun = &nlp_fun;
128  int n = getN();
129  double dt = getDt();
130 
131  // check if hysteresis is satisfied
132  if (dt >= _dt_ref * (1.0 - _dt_hyst_ratio) && dt <= _dt_ref * (1.0 + _dt_hyst_ratio)) return false;
133 
134  // estimate number of samples based on the fraction dt/dt_ref.
135  // dt is the time difference obtained in a previous solution (with a coarser resp. finer trajectory resolution)
136  int new_n = std::round((double)n * (dt / _dt_ref));
137 
138  // bound value
139  if (new_n > _n_max)
140  new_n = _n_max;
141  else if (new_n < _n_min)
142  new_n = _n_min;
143 
144  if (new_n == n) return false;
145 
146  // and now resample
147  resampleTrajectory(new_n);
148  _n_adapt = new_n;
149  return true;
150 }
151 
153 {
154  int n = getN();
155  if (n > _n_min)
156  {
157  resampleTrajectory(n - 1);
158  _n_adapt = n - 1;
159  }
160  return false;
161 }
162 
163 } // namespace mpc_local_planner
PRINT_WARNING_COND_NAMED
#define PRINT_WARNING_COND_NAMED(cond, msg)
mpc_local_planner::FullDiscretizationGridBaseSE2::resampleTrajectory
virtual void resampleTrajectory(int n_new)
Definition: full_discretization_grid_base_se2.cpp:460
PRINT_ERROR_NAMED
#define PRINT_ERROR_NAMED(msg)
mpc_local_planner::FiniteDifferencesVariableGridSE2::GridAdaptStrategy::NoGridAdapt
@ NoGridAdapt
mpc_local_planner::FiniteDifferencesVariableGridSE2::_n_min
int _n_min
Definition: finite_differences_variable_grid_se2.h:132
mpc_local_planner::FullDiscretizationGridBaseSE2::_dt_lb
double _dt_lb
Definition: full_discretization_grid_base_se2.h:264
mpc_local_planner::FullDiscretizationGridBaseSE2::isTimeVariableGrid
bool isTimeVariableGrid() const override
Definition: full_discretization_grid_base_se2.h:144
mpc_local_planner::FiniteDifferencesVariableGridSE2::_adapt_first_iter
bool _adapt_first_iter
Definition: finite_differences_variable_grid_se2.h:133
mpc_local_planner::FullDiscretizationGridBaseSE2::_dt_ub
double _dt_ub
Definition: full_discretization_grid_base_se2.h:265
mpc_local_planner::FiniteDifferencesVariableGridSE2::GridAdaptStrategy::TimeBasedAggressiveEstimate
@ TimeBasedAggressiveEstimate
mpc_local_planner
Definition: controller.h:44
mpc_local_planner::FiniteDifferencesVariableGridSE2::_n_max
int _n_max
Definition: finite_differences_variable_grid_se2.h:130
mpc_local_planner::FiniteDifferencesVariableGridSE2::setGridAdaptSimpleShrinkingHorizon
void setGridAdaptSimpleShrinkingHorizon(bool adapt_first_iter=false)
Set grid adaptation strategy to 'shrinking horizon'.
Definition: finite_differences_variable_grid_se2.cpp:78
console.h
mpc_local_planner::FiniteDifferencesVariableGridSE2::setDtBounds
void setDtBounds(double dt_lb, double dt_ub)
Set lower and upper bounds for the temporal resolution.
Definition: finite_differences_variable_grid_se2.cpp:56
mpc_local_planner::FiniteDifferencesVariableGridSE2::adaptGridSimpleShrinkingHorizon
bool adaptGridSimpleShrinkingHorizon(NlpFunctions &nlp_fun)
Definition: finite_differences_variable_grid_se2.cpp:172
mpc_local_planner::FullDiscretizationGridBaseSE2::_n_adapt
int _n_adapt
Definition: full_discretization_grid_base_se2.h:256
mpc_local_planner::FiniteDifferencesVariableGridSE2::GridAdaptStrategy::TimeBasedSingleStep
@ TimeBasedSingleStep
mpc_local_planner::FullDiscretizationGridBaseSE2::_dt_ref
double _dt_ref
Definition: full_discretization_grid_base_se2.h:257
mpc_local_planner::FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedAggressiveEstimate
void setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
Set grid adaptation strategy to 'aggressive'.
Definition: finite_differences_variable_grid_se2.cpp:70
mpc_local_planner::FullDiscretizationGridBaseSE2::_nlp_fun
const NlpFunctions * _nlp_fun
Definition: full_discretization_grid_base_se2.h:253
finite_differences_variable_grid_se2.h
mpc_local_planner::FiniteDifferencesVariableGridSE2::_dt_hyst_ratio
double _dt_hyst_ratio
Definition: finite_differences_variable_grid_se2.h:131
mpc_local_planner::FiniteDifferencesVariableGridSE2::adaptGridTimeBasedSingleStep
bool adaptGridTimeBasedSingleStep(NlpFunctions &nlp_fun)
Definition: finite_differences_variable_grid_se2.cpp:119
mpc_local_planner::FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedSingleStep
void setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
Set grid adaptation strategy to 'single step'.
Definition: finite_differences_variable_grid_se2.cpp:62
mpc_local_planner::FullDiscretizationGridBaseSE2::getN
int getN() const override
get current horizon length
Definition: full_discretization_grid_base_se2.h:183
mpc_local_planner::FiniteDifferencesVariableGridSE2::adaptGridTimeBasedAggressiveEstimate
bool adaptGridTimeBasedAggressiveEstimate(NlpFunctions &nlp_fun)
Definition: finite_differences_variable_grid_se2.cpp:143
mpc_local_planner::FiniteDifferencesVariableGridSE2::adaptGrid
bool adaptGrid(bool new_run, NlpFunctions &nlp_fun) override
Definition: finite_differences_variable_grid_se2.cpp:84
n
PlainMatrixType mat * n
mpc_local_planner::FullDiscretizationGridBaseSE2::getDt
double getDt() const
get current temporal resolution
Definition: full_discretization_grid_base_se2.h:191
mpc_local_planner::FiniteDifferencesVariableGridSE2::_grid_adapt
GridAdaptStrategy _grid_adapt
Definition: finite_differences_variable_grid_se2.h:129
mpc_local_planner::FiniteDifferencesVariableGridSE2::GridAdaptStrategy::SimpleShrinkingHorizon
@ SimpleShrinkingHorizon
finite_differences_collocation_edges.h


mpc_local_planner
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:35:06