finite_differences_variable_grid.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
26 
28 
30 #include <corbo-core/console.h>
31 
32 #include <algorithm>
33 #include <cmath>
34 #include <memory>
35 
36 namespace corbo {
37 
38 void FiniteDifferencesVariableGrid::setDtBounds(double dt_lb, double dt_ub)
39 {
40  _dt_lb = dt_lb;
41  _dt_ub = dt_ub;
42 }
43 
44 void FiniteDifferencesVariableGrid::setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
45 {
47  _n_max = n_max;
48  _dt_hyst_ratio = dt_hyst_ratio;
49  _adapt_first_iter = adapt_first_iter;
50 }
51 
52 void FiniteDifferencesVariableGrid::setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
53 {
55  _n_max = n_max;
56  _dt_hyst_ratio = dt_hyst_ratio;
57  _adapt_first_iter = adapt_first_iter;
58 }
59 
61 {
63  _adapt_first_iter = adapt_first_iter;
64 }
65 
67 {
68  // do not adapt grid in a new run
69  if (new_run && !_adapt_first_iter) return false;
70 
71  bool changed = false;
72  switch (_grid_adapt)
73  {
75  {
76  break;
77  }
79  {
80  changed = adaptGridTimeBasedSingleStep(nlp_fun);
81  break;
82  }
84  {
85  changed = adaptGridTimeBasedAggressiveEstimate(nlp_fun);
86  break;
87  }
89  {
90  changed = adaptGridSimpleShrinkingHorizon(nlp_fun);
91  break;
92  }
93  default:
94  {
95  PRINT_ERROR_NAMED("selected grid adaptation strategy not implemented.");
96  }
97  }
98  return changed;
99 }
100 
102 {
103  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
104 
105  _nlp_fun = &nlp_fun;
106 
107  int n = getN();
108 
109  double dt = getDt();
110  if (dt > _dt_ref * (1.0 + _dt_hyst_ratio) && n < _n_max)
111  {
112  resampleTrajectory(n + 1);
113  _n_adapt = n + 1;
114  return true;
115  }
116  else if (dt < _dt_ref * (1.0 - _dt_hyst_ratio) && n > _n_min)
117  {
118  resampleTrajectory(n - 1);
119  _n_adapt = n - 1;
120  return true;
121  }
122  return false;
123 }
124 
126 {
127  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
128 
129  _nlp_fun = &nlp_fun;
130  int n = getN();
131  double dt = getDt();
132 
133  // check if hysteresis is satisfied
134  if (dt >= _dt_ref * (1.0 - _dt_hyst_ratio) && dt <= _dt_ref * (1.0 + _dt_hyst_ratio)) return false;
135 
136  // estimate number of samples based on the fraction dt/dt_ref.
137  // dt is the time difference obtained in a previous solution (with a coarser resp. finer trajectory resolution)
138  int new_n = std::round((double)n * (dt / _dt_ref));
139 
140  // bound value
141  if (new_n > _n_max)
142  new_n = _n_max;
143  else if (new_n < _n_min)
144  new_n = _n_min;
145 
146  if (new_n == n) return false;
147 
148  // and now resample
149  resampleTrajectory(new_n);
150  _n_adapt = new_n;
151  return true;
152 }
153 
155 {
156  int n = getN();
157  if (n > _n_min)
158  {
159  resampleTrajectory(n - 1);
160  _n_adapt = n - 1;
161  }
162  return false;
163 }
164 
165 #ifdef MESSAGE_SUPPORT
166 void FiniteDifferencesVariableGrid::fromMessage(const messages::FiniteDifferencesVariableGrid& message, std::stringstream* issues)
167 {
168  FiniteDifferencesGrid::fromMessage(message.finite_differences_grid(), issues);
169 
170  // xf fixed states
171  // if (grid_msg.xf_fixed_size() != _p && issues) *issues << "FullDiscretizationGrid: xf_fixed size does not match state dimension " << _p <<
172  // ".\n";
173  // TODO(roesmann): we need a warning in the gui if xf_fixed has the wrong dimension.
174  // maybe we could add a "verifyParameters()" method to all interfaces, so predictive control asks the ocp, the ocp the grid and so
175  // on
176  _xf_fixed = Eigen::Map<const Eigen::Matrix<bool, -1, 1>>(message.xf_fixed().data(), message.xf_fixed_size());
177 
178  // dt bounds
179  setDtBounds(message.dt_min(), message.dt_max());
180 
181  // auto resize
182  if (message.has_grid_adapt_strategy())
183  {
184  if (message.grid_adapt_strategy().has_no_grid_adapt())
185  {
187  }
188  else if (message.grid_adapt_strategy().has_time_based_single_step())
189  {
190  setGridAdaptTimeBasedSingleStep(message.grid_adapt_strategy().time_based_single_step().n_max(),
191  message.grid_adapt_strategy().time_based_single_step().dt_hyst_ratio());
192  }
193  else if (message.grid_adapt_strategy().has_time_based_aggr_estim())
194  {
195  setGridAdaptTimeBasedAggressiveEstimate(message.grid_adapt_strategy().time_based_aggr_estim().n_max(),
196  message.grid_adapt_strategy().time_based_aggr_estim().dt_hyst_ratio());
197  }
198  else if (message.grid_adapt_strategy().has_simple_shrinking_horizon())
199  {
201  }
202  }
203  _adapt_first_iter = message.grid_adapt_strategy().adapt_first_iter();
204 
205  // others
206  _n_min = message.n_min();
207 }
208 
209 void FiniteDifferencesVariableGrid::toMessage(messages::FiniteDifferencesVariableGrid& message) const
210 {
211  FiniteDifferencesGrid::toMessage(*message.mutable_finite_differences_grid());
212 
213  // xf fixed states
214  if (_xf_fixed.size() > 0)
215  {
216  message.mutable_xf_fixed()->Resize(_xf_fixed.size(), false);
217  Eigen::Map<Eigen::Matrix<bool, -1, 1>>(message.mutable_xf_fixed()->mutable_data(), _xf_fixed.size()) = _xf_fixed;
218  }
219 
220  // dt bounds
221  message.set_dt_min(_dt_lb);
222  message.set_dt_max(_dt_ub);
223 
224  // auto resize
225  switch (_grid_adapt)
226  {
228  {
229  message.mutable_grid_adapt_strategy()->mutable_no_grid_adapt();
230  break;
231  }
233  {
234  message.mutable_grid_adapt_strategy()->mutable_time_based_single_step()->set_n_max(_n_max);
235  message.mutable_grid_adapt_strategy()->mutable_time_based_single_step()->set_dt_hyst_ratio(_dt_hyst_ratio);
236  break;
237  }
239  {
240  message.mutable_grid_adapt_strategy()->mutable_time_based_aggr_estim()->set_n_max(_n_max);
241  message.mutable_grid_adapt_strategy()->mutable_time_based_aggr_estim()->set_dt_hyst_ratio(_dt_hyst_ratio);
242  break;
243  }
245  {
246  message.mutable_grid_adapt_strategy()->mutable_simple_shrinking_horizon();
247  break;
248  }
249  default:
250  {
251  PRINT_ERROR_NAMED("exporting of the selected auto resize strategy not implemented.");
252  }
253  }
254  message.mutable_grid_adapt_strategy()->set_adapt_first_iter(_adapt_first_iter);
255 
256  // others
257  message.set_n_min(_n_min);
258 }
259 #endif
260 
261 } // namespace corbo
#define PRINT_ERROR_NAMED(msg)
Definition: console.h:260
#define PRINT_WARNING_COND_NAMED(cond, msg)
Definition: console.h:257
bool adaptGridTimeBasedAggressiveEstimate(NlpFunctions &nlp_fun)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
bool adaptGrid(bool new_run, NlpFunctions &nlp_fun) override
void setGridAdaptSimpleShrinkingHorizon(bool adapt_first_iter=false)
void setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
void setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
EIGEN_DEVICE_FUNC const RoundReturnType round() const


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:51