trajectory_simulator_online.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
34 #include <tuw_control/utils.h>
35 
36 #include <algorithm>
37 
38 using namespace std;
39 using namespace tuw;
40 
41 TrajectorySimulatorOnline::TrajectorySimulatorOnline(StateSimPtr _stateSim) : TrajectorySimulator(_stateSim)
42 {
43 }
45  unique_ptr<CostsEvaluatorClass> _costsEvaluator)
46  : TrajectorySimulator(_stateSim, std::move(_costsEvaluator))
47 {
48 }
49 
51 
53 {
54  _lastValidArc = 0;
56  size_t firstLaticeInvalidIdx = 0;
58  if (initSimLatticeState0(_lastValidArc, firstLaticeInvalidIdx))
59  {
60  populateTrajSimPartLattice(firstLaticeInvalidIdx);
61 
62  if (costsEvaluator_)
63  {
64  costsEvaluator_->evaluateAllCosts();
65  }
66  }
67 }
68 
69 double TrajectorySimulatorOnline::toNextIntMult(const double& _x, const double& _dx) const
70 {
71  static double add, ans;
72  static double temp;
73  temp = ceil(_x / _dx);
74  ans = temp * _dx;
75  add = 0;
76  while (_x >= ans /*- 1e-8*/)
77  {
78  ans = (++add + temp) * _dx;
79  };
80  return ans;
81 }
82 
83 void TrajectorySimulatorOnline::populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, double _arcParamMax,
84  double _minArcLatticeVal)
85 {
86  resizeBeginGeneral(_arcParamMax);
87  setBeginEndArcsToLattices(0, _arcParamMax);
88  initExtLatticeCache(_minArcLatticeVal);
89 
90  double& arcLatNextMin = _minArcLatticeVal = 0;
91  double arcLatEqDtNext = 0;
92  double arcLatEqDsNext = 0;
93  double arcLatOldMin = arcLatNextMin;
94  StateSPtr newState;
95  while (arcLatNextMin < _arcParamMax)
96  {
97  // computing next equal arc lattice value
98  arcLatEqDtNext = toNextIntMult(stateSim_->stateArc(), dt());
99  arcLatNextMin = arcLatEqDtNext;
100 
101  // computing next dist lattice value
102  arcLatEqDsNext = FLT_MAX;
103  if (canComputeDistLattice_ && (ds() > 0))
104  {
105  static double arcTemp;
106  arcTemp = stateSim_->stateArc();
107  double distLatticeNext = toNextIntMult(stateSim_->stateDist(), ds());
108  stateSim_->paramFuncsDist()->setEvalDist(distLatticeNext);
109  arcLatEqDsNext = stateSim_->paramFuncs()->funcsArcEval();
110  arcLatNextMin = fmin(arcLatNextMin, arcLatEqDsNext);
111  stateSim_->paramFuncs()->setEvalArc(arcTemp);
112  }
113 
114  // computing next extended (i.e. equal knots) lattice values
115  for (size_t iPart = lattTypeIdx(0); iPart < partLattices_.size(); ++iPart)
116  {
117  if (partLattices_[iPart]->empty())
118  {
119  partLatIdxCache_[iPart] = -1;
120  continue;
121  }
122  for (; partLatIdxCache_[iPart] < (int)partLattices_[iPart]->size(); ++partLatIdxCache_[iPart])
123  {
124  if (partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc > arcLatOldMin)
125  {
126  ++partLatIdxCache_[iPart];
127  break;
128  }
129  }
130  partLatIdxCache_[iPart] = max(0, partLatIdxCache_[iPart] - 1);
131  arcLatNextMin = fmin(arcLatNextMin, partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc);
132  }
133 
134  // simulating till next lattice point
135  if (arcLatNextMin == 0)
136  {
137  continue;
138  }
139  static constexpr const int uninitLattTypeVal = -100;
140  if (arcLatNextMin <= _arcParamMax)
141  {
142  stateSim_->advance(arcLatNextMin);
143  newState = stateSim_->cloneState();
144  simulationLattice_.emplace_back(LatticePointType(arcLatNextMin, uninitLattTypeVal, newState));
145  }
146 
147  for (size_t iPart = lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT)); iPart < partLattices_.size(); ++iPart)
148  {
149  if (partLattices_[iPart]->empty())
150  {
151  continue;
152  }
153  if (((iPart == lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT))) &&
154  (fabs(arcLatNextMin - arcLatEqDtNext) <= FLT_MIN)) ||
155  ((iPart == lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DS))) && (fabs(arcLatNextMin - arcLatEqDsNext) <= FLT_MIN)))
156  {
157  if (arcLatNextMin < _arcParamMax)
158  {
159  partLattices_[iPart]->resize(partLattices_[iPart]->size() + 1, LatticePoint(_arcParamMax));
160  partLattices_[iPart]->at(partLattices_[iPart]->size() - 2) = LatticePoint(arcLatNextMin, newState);
161  }
162  else
163  {
164  partLattices_[iPart]->back() = LatticePoint(arcLatNextMin, newState);
165  }
166  if (simulationLattice_.back().latticeType == uninitLattTypeVal)
167  {
168  simulationLattice_.back().latticeType = iPart - asInt(BSLT::LATTICE_ENUM_SIZE);
169  }
170  }
171  else
172  {
173  int& extArcIdxCacheI = partLatIdxCache_[iPart];
174  TrajectorySimulator::LatticePoint& partLattII = partLattices_[iPart]->at(extArcIdxCacheI);
175  if (fabs(arcLatNextMin - partLattII.arc) <= FLT_MIN)
176  {
177  if (simulationLattice_.back().latticeType == uninitLattTypeVal)
178  {
179  simulationLattice_.back().latticeType = iPart - asInt(BSLT::LATTICE_ENUM_SIZE);
180  }
181  partLattII.statePtr = newState;
182  if (extArcIdxCacheI + 1 < (int)partLattices_[iPart]->size())
183  {
184  ++extArcIdxCacheI;
185  }
186  partLattII.arc = arcLatNextMin;
187  }
188  }
189  }
190 
191  if (costsEvaluator_ /*&& !firstCycle*/ /*&& (arcLatOldMin < arcLatNextMin)*/)
192  {
193  size_t tempDummy;
194  double temppDummy;
195  if (!costsEvaluator_->evalValidCostStep(CostEvaluatorCostType::H, arcLatNextMin, tempDummy, temppDummy))
196  {
197  simulationLattice_.pop_back();
198  _arcParamMax = simulationLattice_.back().arc;
199  for (size_t i = lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT)); i < partLattices_.size(); ++i)
200  {
201  if (partLattices_[i]->empty())
202  {
203  continue;
204  }
205  partLattices_[i]->pop_back();
206  }
207  for (size_t i = lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT)); i < lattTypeIdx(0); ++i)
208  {
209  if (partLattices_[i]->empty())
210  {
211  continue;
212  }
213  partLattices_[i]->pop_back();
214  }
215 
216  break;
217  }
218  }
219 
220  arcLatOldMin = arcLatNextMin;
221  }
222  // std::cout<<"simLattX:"; for(auto simLatI : simulationLattice_){ cout<<simLatI.statePtr->state(0)<<", ";
223  // }cout<<endl;
224  if (simulationLattice_.empty())
225  {
226  return;
227  }
228  newState = simulationLattice_.back().statePtr->cloneState();
229  double arc = simulationLattice_.back().arc;
230  for (size_t iPart = lattTypeIdx(asInt(BSLT::ARC_BG_BK)); iPart < partLattices_.size(); ++iPart)
231  {
232  if (partLattices_[iPart]->empty())
233  {
234  continue;
235  }
236  partLattices_[iPart]->back().statePtr = newState;
237  partLattices_[iPart]->back().arc = arc;
238  }
239 }
240 
241 void TrajectorySimulatorOnline::resizeBeginGeneral(const double& _arcParamMax)
242 {
243  size_t idxVecInit;
244  StateSPtr& newState = simulationLattice_.back().statePtr;
245  idxVecInit = lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT));
246  if ((dt() > 0))
247  {
248  partLattices_[idxVecInit]->assign(max(2, partLatIdxCache_[idxVecInit] + 1), LatticePoint(_arcParamMax));
249  partLattices_[idxVecInit]->at(0) = LatticePoint(0, newState);
250  }
251  idxVecInit = lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DS));
252  if ((ds() > 0))
253  {
254  partLattices_[idxVecInit]->assign(max(2, partLatIdxCache_[idxVecInit] + 1), LatticePoint(_arcParamMax));
255  partLattices_[idxVecInit]->at(0) = LatticePoint(0, newState);
256  }
257 }
258 
259 void TrajectorySimulatorOnline::populatePartSimLatticesDtOnly(const size_t& _firstLaticeInvalidIdx, double _arcParamMax)
260 {
261  resizeBeginDtOnly(_arcParamMax);
262  setBeginEndArcsToLattices(0, _arcParamMax);
263 
264  size_t itEnd = (int)_arcParamMax / dt() + 1;
265  // push back DT lattice points
266  for (size_t aPLIdx = _firstLaticeInvalidIdx + 1; aPLIdx < itEnd; ++aPLIdx)
267  {
268  double arcLatNextMin = aPLIdx * dt();
269  simAppendToSimPartLat(arcLatNextMin, asInt(BSLT::LATTICE_ARC_EQ_DT), aPLIdx);
270  if (costsEvaluator_)
271  {
272  size_t latKnotMin;
273  if (!costsEvaluator_->evalValidCostStep(CostEvaluatorCostType::H, arcLatNextMin, latKnotMin, _arcParamMax))
274  {
275  simulationLattice_.pop_back();
276  size_t i = 1;
277  if (_arcParamMax == 0)
278  {
279  i = 0;
280  }
281  for (; i < partLattices_.size(); ++i)
282  {
283  if (partLattices_[i]->empty())
284  {
285  continue;
286  }
287  size_t firstInvalidIdx = distance(
288  partLattices_[i]->begin(),
289  upper_bound(partLattices_[i]->begin(), partLattices_[i]->end(), _arcParamMax,
291  {
292  return a.arc < b.arc;
293  })) /*+ 1 * !(i == latKnotMin)*/;
294  partLattices_[i]->erase(partLattices_[i]->begin() + min(firstInvalidIdx, partLattices_[i]->size()),
295  partLattices_[i]->end());
296  }
297  // partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT))]->resize( aPLIdx );
298  break;
299  }
300  }
301  }
302 
303  // push back final lattice point
304  setBeginEndArcsToLattices(0, _arcParamMax);
305  setEndStateToLattices(_arcParamMax);
306 }
307 
308 void TrajectorySimulatorOnline::resizeBeginDtOnly(const double& _arcParamMax)
309 {
310  size_t simLatticeSize = max(0, (int)(ceil(_arcParamMax / dt()) + 1));
311  auto& partLatTime = partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT))];
312  partLatTime->assign(simLatticeSize, LatticePoint(0));
313  for (size_t i = 0; i < partLatTime->size(); ++i)
314  {
315  partLatTime->at(i).arc = i * dt();
316  }
317  partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT))]->at(0) = LatticePoint(0, simulationLattice_[0].statePtr);
318 }
StateSimPtr stateSim_
State simulator object.
LatticeVecSPtrVec partLattices_
Vector containing the ordered sequence of arc parametrizations for each of the used lattices...
std::vector< LatticePointType > simulationLattice_
Lattice requesting each simulated trajectory state.
void populateTrajSimPartLattice(const size_t &_firstLaticeInvalidIdx)
Main function that performs resizing, reserving and calls the proper population function.
void setBeginEndArcsToLattices(const double &_arcBegin, const double &_arcEnd)
Sets begin and end arcs to all partial lattices on the first and last container positions.
std::vector< int > partLatIdxCache_
Vector containing cached container indices for each partial lattice related to the the highest arc lo...
void initExtLatticeCache(const double &_minArcLatticeVal)
Initializes the cached partial lattices index at the highest arc lower than.
void populatePartSimLatticesDtOnly(const size_t &_firstLaticeInvalidIdx, double _arcParamMax) override
Performs simulation and populates simulation and partial lattice when only equal dt lattice is enable...
void simulateTrajectory(double _lastValidArc=0) override
Simulates (discrete numerical evaluation) an entire trajectory according to the specified intervals a...
std::unique_ptr< CostsEvaluatorClass > costsEvaluator_
Structure containing the lattice type afferent to a.
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
void populatePartSimLatticesGeneral(std::size_t _firstLaticeInvalidIdx, double _arcParamMax, double _minArcLatticeVal) override
const double & ds() const
Const reference to arc parametrization interval used for the equal distance lattice.
double toNextIntMult(const double &_x, const double &_dx) const
Numeric safe incremental rounding of x (finds the smallest multiple of.
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
bool initSimLatticeState0(const double &_lastValidArc, size_t &_firstLaticeInvalidIdx)
Initializes the simulation lattice (truncation from the.
BaseSimLatticeType
Fundamental lattice types.
Structure containing an evaluation arc and a state pointer.
bool canComputeDistLattice_
Flags if the StateSim object has access to the StateSimDist functionality.
void resizeBeginGeneral(const double &_arcParamMax)
Performs lattices resizing relevant to the general online simulation algorithm.
TrajectorySimulator(StateSimPtr _stateSim)
void setEndStateToLattices(const double &_arcEnd)
Binds reference to the final simulated state (at.
const double & dt() const
Const reference to arc parametrization interval used for the equal arc-length lattice.
std::shared_ptr< StateSim > StateSimPtr
void simAppendToSimPartLat(const double &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Performs a simulation step (if.
void resizeBeginDtOnly(const double &_arcParamMax)
Performs lattices resizing relevant to the DT-only online simulation algorithm.
std::shared_ptr< StateType > StateSPtr
static size_t lattTypeIdx(int _enumIdx)
Converts shifted (int) lattice index to container (size_t) index.


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:22