33 #ifndef TRAJECTORY_SIMULATOR2_HPP 34 #define TRAJECTORY_SIMULATOR2_HPP 47 template <
typename TNumType,
typename TStateType>
76 template <
class TDerived,
typename TNumType,
class TSimType,
class... TLatticeCostFuncs>
109 computeLatticeArcs(_sim, latticeArcs_);
110 const size_t& latticeSize = latticeArcs_.size();
111 if (latticeSize != lattice.size())
114 latticeVecIterEnd_ = lattice.end();
116 latticeVecCacheIter_ = lattice.begin();
117 for (
size_t i = 0; i < latticeSize; ++i)
119 lattice[i].arc = latticeArcs_[i];
126 thisDerived().computeLatticeArcsImpl(_sim, latticeArcs_);
130 virtual void precompute(TSimType& _sim) = 0;
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>
137 return std::tuple_size<
typename std::tuple_element<FuncsNr, std::tuple<TLatticeCostFuncs...>>::type>
::value;
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>
151 return std::tuple_size<std::tuple<TLatticeCostFuncs...>>
::value;
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)
159 for_each_tuple(std::get<FuncsNr>(costFuncs_), [
this, &_x, &_sim, &_ansPtr, &_i](
auto& costFuncI)
161 *_ansPtr = costFuncI.f(_x, _i, _sim, this->thisDerived());
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)
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)
180 [
this, &_x, &_gradX, &_sim, &_ansPtr, &_ansGradPtr, &elSize, &_i](
auto& costFuncI)
182 *_ansPtr = costFuncI.f(_x, _i, _sim, this->thisDerived());
184 #ifdef USE_MAP_ALIGNMENT 185 Eigen::Map<Eigen::Matrix<TNumType, -1, 1>, MapAlignment> map(_ansGradPtr, elSize, 1);
187 Eigen::Map<Eigen::Matrix<TNumType, -1, 1>> map(_ansGradPtr, elSize, 1);
189 costFuncI.gradF(map, _x, _gradX, _i, _sim, this->thisDerived());
190 _ansGradPtr += elSize;
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)
205 return static_cast<TDerived&
>(*this);
211 return static_cast<const TDerived&
>(*this);
215 std::vector<LatticePoint<NumType, StateType>>
lattice;
229 template <
typename TNumType2,
typename TSimType2,
bool TUseStateNm2,
230 template <
typename,
typename>
class... TLatticeTypes2>
232 template <
typename T,
typename... TTuple>
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...>&,
242 template <std::size_t II = 0,
class FuncT,
typename... Tp>
243 constexpr
inline typename std::enable_if <
246 f[II] = [&t](
const auto& _a,
const auto& _b,
const auto& _c,
auto& _d)
248 std::get<II>(t).computeDArcIdPImpl(_a, _b, _c, _d);
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)
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)
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);
271 template <
typename TNumType,
typename TSimType,
bool TUseStateNm,
template <
typename,
typename>
class... TLatticeTypes>
278 using StateType =
typename TSimType::StateType;
281 using StateSPtr = std::shared_ptr<StateType>;
284 using StateForSimType =
typename TSimType::StateForSimType;
290 static constexpr
const bool CanComputeStateGrad =
291 !std::is_same<EmptyGradType, typename StateMapBaseTraits<StateType>::StateWithGradNmType>
::value;
294 static constexpr
const size_t CostFuncsTypesNr =
295 std::tuple_element<0, std::tuple<TLatticeTypes<TNumType, TSimType>...>>::type::costFuncsTypesNr();
303 #ifdef USE_MAP_ALIGNMENT 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));
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));
321 #ifdef USE_MAP_ALIGNMENT 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));
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));
345 LatticePointType& simLatticeI(
const size_t& _i)
351 const LatticePointType& simLatticeI(
const size_t& _i)
const 357 size_t simLatticeSize()
const 363 template <
template <
typename,
typename>
class TLatticeType>
364 TLatticeType<TNumType, TSimType>& partialLattice()
370 template <
template <
typename,
typename>
class TLatticeType>
371 const TLatticeType<TNumType, TSimType>& partialLattice()
const 377 template <
size_t TLatticeIdx>
378 auto& partialLattice()
384 template <
size_t TLatticeIdx>
385 const auto& partialLattice()
const 391 using AdvanceFunction =
395 AdvanceFunction advanceFunc;
398 void advanceFuncSimEmpty(
const TNumType& _arcNow)
403 void advanceFuncSim(
const TNumType& _arcNow)
409 template <
bool canComputeStateGrad = CanComputeStateGrad,
410 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
420 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* =
nullptr>
423 advanceFunc = &
TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSim;
427 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* =
nullptr>
430 advanceFunc = &
TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSimEmpty;
434 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* =
nullptr>
441 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* =
nullptr>
444 advanceFunc = &
TrajectorySimulator<TNumType, TSimType, TUseStateNm, TLatticeTypes...>::advanceFuncSimEmpty;
451 simulatingWithGrad =
false;
456 template <
bool canComputeStateGrad = CanComputeStateGrad,
457 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
461 simulatingWithGrad =
true;
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)
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)
475 evaluateCosts<II>(_cfPtr, _cfGradPtr, _optParamSize);
476 eval_all_costs<IIMax, II + 1>(_cfPtr, _cfGradPtr, _optParamSize);
482 size_t sizeCosts = 0;
487 size_t optParamSize =
stateSim_->state().stateGradCf().sub(0).data().size();
489 TNumType* cfPtr =
costs_.memStartRef();
490 TNumType* cfGradPtr =
gradCosts_.memStartRef();
491 eval_all_costs<CostFuncsTypesNr>(cfPtr, cfGradPtr, optParamSize);
498 size_t sizeCosts = 0;
502 size_t optParamSize =
stateSim_->state().stateGradCf().sub(0).data().size();
511 size_t totalPartLatticePtsSize = 0, i = 0;
516 partLatI.computeLatticeStructure(*
stateSim_, i);
517 totalPartLatticePtsSize += partLatI.lattice.size();
519 _sizeCosts += sizeCostsPerPartLattice_[i];
522 return totalPartLatticePtsSize;
526 void resizeCosts(
const size_t& _optParamSize,
const bool& _simulatingWithGrad)
528 for (
size_t i = 0; i <
costs_.subSize(); ++i)
533 if (_simulatingWithGrad)
535 for (
size_t i = 0; i <
gradCosts_.subSize(); ++i)
539 for (
size_t j = 0; j < gradCostsI.subSize(); ++j)
541 gradCostsI.sub(j).subResize(_optParamSize);
544 for (
size_t i = 0; i <
gradCosts_.subSize(); ++i)
547 for (
size_t j = 0; j < gradCostsI.subSize(); ++j)
549 #ifdef USE_MAP_ALIGNMENT 550 new (
gradCostsMap_[i].get()) Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>(
553 new (
gradCostsMap_[i].get()) Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>(
562 template <
size_t FuncNr = 0>
563 void evaluateCosts(TNumType*& _cfPtr, TNumType*& _cfGradPtr,
const size_t& _optParamSize)
567 size_t latticeISize = partLatI.lattice.size();
569 if (simulatingWithGrad)
571 for (
size_t i = 0; i < latticeISize; ++i)
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);
581 for (
size_t i = 0; i < latticeISize; ++i)
583 const auto& latticeI = partLatI.lattice[i];
584 partLatI.template evaluate<FuncNr>(*latticeI.statePtr, i, *
stateSim_, _cfPtr);
593 TNumType _arcMin = FLT_MAX;
597 auto& vecIter = latI.latticeVecCacheIter_;
598 if (vecIter != latI.latticeVecIterEnd_)
600 if (vecIter->arc < _arcMin)
602 _arcMin = vecIter->arc;
604 _latVecIdx = std::distance(latI.lattice.begin(), vecIter);
611 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(useStateSimNm)>::type* =
nullptr>
618 template <bool useStateSimNm = TUseStateNm, typename std::enable_if<(!useStateSimNm)>::type* =
nullptr>
625 int simLatticeIdx_ = -1;
627 static typename std::vector<LatticePointType>::iterator* itMinPtr;
634 while (itMinPtr !=
nullptr)
636 const TNumType& arcNow = (*itMinPtr)->arc;
639 (this->*advanceFunc)(arcNow);
644 (this->*advanceFunc)(arcNow);
648 simLatticeI.arc = arcNow;
649 simLatticeI.latticeIdx = (*(*itMinPtr)).
latticeIdx;
650 (*(*itMinPtr)).
statePtr = simLatticeI.statePtr;
661 simLatticeI.statePtr->stateGrad());
675 return simLatticeIdx_;
681 bool forceResize =
false;
713 template <
typename TSimType2,
typename TLatticePointType,
bool canComputeStateGrad = CanComputeStateGrad,
714 typename std::enable_if<(!canComputeStateGrad)>::type* =
nullptr>
721 template <
typename TSimType2,
typename TLatticePointType,
bool canComputeStateGrad = CanComputeStateGrad,
722 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
733 template <
typename TSimType2,
typename TLatticePointType,
bool useStateSimNm = TUseStateNm,
734 typename std::enable_if<(useStateSimNm)>::type* =
nullptr>
737 auto& latticeState = *_latticePtI.statePtr;
738 const auto& simState = _sim.state();
739 latticeState.stateNm().data() = simState.stateNm().data();
743 template <
typename TSimType2,
typename TLatticePointType,
bool useStateSimNm = TUseStateNm,
744 typename std::enable_if<(!useStateSimNm)>::type* =
nullptr>
750 template <
typename TSimType2,
typename TLatticePointType,
bool useStateSimNm = TUseStateNm,
751 typename std::enable_if<(useStateSimNm)>::type* =
nullptr>
754 auto& latticeState = *_latticePtI.statePtr;
755 const auto& simState = _sim.state();
756 latticeState.stateGradNm().data() = simState.stateGradNm().data();
760 template <
typename TSimType2,
typename TLatticePointType,
bool useStateSimNm = TUseStateNm,
761 typename std::enable_if<(!useStateSimNm)>::type* =
nullptr>
767 template <
typename TSimType2,
typename TLatticePo
intType>
770 auto& latticeState = *_latticePtI.statePtr;
771 const auto& simState = _sim.state();
772 latticeState.stateCf().data() = simState.stateCf().data();
777 template <
typename TSimType2,
typename TLatticePointType,
bool canComputeStateGrad = CanComputeStateGrad,
778 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
781 auto& latticeState = *_latticePtI.statePtr;
782 const auto& simState = _sim.state();
783 latticeState.stateGradCf().data() = simState.stateGradCf().data();
788 template <
typename TSimType2,
typename TLatticePointType,
bool canComputeStateGrad = CanComputeStateGrad,
789 typename std::enable_if<(!canComputeStateGrad)>::type* =
nullptr>
792 auto& latticeState = *_latticePtI.statePtr;
793 const auto& simState = _sim.state();
794 latticeState.stateCf() = simState.stateCf();
795 latticeState.stateNm() = simState.stateNm();
799 template <
typename TSimType2,
typename TLatticePointType,
bool canComputeStateGrad = CanComputeStateGrad,
800 typename std::enable_if<(canComputeStateGrad)>::type* =
nullptr>
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();
825 std::array<std::function<void(
const TSimType&,
const TNumType&,
const size_t&,
826 typename StateMapBaseTraits<typename StateType::StateMapBaseType>::StateGradType&)>,
836 #ifdef USE_MAP_ALIGNMENT 837 std::array<std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>, MapAlignment>>,
840 std::array<std::shared_ptr<Eigen::Map<Eigen::Matrix<TNumType, -1, -1, Eigen::RowMajor>>>,
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
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)
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_
LatticePoint(TNumType _arc)
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.
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
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple(std::tuple< Tp... > &, FuncT)
std::shared_ptr< StateType > StateSPtr
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
typename TSimType::StateType StateType
static constexpr auto value
void advanceFuncSimGrad(const TNumType &_arcNow)
std::shared_ptr< StateSim > StateSimSPtr
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...