test_grid_astar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <list>
31 #include <vector>
32 
33 #include <boost/thread.hpp>
34 
35 #include <omp.h>
36 
37 #include <gtest/gtest.h>
38 
40 
41 TEST(GridAstar, ParallelSearch)
42 {
43  using Vec = CyclicVecInt<1, 1>;
44  GridAstar<1, 1> as(Vec(16));
45  as.setSearchTaskNum(8);
46  omp_set_num_threads(2);
47 
48  const auto cb_cost = [](
49  const Vec&, Vec&, const Vec&, const Vec&) -> float
50  {
51  return 1.0;
52  };
53  const auto cb_cost_estim = [](const Vec&, const Vec&) -> float
54  {
55  return 1e-6;
56  };
57  std::vector<Vec> search[16];
58  for (int i = 1; i < 15; ++i)
59  {
60  search[0].push_back(Vec(i));
61  for (int j = 1; j < 15; ++j)
62  {
63  if (i == j)
64  continue;
65  search[i].push_back(Vec(j));
66  }
67  search[i].push_back(Vec(15));
68  }
69 
70  const auto cb_search = [&search](
71  const Vec& p, const Vec&, const Vec&) -> std::vector<Vec>&
72  {
73  return search[p[0]];
74  };
75  const auto cb_progress = [](const std::list<Vec>&)
76  {
77  return true;
78  };
79 
80  for (int i = 0; i < 1000; ++i)
81  {
82  std::list<Vec> path;
83  ASSERT_TRUE(
84  as.search(
85  Vec(0), Vec(15), path,
86  cb_cost, cb_cost_estim, cb_search, cb_progress,
87  0, 1.0));
88  ASSERT_EQ(path.size(), 3u);
89  ASSERT_EQ(path.front(), Vec(0));
90  ASSERT_EQ(path.back(), Vec(15));
91  }
92 }
93 
94 class GridAstarTestWrapper : public GridAstar<1, 1>
95 {
96 public:
97  explicit GridAstarTestWrapper(const Vec& size)
98  : GridAstar(size)
99  {
100  }
101  std::unordered_map<Vec, Vec, Vec>& parentMap()
102  {
103  return parents_;
104  }
105  bool findPath(const Vec& s, const Vec& e, std::list<Vec>& path)
106  {
107  return GridAstar::findPath(s, e, path);
108  }
109 };
110 
111 TEST(GridAstar, FindPathLooped)
112 {
114  GridAstarTestWrapper as(Vec(4));
115 
116  as.parentMap()[Vec(3)] = Vec(2);
117  as.parentMap()[Vec(2)] = Vec(1);
118  as.parentMap()[Vec(1)] = Vec(2);
119 
120  std::list<Vec> path;
121  const auto timeout_func = []()
122  {
123  try
124  {
125  boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
126  }
127  catch (boost::thread_interrupted&)
128  {
129  return;
130  }
131  EXPECT_TRUE(false) << "Looks entered endless loop. Test will be aborted.";
132  abort();
133  };
134  boost::thread timeout(timeout_func);
135  ASSERT_FALSE(as.findPath(Vec(0), Vec(3), path));
136  timeout.interrupt();
137 }
138 
139 TEST(GridAstar, FindPathUnconnected)
140 {
142  GridAstarTestWrapper as(Vec(3));
143 
144  as.parentMap()[Vec(2)] = Vec(1);
145 
146  std::list<Vec> path;
147  ASSERT_FALSE(as.findPath(Vec(0), Vec(2), path));
148 }
149 
150 TEST(GridAstar, FindPath)
151 {
153  GridAstarTestWrapper as(Vec(3));
154 
155  as.parentMap()[Vec(2)] = Vec(1);
156  as.parentMap()[Vec(1)] = Vec(0);
157 
158  std::list<Vec> path;
159  ASSERT_TRUE(as.findPath(Vec(0), Vec(2), path));
160  ASSERT_EQ(path.size(), 3u);
161  auto it = path.cbegin();
162  ASSERT_EQ(*(it++), Vec(0));
163  ASSERT_EQ(*(it++), Vec(1));
164  ASSERT_EQ(*it, Vec(2));
165 }
166 
167 int main(int argc, char** argv)
168 {
169  testing::InitGoogleTest(&argc, argv);
170 
171  return RUN_ALL_TESTS();
172 }
void setSearchTaskNum(const size_t &search_task_num)
Definition: grid_astar.h:131
bool findPath(const Vec &s, const Vec &e, std::list< Vec > &path)
CyclicVecInt< DIM, NONCYCLIC > Vec
Definition: grid_astar.h:54
bool search(const Vec &s, const Vec &e, std::list< Vec > &path, std::function< float(const Vec &, Vec &, const Vec &, const Vec &)> cb_cost, std::function< float(const Vec &, const Vec &)> cb_cost_estim, std::function< std::vector< Vec > &(const Vec &, const Vec &, const Vec &)> cb_search, std::function< bool(const std::list< Vec > &)> cb_progress, const float cost_leave, const float progress_interval, const bool return_best=false)
Definition: grid_astar.h:158
GridAstarTestWrapper(const Vec &size)
std::unordered_map< Vec, Vec, Vec > parents_
Definition: grid_astar.h:347
int main(int argc, char **argv)
TEST(GridAstar, ParallelSearch)
ROSCPP_DECL bool search(const std::string &ns, const std::string &key, std::string &result)
bool findPath(const Vec &s, const Vec &e, std::list< Vec > &path)
Definition: grid_astar.h:328
std::unordered_map< Vec, Vec, Vec > & parentMap()


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Tue Jul 9 2019 05:00:13