2/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_SIMULATOR2_HPP
34 #define TRAJECTORY_SIMULATOR2_HPP
35 
39 
40 #include <functional>
41 #include <memory>
42 #include <queue>
43 
44 namespace tuw
45 {
47 template <typename TNumType, typename TStateType>
49 {
50  using StateSPtr = std::shared_ptr<TStateType>;
51 
52  LatticePoint() : arc(-1), statePtr(std::shared_ptr<TStateType>(new TStateType))
53  {
54  }
55  LatticePoint(StateSPtr _statePtr) : arc(-1), statePtr(_statePtr)
56  {
57  }
58  LatticePoint(StateSPtr _statePtr, const size_t& _latticeIdx) : arc(-1), statePtr(_statePtr), latticeIdx(_latticeIdx)
59  {
60  }
61  LatticePoint(TNumType _arc) : arc(_arc), statePtr(nullptr)
62  {
63  }
64  LatticePoint(TNumType _arc, StateSPtr _statePtr) : arc(_arc), statePtr(_statePtr)
65  {
66  }
67  virtual ~LatticePoint()
68  {
69  }
70 
71  TNumType arc;
73  size_t latticeIdx;
74 };
75 
76 template <class TDerived, typename TNumType, class TSimType, class... TLatticeCostFuncs>
78 {
79 public:
80  using NumType = TNumType;
81 
82 public:
83  using StateType = typename TSimType::StateType;
84 
85 public:
86  using StateSPtr = std::shared_ptr<StateType>;
87 
88 public:
89  LatticeTypeBaseCRTP() = default;
90 
91 public:
92  virtual ~LatticeTypeBaseCRTP() = default;
93 
94 public:
95  LatticeTypeBaseCRTP(const LatticeTypeBaseCRTP&) = default;
96 
97 public:
98  LatticeTypeBaseCRTP& operator=(const LatticeTypeBaseCRTP&) = default;
99 
100 public:
102 
103 public:
104  LatticeTypeBaseCRTP& operator=(LatticeTypeBaseCRTP&&) = default;
105 
106 public:
107  void computeLatticeStructure(TSimType& _sim, const size_t _latticeIdx)
108  {
109  computeLatticeArcs(_sim, latticeArcs_);
110  const size_t& latticeSize = latticeArcs_.size();
111  if (latticeSize != lattice.size())
112  {
113  lattice.resize(latticeSize, LatticePoint<NumType, StateType>(nullptr, _latticeIdx));
114  latticeVecIterEnd_ = lattice.end();
115  }
116  latticeVecCacheIter_ = lattice.begin();
117  for (size_t i = 0; i < latticeSize; ++i)
118  {
119  lattice[i].arc = latticeArcs_[i];
120  }
121  }
122 
123 public:
124  void computeLatticeArcs(TSimType& _sim, std::vector<NumType>& _latticeArcs)
125  {
126  thisDerived().computeLatticeArcsImpl(_sim, latticeArcs_);
127  }
128 
129 public:
130  virtual void precompute(TSimType& _sim) = 0;
131 
132 public:
133  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
134  typename std::enable_if<(TupSize > 0)>::type* = nullptr>
135  static constexpr const size_t costFuncsNr()
136  { /*const*/
137  return std::tuple_size<typename std::tuple_element<FuncsNr, std::tuple<TLatticeCostFuncs...>>::type>::value;
138  }
139 
140 public:
141  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
142  typename std::enable_if<(TupSize == 0)>::type* = nullptr>
143  static constexpr const size_t costFuncsNr()
144  { /*const*/
145  return 0;
146  }
147 
148 public:
149  static constexpr const size_t costFuncsTypesNr()
150  { /*const*/
151  return std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value;
152  }
153 
154 public:
155  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
156  typename std::enable_if<(TupSize > 0)>::type* = nullptr>
157  void evaluate(const auto& _x, const size_t& _i, TSimType& _sim, auto& _ansPtr)
158  {
159  for_each_tuple(std::get<FuncsNr>(costFuncs_), [this, &_x, &_sim, &_ansPtr, &_i](auto& costFuncI)
160  {
161  *_ansPtr = costFuncI.f(_x, _i, _sim, this->thisDerived());
162  _ansPtr++;
163  });
164  }
165 
166 public:
167  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
168  typename std::enable_if<(TupSize == 0)>::type* = nullptr>
169  void evaluate(const auto& _x, const size_t& _i, TSimType& _sim, auto& _ansPtr)
170  {
171  }
172 
173 public:
174  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
175  typename std::enable_if<(TupSize > 0)>::type* = nullptr>
176  void evaluateWithGrad(const auto& _x, const size_t& _i, const auto& _gradX, TSimType& _sim, auto& _ansPtr,
177  auto& _ansGradPtr, const size_t& elSize)
178  {
179  for_each_tuple(std::get<FuncsNr>(costFuncs_),
180  [this, &_x, &_gradX, &_sim, &_ansPtr, &_ansGradPtr, &elSize, &_i](auto& costFuncI)
181  {
182  *_ansPtr = costFuncI.f(_x, _i, _sim, this->thisDerived());
183  _ansPtr++;
184 #ifdef USE_MAP_ALIGNMENT
185  Eigen::Map<Eigen::Matrix<TNumType, -1, 1>, MapAlignment> map(_ansGradPtr, elSize, 1);
186 #else
187  Eigen::Map<Eigen::Matrix<TNumType, -1, 1>> map(_ansGradPtr, elSize, 1);
188 #endif
189  costFuncI.gradF(map, _x, _gradX, _i, _sim, this->thisDerived());
190  _ansGradPtr += elSize;
191  });
192  }
193 
194 public:
195  template <size_t FuncsNr = 0, size_t TupSize = std::tuple_size<std::tuple<TLatticeCostFuncs...>>::value,
196  typename std::enable_if<(TupSize == 0)>::type* = nullptr>
197  void evaluateWithGrad(const auto& _x, const size_t& _i, const auto& _gradX, TSimType& _sim, auto& _ansPtr,
198  auto& _ansGradPtr, const size_t& elSize)
199  {
200  }
201 
202 private:
203  TDerived& thisDerived()
204  {
205  return static_cast<TDerived&>(*this);
206  }
207 
208 private:
209  const TDerived& thisDerived() const
210  {
211  return static_cast<const TDerived&>(*this);
212  }
213 
214 public:
215  std::vector<LatticePoint<NumType, StateType>> lattice;
216 
217 private:
218  std::vector<NumType> latticeArcs_;
219 
220 private:
221  std::tuple<TLatticeCostFuncs...> costFuncs_;
222 
223 private:
224  typename std::vector<LatticePoint<NumType, StateType>>::iterator latticeVecCacheIter_;
225 
226 private:
227  typename std::vector<LatticePoint<NumType, StateType>>::iterator latticeVecIterEnd_;
228 
229  template <typename TNumType2, typename TSimType2, bool TUseStateNm2,
230  template <typename, typename> class... TLatticeTypes2>
231  friend class TrajectorySimulator;
232  template <typename T, typename... TTuple>
233  friend size_t bindFromPartLatticesToSimLattice(std::tuple<TTuple...>&, std::vector<T>&);
234 };
235 
236 template <std::size_t II = 0, class FuncT, typename... Tp>
237 constexpr inline typename std::enable_if<II == sizeof...(Tp), void>::type for_each_tuple_class(std::tuple<Tp...>&,
238  FuncT&)
239 {
240 }
241 
242 template <std::size_t II = 0, class FuncT, typename... Tp>
243  constexpr inline typename std::enable_if <
244  II<sizeof...(Tp), void>::type for_each_tuple_class(std::tuple<Tp...>& t, FuncT& f)
245 {
246  f[II] = [&t](const auto& _a, const auto& _b, const auto& _c, auto& _d)
247  {
248  std::get<II>(t).computeDArcIdPImpl(_a, _b, _c, _d);
249  };
250  for_each_tuple_class<II + 1, FuncT, Tp...>(t, f);
251 }
252 
253 template <std::size_t IIMax, std::size_t II = 0>
254 constexpr inline typename std::enable_if<(II == IIMax), void>::type get_costs_sizes(auto& partLatI, const size_t& _i,
255  auto& _sizeCostsPerPartLattice,
256  auto& _sizeCostsPerType)
257 {
258 }
259 
260 template <std::size_t IIMax, std::size_t II = 0>
261 constexpr inline typename std::enable_if<(II < IIMax), void>::type get_costs_sizes(auto& partLatI, const size_t& _i,
262  auto& _sizeCostsPerPartLattice,
263  auto& _sizeCostsPerType)
264 {
265  size_t partLatICostsTypeJ = partLatI.lattice.size() * partLatI.template costFuncsNr<II>();
266  _sizeCostsPerPartLattice[_i] += partLatICostsTypeJ;
267  _sizeCostsPerType[II] += partLatICostsTypeJ;
268  get_costs_sizes<IIMax, II + 1>(partLatI, _i, _sizeCostsPerPartLattice, _sizeCostsPerType);
269 }
270 
271 template <typename TNumType, typename TSimType, bool TUseStateNm, template <typename, typename> class... TLatticeTypes>
273 {
274 public:
275  using StateSimSPtr = std::shared_ptr<TSimType>;
276 
277 public:
278  using StateType = typename TSimType::StateType;
279 
280 public:
281  using StateSPtr = std::shared_ptr<StateType>;
282 
283 public:
284  using StateForSimType = typename TSimType::StateForSimType;
285 
286 public:
287  using LatticePointType = LatticePoint<TNumType, StateType>;
288 
289 public:
290  static constexpr const bool CanComputeStateGrad =
291  !std::is_same<EmptyGradType, typename StateMapBaseTraits<StateType>::StateWithGradNmType>::value;
292 
293 public:
294  static constexpr const size_t CostFuncsTypesNr =
295  std::tuple_element<0, std::tuple<TLatticeTypes<TNumType, TSimType>...>>::type::costFuncsTypesNr();
296 
297 public:
298  TrajectorySimulator() : stateSim_(std::shared_ptr<TSimType>(new TSimType))
299  {
301  for (size_t i = 0; i < gradCostsMap_.size(); ++i)
302  {
303 #ifdef USE_MAP_ALIGNMENT
304  gradCostsMap_[i] =
305  std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>>(new Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>(nullptr, 0, 0));
306 #else
307  gradCostsMap_[i] =
308  std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>>(new Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>(nullptr, 0, 0));
309 
310 #endif
311 
312  }
313  }
314 
315 public:
316  TrajectorySimulator(StateSimSPtr& _stateSim) : stateSim_(_stateSim)
317  {
319  for (size_t i = 0; i < gradCostsMap_.size(); ++i)
320  {
321 #ifdef USE_MAP_ALIGNMENT
322  gradCostsMap_[i] =
323  std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>>(new Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>(nullptr, 0, 0));
324 #else
325  gradCostsMap_[i] =
326  std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>>(new Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>(nullptr, 0, 0));
327 #endif
328  }
329  }
330 
332 public:
333  StateSimSPtr& stateSim()
334  {
335  return stateSim_;
336  }
338 public:
339  const StateSimSPtr& stateSim() const
340  {
341  return stateSim_;
342  }
343 
344 public:
345  LatticePointType& simLatticeI(const size_t& _i)
346  {
347  return simulationLattice_[_i];
348  }
349 
350 public:
351  const LatticePointType& simLatticeI(const size_t& _i) const
352  {
353  return simulationLattice_[_i];
354  }
355 
356 public:
357  size_t simLatticeSize() const
358  {
360  }
361 
362 public:
363  template <template <typename, typename> class TLatticeType>
364  TLatticeType<TNumType, TSimType>& partialLattice()
365  {
366  return std::get<TLatticeType<TNumType, TSimType>>(partialLattices_);
367  }
368 
369 public:
370  template <template <typename, typename> class TLatticeType>
371  const TLatticeType<TNumType, TSimType>& partialLattice() const
372  {
373  return std::get<TLatticeType<TNumType, TSimType>>(partialLattices_);
374  }
375 
376 public:
377  template <size_t TLatticeIdx>
378  auto& partialLattice()
379  {
380  return std::get<TLatticeIdx>(partialLattices_);
381  }
382 
383 public:
384  template <size_t TLatticeIdx>
385  const auto& partialLattice() const
386  {
387  return std::get<TLatticeIdx>(partialLattices_);
388  }
389 
390 private:
391  using AdvanceFunction =
393 
394 private:
395  AdvanceFunction advanceFunc;
396 
397 private:
398  void advanceFuncSimEmpty(const TNumType& _arcNow)
399  {
400  }
401 
402 private:
403  void advanceFuncSim(const TNumType& _arcNow)
404  {
405  stateSim_->advance(_arcNow);
406  }
407 
408 private:
409  template <bool canComputeStateGrad = CanComputeStateGrad,
410  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
411  void advanceFuncSimGrad(const TNumType& _arcNow)
412  {
413  stateSim_->advanceWithGrad(_arcNow);
414  }
415 
416 private:
418 
419 private:
420  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* = nullptr>
422  {
423  advanceFunc = &TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSim;
424  }
425 
426 private:
427  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* = nullptr>
428  void bindAdvanceFunc()
429  {
430  advanceFunc = &TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSimEmpty;
431  }
432 
433 private:
434  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* = nullptr>
436  {
437  advanceFunc = &TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSimGrad;
438  }
439 
440 private:
441  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* = nullptr>
442  void bindAdvanceFuncGrad()
443  {
444  advanceFunc = &TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSimEmpty;
445  }
446 
447 public:
448  void simulateTrajectory(const bool& _saveLatticeStates = false)
449  {
450  bindAdvanceFunc();
451  simulatingWithGrad = false;
452  simulateTrajectoryImpl(_saveLatticeStates);
453  }
454 
455 public:
456  template <bool canComputeStateGrad = CanComputeStateGrad,
457  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
458  void simulateTrajectoryWithGrad(const bool& _saveLatticeStates = false)
459  {
461  simulatingWithGrad = true;
462  simulateTrajectoryImpl(_saveLatticeStates);
463  }
464 
465  template <std::size_t IIMax, std::size_t II = 0>
466  constexpr typename std::enable_if<(II == IIMax), void>::type eval_all_costs(TNumType*& _cfPtr, TNumType*& _cfGradPtr,
467  const size_t& _optParamSize)
468  {
469  }
470 
471  template <std::size_t IIMax, std::size_t II = 0>
472  constexpr typename std::enable_if<(II < IIMax), void>::type eval_all_costs(TNumType*& _cfPtr, TNumType*& _cfGradPtr,
473  const size_t& _optParamSize)
474  {
475  evaluateCosts<II>(_cfPtr, _cfGradPtr, _optParamSize);
476  eval_all_costs<IIMax, II + 1>(_cfPtr, _cfGradPtr, _optParamSize);
477  }
478 
479 public:
480  void simulateTrajectoryImpl(const bool& _saveLatticeStates = false)
481  {
482  size_t sizeCosts = 0;
485  if (sizeCosts > 0)
486  {
487  size_t optParamSize = stateSim_->state().stateGradCf().sub(0).data().size();
488  resizeCosts(optParamSize, simulatingWithGrad);
489  TNumType* cfPtr = costs_.memStartRef();
490  TNumType* cfGradPtr = gradCosts_.memStartRef();
491  eval_all_costs<CostFuncsTypesNr>(cfPtr, cfGradPtr, optParamSize);
492  }
493  }
494 
495 public:
497  {
498  size_t sizeCosts = 0;
499  populatePartLattices(sizeCosts);
500  if (sizeCosts > 0)
501  {
502  size_t optParamSize = stateSim_->state().stateGradCf().sub(0).data().size();
503  resizeCosts(optParamSize, true);
504  }
505  }
506 
507 private:
508  size_t populatePartLattices(size_t& _sizeCosts)
509  {
510  stateSim_->toState0();
511  size_t totalPartLatticePtsSize = 0, i = 0;
512  sizeCostsPerPartLattice_.fill(0);
513  sizeCostsPerType_.fill(0);
514  for_each_tuple(partialLattices_, [this, &totalPartLatticePtsSize, &i, &_sizeCosts](auto& partLatI)
515  {
516  partLatI.computeLatticeStructure(*stateSim_, i);
517  totalPartLatticePtsSize += partLatI.lattice.size();
518  get_costs_sizes<CostFuncsTypesNr>(partLatI, i, sizeCostsPerPartLattice_, sizeCostsPerType_);
519  _sizeCosts += sizeCostsPerPartLattice_[i];
520  i++;
521  });
522  return totalPartLatticePtsSize;
523  }
524 
525 private:
526  void resizeCosts(const size_t& _optParamSize, const bool& _simulatingWithGrad)
527  {
528  for (size_t i = 0; i < costs_.subSize(); ++i)
529  {
530  costs_.sub(i).subResize(sizeCostsPerType_[i]);
531  }
532 
533  if (_simulatingWithGrad)
534  {
535  for (size_t i = 0; i < gradCosts_.subSize(); ++i)
536  {
537  auto& gradCostsI = gradCosts_.sub(i);
538  gradCostsI.subResize(sizeCostsPerType_[i]);
539  for (size_t j = 0; j < gradCostsI.subSize(); ++j)
540  {
541  gradCostsI.sub(j).subResize(_optParamSize);
542  }
543  }
544  for (size_t i = 0; i < gradCosts_.subSize(); ++i)
545  {
546  auto& gradCostsI = gradCosts_.sub(i);
547  for (size_t j = 0; j < gradCostsI.subSize(); ++j)
548  {
549 #ifdef USE_MAP_ALIGNMENT
550  new (gradCostsMap_[i].get()) Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>(
551  gradCostsI.memStartRef(), sizeCostsPerType_[i], _optParamSize);
552 #else
553  new (gradCostsMap_[i].get()) Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>(
554  gradCostsI.memStartRef(), sizeCostsPerType_[i], _optParamSize);
555 #endif
556  }
557  }
558  }
559  }
560 
561 private:
562  template <size_t FuncNr = 0>
563  void evaluateCosts(TNumType*& _cfPtr, TNumType*& _cfGradPtr, const size_t& _optParamSize)
564  {
565  for_each_tuple(partialLattices_, [this, &_cfPtr, &_optParamSize, &_cfGradPtr](auto& partLatI)
566  {
567  size_t latticeISize = partLatI.lattice.size();
568  partLatI.precompute(*stateSim_);
569  if (simulatingWithGrad)
570  {
571  for (size_t i = 0; i < latticeISize; ++i)
572  {
573  const auto& latticeI = partLatI.lattice[i];
574  partLatI.template evaluateWithGrad<FuncNr>(latticeI.statePtr->state(), i,
575  latticeI.statePtr->stateGrad(), *stateSim_, _cfPtr,
576  _cfGradPtr, _optParamSize);
577  }
578  }
579  else
580  {
581  for (size_t i = 0; i < latticeISize; ++i)
582  {
583  const auto& latticeI = partLatI.lattice[i];
584  partLatI.template evaluate<FuncNr>(*latticeI.statePtr, i, *stateSim_, _cfPtr);
585  }
586  }
587  });
588  }
589 
590 private:
591  void getMinArcLatCacheIdx(typename std::vector<LatticePointType>::iterator*& _itMin, size_t& _latVecIdx)
592  {
593  TNumType _arcMin = FLT_MAX;
594  _itMin = nullptr;
595  for_each_tuple(partialLattices_, [&_arcMin, &_itMin, &_latVecIdx](auto& latI)
596  {
597  auto& vecIter = latI.latticeVecCacheIter_;
598  if (vecIter != latI.latticeVecIterEnd_)
599  {
600  if (vecIter->arc < _arcMin)
601  {
602  _arcMin = vecIter->arc;
603  _itMin = &vecIter;
604  _latVecIdx = std::distance(latI.lattice.begin(), vecIter);
605  }
606  }
607  });
608  }
609 
610 private:
611  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* = nullptr>
612  void setXNmDot(const TNumType& _arcNow)
613  {
614  stateSim_->setXNmDot(_arcNow, PfEaG::NEAR_LAST);
615  }
616 
617 private:
618  template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* = nullptr>
619  void setXNmDot(const TNumType& _arcNow)
620  {
621  }
622 
623  size_t bindFromPartLatticesToSimLattice(const bool& _saveLatticeStates = false)
624  {
625  int simLatticeIdx_ = -1;
626  bool first = true;
627  static typename std::vector<LatticePointType>::iterator* itMinPtr;
628  size_t latVecIdx;
629  getMinArcLatCacheIdx(itMinPtr, latVecIdx);
630  for (size_t i = 0; i < simulationLattice_.size(); ++i)
631  {
632  simulationLattice_[i].arc = -1;
633  }
634  while (itMinPtr != nullptr)
635  {
636  const TNumType& arcNow = (*itMinPtr)->arc;
637  if (first)
638  {
639  (this->*advanceFunc)(arcNow);
640  first = false;
641  }
642  else if (simulationLattice_[simLatticeIdx_].arc < arcNow)
643  {
644  (this->*advanceFunc)(arcNow);
645  }
646  ++simLatticeIdx_;
647  auto& simLatticeI = simulationLattice_[simLatticeIdx_];
648  simLatticeI.arc = arcNow;
649  simLatticeI.latticeIdx = (*(*itMinPtr)).latticeIdx;
650  (*(*itMinPtr)).statePtr = simLatticeI.statePtr;
651  if ((sizeCostsPerPartLattice_[simLatticeI.latticeIdx] > 0) || (_saveLatticeStates))
652  {
653  stateSim_->setXCf(arcNow, PfEaG::NEAR_LAST);
654  if (simulatingWithGrad)
655  {
656  stateSim_->setGradXCf(arcNow, PfEaG::NEAR_LAST);
657  stateSim_->setXCfDot(arcNow, PfEaG::NEAR_LAST);
658  setXNmDot(arcNow);
660  correctStateGradFunc[simLatticeI.latticeIdx](*stateSim_, arcNow, latVecIdx,
661  simLatticeI.statePtr->stateGrad());
662  }
663  else
664  {
666  }
667  }
668  (*itMinPtr)++;
669  getMinArcLatCacheIdx(itMinPtr, latVecIdx);
670  }
671  for (size_t i = ++simLatticeIdx_; i < simulationLattice_.size(); ++i)
672  {
673  simulationLattice_[i].arc = -1;
674  }
675  return simLatticeIdx_;
676  }
677 
678 private:
679  void resizeSimLattice(const size_t& _totalPartLatticePtsSize)
680  {
681  bool forceResize = false;
682  bool resize = false;
683  if ((_totalPartLatticePtsSize > simulationLattice_.size()))
684  {
685  forceResize = true;
686  resize = true;
687  }
688  else if (simulationLattice_.size() > 0)
689  {
690  if (stateSim_->state().data().size() != simulationLattice_[0].statePtr->data().size())
691  {
692  forceResize = true;
693  }
694  }
695  else
696  {
697  forceResize = true;
698  }
699  if (resize)
700  {
701  simulationLattice_.resize(2 * _totalPartLatticePtsSize);
702  }
703  if (forceResize)
704  {
705  for (auto& simLatticeI : simulationLattice_)
706  {
708  }
709  }
710  }
711 
712 private:
713  template <typename TSimType2, typename TLatticePointType, bool canComputeStateGrad = CanComputeStateGrad,
714  typename std::enable_if<(!canComputeStateGrad)>::type* = nullptr>
715  void copyReqDataFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
716  {
717  copyDataFromSimToSimLatticeI(_sim, _latticePtI);
718  }
719 
720 private:
721  template <typename TSimType2, typename TLatticePointType, bool canComputeStateGrad = CanComputeStateGrad,
722  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
723  void copyReqDataFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
724  {
725  copyDataFromSimToSimLatticeI(_sim, _latticePtI);
726  if (simulatingWithGrad)
727  {
728  copyGradDataFromSimToSimLatticeI(_sim, _latticePtI);
729  }
730  }
731 
732 private:
733  template <typename TSimType2, typename TLatticePointType, bool useStateSimNm = TUseStateNm,
734  typename std::enable_if<(useStateSimNm)>::type* = nullptr>
735  static void copyDataNmFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
736  {
737  auto& latticeState = *_latticePtI.statePtr;
738  const auto& simState = _sim.state();
739  latticeState.stateNm().data() = simState.stateNm().data();
740  }
741 
742 private:
743  template <typename TSimType2, typename TLatticePointType, bool useStateSimNm = TUseStateNm,
744  typename std::enable_if<(!useStateSimNm)>::type* = nullptr>
745  static void copyDataNmFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
746  {
747  }
748 
749 private:
750  template <typename TSimType2, typename TLatticePointType, bool useStateSimNm = TUseStateNm,
751  typename std::enable_if<(useStateSimNm)>::type* = nullptr>
752  static void copyGradDataNmFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
753  {
754  auto& latticeState = *_latticePtI.statePtr;
755  const auto& simState = _sim.state();
756  latticeState.stateGradNm().data() = simState.stateGradNm().data();
757  }
758 
759 private:
760  template <typename TSimType2, typename TLatticePointType, bool useStateSimNm = TUseStateNm,
761  typename std::enable_if<(!useStateSimNm)>::type* = nullptr>
762  static void copyGradDataNmFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
763  {
764  }
765 
766 private:
767  template <typename TSimType2, typename TLatticePointType>
768  static void copyDataFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
769  {
770  auto& latticeState = *_latticePtI.statePtr;
771  const auto& simState = _sim.state();
772  latticeState.stateCf().data() = simState.stateCf().data();
773  copyDataNmFromSimToSimLatticeI(_sim, _latticePtI);
774  }
775 
776 private:
777  template <typename TSimType2, typename TLatticePointType, bool canComputeStateGrad = CanComputeStateGrad,
778  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
779  static void copyGradDataFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
780  {
781  auto& latticeState = *_latticePtI.statePtr;
782  const auto& simState = _sim.state();
783  latticeState.stateGradCf().data() = simState.stateGradCf().data();
784  copyGradDataNmFromSimToSimLatticeI(_sim, _latticePtI);
785  }
786 
787 private:
788  template <typename TSimType2, typename TLatticePointType, bool canComputeStateGrad = CanComputeStateGrad,
789  typename std::enable_if<(!canComputeStateGrad)>::type* = nullptr>
790  static void copyStructureFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
791  {
792  auto& latticeState = *_latticePtI.statePtr;
793  const auto& simState = _sim.state();
794  latticeState.stateCf() = simState.stateCf();
795  latticeState.stateNm() = simState.stateNm();
796  }
797 
798 private:
799  template <typename TSimType2, typename TLatticePointType, bool canComputeStateGrad = CanComputeStateGrad,
800  typename std::enable_if<(canComputeStateGrad)>::type* = nullptr>
801  static void copyStructureFromSimToSimLatticeI(const TSimType2& _sim, TLatticePointType& _latticePtI)
802  {
803  auto& latticeState = *_latticePtI.statePtr;
804  const auto& simState = _sim.state();
805  latticeState.stateCf() = simState.stateCf();
806  latticeState.stateNm() = simState.stateNm();
807  latticeState.stateGradNm() = simState.stateGradNm();
808  latticeState.stateGradCf() = simState.stateGradCf();
809  latticeState.stateGrad().bindMat();
810  }
812 protected:
815 private:
816  std::vector<LatticePointType> simulationLattice_;
817 
818 private:
821 public:
822  std::tuple<TLatticeTypes<TNumType, TSimType>...> partialLattices_;
823 
824 public:
825  std::array<std::function<void(const TSimType&, const TNumType&, const size_t&,
826  typename StateMapBaseTraits<typename StateType::StateMapBaseType>::StateGradType&)>,
827  sizeof...(TLatticeTypes)> correctStateGradFunc;
828 
829 public:
831 
832 public:
834 
835 public:
836 #ifdef USE_MAP_ALIGNMENT
837  std::array<std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>>,
838  CostFuncsTypesNr> gradCostsMap_;
839 #else
840  std::array<std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>>,
841  CostFuncsTypesNr> gradCostsMap_;
842 #endif
843 
844 private:
845  std::array<size_t, sizeof...(TLatticeTypes)> sizeCosts_;
846 
847 private:
848  std::array<size_t, sizeof...(TLatticeTypes)> sizeCostsPerPartLattice_;
849 
850 private:
851  std::array<size_t, CostFuncsTypesNr> sizeCostsPerType_;
852 };
853 }
854 
855 #endif // TRAJECTORY_SIMULATOR_HPP
void simulateTrajectoryImpl(const bool &_saveLatticeStates=false)
LatticePoint(TNumType _arc, StateSPtr _statePtr)
void evaluateWithGrad(const auto &_x, const size_t &_i, const auto &_gradX, TSimType &_sim, auto &_ansPtr, auto &_ansGradPtr, const size_t &elSize)
std::shared_ptr< State > StateSPtr
Definition: state.h:129
std::vector< NumType > latticeArcs_
void computeLatticeArcs(TSimType &_sim, std::vector< NumType > &_latticeArcs)
std::array< size_t, sizeof...(TLatticeTypes)> sizeCostsPerPartLattice_
void getMinArcLatCacheIdx(typename std::vector< LatticePointType >::iterator *&_itMin, size_t &_latVecIdx)
void resizeInternal()
std::vector< LatticePoint< NumType, StateType > >::iterator latticeVecCacheIter_
void bindAdvanceFuncGrad()
static void copyGradDataNmFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
std::vector< LatticePointType > simulationLattice_
Lattice requesting each simulated trajectory state.
void resizeCosts(const size_t &_optParamSize, const bool &_simulatingWithGrad)
StateMapArray< TNumType, StateMapVector< TNumType, StateMapVector< TNumType, TNumType > >, CostFuncsTypesNr > gradCosts_
std::array< std::shared_ptr< Eigen::Map< Eigen::Matrix< TNumType,-1,-1, Eigen::RowMajor > > >, CostFuncsTypesNr > gradCostsMap_
Structure containing an evaluation arc and a state pointer.
void simulateTrajectoryWithGrad(const bool &_saveLatticeStates=false)
void copyReqDataFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
std::array< size_t, CostFuncsTypesNr > sizeCostsPerType_
std::array< std::function< void(const TSimType &, const TNumType &, const size_t &, typename StateMapBaseTraits< typename StateType::StateMapBaseType >::StateGradType &)>, sizeof...(TLatticeTypes)> correctStateGradFunc
void bindAdvanceFunc()
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple(std::tuple< Tp... > &, FuncT)
Definition: utils.h:101
static constexpr const size_t costFuncsNr()
constexpr std::enable_if<(II==IIMax), void >::type eval_all_costs(TNumType *&_cfPtr, TNumType *&_cfGradPtr, const size_t &_optParamSize)
static void copyDataFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
std::shared_ptr< TStateType > StateSPtr
size_t bindFromPartLatticesToSimLattice(const bool &_saveLatticeStates=false)
void simulateTrajectory(const bool &_saveLatticeStates=false)
std::array< size_t, sizeof...(TLatticeTypes)> sizeCosts_
size_t populatePartLattices(size_t &_sizeCosts)
std::vector< LatticePoint< NumType, StateType > > lattice
static constexpr auto value
Definition: utils.h:205
void advanceFuncSimGrad(const TNumType &_arcNow)
std::shared_ptr< StateSim > StateSimSPtr
Definition: state_sim.h:54
std::tuple< TLatticeCostFuncs... > costFuncs_
size_t simulationLatticeActiveSize_
void computeLatticeStructure(TSimType &_sim, const size_t _latticeIdx)
StateMapArray< TNumType, StateMapVector< TNumType, TNumType >, CostFuncsTypesNr > costs_
void setXNmDot(const TNumType &_arcNow)
static void copyDataNmFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple_class(std::tuple< Tp... > &, FuncT &)
close to previous evaluation arc
void resizeSimLattice(const size_t &_totalPartLatticePtsSize)
static void copyGradDataFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
void evaluateCosts(TNumType *&_cfPtr, TNumType *&_cfGradPtr, const size_t &_optParamSize)
LatticePoint(StateSPtr _statePtr, const size_t &_latticeIdx)
const TDerived & thisDerived() const
std::vector< LatticePoint< NumType, StateType > >::iterator latticeVecIterEnd_
void evaluate(const auto &_x, const size_t &_i, TSimType &_sim, auto &_ansPtr)
static void copyStructureFromSimToSimLatticeI(const TSimType2 &_sim, TLatticePointType &_latticePtI)
static constexpr const size_t costFuncsTypesNr()
LatticePoint(StateSPtr _statePtr)
StateSimSPtr stateSim_
State simulator object.
std::tuple< TLatticeTypes< TNumType, TSimType >... > partialLattices_
Vector containing the ordered sequence of arc parametrizations for each of the used lattices...


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