trajectory_simulator_precalc.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 #include <iostream>
38 
39 using namespace std;
40 using namespace tuw;
41 
43 
44 TrajectorySimulatorPrecalc::TrajectorySimulatorPrecalc(StateSimPtr _stateSim) : TrajectorySimulator(_stateSim)
45 {
46 }
48  unique_ptr<CostsEvaluatorClass> _costsEvaluator)
49  : TrajectorySimulator(_stateSim, std::move(_costsEvaluator))
50 {
51 }
52 
54 {
57 
58  // resize equal dt lattice
59  const double arcParamMax = stateSim_->paramFuncs()->funcsArcEnd();
60  size_t simLatticeSize = max(0, (int)(ceil(arcParamMax / dt()) + 1));
61  partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DT))]->resize(simLatticeSize, LatticePoint(FLT_MAX));
62 
63  // set the dist-extended sim lattice points on the last lattice entries
64  if (canComputeDistLattice_ && (ds() > 0))
65  {
66  static vector<double> dsLattice;
67  auto& partLatticeDs = partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DS))];
68  if (scaleDs_ || scaleDt_)
69  {
70  stateSim_->paramFuncsDist()->computeS2TLattice(0, ds(), dsLattice);
71  partLatticeDs->resize(dsLattice.size());
72  for (size_t i = 0; i < dsLattice.size(); ++i)
73  {
74  partLatticeDs->at(i).arc = dsLattice[i];
75  }
76  }
77  else
78  {
79  stateSim_->paramFuncsDist()->computeS2TLattice(_lastValidArc, ds(), dsLattice);
80  const double& firstDsLattice = dsLattice[0];
81  size_t idxFirstInvalidDs = max(0, (int)partLatticeDs->size() - 2);
82  for (size_t i = 1; i < partLatticeDs->size(); ++i)
83  {
84  if (partLatticeDs->at(i).arc > firstDsLattice + 1e-3)
85  {
86  idxFirstInvalidDs = --i;
87  break;
88  }
89  }
90  partLatticeDs->resize(idxFirstInvalidDs + dsLattice.size());
91  for (size_t i = 0; i < dsLattice.size(); ++i)
92  {
93  partLatticeDs->at(i + idxFirstInvalidDs).arc = dsLattice[i];
94  }
95  }
96  }
97  else
98  {
99  partLattices_[lattTypeIdx(asInt(BSLT::LATTICE_ARC_EQ_DS))]->clear();
100  }
101 
102  size_t firstLaticeInvalidIdx = 0;
103  if (initSimLatticeState0(_lastValidArc, firstLaticeInvalidIdx))
104  {
105  populateTrajSimPartLattice(firstLaticeInvalidIdx);
106  if (costsEvaluator_)
107  {
108  costsEvaluator_->evaluateAllCosts();
109  }
110  }
111 }
112 
113 void TrajectorySimulatorPrecalc::populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, double _arcParamMax,
114  double _minArcLatticeVal)
115 {
116  setBeginEndArcsToLattices(0, _arcParamMax);
117  initExtLatticeCache(_minArcLatticeVal);
118 
119  size_t arcParamLatticeIdx = max(0, (int)(_minArcLatticeVal / dt()));
120  size_t minArcLatCacheIdx =
121  getMinArcLatCacheIdx(); // find lattice type index of next smallest required simulation lattice value
122  while ((_minArcLatticeVal = partLattices_[minArcLatCacheIdx]->at(partLatIdxCache_[minArcLatCacheIdx]).arc) <
123  _arcParamMax)
124  {
125  const size_t deltaArcParamLattice = max(0, (int)(_minArcLatticeVal / dt()) - (int)arcParamLatticeIdx);
126  const size_t simLatticeInjectEnd = ++_firstLaticeInvalidIdx + deltaArcParamLattice;
127 
128  for (; _firstLaticeInvalidIdx < simLatticeInjectEnd; ++_firstLaticeInvalidIdx)
129  { // push_back the equal time lattice points before the extended one first
130  simAppendToSimPartLat(++arcParamLatticeIdx * dt(), asInt(BSLT::LATTICE_ARC_EQ_DT), arcParamLatticeIdx);
131  }
132  simAppendToSimPartLat(_minArcLatticeVal, (int)minArcLatCacheIdx - asInt(BSLT::LATTICE_ENUM_SIZE),
133  partLatIdxCache_[minArcLatCacheIdx]); // push back extended lattice point
134 
135  int& idxMinLatticePt = partLatIdxCache_[minArcLatCacheIdx];
136  if (idxMinLatticePt + 1 < (int)partLattices_[minArcLatCacheIdx]->size())
137  {
138  ++idxMinLatticePt;
139  }
140  minArcLatCacheIdx = getMinArcLatCacheIdx(); // update the minimum extended lattice point
141  }
142  const double simLatticeInjectEnd = _arcParamMax / dt();
143  ++arcParamLatticeIdx;
144  for (; arcParamLatticeIdx < simLatticeInjectEnd; ++arcParamLatticeIdx)
145  {
146  simAppendToSimPartLat(arcParamLatticeIdx * dt(), asInt(BSLT::LATTICE_ARC_EQ_DT), arcParamLatticeIdx);
147  }
148 
149  setEndStateToLattices(_arcParamMax);
150 }
151 
153 {
154  size_t idxMin = 0;
155  double minArc = FLT_MAX;
156  for (size_t iPart = extArcLatIdxBegin; iPart < partLattices_.size(); ++iPart)
157  {
158  if (partLattices_[iPart]->empty())
159  {
160  continue;
161  }
162  const double& arcI = partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc;
163  if (minArc > arcI)
164  {
165  minArc = arcI;
166  idxMin = iPart;
167  }
168  }
169  return idxMin;
170 }
StateSimPtr stateSim_
State simulator object.
LatticeVecSPtrVec partLattices_
Vector containing the ordered sequence of arc parametrizations for each of the used lattices...
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.
void populatePartSimLatticesGeneral(size_t _firstLaticeInvalidIdx, double _arcParamMax, double _minArcLatticeVal) override
Performs simulation and populates simulation and partial lattices in the general case of various enab...
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.
std::unique_ptr< CostsEvaluatorClass > costsEvaluator_
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
size_t getMinArcLatCacheIdx() const
Returns the partial lattice cached index that has the smallest arc.
bool initSimLatticeState0(const double &_lastValidArc, size_t &_firstLaticeInvalidIdx)
Initializes the simulation lattice (truncation from the.
BaseSimLatticeType
Fundamental lattice types.
static constexpr const std::size_t extArcLatIdxBegin
Structure containing an evaluation arc and a state pointer.
bool canComputeDistLattice_
Flags if the StateSim object has access to the StateSimDist functionality.
TrajectorySimulator(StateSimPtr _stateSim)
void setEndStateToLattices(const double &_arcEnd)
Binds reference to the final simulated state (at.
void simulateTrajectory(double _lastValidArc=0) override
Simulates (discrete numerical evaluation) an entire trajectory according to the specified intervals a...
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.
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