trajectory_simulator.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2017 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 
33 #ifndef TRAJECTORY_SIMULATOR_HPP
34 #define TRAJECTORY_SIMULATOR_HPP
35 
39 
40 #include <functional>
41 #include <memory>
42 
43 namespace tuw
44 {
45 // class TrajectorySimulator;
46 // using TrajectorySimulatorSPtr = std::shared_ptr<TrajectorySimulator>;
47 // using TrajectorySimulatorConstSPtr = std::shared_ptr<TrajectorySimulator const>;
48 // using TrajectorySimulatorUPtr = std::unique_ptr<TrajectorySimulator>;
49 // using TrajectorySimulatorConstUPtr = std::unique_ptr<TrajectorySimulator const>;
50 
51 template <typename TNumType, typename TSimType>
52 class TrajectorySimulator
53 {
54 public:
55  using StateSimSPtr = std::shared_ptr<TSimType>;
56 
57 public:
58  using StateType = typename TSimType::StateType;
59 
60 public:
61  using StateSPtr = std::shared_ptr<StateType>;
62 
63 public:
64  using StateForSimType = typename TSimType::StateForSimType;
65 
66 public:
67  static constexpr const bool CanComputeStateGrad =
68  !std::is_same<EmptyGradType, typename StateMapBaseTraits<StateType>::StateWithGradNmType>::value;
69 
70  // enums
72 public:
73  enum class BaseSimLatticeType : signed int
74  {
75  ARC_BG_BK = -3,
76  LATTICE_ARC_EQ_DT = -2,
77  LATTICE_ARC_EQ_DS = -1,
79  };
80  static constexpr const std::size_t extArcLatIdxBegin =
82 
84 public:
85  struct LatticePoint
86  {
87  LatticePoint() : arc(-1), statePtr(nullptr)
88  {
89  }
90  LatticePoint(TNumType _arc) : arc(_arc), statePtr(nullptr)
91  {
92  }
93  LatticePoint(TNumType _arc, StateSPtr& _statePtr) : arc(_arc), statePtr(_statePtr)
94  {
95  }
96  LatticePoint(bool _makePtrShared) : arc(-1), statePtr(std::shared_ptr<StateType>(new StateType))
97  {
98  }
99  virtual ~LatticePoint()
100  {
101  }
102  TNumType arc;
103  StateSPtr statePtr;
104  };
105  using LatticeVec = std::vector<LatticePoint>;
106  using LatticeVecSPtr = std::shared_ptr<std::vector<LatticePoint>>;
107  using LatticeVecSPtrVec = std::vector<std::shared_ptr<std::vector<LatticePoint>>>;
108 
110 public:
111  struct LatticePointType : public LatticePoint
112  {
113  LatticePointType() : LatticePoint(true), latticeType(-5)
114  {
115  }
116  LatticePointType(TNumType _arc, int _latticeType) : LatticePoint(_arc), latticeType(_latticeType)
117  {
118  }
119  LatticePointType(TNumType _arc, int _latticeType, StateSPtr& _statePtr)
120  : LatticePoint(_arc, _statePtr), latticeType(_latticeType)
121  {
122  }
123  int latticeType;
124  };
125 
126 public:
128 
129  // special class member functions
130 public:
132  : stateSim_(_stateSim)
135  , canComputeDistLattice_(stateSim_->paramFuncsDist() != nullptr)
136  , dtBase_(-1)
137  , dsBase_(-1)
138  , scaleDt_(false)
139  , scaleDs_(false)
140  {
141  for (size_t i = 0; i < partLattices_.size(); ++i)
142  {
143  partLattices_[i] = std::shared_ptr<LatticeVec>(new LatticeVec);
144  }
147  }
148 
149 public:
150  TrajectorySimulator(StateSimSPtr& _stateSim, std::unique_ptr<CostsEvaluatorClass> _costsEvaluator)
151  : TrajectorySimulator(_stateSim)
152  {
153  costsEvaluator_ = std::move(_costsEvaluator);
155  }
156 
157 public:
158  virtual ~TrajectorySimulator() = default;
159 
160 public:
161  TrajectorySimulator(const TrajectorySimulator&) = default;
162 
163 public:
165 
166 public:
168 
169 public:
171 
172 public:
173  void setBoolDtScale(const bool& _doScale)
174  {
175  scaleDt_ = _doScale;
176  }
177 
178 public:
179  void setBoolDsScale(const bool& _doScale)
180  {
181  scaleDs_ = _doScale;
182  }
183 
185 public:
186  TNumType& dtBase()
187  {
188  return dtBase_;
189  }
191 public:
192  const TNumType& dt() const
193  {
194  return dt_;
195  }
197 public:
198  TNumType& dsBase()
199  {
200  return dsBase_;
201  }
203 public:
204  const TNumType& ds() const
205  {
206  return ds_;
207  }
209 public:
211  {
212  return stateSim_;
213  }
215 public:
216  const StateSimSPtr& stateSim() const
217  {
218  return stateSim_;
219  }
221 public:
222  void setUserDefLattice(const std::vector<std::vector<TNumType*>>& _userDefLattices)
223  {
224  userDefPartLattices_ = _userDefLattices;
225  partLattices_.resize(asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE) + _userDefLattices.size());
226  for (size_t i = asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE); i < partLattices_.size(); ++i)
227  {
228  partLattices_[i] = std::shared_ptr<LatticeVec>(new LatticeVec);
229  }
230  partLatIdxCache_.resize(partLattices_.size());
231  for (size_t i = 0; i < _userDefLattices.size(); ++i)
232  {
233  partLattices_[lattTypeIdx(i)]->resize(_userDefLattices[i].size());
234  }
236  if (costsEvaluator_)
237  {
239  }
240  }
241 
242 public:
244  {
245  partLatIdxCache_.resize(partLattices_.size());
246  for (size_t i = 0; i < userDefPartLattices_.size(); ++i)
247  {
248  for (size_t j = 0; j < userDefPartLattices_[i].size(); ++j)
249  {
250  partLattices_[lattTypeIdx(i)]->at(j).arc = *userDefPartLattices_[i][j];
251  }
252  }
253  }
254 
264 
265 public:
266  void simulateTrajectory(TNumType _lastValidArc = 0)
267  {
269  simulatingWithGrad = false;
270  simulateTrajectoryImpl(_lastValidArc);
271  }
272 
273 public:
274  template <bool canComputeStateGrad = CanComputeStateGrad,
275  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
276  void simulateTrajectoryWithGrad(TNumType _lastValidArc = 0)
277  {
279  simulatingWithGrad = true;
280  simulateTrajectoryImpl(_lastValidArc);
281  }
282 
283 private:
284  void simulateTrajectoryImpl(TNumType _lastValidArc)
285  {
286  firstTime_ = true;
289 
290  // resize equal dt lattice
291  const TNumType arcParamMax = stateSim_->paramFuncs()->funcsArcEnd();
292  size_t simLatticeSize = std::max(0, (int)(ceil(arcParamMax / dt()) + 1));
294  LatticePoint(FLT_MAX));
295 
296  // set the dist-extended sim lattice points on the last lattice entries
297  if (canComputeDistLattice_ && (ds() > 0))
298  {
299  static std::vector<TNumType> dsLattice;
301  if (scaleDs_ || scaleDt_)
302  {
303  stateSim_->paramFuncsDist()->computeS2TLattice(0, ds(), dsLattice);
304  partLatticeDs->resize(dsLattice.size());
305  for (size_t i = 0; i < dsLattice.size(); ++i)
306  {
307  partLatticeDs->at(i).arc = dsLattice[i];
308  }
309  }
310  else
311  {
312  stateSim_->paramFuncsDist()->computeS2TLattice(_lastValidArc, ds(), dsLattice);
313  const TNumType& firstDsLattice = dsLattice[0];
314  size_t idxFirstInvalidDs = std::max(0, (int)partLatticeDs->size() - 2);
315  for (size_t i = 1; i < partLatticeDs->size(); ++i)
316  {
317  if (partLatticeDs->at(i).arc > firstDsLattice + 1e-3)
318  {
319  idxFirstInvalidDs = --i;
320  break;
321  }
322  }
323  partLatticeDs->resize(idxFirstInvalidDs + dsLattice.size());
324  for (size_t i = 0; i < dsLattice.size(); ++i)
325  {
326  partLatticeDs->at(i + idxFirstInvalidDs).arc = dsLattice[i];
327  }
328  }
329  }
330  else
331  {
333  }
334 
335  size_t firstLaticeInvalidIdx = 0;
336  if (initSimLatticeState0(_lastValidArc, firstLaticeInvalidIdx))
337  {
338  populateTrajSimPartLattice(firstLaticeInvalidIdx);
339  if (costsEvaluator_)
340  {
341  costsEvaluator_->evaluateAllCosts();
342  }
343  }
344  }
345 
347 protected:
348  void populatePartSimLatticesDtOnly(const size_t& _firstLaticeInvalidIdx, TNumType _arcParamMax)
349  {
350  setBeginEndArcsToLattices(0, _arcParamMax);
351 
352  size_t itEnd = (int)_arcParamMax / dt() + 1;
353  // push back DT lattice points
354  for (size_t aPLIdx = _firstLaticeInvalidIdx + 1; aPLIdx < itEnd; ++aPLIdx)
355  {
357  }
358 
359  // push back final lattice point
360  setEndStateToLattices(_arcParamMax);
361  }
364 protected:
365  void populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, TNumType _arcParamMax, TNumType _minArcLatticeVal)
366  {
367  setBeginEndArcsToLattices(0, _arcParamMax);
368  initExtLatticeCache(_minArcLatticeVal);
369 
370  size_t arcParamLatticeIdx = std::max(0, (int)(_minArcLatticeVal / dt()));
371  size_t minArcLatCacheIdx =
372  getMinArcLatCacheIdx(); // find lattice type index of next smallest required simulation lattice value
373  while ((_minArcLatticeVal = partLattices_[minArcLatCacheIdx]->at(partLatIdxCache_[minArcLatCacheIdx]).arc) <
374  _arcParamMax)
375  {
376  const size_t deltaArcParamLattice = std::max(0, (int)(_minArcLatticeVal / dt()) - (int)arcParamLatticeIdx);
377  const size_t simLatticeInjectEnd = ++_firstLaticeInvalidIdx + deltaArcParamLattice;
378 
379  for (; _firstLaticeInvalidIdx < simLatticeInjectEnd; ++_firstLaticeInvalidIdx)
380  { // push_back the equal time lattice points before the extended one first
382  arcParamLatticeIdx);
383  }
384  simAppendToSimPartLat(_minArcLatticeVal, (int)minArcLatCacheIdx - asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE),
385  partLatIdxCache_[minArcLatCacheIdx]); // push back extended lattice point
386 
387  int& idxMinLatticePt = partLatIdxCache_[minArcLatCacheIdx];
388  if (idxMinLatticePt + 1 < (int)partLattices_[minArcLatCacheIdx]->size())
389  {
390  ++idxMinLatticePt;
391  }
392  minArcLatCacheIdx = getMinArcLatCacheIdx(); // update the minimum extended lattice point
393  }
394  const TNumType simLatticeInjectEnd = _arcParamMax / dt();
395  ++arcParamLatticeIdx;
396  for (; arcParamLatticeIdx < simLatticeInjectEnd; ++arcParamLatticeIdx)
397  {
399  arcParamLatticeIdx);
400  }
401 
402  setEndStateToLattices(_arcParamMax);
403  }
404 
405 private:
406  size_t getMinArcLatCacheIdx() const
407  {
408  size_t idxMin = 0;
409  TNumType minArc = FLT_MAX;
410  for (size_t iPart = extArcLatIdxBegin; iPart < partLattices_.size(); ++iPart)
411  {
412  if (partLattices_[iPart]->empty())
413  {
414  continue;
415  }
416  const TNumType& arcI = partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc;
417  if (minArc > arcI)
418  {
419  minArc = arcI;
420  idxMin = iPart;
421  }
422  }
423  return idxMin;
424  }
425 
426  using AdvanceFunction = void (TrajectorySimulator<TNumType, TSimType>::*)(const TNumType&);
428 
429 private:
430  void advanceFuncSim(const TNumType& _arcNow)
431  {
432  stateSim_->advance(_arcNow);
433  }
434 
435 private:
436  template <bool canComputeStateGrad = CanComputeStateGrad,
437  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
438  void advanceFuncSimGrad(const TNumType& _arcNow)
439  {
440  stateSim_->advanceWithGrad(_arcNow);
441  }
442 
443 private:
444  auto& dtIdpNm(const size_t _i, const TNumType& _arcNow, const int& _latticePtType) const
445  {
446  static Eigen::Matrix<TNumType, 1, -1> ans;
447  ans.resize(stateSim_->state().stateGradNm().sub(0).data().size());
448  ans.setZero();
449  if (_arcNow != 0)
450  {
451  ans(ans.cols() - 1) = (TNumType)1. / (TNumType)(partLattices_[lattTypeIdx(-2)]->size() - 1);
452  }
453  return ans;
454  }
455 
456 private:
457  auto& dtIdpCf(const size_t _i, const TNumType& _arcNow, const int& _latticePtType) const
458  {
459  static Eigen::Matrix<TNumType, 1, -1> ans;
460  ans.resize(stateSim_->state().stateGradNm().sub(0).data().size());
461  ans.setZero();
462  if (_arcNow == stateSim_->paramFuncs()->funcsArcEnd())
463  {
464  ans(ans.cols() - 1) = (TNumType)_i / (TNumType)(partLattices_[lattTypeIdx(-2)]->size() - 1);
465  }
466  return ans;
467  }
468 
469 private:
470  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
471  typename std::enable_if<(!canComputeStateGrad)>::type* = nullptr>
472  static void copyDataFromSimStateToState(const TSimState& _simState, TState& _state)
473  {
474  _state.stateCf().data() = _simState.stateCf().data();
475  _state.stateNm().data() = _simState.stateNm().data();
476  }
477 
478 private:
479  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
480  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
481  static void copyDataFromSimStateToState(const TSimState& _simState, TState& _state)
482  {
483  _state.stateCf().data() = _simState.stateCf().data();
484  _state.stateNm().data() = _simState.stateNm().data();
485  _state.stateGradNm().data() = _simState.stateGradNm().data();
486  _state.stateGradCf().data() = _simState.stateGradCf().data();
487  }
488 
489 private:
490  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
491  typename std::enable_if<(!canComputeStateGrad)>::type* = nullptr>
492  static void copyStructureFromSimStateToState(const TSimState& _simState, TState& _state)
493  {
494  _state.stateCf() = _simState.stateCf();
495  _state.stateNm() = _simState.stateNm();
496  }
497 
498 private:
499  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
500  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
501  static void copyStructureFromSimStateToState(const TSimState& _simState, TState& _state)
502  {
503  _state.stateCf() = _simState.stateCf();
504  _state.stateNm() = _simState.stateNm();
505  _state.stateGradNm() = _simState.stateGradNm();
506  _state.stateGradCf() = _simState.stateGradCf();
507  }
508 
509 private:
510  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
511  typename std::enable_if<(!canComputeStateGrad)>::type* = nullptr>
512  static void copyDataFromStateToSimState(const TState& _state, TSimState& _simState)
513  {
514  _simState.stateCf().data() = _state.stateCf().data();
515  _simState.stateNm().data() = _state.stateNm().data();
516  }
517 
518 private:
519  template <typename TSimState, typename TState, bool canComputeStateGrad = CanComputeStateGrad,
520  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
521  static void copyDataFromStateToSimState(const TState& _state, TSimState& _simState)
522  {
523  _simState.stateCf().data() = _state.stateCf().data();
524  _simState.stateNm().data() = _state.stateNm().data();
525  _simState.stateGradNm().data() = _state.stateGradNm().data();
526  _simState.stateGradCf().data() = _state.stateGradCf().data();
527  }
528 
531 protected:
532  void simAppendToSimPartLat(const TNumType& _arcNow, const int& _latticePtType, const std::size_t& _minArcLatCacheIdx)
533  {
534  if ((stateSim_->stateArc() < _arcNow) || firstTime_)
535  {
536  (this->*advanceFunc)(_arcNow);
538 
539  auto& stateSimState = stateSim_->state();
540  simLatticeI.arc = _arcNow;
541 
542  if (simulatingWithGrad)
543  {
544  stateSim_->setStateCfWithGrad(_arcNow, PfEaG::NEAR_LAST);
546  stateSim_->stateNmDot(stateSim_->state().stateNm(), stateSim_->state().stateCf(), _arcNow, PfEaG::NEAR_LAST);
547  stateSim_->stateCfDot();
548  const auto& dtIdpVal = dtIdpCf(simAppendIdx_, _arcNow, _latticePtType);
549  static Eigen::Matrix<TNumType, -1, -1> fCfdtIdpVal;
550  static Eigen::Matrix<TNumType, -1, -1> fNmdtIdpVal;
551 
552  fCfdtIdpVal = (stateSim_->stateCfDotCache_.data() * dtIdpVal).transpose();
553  fNmdtIdpVal = (stateSim_->stateNmDotCache_.data() * dtIdpVal).transpose();
554 
555  auto& stateGradCf = simLatticeI.statePtr->stateGradCf();
556  auto& stateGradNm = simLatticeI.statePtr->stateGradNm();
557  stateGradCf.data() += Eigen::Map<Eigen::Matrix<TNumType, -1, 1>>(fCfdtIdpVal.data(), fCfdtIdpVal.size());
558  stateGradNm.data() += Eigen::Map<Eigen::Matrix<TNumType, -1, 1>>(fNmdtIdpVal.data(), fNmdtIdpVal.size());
559  }
560  else
561  {
562  stateSim_->setStateCf(_arcNow, PfEaG::NEAR_LAST);
564  }
565 
566  simAppendIdx_++;
567  firstTime_ = false;
568  }
569  appendToPartLat(_arcNow, _latticePtType, _minArcLatCacheIdx);
570  }
571 
572  // protected: void simAppendToSimPartLat ( const TNumType& _arcNow, const int& _latticePtType, const
573  // std::size_t& _minArcLatCacheIdx) {
574  //
575  //
576  // auto& dtIdpVal = dtIdpCf(simAppendIdx_, _arcNow, _latticePtType);
577  //
578  // Eigen::Matrix<TNumType,-1,-1> fNmdtIdpVal;
579  // static Eigen::Matrix<TNumType,-1,-1> fNmdtIdpValOld;
580  // if(_arcNow == 0) {
581  // fNmdtIdpVal = (stateSim_->stateNmDotCache_.data() * dtIdpVal).transpose();
582  // fNmdtIdpValOld = fNmdtIdpVal;
583  // }
584  //
585  // if( ( stateSim_->stateArc() < _arcNow ) || firstTime_ ) {
586  // (this->*advanceFunc)(_arcNow);
587  // auto& simLatticeI = simulationLattice_[simAppendIdx_];
588  //
589  // auto& stateSimState = stateSim_->state();
590  // simLatticeI.arc = _arcNow;
591  // copyDataFromSimStateToState(stateSimState, *simLatticeI.statePtr);
592  //
593  //
594  // if(simulatingWithGrad) {
595  //
596  // stateSim_->stateCfDot();
597  // Eigen::Matrix<TNumType,-1,-1> fCfdtIdpVal = (stateSim_->stateCfDotCache_.data() * dtIdpVal).transpose();
598  // if(_arcNow>0){
599  // fNmdtIdpVal = ((stateSim_->state().stateNm().data() -
600  // simulationLattice_[simAppendIdx_-1].statePtr->stateNm().data())/(_arcNow-simulationLattice_[simAppendIdx_-1].arc) *
601  // dtIdpVal).transpose();
602  // }
603  //
604  // auto& stateGradCf = simLatticeI.statePtr->stateGradCf();
605  // auto& stateGradNm = simLatticeI.statePtr->stateGradNm();
606  // stateGradCf.data() += Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fCfdtIdpVal.data(), fCfdtIdpVal.size());
607  // stateSim_->state().stateGrad() =
608  // stateGradNm.data() += Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fNmdtIdpVal.data(), fNmdtIdpVal.size());
609  //
610  // // auto& stateSimGradNm = stateSim_->state().stateGradNm();
611  // // stateSimGradNm.data() += Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fNmdtIdpVal .data(), fNmdtIdpVal
612  // .size());
613  // // stateSimGradNm.data() -= Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fNmdtIdpValOld.data(),
614  // fNmdtIdpValOld.size());
615  // // stateGradNm.data() = stateSimGradNm.data();
616  // // std::cout<<Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fNmdtIdpVal .data(), fNmdtIdpVal
617  // .size())-Eigen::Map<Eigen::Matrix<TNumType,-1,1>>(fNmdtIdpValOld.data(), fNmdtIdpValOld.size())<<std::endl;
618  //
619  // // std::cout<<"_arcNow="<<_arcNow<<std::endl;
620  // // std::cout<<"fNmdtIdpVal="<<std::endl<<fNmdtIdpVal<<std::endl;
621  // // std::cout<<"fNmdtIdpValOld="<<std::endl<<fNmdtIdpValOld<<std::endl<<std::endl;
622  //
623  // fNmdtIdpValOld = fNmdtIdpVal;
624  // }
625  //
626  //
627  //
628  // simAppendIdx_++;
629  // firstTime_ = false;
630  // }
631  // appendToPartLat( _arcNow, _latticePtType, _minArcLatCacheIdx );
632  // }
633 
635 protected:
636  void appendToPartLat(const TNumType& _arcNow, const int& _latticePtType, const std::size_t& _minArcLatCacheIdx)
637  {
638  auto& partLatticeNow = partLattices_[lattTypeIdx(_latticePtType)]->at(_minArcLatCacheIdx);
639  partLatticeNow.arc = _arcNow;
640  partLatticeNow.statePtr = simulationLattice_[simAppendIdx_ - 1].statePtr;
641  }
643 protected:
644  void setBeginStateToLattices(const TNumType& _arcBegin)
645  {
647  for (size_t i = lattTypeIdx(asInt(BaseSimLatticeType::ARC_BG_BK)); i < partLattices_.size(); ++i)
648  {
649  if (!partLattices_[i]->empty())
650  {
651  partLattices_[i]->at(0).statePtr = simulationLattice_[simAppendIdx_ - 1].statePtr;
652  }
653  }
654  }
656 protected:
657  void setEndStateToLattices(const TNumType& _arcEnd)
658  {
661  std::min(partLattices_[lattTypeIdx(asInt(BaseSimLatticeType::ARC_BG_BK))]->size() - 1, (size_t)1));
663  for (size_t i = lattTypeIdx(asInt(BaseSimLatticeType::ARC_BG_BK)); i < partLattices_.size(); ++i)
664  {
665  if (!partLattices_[i]->empty())
666  {
667  auto& partLatI0 = partLattices_[i]->back();
668  partLatI0.statePtr = simulationLattice_[simAppendIdx_ - 1].statePtr;
669  }
670  }
671  }
673 protected:
674  void setBeginEndArcsToLattices(const TNumType& _arcBegin, const TNumType& _arcEnd)
675  {
676  for (size_t i = lattTypeIdx(asInt(BaseSimLatticeType::ARC_BG_BK)); i < partLattices_.size(); ++i)
677  {
678  if (!partLattices_[i]->empty())
679  {
680  partLattices_[i]->at(0).arc = _arcBegin;
681  partLattices_[i]->back().arc = _arcEnd;
682  }
683  }
684  }
685 
687  {
688  return a.arc < b.arc;
689  }
690 
693 protected:
694  bool initSimLatticeState0(const TNumType& _lastValidArc, size_t& _firstLaticeInvalidIdx)
695  {
696  if ((simulationLattice_.size() > 0) && (_lastValidArc > 0))
697  {
698  _firstLaticeInvalidIdx = std::upper_bound(simulationLattice_.begin(), simulationLattice_.end(),
699  LatticePoint(_lastValidArc), cmpLatticePt) -
700  simulationLattice_.begin();
701  if (_firstLaticeInvalidIdx >= simulationLattice_.size())
702  {
703  return false;
704  }
705  }
706  if (_firstLaticeInvalidIdx == 0)
707  {
708  stateSim_->toState0();
709  }
710  else if (_firstLaticeInvalidIdx <= simulationLattice_.size())
711  {
712  auto& lastValidSimState = simulationLattice_[_firstLaticeInvalidIdx - 1];
713  stateSim_->toState(*lastValidSimState.statePtr, lastValidSimState.arc);
714  }
715  return true;
716  }
718 protected:
719  void initExtLatticeCache(const TNumType& _minArcLatticeVal)
720  {
721  partLatIdxCache_.assign(partLattices_.size(), 0);
722  for (size_t iPart = extArcLatIdxBegin; iPart < partLattices_.size(); ++iPart)
723  {
724  if (partLattices_[iPart]->empty())
725  {
726  partLatIdxCache_[iPart] = -1;
727  continue;
728  }
729  for (; partLatIdxCache_[iPart] < (int)partLattices_[iPart]->size(); ++partLatIdxCache_[iPart])
730  {
731  if (partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc > _minArcLatticeVal)
732  {
733  ++partLatIdxCache_[iPart];
734  break;
735  }
736  }
737  partLatIdxCache_[iPart] = std::max(0, (int)partLatIdxCache_[iPart] - 1);
738  }
739  }
741 protected:
742  void populateTrajSimPartLattice(const size_t& _firstLaticeInvalidIdx)
743  {
744  const TNumType arcParamMax = stateSim_->paramFuncs()->funcsArcEnd();
745  size_t simLatticeSize = std::max(0, (int)(ceil(arcParamMax / dt()) + 2));
746 
748  // reserve maximum computable lattice points
749  for (size_t i = extArcLatIdxBegin; i < partLattices_.size(); ++i)
750  {
751  simLatticeSize += partLattices_[i]->size();
752  }
753  simLatticeSize = std::max(simLatticeSize, (size_t)0);
754  bool forceResize = false;
755  bool resize = false;
756  if ((simLatticeSize > simulationLattice_.size()))
757  {
758  forceResize = true;
759  resize = true;
760  }
761  else if (simulationLattice_.size() > 0)
762  {
763  if (stateSim_->state0().data().size() != simulationLattice_[0].statePtr->data().size())
764  {
765  forceResize = true;
766  }
767  }
768  else
769  {
770  forceResize = true;
771  }
772  if (resize)
773  {
774  simulationLattice_.resize(simLatticeSize);
775  }
776  if (forceResize)
777  {
778  for (auto& simLatticeI : simulationLattice_)
779  {
781  simLatticeI.statePtr->stateGrad().bindMat();
782  }
783  }
784 
785  // compute equal time lattice initial value and multiplier; if init, push_back the 0 lattice point
786  simAppendIdx_ = _firstLaticeInvalidIdx;
787  TNumType minArcLatticeVal = 0;
788  if ((_firstLaticeInvalidIdx == 0) || (simulationLattice_.size() == 0))
789  {
790  setBeginStateToLattices(minArcLatticeVal);
791  }
792  else
793  {
794  minArcLatticeVal = simulationLattice_[simAppendIdx_ - 1].arc;
795  }
796 
797  if (costsEvaluator_)
798  {
799  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::F);
800  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::G);
801  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::H);
802  }
803 
804  if (isEmptyAllExtLattices() && (dt() > 0) && (ds() <= 0))
805  {
806  populatePartSimLatticesDtOnly(_firstLaticeInvalidIdx, arcParamMax);
807  } // case when only a dt lattice is available
808  else
809  {
810  populatePartSimLatticesGeneral(_firstLaticeInvalidIdx, arcParamMax, minArcLatticeVal);
811  } // general case
812  }
813 
815 protected:
817  {
818  bool emptyExtArcLat = true;
819  for (size_t i = extArcLatIdxBegin; i < partLattices_.size(); ++i)
820  {
821  if (!partLattices_[i]->empty())
822  {
823  emptyExtArcLat = false;
824  break;
825  }
826  }
827  return emptyExtArcLat;
828  }
830 public:
831  static size_t lattTypeIdx(int _enumIdx)
832  {
833  return _enumIdx + asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE);
834  }
835 
836 protected:
838  {
839  dt_ = dtBase_;
840  if (scaleDt_)
841  {
842  dt_ *= stateSim_->paramFuncs()->funcsArcEnd();
843  }
844  ds_ = dsBase_;
845  if (scaleDs_)
846  {
847  stateSim_->paramFuncs()->setEvalArc(stateSim_->paramFuncs()->funcsArcEnd());
848  ds_ *= stateSim_->paramFuncsDist()->computeS();
849  stateSim_->paramFuncs()->setEvalArc(stateSim_->paramFuncs()->funcsArcBegin());
850  }
851  }
852 
853 public:
854  LatticePointType& simLatticeI(const size_t& _i)
855  {
856  return simulationLattice_[_i];
857  }
858 
859 public:
860  const LatticePointType& simLatticeI(const size_t& _i) const
861  {
862  return simulationLattice_[_i];
863  }
864 
865 public:
866  size_t simLatticeSize() const
867  {
869  }
870 
872 protected:
875 private:
876  std::vector<LatticePointType> simulationLattice_;
877 
878 private:
881 public:
885 protected:
886  std::vector<int> partLatIdxCache_;
888 protected:
891 private:
892  TNumType dt_;
894 private:
895  TNumType ds_;
896 
897 private:
898  TNumType dtBase_;
899 
900 private:
901  TNumType dsBase_;
902 
903 protected:
904  bool scaleDt_;
905 
906 protected:
907  bool scaleDs_;
908 
909 private:
910  std::vector<std::vector<TNumType*>> userDefPartLattices_;
911 
912 private:
914 
915 private:
917 
918 public:
919  std::unique_ptr<CostsEvaluatorClass> costsEvaluator_;
920 
921  friend class TrajectorySimGrade;
922  // public : size_t partLatticeActiveSize_;
923 };
924 }
925 
926 #endif // TRAJECTORY_SIMULATOR_HPP
const StateSimSPtr & stateSim() const
Const reference of the state simulator object.
StateSimPtr stateSim_
State simulator object.
StateSimSPtr & stateSim()
Reference of the state simulator object.
LatticePointType(TNumType _arc, int _latticeType)
std::shared_ptr< std::vector< LatticePoint > > LatticeVecSPtr
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.
static bool cmpLatticePt(const TrajectorySimulator::LatticePoint &a, const TrajectorySimulator::LatticePoint &b)
void initExtLatticeCache(const TNumType &_minArcLatticeVal)
Initializes the cached partial lattices index at the highest arc lower than.
void setUserDefLattice(const std::vector< std::vector< TNumType * >> &_userDefLattices)
void populateTrajSimPartLattice(const size_t &_firstLaticeInvalidIdx)
Main function that performs resizing, reserving and calls the proper population function.
static size_t lattTypeIdx(int _enumIdx)
Converts shifted (int) lattice index to container (size_t) index.
void simulateTrajectoryImpl(TNumType _lastValidArc)
auto & dtIdpNm(const size_t _i, const TNumType &_arcNow, const int &_latticePtType) const
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 setBoolDsScale(const bool &_doScale)
void setBeginStateToLattices(const TNumType &_arcBegin)
Binds reference to the initial simulated state (at.
TNumType dt_
Arc parametrization interval used for the equal arc-length lattice.
virtual void populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, double _arcParamMax, double _minArcLatticeVal)=0
Performs simulation and populates simulation and partial lattices in the general case of various enab...
void populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, TNumType _arcParamMax, TNumType _minArcLatticeVal)
Performs simulation and populates simulation and partial lattices in the general case of various enab...
LatticePointType & simLatticeI(const size_t &_i)
std::unique_ptr< CostsEvaluatorClass > costsEvaluator_
void appendToPartLat(const double &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Appends the new arc and state pointer to the afferent partial lattice point.
Structure containing the lattice type afferent to a.
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
const double & ds() const
Const reference to arc parametrization interval used for the equal distance lattice.
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
const TNumType & ds() const
Const reference to arc parametrization interval used for the equal distance lattice.
void setBeginStateToLattices(const double &_arcBegin)
Binds reference to the initial simulated state (at.
std::vector< LatticePoint > LatticeVec
bool initSimLatticeState0(const double &_lastValidArc, size_t &_firstLaticeInvalidIdx)
Initializes the simulation lattice (truncation from the.
auto & dtIdpCf(const size_t _i, const TNumType &_arcNow, const int &_latticePtType) const
bool simulatingWithGrad
Simulates (discrete numerical evaluation) an entire trajectory according to the specified intervals a...
double dt_
Arc parametrization interval used for the equal arc-length lattice.
std::vector< std::vector< double * > > userDefPartLattices_
void(TrajectorySimulator< TNumType, TSimType >::*)(const TNumType &) AdvanceFunction
void setBeginEndArcsToLattices(const TNumType &_arcBegin, const TNumType &_arcEnd)
Sets begin and end arcs to all partial lattices on the first and last container positions.
virtual void populatePartSimLatticesDtOnly(const size_t &_firstLaticeInvalidIdx, double _arcParamMax)
Performs simulation and populates simulation and partial lattice when only equal dt lattice is enable...
typename TSimType::StateForSimType StateForSimType
bool isEmptyAllExtLattices() const
Returns true if all extended lattices are empty (the DS lattice as well as user-defined lattices)...
BaseSimLatticeType
Fundamental lattice types.
static void copyDataFromSimStateToState(const TSimState &_simState, TState &_state)
bool isEmptyAllExtLattices() const
Returns true if all extended lattices are empty (the DS lattice as well as user-defined lattices)...
void simulateTrajectoryWithGrad(TNumType _lastValidArc=0)
static constexpr const std::size_t extArcLatIdxBegin
Structure containing an evaluation arc and a state pointer.
StateSimSPtr stateSim_
State simulator object.
void simAppendToSimPartLat(const TNumType &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Performs a simulation step (if.
void appendToPartLat(const TNumType &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Appends the new arc and state pointer to the afferent partial lattice point.
bool canComputeDistLattice_
Flags if the StateSim object has access to the StateSimDist functionality.
TNumType & dsBase()
Reference to arc parametrization interval used for the equal distance lattice.
static void copyStructureFromSimStateToState(const TSimState &_simState, TState &_state)
TrajectorySimulator(StateSimPtr _stateSim)
TrajectorySimulator & operator=(const TrajectorySimulator &)=default
TNumType & dtBase()
Reference to arc parametrization interval used for the equal arc-length lattice.
static constexpr auto value
Definition: utils.h:205
void setBoolDtScale(const bool &_doScale)
void setEndStateToLattices(const double &_arcEnd)
Binds reference to the final simulated state (at.
std::vector< std::vector< TNumType * > > userDefPartLattices_
void advanceFuncSimGrad(const TNumType &_arcNow)
const TNumType & dt() const
Const reference to arc parametrization interval used for the equal arc-length lattice.
std::vector< std::shared_ptr< std::vector< LatticePoint > > > LatticeVecSPtrVec
const double & dt() const
Const reference to arc parametrization interval used for the equal arc-length lattice.
void populateTrajSimPartLattice(const size_t &_firstLaticeInvalidIdx)
Main function that performs resizing, reserving and calls the proper population function.
typename TSimType::StateType StateType
close to previous evaluation arc
double ds_
Arc parametrization interval used for the equal distance lattice.
static constexpr const bool CanComputeStateGrad
void setEndStateToLattices(const TNumType &_arcEnd)
Binds reference to the final simulated state (at.
void advanceFuncSim(const TNumType &_arcNow)
void simAppendToSimPartLat(const double &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Performs a simulation step (if.
std::shared_ptr< TSimType > StateSimSPtr
std::shared_ptr< StateType > StateSPtr
virtual ~TrajectorySimulator()=default
bool initSimLatticeState0(const TNumType &_lastValidArc, size_t &_firstLaticeInvalidIdx)
Initializes the simulation lattice (truncation from the.
void populatePartSimLatticesDtOnly(const size_t &_firstLaticeInvalidIdx, TNumType _arcParamMax)
Performs simulation and populates simulation and partial lattice when only equal dt lattice is enable...
TNumType ds_
Arc parametrization interval used for the equal distance lattice.
static size_t lattTypeIdx(int _enumIdx)
Converts shifted (int) lattice index to container (size_t) index.
TrajectorySimulator(StateSimSPtr &_stateSim)
void simulateTrajectory(TNumType _lastValidArc=0)
const LatticePointType & simLatticeI(const size_t &_i) const
static void copyDataFromStateToSimState(const TState &_state, TSimState &_simState)
LatticePointType(TNumType _arc, int _latticeType, StateSPtr &_statePtr)
TrajectorySimulator(StateSimSPtr &_stateSim, std::unique_ptr< CostsEvaluatorClass > _costsEvaluator)
LatticePoint(TNumType _arc, StateSPtr &_statePtr)


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