costs_evaluator.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 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 COSTS_EVALUATOR_H
34 #define COSTS_EVALUATOR_H
35 
36 #include <vector>
37 #include <math.h>
38 #include <float.h>
39 #include <functional>
40 
42 
43 #include <eigen3/Eigen/Core>
44 
45 #include <iostream>
46 
47 namespace tuw
48 {
50 {
51  F,
52  H,
53  G,
54  ENUM_SIZE
55 };
56 
57 template <typename Lattice>
59 {
60  // special class member functions
61 public:
63  {
64  }
65 
66 public:
67  virtual ~CostsEvaluatorBase() = default;
68 
69 public:
70  CostsEvaluatorBase(const CostsEvaluatorBase&) = default;
71 
72 public:
73  CostsEvaluatorBase& operator=(const CostsEvaluatorBase&) = default;
74 
75 public:
77 
78 public:
79  CostsEvaluatorBase& operator=(CostsEvaluatorBase&&) = default;
80 
81 public:
82  virtual void loadCostFunctions() = 0;
83 
84 public:
85  virtual void init(std::vector<std::shared_ptr<Lattice>>& _lattPtr) = 0;
86 
87 public:
88  virtual void resetCostFunctions(const CostEvaluatorCostType& _arrayType) = 0;
89 
90 public:
91  virtual bool evalValidCostStep(const CostEvaluatorCostType& _arrayType, double _arcNow, size_t& _violatingLatIdx,
92  double& _arcMax) = 0;
93 
94 protected:
95  virtual void computeScalarCost(double& _f) = 0;
96 
97 protected:
98  virtual void computeArrayCost(std::vector<double>& _arr, const CostEvaluatorCostType& _arrayType) = 0;
99 
100 public:
101  void evaluateF()
102  {
103  computeScalarCost(f);
104  }
105 
106 public:
107  void evaluateH()
108  {
109  computeArrayCost(h, CostEvaluatorCostType::H);
110  }
111 
112 public:
113  void evaluateG()
114  {
115  computeArrayCost(g, CostEvaluatorCostType::G);
116  }
117 
118 public:
120  {
121  computeScalarCost(f);
122  computeArrayCost(h, CostEvaluatorCostType::H);
123  computeArrayCost(g, CostEvaluatorCostType::G);
124  }
125 
126 public:
127  bool hIsValid() const
128  {
129  if ((f > FLT_MAX) || (std::isnan(f)))
130  {
131  return false;
132  }
133  for (const auto& hI : h)
134  {
135  if (hI <= 0)
136  {
137  return false;
138  }
139  }
140  return true;
141  }
142 
143 public:
144  bool gIsValid(const double& _boxBound = 1e-2) const
145  {
146  for (const auto& gI : g)
147  {
148  if (fabs(gI) >= _boxBound)
149  {
150  return false;
151  }
152  }
153  return true;
154  }
155 
156 public:
157  bool gIsValid(const size_t _Idx, const double& _boxBound = 1e-2) const
158  {
159  if (fabs(g[_Idx]) >= _boxBound)
160  {
161  return false;
162  }
163  return true;
164  }
165 
166 public:
167  double f;
168 
169 public:
170  std::vector<double> h;
171 
172 public:
173  std::vector<double> g;
174 
175 public:
176  std::vector<double> gradF;
177 
178 public:
179  Eigen::MatrixXd gradH;
180 
181 public:
182  Eigen::MatrixXd gradG;
183 };
184 
185 template <typename Lattice, typename MapData>
186 class CostsEvaluator : public CostsEvaluatorBase<Lattice>
187 {
188 public:
189  CostsEvaluator(std::shared_ptr<MapData>& _mapDataPtr)
190  : partialCostsArray_(asInt(CostEvaluatorCostType::ENUM_SIZE)), mapDataPtr_(_mapDataPtr)
191  {
192  }
193 
194 public:
195  virtual ~CostsEvaluator() = default;
196 
197 public:
198  CostsEvaluator(const CostsEvaluator&) = default;
199 
200 public:
201  CostsEvaluator& operator=(const CostsEvaluator&) = default;
202 
203 public:
204  CostsEvaluator(CostsEvaluator&&) = default;
205 
206 public:
207  CostsEvaluator& operator=(CostsEvaluator&&) = default;
208 
209 public:
210  void init(std::vector<std::shared_ptr<Lattice>>& _lattPtr) override
211  {
212  for (auto& partialCostsArrayI : partialCostsArray_)
213  {
214  partialCostsArrayI.clear();
215  }
216  this->loadCostFunctions();
217  for (auto& funcI : partialCostsArray_)
218  {
219  for (auto& partFuncI : funcI)
220  {
221  partFuncI->initLatticeMap(_lattPtr, mapDataPtr_);
222  }
223  }
224  }
225 
226 protected:
227  void computeScalarCost(double& _f)
228  {
229  {
230  auto& funcI = partialCostsArray_[asInt(CostEvaluatorCostType::F)];
231  for (auto& partFuncI : funcI)
232  {
233  partFuncI->reset();
234  partFuncI->calcCostsFull();
235  }
236  }
237 
238  _f = 0;
239  for (auto& partFuncI : partialCostsArray_[asInt(CostEvaluatorCostType::F)])
240  {
241  for (size_t k = 0; k < partFuncI->costsSize(); ++k)
242  {
243  _f += partFuncI->cost(k);
244  }
245  }
246  }
247 
248 protected:
249  void computeArrayCost(std::vector<double>& _arr, const CostEvaluatorCostType& _arrayType) override
250  {
251  {
252  auto& funcI = partialCostsArray_[asInt(_arrayType)];
253  for (auto& partFuncI : funcI)
254  {
255  partFuncI->reset();
256  partFuncI->calcCostsFull();
257  }
258  }
259 
260  size_t arrSize = 0;
261  for (auto& partFuncI : partialCostsArray_[asInt(_arrayType)])
262  {
263  arrSize += partFuncI->costsSize();
264  }
265  _arr.resize(arrSize);
266 
267  size_t iH = 0, iP;
268  iP = 0;
269  for (; iP < partialCostsArray_[asInt(_arrayType)].size(); ++iP)
270  {
271  for (size_t k = 0; k < partialCostsArray_[asInt(_arrayType)][iP]->costsSize(); ++k, ++iH)
272  {
273  _arr[iH] = partialCostsArray_[asInt(_arrayType)][iP]->cost(k);
274  }
275  }
276  }
277 
278 public:
279  void resetCostFunctions(const CostEvaluatorCostType& _arrayType) override
280  {
281  {
282  auto& funcI = partialCostsArray_[asInt(_arrayType)];
283  for (auto& partFuncI : funcI)
284  {
285  partFuncI->reset();
286  }
287  }
288  firstAfterReset_ = true;
289  }
290 
291 public:
292  bool evalValidCostStep(const CostEvaluatorCostType& _arrayType, double _arcNow, size_t& _violatingLatIdx,
293  double& _arcMax) override
294  {
295  if (firstAfterReset_)
296  {
297  {
298  auto& funcI = partialCostsArray_[asInt(_arrayType)];
299  for (auto& partFuncI : funcI)
300  {
301  partFuncI->reset();
302  }
303  firstAfterReset_ = false;
304  }
305  }
306  {
307  auto& funcI = partialCostsArray_[asInt(_arrayType)];
308  for (auto& partFuncI : funcI)
309  {
310  partFuncI->resetNew();
311  }
312  }
313  double minCost = FLT_MAX;
314  double arcMin = FLT_MAX;
315  {
316  auto& funcI = partialCostsArray_[asInt(_arrayType)];
317  for (auto& partFuncI : funcI)
318  {
319  size_t k = partFuncI->iterIdxPartBegin_;
320  partFuncI->calcCosts1KnotStep(_arcNow);
321  size_t kend = std::min(k + 1, partFuncI->costsSize());
322  for (; k < kend; ++k)
323  {
324  const double& costK = partFuncI->cost(k);
325  if (costK < 0)
326  {
327  double violLatNew = partFuncI->arcAtLattIdxPrev();
328  if ((minCost >= 0) || (violLatNew < arcMin))
329  {
330  minCost = costK;
331  arcMin = violLatNew;
332  _violatingLatIdx = partFuncI->knotLatIdx();
333  _arcMax = partFuncI->arcAtLattIdxPrev();
334  }
335  }
336  }
337  }
338  }
339  return minCost >= 0;
340  }
341 
342 public:
343  std::vector<std::vector<std::unique_ptr<cost_functions::CostsArrayLatBase<Lattice, MapData>>>> partialCostsArray_;
344 
345 public:
346  std::shared_ptr<MapData>& mapDataPtr()
347  {
348  return mapDataPtr_;
349  }
350 
351 private:
352  std::shared_ptr<MapData> mapDataPtr_;
353 
354 private:
356 };
357 }
358 
359 #endif // COSTS_EVALUATOR_H
std::vector< double > h
std::shared_ptr< MapData > mapDataPtr_
void init(std::vector< std::shared_ptr< Lattice >> &_lattPtr) override
bool evalValidCostStep(const CostEvaluatorCostType &_arrayType, double _arcNow, size_t &_violatingLatIdx, double &_arcMax) override
void resetCostFunctions(const CostEvaluatorCostType &_arrayType) override
void computeArrayCost(std::vector< double > &_arr, const CostEvaluatorCostType &_arrayType) override
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
std::vector< std::vector< std::unique_ptr< cost_functions::CostsArrayLatBase< Lattice, MapData > > > > partialCostsArray_
std::vector< double > g
CostEvaluatorCostType
std::shared_ptr< MapData > & mapDataPtr()
bool gIsValid(const double &_boxBound=1e-2) const
void computeScalarCost(double &_f)
bool gIsValid(const size_t _Idx, const double &_boxBound=1e-2) const
std::vector< double > gradF
CostsEvaluator(std::shared_ptr< MapData > &_mapDataPtr)


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