agv_diff_drive_v_w_lattices.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2017 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 
33 #ifndef AGV_DIFF_DRIVE_V_W_LATTICES_HPP
34 #define AGV_DIFF_DRIVE_V_W_LATTICES_HPP
35 
38 #include <tuw_control/utils.h>
39 
40 namespace tuw
41 {
42 template <typename TNumType, class TSimType, class... TCostFuncsType>
44  : public LatticeTypeBaseCRTP<LatticeTypeStateSimBeginEnd<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
45  TCostFuncsType...>
46 {
47 public:
48  using LatticeTypeBaseCRTP<LatticeTypeStateSimBeginEnd, TNumType, TSimType, TCostFuncsType...>::LatticeTypeBaseCRTP;
49 
50 public:
51  static void computeLatticeArcsImpl(TSimType& _sim, std::vector<TNumType>& _latticeArcs)
52  {
53  _latticeArcs.resize(1);
54  _latticeArcs[0] = _sim.paramStruct->paramFuncs.funcsArcEnd();
55  }
56 
57 public:
58  void computeDArcIdPImpl(const TSimType& _sim, const TNumType _arc, const size_t& _lattIdx, auto& _dstStateGrad)
59  {
60  auto& dstStateGradCf = _dstStateGrad.stateCf();
61  auto& dstStateGradNm = _dstStateGrad.stateNm();
62  size_t optParamTBackIdx = dstStateGradCf.sub(0).data().size() - 1;
63  dstStateGradCf.mat().col(optParamTBackIdx).noalias() += _sim.stateCfDotCache().data();
64  dstStateGradNm.mat().col(optParamTBackIdx).noalias() += _sim.stateNmDotCache().data();
65  }
66 
67 public:
68  virtual void precompute(TSimType& _sim) override
69  {
70  }
71 };
72 
73 template <typename TNumType, class TSimType, class... TCostFuncsType>
75  : public LatticeTypeBaseCRTP<LatticeTypeStateSimEqDt<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
76  TCostFuncsType...>
77 {
78 public:
79  using LatticeTypeBaseCRTP<LatticeTypeStateSimEqDt, TNumType, TSimType, TCostFuncsType...>::LatticeTypeBaseCRTP;
80 
81 public:
82  void computeLatticeArcsImpl(TSimType& _sim, std::vector<TNumType>& _latticeArcs) const
83  {
84  const TNumType arcParamMax = _sim.paramStruct->paramFuncs.funcsArcEnd();
85  double dt = dt_;
86  if (nrPts_ >= 1)
87  {
88  dt = arcParamMax / (TNumType)(nrPts_);
89  }
90  // const size_t latticeSize = std::max( 0, (int)( ceil( arcParamMax / dt ) ) );
91  // if(latticeSize != _latticeArcs.size()) { _latticeArcs.resize( latticeSize ); }
92  // for ( size_t i = 0; i < latticeSize; ++i ) { _latticeArcs[i] = (i+1) * dt; }
93  // _latticeArcs.back() = arcParamMax;
94 
95  if ((int)_latticeArcs.size() != nrPts_)
96  {
97  _latticeArcs.resize(nrPts_);
98  }
99  for (int i = 0; i < nrPts_; ++i)
100  {
101  _latticeArcs[i] = (i + 1) * dt;
102  }
103  _latticeArcs.back() = arcParamMax;
104  }
105 
106 public:
107  void computeDArcIdPImpl(const TSimType& _sim, const TNumType _arc, const size_t& _lattIdx, auto& _dstStateGrad)
108  {
109  // if (nrPts_ > 1) {
110  auto& dstStateGradCf = _dstStateGrad.stateCf();
111  auto& dstStateGradNm = _dstStateGrad.stateNm();
112 
113  const TNumType dtIdpEnd = (TNumType)(_lattIdx + 1) / (TNumType)(this->lattice.size() /*-1*/);
114  size_t optParamTBackIdx = dstStateGradCf.sub(0).data().size() - 1;
115 
116  dstStateGradCf.mat().col(optParamTBackIdx).noalias() += _sim.stateCfDotCache().data() * dtIdpEnd;
117  dstStateGradNm.mat().col(optParamTBackIdx).noalias() += _sim.stateNmDotCache().data() * dtIdpEnd;
118  // }
119  }
120 
121 public:
122  virtual void precompute(TSimType& _sim) override
123  {
124  }
125 
126 public:
127  const TNumType& dt()
128  {
129  return dt_;
130  }
131 
132 public:
133  void setDt(const TNumType& _dt)
134  {
135  dt_ = _dt;
136  nrPts_ = -1;
137  }
138 
139 public:
140  void setNrPts(const size_t& _nrPts)
141  {
142  nrPts_ = _nrPts;
143  }
144 
145 private:
146  TNumType dt_;
147 
148 private:
149  int nrPts_;
150 };
151 
152 template <typename TNumType, class TSimType, class... TCostFuncsType>
154  : public LatticeTypeBaseCRTP<LatticeTypeStateSimEqDs<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
155  TCostFuncsType...>
156 {
157 public:
158  using LatticeTypeBaseCRTP<LatticeTypeStateSimEqDs, TNumType, TSimType, TCostFuncsType...>::LatticeTypeBaseCRTP;
159 
160 public:
161  void computeLatticeArcsImpl(TSimType& _sim, std::vector<TNumType>& _latticeArcs)
162  {
163  // if ( (ds_ > 0) || (nrPts_ > 1) ) {
164  // auto& paramFuncs = _sim.paramStruct->paramFuncs;
165  // if(nrPts_ > 1) {
166  // paramFuncs.setEvalArc( paramFuncs.funcsArcEnd() );
167  // ds_ = paramFuncs.computeS() / (TNumType)(nrPts_);
168  // paramFuncs.setEvalArc( paramFuncs.funcsArcBegin() );
169  // }
170  // paramFuncs.computeS2TLattice( 0, ds_, _latticeArcs );
171  // _latticeArcs.erase(_latticeArcs.begin());
172  // }
173  // else { _latticeArcs.clear(); }
174 
175  auto& paramFuncs = _sim.paramStruct->paramFuncs;
176  double sEnd;
177  if (nrPts_ > 1)
178  {
179  paramFuncs.setEvalArc(paramFuncs.funcsArcEnd());
180  sEnd = paramFuncs.computeS();
181  ds_ = sEnd / (TNumType)(nrPts_);
182  paramFuncs.setEvalArc(paramFuncs.funcsArcBegin());
183  if ((int)_latticeArcs.size() != nrPts_)
184  {
185  _latticeArcs.resize(nrPts_);
186  }
187  for (int i = 0; i < nrPts_; ++i)
188  {
189  _latticeArcs[i] = (i + 1) * ds_;
190  }
191  _latticeArcs.back() = sEnd;
192  paramFuncs.computeS2TLattice(_latticeArcs, _latticeArcs);
193  }
194  }
195 
196 public:
197  void computeDArcIdPImpl(const TSimType& _sim, const TNumType _arc, const size_t& _lattIdx, auto& _dstStateGrad)
198  {
199  if (nrPts_ > 1)
200  {
201  if (_lattIdx == 0)
202  {
203  TNumType tEnd = _sim.paramStruct->paramFuncs.funcsArcEnd();
204  auto& simNonConst = const_cast<TSimType&>(_sim);
205  simNonConst.setXCf(tEnd, PfEaG::NONE);
206  simNonConst.setGradXCf(tEnd, PfEaG::NEAR_LAST);
207  simNonConst.setXCfDot(tEnd, PfEaG::NEAR_LAST);
208 
209  dsdp = _sim.state().stateGradCf().s().data();
210  dsdp(dsdp.rows() - 1) += _sim.stateCfDotCache().s();
211 
212  simNonConst.setXCf(_arc, PfEaG::AT_BEGIN);
213  simNonConst.setGradXCf(_arc, PfEaG::NEAR_LAST);
214  simNonConst.setXCfDot(_arc, PfEaG::NEAR_LAST);
215  stateDotCache.resize(_sim.stateNmDotCache().data().size() + _sim.stateCfDotCache().data().size());
216  }
217  stateDotCache.block(0, 0, _sim.stateNmDotCache().data().size(), 1).noalias() = _sim.stateNmDotCache().data();
218  stateDotCache.block(_sim.stateNmDotCache().data().size(), 0, _sim.stateCfDotCache().data().size(), 1).noalias() =
219  _sim.stateCfDotCache().data();
220  auto& v = _sim.state().stateCf().v();
221  if (fabs(v) > 1e-4)
222  {
223  TNumType iByNS = (TNumType)(_lattIdx + 1) /
224  (TNumType)(this->lattice.size() /*-1*/) /*_sim.state().stateCf().s() / (ds_*nrPts_)*/;
225  _dstStateGrad.mat() += (stateDotCache) * ((iByNS * dsdp - _dstStateGrad.stateCf().s().data()) / v).transpose();
226  }
227  }
228  }
229 
230 public:
231  virtual void precompute(TSimType& _sim) override
232  {
233  }
234 
235 public:
236  const TNumType& dt()
237  {
238  return ds_;
239  }
240 
241 public:
242  void setDs(const TNumType& _ds)
243  {
244  ds_ = _ds;
245  nrPts_ = -1;
246  }
247 
248 public:
249  void setNrPts(const size_t& _nrPts)
250  {
251  nrPts_ = _nrPts;
252  }
253 
254 public:
255  TNumType ds_;
256 
257 private:
258  int nrPts_;
259 
260 private:
261  Eigen::Matrix<TNumType, -1, 1> dsdp;
262 
263 private:
264  Eigen::Matrix<TNumType, -1, 1> stateDotCache;
265 };
266 
267 template <typename TNumType, class TSimType, class... TCostFuncsType>
269  : public LatticeTypeBaseCRTP<LatticeTypeStateSimCtrlPtKnots<TNumType, TSimType, TCostFuncsType...>, TNumType,
270  TSimType, TCostFuncsType...>
271 {
272 public:
273  using LatticeTypeBaseCRTP<LatticeTypeStateSimCtrlPtKnots, TNumType, TSimType, TCostFuncsType...>::LatticeTypeBaseCRTP;
274 
275 public:
276  void computeLatticeArcsImpl(TSimType& _sim, std::vector<TNumType>& _latticeArcs) const
277  {
278  if (inUse)
279  {
280  _latticeArcs.resize(_sim.paramStruct->paramFuncs.funcsArcSize(0) - 1);
281  auto& paramFuncs = _sim.paramStruct->paramFuncs;
282  for (size_t i = 0; i < _latticeArcs.size(); ++i)
283  {
284  _latticeArcs[i] = paramFuncs.funcsArc(0, i + 1);
285  }
286  }
287  else
288  {
289  _latticeArcs.clear();
290  }
291  }
292 
293 public:
294  void computeDArcIdPImpl(const TSimType& _sim, const TNumType _arc, const size_t& _lattIdx, auto& _dstStateGrad)
295  {
296  if (inUse)
297  {
298  auto& dstStateGradCf = _dstStateGrad.stateCf();
299  auto& dstStateGradNm = _dstStateGrad.stateNm();
300  size_t optParamTIdx =
301  dstStateGradCf.sub(0).optParamV().data().size() + dstStateGradCf.sub(0).optParamW().data().size();
302  dstStateGradCf.mat().col(optParamTIdx + _lattIdx).noalias() += _sim.stateCfDotCache().data();
303  dstStateGradNm.mat().col(optParamTIdx + _lattIdx).noalias() += _sim.stateNmDotCache().data();
304  }
305  }
306 
307 public:
308  virtual void precompute(TSimType& _sim) override
309  {
310  }
311 
312 public:
313  bool inUse;
314 };
315 }
316 
317 #endif // AGV_DIFF_DRIVE_V_W_LATTICES_HPP
virtual void precompute(TSimType &_sim) override
static void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs)
virtual void precompute(TSimType &_sim) override
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs) const
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs)
Eigen::Matrix< TNumType,-1, 1 > dsdp
virtual void precompute(TSimType &_sim) override
close to previous evaluation arc
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs) const
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
Eigen::Matrix< TNumType,-1, 1 > stateDotCache
this evaluation arc is at the arc parametrization begin
virtual void precompute(TSimType &_sim) override
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)


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