2 #include "gtest/gtest.h" 4 #include <boost/concept_check.hpp> 18 namespace cost_functions
20 template <
typename MapData>
27 this->arcAcc = [
this](
const size_t& _i)
29 return this->latticePtr_->at(_i).arc;
53 enum class StateNmVars
61 enum class StateCfVars
70 enum class ParamFuncVars
77 enum class ParamFuncLattices
94 double stateArc()
const override 96 return stateCf_.value(
asInt(StateCfVars::T));
100 double stateDist()
const override 102 return stateCf_.value(
asInt(StateCfVars::S));
121 paramFuncs_.
setEvalArc(_arc, _evalArcGuarantee);
122 stateCf_.value(
asInt(StateCfVars::T)) = _arc;
124 stateCf_.value(
asInt(StateCfVars::W)) = paramFuncs_.computeFuncVal(
asInt(ParamFuncVars::W));
125 stateCf_.value(
asInt(StateCfVars::S)) = paramFuncs_.computeS();
129 State& stateNmDot()
override 131 stateNmDotCache_.
value(
asInt(StateNmVars::X)) =
133 stateNmDotCache_.value(
asInt(StateNmVars::Y)) =
135 stateNmDotCache_.value(
asInt(StateNmVars::THETA)) = stateCf_.value(
asInt(StateCfVars::W));
136 return stateNmDotCache_;
140 State& stateNmDelta(
const double& _dArc)
override 143 const double& w = stateCf_.value(
asInt(StateCfVars::W));
144 const double& theta = stateNm_.value(
asInt(StateNmVars::THETA));
145 double& dx = stateNmDotCache_.value(
asInt(StateNmVars::X));
146 double& dy = stateNmDotCache_.value(
asInt(StateNmVars::Y));
147 double& da = stateNmDotCache_.value(
asInt(StateNmVars::THETA));
149 if (fabs(w) > FLT_MIN)
151 const double r = v / w;
152 dx = (-r * sin(theta) + r * sin(theta + w * _dArc));
153 dy = (+r * cos(theta) - r * cos(theta + w * _dArc));
158 dx = v * _dArc * cos(theta);
159 dy = v * _dArc * sin(theta);
162 return stateNmDotCache_;
169 using SNF = StateSimTest::StateVars;
170 using SNV = StateSimTest::StateNmVars;
171 using SCF = StateSimTest::StateCfVars;
172 using PFV = StateSimTest::ParamFuncVars;
186 class TrajOptTest :
public ::testing::Test
194 dummyMapData = shared_ptr<double>(
new double(0));
201 vector<PFS> pf(2, PFS());
202 static constexpr
const int paramFuncArcRefIdxAll = 0;
203 pf[0].ctrlPtsSize = 5;
204 pf[0].ctrlPtsArcRefIdx = paramFuncArcRefIdxAll;
205 pf[1].ctrlPtsSize = 5;
206 pf[1].ctrlPtsArcRefIdx = paramFuncArcRefIdxAll;
211 stateSimPtr = std::shared_ptr<StateSimTest>(
new StateSimTest);
212 stateSimPtr->paramFuncs()->init(pf);
216 ParamFuncs* funcs = stateSimPtr->paramFuncs();
240 virtual ~TrajOptTest()
251 virtual void TearDown()
259 shared_ptr<double> dummyMapData;
265 using TSBSLT = TS::BaseSimLatticeType;
266 using TSLatVec = TS::LatticeVec;
268 using MapData = double;
277 stAcc = [
this](
const size_t& _i)
279 return latticePtr_->at(_i).statePtr->value(
asInt(
SNF::V));
281 funcPredef_FL1::lin(
this);
282 funcPredef_FL2::sum(
this);
283 funcPredef_FL3::weight(
this);
290 class TestCost_LinIntWNorm_W :
public CFLatMap1Weight<TSLatVec, MapData>
293 TestCost_LinIntWNorm_W()
295 stAcc = [
this](
const size_t& _i)
297 return latticePtr_->at(_i).statePtr->value(
asInt(SNF::W));
299 funcPredef_FL1::lin(
this);
300 funcPredef_FL2::intTrap(
this);
301 funcPredef_FL3::weightNorm(
this);
313 stAcc = [
this](
const size_t& _i)
315 return latticePtr_->at(_i).statePtr->value(
asInt(SNF::X)) ;
317 funcPredef_FL1::lin(
this);
318 funcPredef_FL2::sum(
this);
319 funcPredef_FL3::weight(
this);
326 class Test1CostArray_LinSumW_V :
public CostsArrayLat<TSLatVec, MapData, TestCost_LinSumW_V>
328 size_t latFuncLayerIdx()
override 330 return TS::lattTypeIdx(
asInt(TSBSLT::LATTICE_ARC_EQ_DT));
332 size_t latKnotLayerIdx()
override 334 return TS::lattTypeIdx(
asInt(TSBSLT::ARC_BG_BK));
337 class Test1CostArray_LinSumW_W :
public CostsArrayLat<TSLatVec, MapData, TestCost_LinIntWNorm_W>
339 size_t latFuncLayerIdx()
override 341 return TS::lattTypeIdx(
asInt(TSBSLT::LATTICE_ARC_EQ_DT));
343 size_t latKnotLayerIdx()
override 345 return TS::lattTypeIdx(
asInt(TSBSLT::ARC_BG_BK));
348 class Test1CostArray_Lin_TPos :
public CostsArrayLat<TSLatVec, MapData, TestCost_Lin_TPos>
350 size_t latFuncLayerIdx()
override 352 return TS::lattTypeIdx(0);
354 size_t latKnotLayerIdx()
override 356 return TS::lattTypeIdx(0);
360 class TestCostsEvaluatorT1 :
public CostsEvaluator<TSLatVec, MapData>
363 TestCostsEvaluatorT1(std::shared_ptr<MapData>& _mapDataPtr) :
CostsEvaluator(_mapDataPtr)
368 void loadCostFunctions()
override 385 return std::shared_ptr<OptimizationStateDiffDrive>(
new OptimizationStateDiffDrive(*
this));
391 auto* paramFuncs = _trajSim.
stateSim()->paramFuncs();
392 size_t idxOptVec = 0;
393 for (
size_t i = 0; i < paramFuncs->funcsSize(); ++i)
395 for (
size_t j = 1; j < paramFuncs->funcCtrlPtSize(i); ++j)
400 for (
size_t j = 1; j < paramFuncs->funcsArcSize(0); ++j)
402 paramFuncs->funcsArc(0, j) = vars[idxOptVec++];
409 auto* paramFuncs = _trajSim.
stateSim()->paramFuncs();
410 vars.resize(paramFuncs->funcsSize() * (paramFuncs->funcCtrlPtSize(0) - 1) + (paramFuncs->funcsArcSize(0) - 1));
411 size_t idxOptVec = 0;
412 for (
size_t i = 0; i < paramFuncs->funcsSize(); ++i)
414 for (
size_t j = 1; j < paramFuncs->funcCtrlPtSize(i); ++j)
419 for (
size_t j = 1; j < paramFuncs->funcsArcSize(0); ++j)
421 vars[idxOptVec++] = paramFuncs->funcsArc(0, j);
426 TEST_F(TrajOptTest, GenericTest)
428 trajOpt = shared_ptr<TrajectoryOptimizer>(
new TrajectoryOptimizer(stateSimPtr, std::make_unique<TestCostsEvaluatorT1>(dummyMapData),
429 std::shared_ptr<OptimizationStateDiffDrive>(
new OptimizationStateDiffDrive)));
431 vector<vector<double*> > funcKnotsLattice(1, vector<double*>(stateSimPtr->paramFuncs()->funcsArcSize(0), 0));
432 for (
size_t i = 0; i < funcKnotsLattice[0].size(); ++i)
434 funcKnotsLattice[0][i] = &stateSimPtr->paramFuncs()->funcsArc(0, i);
436 trajOpt->trajSim()->setUserDefLattice(funcKnotsLattice);
442 trajOpt->trajSim()->dtBase() = dt;
443 trajOpt->trajSim()->dsBase() = ds;
445 trajOpt->stepSize() = 1e-5;
447 trajOpt->computeJacobian();
449 auto& costsEvaluator = trajOpt->trajSim()->costsEvaluator_;
450 auto f = costsEvaluator->f;
451 auto h = costsEvaluator->h;
452 auto g = costsEvaluator->g;
453 auto gradF(costsEvaluator->gradF);
454 auto gradH(costsEvaluator->gradH);
455 auto gradG(costsEvaluator->gradG);
457 cout <<
"gradH Precalc=" << endl
466 int main(
int argc,
char** argv)
468 ::testing::InitGoogleTest(&argc, argv);
469 return RUN_ALL_TESTS();
std::shared_ptr< State > StateSPtr
previous evaluation arc <= this evaluation arc
virtual double & value(const std::size_t &_i)=0
Access state variable based on index _i.
std::unique_ptr< StateSim > StateSimUPtr
control point arc parameter
Helper function needed to upgrade c++ 2011.
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
std::shared_ptr< TrajectoryOptimizer > TrajectoryOptimizerSPtr
Templated partial implementation of StateSim.
EvalArcGuarantee
Flags if any guarantees about evaluation arc relative to last evaluation arc are present.
agent base center linear velocity is parametric function
Containts parametric function initialization data.
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
double & ctrlPtVal(const std::size_t &_funcIdx, const std::size_t &_funcCtrlPtIdx, const CtrlPtDim &_ctrlPtDim=CtrlPtDim::VAL)
Access of a parametric function control point dimension.
CtrlPtDim
Control point variable dimension.
Extends manipulation of parametric functions collection with closed-form arc length (distance) comput...
FuncEvalMode
Required type of computation relative to the parametric function.
std::shared_ptr< StateSim > StateSimPtr
StateSimPtr stateSim()
Reference of the state simulator object.
virtual void precompute()=0
Precomputes cached data.
agent base center linear velocity is parametric function
virtual void setEvalArc(const double &_funcsArcEval, const EvalArcGuarantee &_evalArcGuarantee=EvalArcGuarantee::NONE)=0
Sets parametric function evaluation arc.
int main(int argc, char **argv)
Storage and manipulation of parametric functions collection.