This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
breadthfirstsearch.cpp
152 lines (143 loc) · 3.81 KB
/
breadthfirstsearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "breadthfirstsearch.h"
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
typedef boost::chrono::high_resolution_clock Clock;
typedef boost::chrono::milliseconds Miliseconds;
typedef Clock::time_point TimePoint;
typedef Clock::duration Duration;
//#define DEBUG
static inline bool inList(std::set<int> s, int v)
{
for(int i : s)
if(i==v)
return true;
return false;
}
BreadthFirstSearch::BreadthFirstSearch() : GraphSearch()
{
//std::set<int> o = {1,11};
//setObstacles(o);
//printObstacles();
std::cout << "solving.. \nstart_index : " << start_index << "\ngoal_index : " << goal_index << '\n';
if(std::get<0>(solve())){
std::cout << "solved!\n";
printSolution();
}
else
std::cout << "solution not found\n";
}
BreadthFirstSearch::BreadthFirstSearch(int x, int y) : GraphSearch(x,y)
{
/*
std::cout << "solving.. \nstart_index : " << start_index << "\ngoal_index : " << goal_index << '\n';
if(solve()){
std::cout << "solved!\n";
printSolution();
}
else
std::cout << "solution not found\n";
*/
}
std::tuple<int, double> BreadthFirstSearch::solve()
{
TimePoint t = Clock::now();
Duration d = t.time_since_epoch();
Miliseconds start_ms = boost::chrono::duration_cast<Miliseconds>(d);
Miliseconds end_ms;
Miliseconds dt;
int x;
while(!q.empty())
q.pop();
#ifdef DEBUG
std::pair<int,int> d_idx;
#endif
GraphIterator v_it, v_end;
q.push(start_index);
(*g)[vertex(start_index,*g)].state = Alive;
parent_map[start_index] = -1;
for(int i : visitedList)
(*g)[i].state = Unvisited;
visitedList.clear();
while(!q.empty()) {
x = q.front();
if(x == goal_index) {
markSolutions();
s = true;
need_update = false;
t = Clock::now();
d = t.time_since_epoch();
end_ms = boost::chrono::duration_cast<Miliseconds>(d);
dt = end_ms - start_ms;
return std::make_tuple(1,dt.count()/1000.0);
}
#ifdef DEBUG
d_idx = to_double_index(x);
std::cout << "adjacent vertices of " << x << " (" << d_idx.first << "," << d_idx.second << ") : ";
#endif
for(boost::tie(v_it, v_end) = boost::adjacent_vertices(vertex(x,*g),*g);
v_it != v_end; ++v_it) {
#ifdef DEBUG
std::cout << *v_it << " ";
#endif
auto l = obstacles.find(*v_it);
if(l == obstacles.end()) {
#ifdef DEBUG
std::cout << "free ";
#endif
if((*g)[*v_it].state == Unvisited) {
#ifdef DEBUG
std::cout << "unvisited ";
#endif
(*g)[*v_it].state = Alive;
parent_map[*v_it] = x;
visitedList.insert(*v_it);
q.push(*v_it);
}
else {
(*g)[*v_it].state = Dead;
#ifdef DEBUG
std::cout << "visited ";
#endif
}
}
#ifdef DEBUG
else {
std::cout << "obstacle ";
}
#endif
}
#ifdef DEBUG
std::cout << '\n';
#endif
q.pop();
#ifdef USE_DELAY
if(en_delay)
boost::this_thread::sleep_for(boost::chrono::milliseconds(delay_ms));
#endif
}
need_update = false;
t = Clock::now();
d = t.time_since_epoch();
end_ms = boost::chrono::duration_cast<Miliseconds>(d);
dt = end_ms - start_ms;
s = false;
return std::make_tuple(0,dt.count()/1000.0);
}
std::string BreadthFirstSearch::string(int i, int option)
{
std::pair<int,int> p = to_double_index(i);
std::string s("(");
s += std::to_string(p.first);
s += ",";
s += std::to_string(p.second);
s += ")";
//s << std::to_string(p.first);
//s.insert(std::to_string(p.first));
//s << ",";
//s.insert(std::string(","));
//s << std::to_string(p.second);
//s.insert(std::to_string(p.second));
//s << ")";
//s.insert(std::string(")"));
return s;
}