trajectory_simulator.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 #include <iostream>
39 
40 using namespace std;
41 using namespace tuw;
42 
44 
45 TrajectorySimulator::TrajectorySimulator(StateSimPtr _stateSim)
46  : stateSim_(_stateSim)
47  , partLattices_(asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE))
48  , partLatIdxCache_(asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE))
49  , canComputeDistLattice_(stateSim_->paramFuncsDist() != nullptr)
50  , dtBase_(-1)
51  , dsBase_(-1)
52  , scaleDt_(false)
53  , scaleDs_(false)
54 {
55  for (size_t i = 0; i < partLattices_.size(); ++i)
56  {
57  partLattices_[i] = shared_ptr<LatticeVec>(new LatticeVec);
58  }
60 }
61 
63  unique_ptr<TrajectorySimulator::CostsEvaluatorClass> _costsEvaluator)
64  : TrajectorySimulator(_stateSim)
65 {
66  costsEvaluator_ = std::move(_costsEvaluator);
68 }
69 
70 void TrajectorySimulator::setUserDefLattice(const vector<vector<double*> >& _userDefLattices)
71 {
72  userDefPartLattices_ = _userDefLattices;
73  partLattices_.resize(asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE) + _userDefLattices.size());
74  for (size_t i = asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE); i < partLattices_.size(); ++i)
75  {
76  partLattices_[i] = shared_ptr<LatticeVec>(new LatticeVec);
77  }
78  partLatIdxCache_.resize(partLattices_.size());
79  for (size_t i = 0; i < _userDefLattices.size(); ++i)
80  {
81  partLattices_[lattTypeIdx(i)]->resize(_userDefLattices[i].size());
82  }
84  if (costsEvaluator_)
85  {
87  }
88 }
89 
91 {
92  partLatIdxCache_.resize(partLattices_.size());
93  for (size_t i = 0; i < userDefPartLattices_.size(); ++i)
94  {
95  for (size_t j = 0; j < userDefPartLattices_[i].size(); ++j)
96  {
97  partLattices_[lattTypeIdx(i)]->at(j).arc = *userDefPartLattices_[i][j];
98  }
99  }
100 }
101 
102 void TrajectorySimulator::simAppendToSimPartLat(const double& _arcNow, const int& _latticePtType,
103  const std::size_t& _minArcLatCacheIdx)
104 {
105  if ((simulationLattice_.size() == 0) || (simulationLattice_.back().arc < _arcNow))
106  {
107  stateSim_->advance(_arcNow);
108  StateSPtr stateNow = stateSim_->cloneState();
109  simulationLattice_.emplace_back(LatticePointType(_arcNow, _latticePtType, stateNow));
110  }
111  appendToPartLat(_arcNow, _latticePtType, _minArcLatCacheIdx);
112 }
113 void TrajectorySimulator::appendToPartLat(const double& _arcNow, const int& _latticePtType,
114  const std::size_t& _minArcLatCacheIdx)
115 {
116  auto& partLatticeNow = partLattices_[lattTypeIdx(_latticePtType)]->at(_minArcLatCacheIdx);
117  partLatticeNow.arc = _arcNow;
118  partLatticeNow.statePtr = simulationLattice_.back().statePtr;
119 }
120 
121 void TrajectorySimulator::setBeginStateToLattices(const double& _arcBegin)
122 {
123  simAppendToSimPartLat(_arcBegin, asInt(BSLT::ARC_BG_BK), 0);
124  for (size_t i = lattTypeIdx(asInt(BSLT::ARC_BG_BK)); i < partLattices_.size(); ++i)
125  {
126  if (!partLattices_[i]->empty())
127  {
128  partLattices_[i]->at(0).statePtr = simulationLattice_.back().statePtr;
129  }
130  }
131 }
132 
134 {
135  simAppendToSimPartLat(_arcEnd, asInt(BSLT::ARC_BG_BK),
136  min(partLattices_[lattTypeIdx(asInt(BSLT::ARC_BG_BK))]->size() - 1, (size_t)1));
137  for (size_t i = lattTypeIdx(asInt(BSLT::ARC_BG_BK)); i < partLattices_.size(); ++i)
138  {
139  if (!partLattices_[i]->empty())
140  {
141  auto& partLatI0 = partLattices_[i]->back();
142  partLatI0.statePtr = simulationLattice_.back().statePtr;
143  }
144  }
145 }
146 
147 void TrajectorySimulator::setBeginEndArcsToLattices(const double& _arcBegin, const double& _arcEnd)
148 {
149  for (size_t i = lattTypeIdx(asInt(BSLT::ARC_BG_BK)); i < partLattices_.size(); ++i)
150  {
151  if (!partLattices_[i]->empty())
152  {
153  partLattices_[i]->at(0).arc = _arcBegin;
154  partLattices_[i]->back().arc = _arcEnd;
155  }
156  }
157 }
158 
160 {
161  return a.arc < b.arc;
162 }
163 
164 bool TrajectorySimulator::initSimLatticeState0(const double& _lastValidArc, size_t& _firstLaticeInvalidIdx)
165 {
166  if ((simulationLattice_.size() > 0) && (_lastValidArc > 0))
167  {
168  _firstLaticeInvalidIdx = std::upper_bound(simulationLattice_.begin(), simulationLattice_.end(),
169  LatticePoint(_lastValidArc), cmpLatticePt) -
170  simulationLattice_.begin();
171  if (_firstLaticeInvalidIdx >= simulationLattice_.size())
172  {
173  return false;
174  }
175  }
176  simulationLattice_.erase(simulationLattice_.begin() + _firstLaticeInvalidIdx, simulationLattice_.end());
177  if (_firstLaticeInvalidIdx == 0)
178  {
179  stateSim_->toState0();
180  }
181  else if (_firstLaticeInvalidIdx <= simulationLattice_.size())
182  {
183  StateSPtr st = simulationLattice_[_firstLaticeInvalidIdx - 1].statePtr->cloneState();
184  stateSim_->setState(st);
186  }
187  return true;
188 }
189 
190 void TrajectorySimulator::initExtLatticeCache(const double& _minArcLatticeVal)
191 {
192  partLatIdxCache_.assign(partLattices_.size(), 0);
193  for (size_t iPart = extArcLatIdxBegin; iPart < partLattices_.size(); ++iPart)
194  {
195  if (partLattices_[iPart]->empty())
196  {
197  partLatIdxCache_[iPart] = -1;
198  continue;
199  }
200  for (; partLatIdxCache_[iPart] < (int)partLattices_[iPart]->size(); ++partLatIdxCache_[iPart])
201  {
202  if (partLattices_[iPart]->at(partLatIdxCache_[iPart]).arc > _minArcLatticeVal)
203  {
204  ++partLatIdxCache_[iPart];
205  break;
206  }
207  }
208  partLatIdxCache_[iPart] = max(0, (int)partLatIdxCache_[iPart] - 1);
209  }
210 }
211 
212 void TrajectorySimulator::populateTrajSimPartLattice(const size_t& _firstLaticeInvalidIdx)
213 {
214  const double arcParamMax = stateSim_->paramFuncs()->funcsArcEnd();
215  size_t simLatticeSize = max(0, (int)(ceil(arcParamMax / dt()) + 1));
216 
217  partLattices_[lattTypeIdx(asInt(BSLT::ARC_BG_BK))]->resize(2);
218  // reserve maximum computable lattice points
219  for (size_t i = extArcLatIdxBegin; i < partLattices_.size(); ++i)
220  {
221  simLatticeSize += partLattices_[i]->size();
222  }
223  simLatticeSize = max(simLatticeSize, (size_t)0);
224  simulationLattice_.reserve(simLatticeSize);
225 
226  // compute equal time lattice initial value and multiplier; if init, push_back the 0 lattice point
227  double minArcLatticeVal = 0;
228  if ((_firstLaticeInvalidIdx == 0) || (simulationLattice_.size() == 0))
229  {
230  setBeginStateToLattices(minArcLatticeVal);
231  }
232  else
233  {
234  minArcLatticeVal = simulationLattice_.back().arc;
235  }
236 
237  if (costsEvaluator_)
238  {
239  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::F);
240  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::G);
241  costsEvaluator_->resetCostFunctions(CostEvaluatorCostType::H);
242  }
243 
244  if (isEmptyAllExtLattices() && (dt() > 0) && (ds() <= 0))
245  {
246  populatePartSimLatticesDtOnly(_firstLaticeInvalidIdx, arcParamMax);
247  } // case when only a dt lattice is available
248  else
249  {
250  populatePartSimLatticesGeneral(_firstLaticeInvalidIdx, arcParamMax, minArcLatticeVal);
251  } // general case
252 }
253 
254 void TrajectorySimulator::populatePartSimLatticesDtOnly(const size_t& _firstLaticeInvalidIdx, double _arcParamMax)
255 {
256  setBeginEndArcsToLattices(0, _arcParamMax);
257 
258  size_t itEnd = (int)_arcParamMax / dt() + 1;
259  // push back DT lattice points
260  for (size_t aPLIdx = _firstLaticeInvalidIdx + 1; aPLIdx < itEnd; ++aPLIdx)
261  {
262  simAppendToSimPartLat(aPLIdx * dt(), asInt(BSLT::LATTICE_ARC_EQ_DT), aPLIdx);
263  }
264 
265  // push back final lattice point
266  setEndStateToLattices(_arcParamMax);
267 }
268 
270 {
271  return _enumIdx + asInt(BaseSimLatticeType::LATTICE_ENUM_SIZE);
272 }
273 
275 {
276  bool emptyExtArcLat = true;
277  for (size_t i = extArcLatIdxBegin; i < partLattices_.size(); ++i)
278  {
279  if (!partLattices_[i]->empty())
280  {
281  emptyExtArcLat = false;
282  break;
283  }
284  }
285  return emptyExtArcLat;
286 }
287 
289 {
290  dt_ = dtBase_;
291  if (scaleDt_)
292  {
293  dt_ *= stateSim_->paramFuncs()->funcsArcEnd();
294  }
295  ds_ = dsBase_;
296  if (scaleDs_)
297  {
298  stateSim_->paramFuncs()->setEvalArc(stateSim_->paramFuncs()->funcsArcEnd());
299  ds_ *= stateSim_->paramFuncsDist()->computeS();
300  stateSim_->paramFuncs()->setEvalArc(stateSim_->paramFuncs()->funcsArcBegin());
301  }
302 }
303 
304 void TrajectorySimulator::setBoolDtScale(const bool& _doScale)
305 {
306  scaleDt_ = _doScale;
307 }
308 void TrajectorySimulator::setBoolDsScale(const bool& _doScale)
309 {
310  scaleDs_ = _doScale;
311 }
312 
314 {
315  return dtBase_;
316 }
317 const double& TrajectorySimulator::dt() const
318 {
319  return dt_;
320 }
322 {
323  return dsBase_;
324 }
325 const double& TrajectorySimulator::ds() const
326 {
327  return ds_;
328 }
330 {
331  return stateSim_;
332 }
334 {
335  return stateSim_;
336 }
337 vector<TrajectorySimulator::LatticePointType>& TrajectorySimulator::simLattice()
338 {
339  return simulationLattice_;
340 }
341 const vector<TrajectorySimulator::LatticePointType>& TrajectorySimulator::simLattice() const
342 {
343  return simulationLattice_;
344 }
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.
static bool cmpLatticePt(const TrajectorySimulator::LatticePoint &a, const TrajectorySimulator::LatticePoint &b)
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.
double & dtBase()
Reference to 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 setBoolDsScale(const bool &_doScale)
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
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.
double dt_
Arc parametrization interval used for the equal arc-length lattice.
std::vector< std::vector< double * > > userDefPartLattices_
std::vector< LatticePointType > & simLattice()
Reference to the lattice that requested each simulated trajectory state.
virtual void populatePartSimLatticesDtOnly(const size_t &_firstLaticeInvalidIdx, double _arcParamMax)
Performs simulation and populates simulation and partial lattice when only equal dt lattice is enable...
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 constexpr const std::size_t extArcLatIdxBegin
Structure containing an evaluation arc and a state pointer.
TrajectorySimulator(StateSimPtr _stateSim)
void setBoolDtScale(const bool &_doScale)
void setEndStateToLattices(const double &_arcEnd)
Binds reference to the final simulated state (at.
void setUserDefLattice(const std::vector< std::vector< double * > > &_userDefLattices)
const double & dt() const
Const reference to arc parametrization interval used for the equal arc-length lattice.
std::shared_ptr< StateSim > StateSimPtr
double ds_
Arc parametrization interval used for the equal distance lattice.
StateSimPtr stateSim()
Reference of the state simulator object.
void simAppendToSimPartLat(const double &_arcNow, const int &_latticePtType, const std::size_t &_minArcLatCacheIdx)
Performs a simulation step (if.
std::shared_ptr< StateType > StateSPtr
double & dsBase()
Reference to 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.
StateSimSPtr stateSim_
State simulator object.


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