33 #ifndef COST_FUNCTIONS_H 34 #define COST_FUNCTIONS_H 47 namespace cost_functions
165 std::function<double(const size_t&)>
arcAcc;
168 std::function<double(const size_t&)>
stAcc;
171 std::function<double(const size_t&)>
fL1;
174 std::function<double(const double&, const size_t&)>
fL2;
177 std::function<double(const double&)>
fL3;
194 template <
typename Lattice,
typename MapData>
198 template <
typename Lattice,
typename MapData>
202 void initLatticeMap(std::shared_ptr<Lattice>& _lattPtr, std::shared_ptr<MapData>& _mapDataPtr)
204 latticePtr_ = _lattPtr;
205 mapDataPtr_ = _mapDataPtr;
224 template <
typename Lattice,
typename MapData>
230 return (this->latticePtr_->size() > _iterIdx);
234 template <
typename Lattice,
typename MapData>
237 template <
typename Lattice,
typename MapData>
246 virtual size_t latFuncLayerIdx() = 0;
249 virtual size_t latKnotLayerIdx() = 0;
255 const double&
cost(
const size_t& _i)
const 257 return pieceWiseCosts[_i]->cost();
263 return pieceWiseCosts.size();
270 for (
auto& costI : pieceWiseCosts)
272 costI->setWeight(_weight);
281 calcCosts1KnotStep();
290 if (
iterIdx_ >= pieceWiseCosts.size())
295 pieceWiseCosts[
iterIdx_]->calcCostsFull();
296 if (++
iterIdx_ >= pieceWiseCosts.size())
301 pieceWiseCosts[
iterIdx_]->iterIdx_ = pieceWiseCosts[
iterIdx_ - 1]->iterIdx_;
311 if (
iterIdx_ >= pieceWiseCosts.size())
317 pieceWiseCosts[
iterIdx_]->calcCostsFull(_arcLimit);
322 if (++
iterIdx_ >= pieceWiseCosts.size())
327 pieceWiseCosts[
iterIdx_]->iterIdx_ = pieceWiseCosts[
iterIdx_ - 1]->iterIdx_;
336 iterIdxPartBegin_ = 0;
339 for (
auto& costI : pieceWiseCosts)
341 costI->resetFunction();
349 int firstResetBound = resizeInitNew();
350 resetBounds(firstResetBound);
359 return lattKnotPtr_->at(std::max(0, (
int)
iterIdx_ - 1)).arc;
365 _fistResetBound = std::max(_fistResetBound, 0) + 1;
366 for (
size_t i = _fistResetBound; i <= this->pieceWiseCosts.size(); ++i)
368 this->pieceWiseCosts[i - 1]->setBoundIdxBegin(_fistResetBound);
369 this->pieceWiseCosts[i - 1]->setBoundArcEnd(this->lattKnotPtr_->at(_fistResetBound++).arc);
376 int sizeOld = pieceWiseCosts.size();
379 pieceWiseCosts.clear();
384 if (lattKnotPtr_->size() < 2)
386 pieceWiseCosts.clear();
390 pieceWiseCosts.resize(std::max(0, (
int)lattKnotPtr_->size() - 1));
391 int deltaSize = (int)pieceWiseCosts.size() - sizeOld;
394 for (
size_t i = sizeOld; i < this->pieceWiseCosts.size(); ++i)
396 pieceWiseCosts[i] = allocateCostFunc();
397 pieceWiseCosts[i]->initLatticeMap(lattFuncPtr_, mapDataPtr_);
398 pieceWiseCosts[i]->resetFunction();
399 pieceWiseCosts[i]->setWeight(weight_);
406 void initLatticeMap(std::vector<std::shared_ptr<Lattice>>& _lattPtr, std::shared_ptr<MapData>& _mapDataPtr)
409 latIdx = latFuncLayerIdx();
410 lattFuncPtr_ =
nullptr;
411 if (_lattPtr.size() > latIdx)
413 lattFuncPtr_ = _lattPtr[latIdx];
415 latIdx = latKnotLayerIdx();
416 lattKnotPtr_ =
nullptr;
417 if (_lattPtr.size() > latIdx)
419 lattKnotPtr_ = _lattPtr[latIdx];
420 knotLatIdx_ = latIdx;
422 mapDataPtr_ = _mapDataPtr;
423 pieceWiseCosts.clear();
424 pieceWiseCosts.reserve(1000);
462 template <
typename Lattice,
typename MapData>
470 template <
typename CF>
473 _cf->fL1 = [_cf](
const size_t& _i)
475 return _cf->stAcc(_i);
478 template <
typename CF>
481 _cf->fL1 = [_cf](
const size_t& _i)
483 return _cf->stAcc(_i) * _cf->stAcc(_i);
486 template <
typename CF>
487 static void ln(CF* _cf)
489 _cf->fL1 = [_cf](
const size_t& _i)
491 return log(_cf->stAcc(_i));
498 template <
typename CF>
501 _cf->fL2 = [_cf](
const double& _ansPrev,
const size_t& _i)
503 return _ansPrev + _cf->fL1(_i);
506 template <
typename CF>
509 _cf->fL2 = [_cf](
const double& _ansPrev,
const size_t& _i)
511 return fmin(_ansPrev, _cf->fL1(_i));
514 template <
typename CF>
517 _cf->fL2 = [_cf](
const double& _ansPrev,
const size_t& _i)
519 return fmax(_ansPrev, _cf->fL1(_i));
522 template <
typename CF>
525 _cf->fL2 = [_cf](
const double& _ansPrev,
const size_t& _i)
527 if ((_cf->iterIdx_ == 0))
531 return _ansPrev + (_cf->arcAcc(_i) - _cf->arcAcc(_i - 1)) * (_cf->fL1(_i) + _cf->fL1(_i - 1)) / 2.;
538 template <
typename CF>
541 _cf->fL3 = [_cf](
const double& _ansPrev)
543 return _ansPrev * _cf->weight_;
546 template <
typename CF>
549 _cf->fL3 = [_cf](
const double& _ansPrev)
551 return _ansPrev * _cf->weight_ / 2.;
554 template <
typename CF>
557 _cf->fL3 = [_cf](
const double& _ansPrev)
559 int beginIdx = _cf->latBeginIdx_;
560 if (_cf->iterIdx_ == 0)
564 beginIdx = std::max(0, (
int)_cf->latBeginIdx_ - 1);
565 return _ansPrev * _cf->weight_ / (_cf->arcAcc(_cf->iterIdx_) - _cf->arcAcc(beginIdx));
570 template <
typename Lattice,
typename MapData>
581 template <
typename Lattice,
typename MapData>
592 template <
typename Lattice,
typename MapData>
603 template <
typename Lattice,
typename MapData>
615 template <
typename Lattice,
typename MapData,
typename CostFunction>
621 return std::unique_ptr<CostFunction>(
new CostFunction);
629 #endif // COST_FUNCTIONS_H virtual bool hasReqData(size_t _iterIdx)
std::shared_ptr< Lattice > lattFuncPtr_
static void intTrap(CF *_cf)
void setWeight(const double &_weight)
std::shared_ptr< MapData > mapDataPtr_
const size_t costsSize() const
std::shared_ptr< Lattice > lattKnotPtr_
static void weightNorm(CF *_cf)
CostFuncLatMap1WeightPtr< Lattice, MapData > allocateCostFunc() override
std::shared_ptr< Lattice > latticePtr_
void calcCosts1KnotStep()
const double & cost(const size_t &_i) const
void initLatticeMap(std::shared_ptr< Lattice > &_lattPtr, std::shared_ptr< MapData > &_mapDataPtr)
std::function< double(const size_t &)> fL1
std::function< double(const double &)> fL3
std::function< double(const size_t &)> stAcc
void calcCosts1KnotStep(const double &_arcLimit)
std::shared_ptr< MapData > mapDataPtr_
void setBoundIdxBegin(const std::size_t &_idxBoundBegin)
double arcAtLattIdxPrev() const
void setWeight(const double &_weight)
std::function< double(const size_t &)> arcAcc
bool hasReqData(size_t _iterIdx) override
const size_t & knotLatIdx() const
void resetBounds(int _fistResetBound)
std::vector< CostFuncLatMap1WeightPtr< Lattice, MapData > > pieceWiseCosts
void initLatticeMap(std::vector< std::shared_ptr< Lattice >> &_lattPtr, std::shared_ptr< MapData > &_mapDataPtr)
void setBoundArcEnd(const double &_arcBoundEnd)
std::function< double(const double &, const size_t &)> fL2
std::unique_ptr< CFLatMap1WeightBase< Lattice, MapData >> CostFuncLatMap1WeightPtr
static void weight(CF *_cf)
void setCost0(const double &_cost0)
void calcCostsFull(const double &_arcLimit)
const double & cost() const
static void weightBy2(CF *_cf)