33 #ifndef TRAJECTORY_SIMULATOR_HPP 34 #define TRAJECTORY_SIMULATOR_HPP 51 template <
typename TNumType,
typename TSimType>
52 class TrajectorySimulator
68 !std::is_same<EmptyGradType, typename StateMapBaseTraits<StateType>::StateWithGradNmType>
::value;
120 :
LatticePoint(_arc, _statePtr), latticeType(_latticeType)
192 const TNumType&
dt()
const 204 const TNumType&
ds()
const 231 for (
size_t i = 0; i < _userDefLattices.size(); ++i)
269 simulatingWithGrad =
false;
275 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
279 simulatingWithGrad =
true;
291 const TNumType arcParamMax =
stateSim_->paramFuncs()->funcsArcEnd();
299 static std::vector<TNumType> dsLattice;
303 stateSim_->paramFuncsDist()->computeS2TLattice(0,
ds(), dsLattice);
304 partLatticeDs->resize(dsLattice.size());
305 for (
size_t i = 0; i < dsLattice.size(); ++i)
307 partLatticeDs->at(i).arc = dsLattice[i];
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)
317 if (partLatticeDs->at(i).arc > firstDsLattice + 1e-3)
319 idxFirstInvalidDs = --i;
323 partLatticeDs->resize(idxFirstInvalidDs + dsLattice.size());
324 for (
size_t i = 0; i < dsLattice.size(); ++i)
326 partLatticeDs->at(i + idxFirstInvalidDs).arc = dsLattice[i];
335 size_t firstLaticeInvalidIdx = 0;
352 size_t itEnd = (int)_arcParamMax /
dt() + 1;
354 for (
size_t aPLIdx = _firstLaticeInvalidIdx + 1; aPLIdx < itEnd; ++aPLIdx)
370 size_t arcParamLatticeIdx = std::max(0, (
int)(_minArcLatticeVal /
dt()));
371 size_t minArcLatCacheIdx =
376 const size_t deltaArcParamLattice = std::max(0, (
int)(_minArcLatticeVal /
dt()) - (
int)arcParamLatticeIdx);
377 const size_t simLatticeInjectEnd = ++_firstLaticeInvalidIdx + deltaArcParamLattice;
379 for (; _firstLaticeInvalidIdx < simLatticeInjectEnd; ++_firstLaticeInvalidIdx)
388 if (idxMinLatticePt + 1 < (
int)
partLattices_[minArcLatCacheIdx]->size())
394 const TNumType simLatticeInjectEnd = _arcParamMax /
dt();
395 ++arcParamLatticeIdx;
396 for (; arcParamLatticeIdx < simLatticeInjectEnd; ++arcParamLatticeIdx)
409 TNumType minArc = FLT_MAX;
410 for (
size_t iPart = extArcLatIdxBegin; iPart <
partLattices_.size(); ++iPart)
437 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
444 auto&
dtIdpNm(
const size_t _i,
const TNumType& _arcNow,
const int& _latticePtType)
const 446 static Eigen::Matrix<TNumType, 1, -1> ans;
447 ans.resize(
stateSim_->state().stateGradNm().sub(0).data().size());
457 auto&
dtIdpCf(
const size_t _i,
const TNumType& _arcNow,
const int& _latticePtType)
const 459 static Eigen::Matrix<TNumType, 1, -1> ans;
460 ans.resize(
stateSim_->state().stateGradNm().sub(0).data().size());
462 if (_arcNow ==
stateSim_->paramFuncs()->funcsArcEnd())
470 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
471 typename std::enable_if<(!canComputeStateGrad)>::type* =
nullptr>
474 _state.stateCf().data() = _simState.stateCf().data();
475 _state.stateNm().data() = _simState.stateNm().data();
479 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
480 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
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();
490 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
491 typename std::enable_if<(!canComputeStateGrad)>::type* =
nullptr>
494 _state.stateCf() = _simState.stateCf();
495 _state.stateNm() = _simState.stateNm();
499 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
500 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
503 _state.stateCf() = _simState.stateCf();
504 _state.stateNm() = _simState.stateNm();
505 _state.stateGradNm() = _simState.stateGradNm();
506 _state.stateGradCf() = _simState.stateGradCf();
510 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
511 typename std::enable_if<(!canComputeStateGrad)>::type* =
nullptr>
514 _simState.stateCf().data() = _state.stateCf().data();
515 _simState.stateNm().data() = _state.stateNm().data();
519 template <
typename TSimState,
typename TState,
bool canComputeStateGrad =
CanComputeStateGrad,
520 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
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();
532 void simAppendToSimPartLat(
const TNumType& _arcNow,
const int& _latticePtType,
const std::size_t& _minArcLatCacheIdx)
539 auto& stateSimState =
stateSim_->state();
542 if (simulatingWithGrad)
549 static Eigen::Matrix<TNumType, -1, -1> fCfdtIdpVal;
550 static Eigen::Matrix<TNumType, -1, -1> fNmdtIdpVal;
552 fCfdtIdpVal = (
stateSim_->stateCfDotCache_.data() * dtIdpVal).transpose();
553 fNmdtIdpVal = (
stateSim_->stateNmDotCache_.data() * dtIdpVal).transpose();
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());
636 void appendToPartLat(
const TNumType& _arcNow,
const int& _latticePtType,
const std::size_t& _minArcLatCacheIdx)
639 partLatticeNow.arc = _arcNow;
706 if (_firstLaticeInvalidIdx == 0)
713 stateSim_->toState(*lastValidSimState.statePtr, lastValidSimState.arc);
722 for (
size_t iPart = extArcLatIdxBegin; iPart <
partLattices_.size(); ++iPart)
744 const TNumType arcParamMax =
stateSim_->paramFuncs()->funcsArcEnd();
749 for (
size_t i = extArcLatIdxBegin; i <
partLattices_.size(); ++i)
753 simLatticeSize = std::max(simLatticeSize, (
size_t)0);
754 bool forceResize =
false;
787 TNumType minArcLatticeVal = 0;
818 bool emptyExtArcLat =
true;
819 for (
size_t i = extArcLatIdxBegin; i <
partLattices_.size(); ++i)
823 emptyExtArcLat =
false;
827 return emptyExtArcLat;
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)
size_t simLatticeSize() const
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...
size_t simulationLatticeActiveSize_
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.
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
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
begin and end of the simulation lattice
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)
size_t getMinArcLatCacheIdx() const
TrajectorySimulator & operator=(const TrajectorySimulator &)=default
TNumType & dtBase()
Reference to arc parametrization interval used for the equal arc-length lattice.
static constexpr auto value
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 updateUserDefLattice()
LatticePoint(bool _makePtrShared)
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.
LatticePoint(TNumType _arc)
AdvanceFunction advanceFunc
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)
void updateUserDefLattice()
LatticePoint(TNumType _arc, StateSPtr &_statePtr)