-
Notifications
You must be signed in to change notification settings - Fork 4
/
Search.h
165 lines (143 loc) · 3.99 KB
/
Search.h
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
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef SEARCH_H_
#define SEARCH_H_
#include <vector> // std::vector, std::cout
#include <time.h> // clock_t, clock, CLOCKS_PER_SEC
#include <math.h> // pow, abs, sqrt
#include "Vertex.h"
#include "Edge.h"
#include "Graph.h"
#include "Node.h"
#include "Queue.h"
using std::vector ;
using std::cout ;
typedef unsigned long int ULONG ;
// Path search class to store and search a graph
// A* search with zero, Manhattan or Euclidean distance heuristics
class Search
{
public:
Search(Graph * graph, Vertex * source, Vertex * goal):itsGraph(graph), itsSource(source), itsGoal(goal){}
~Search(){
delete itsQueue ;
itsQueue = 0 ;
}
Graph * GetGraph() const {return itsGraph ;}
Queue * GetQueue() const {return itsQueue ;}
void SetQueue(Queue * queue) {itsQueue = queue ;}
Vertex * GetSource() const {return itsSource ;}
Vertex * GetGoal() const {return itsGoal ;}
vector<Node *> PathSearch(pathOut pType) ;
private:
Graph * itsGraph ;
Queue * itsQueue ;
Vertex * itsSource ;
Vertex * itsGoal ;
ULONG FindSourceID() ;
double ManhattanDistance(Vertex * v1, Vertex * v2) ;
double EuclideanDistance(Vertex * v1, Vertex *v2) ;
void UpdateNode(Node * n) ;
} ;
vector<Node *> Search::PathSearch(pathOut pType)
{
ULONG sourceID = FindSourceID() ;
itsQueue = new Queue(new Node(itsGraph->GetVertices()[sourceID], SOURCE),pType) ;
while (!itsQueue->EmptyQueue()){
// Pop cheapest node from queue
Node * currentNode = itsQueue->PopQueue() ;
if (!currentNode){
// Dominated node was popped off queue
continue ;
}
// Terminate search once one path is found
if (currentNode->GetVertex() == itsGoal){
if (pType == BEST)
break ;
else
continue ;
}
// Find all neighbours excluding ancestor vertices if any
vector<Edge *> neighbours = itsGraph->GetNeighbours(currentNode) ;
// Update neighbours
for (ULONG i = 0; i < (ULONG)neighbours.size(); i++){
// Create neighbour node
Node * currentNeighbour = new Node(currentNode, neighbours[i]) ;
UpdateNode(currentNeighbour) ;
itsQueue->UpdateQueue(currentNeighbour) ;
}
}
// Check if a path is found
bool ClosedAll = false ;
for (ULONG i = 0; i < itsQueue->GetClosed().size(); i++){
if (itsQueue->GetClosed()[i]->GetVertex() == itsGoal){
ClosedAll = true ;
break ;
}
}
if (!ClosedAll){
cout << "No path found from source to goal.\n" ;
vector<Node *> bestPath ;
return bestPath ;
}
else{
ULONG k = 0 ;
vector<Node *> bestPath((ULONG)itsQueue->GetClosed().size()) ;
for (ULONG i = 0; i < (ULONG)itsQueue->GetClosed().size(); i++){
if (itsGoal == itsQueue->GetClosed()[i]->GetVertex()){
bestPath[k] = itsQueue->GetClosed()[i] ;
k++ ;
}
}
bestPath.resize(k) ;
return bestPath ;
}
}
ULONG Search::FindSourceID()
{
for (ULONG i = 0; i < itsGraph->GetNumVertices(); i++)
if (itsSource == itsGraph->GetVertices()[i])
return i ;
cout << "Error: souce ID not found. Exiting.\n" ;
exit(1) ;
}
double Search::ManhattanDistance(Vertex * v1, Vertex * v2)
{
double diffX = abs(v1->GetX() - v2->GetX()) ;
double diffY = abs(v1->GetY() - v2->GetY()) ;
double diff = diffX + diffY ;
return diff ;
}
double Search::EuclideanDistance(Vertex * v1, Vertex *v2)
{
double diffX = pow(v1->GetX() - v2->GetX(),2) ;
double diffY = pow(v1->GetY() - v2->GetY(),2) ;
double diff = sqrt(diffX+diffY) ;
return diff ;
}
void Search::UpdateNode(Node * n)
{
if (SEARCH_TYPE == ASTAR)
{
double diff ;
switch (HEURISTIC)
{
case MANHATTAN:
diff = ManhattanDistance(itsGoal, n->GetVertex()) ;
n->SetHeuristic(diff) ;
break ;
case EUCLIDEAN:
diff = EuclideanDistance(itsGoal, n->GetVertex()) ;
n->SetHeuristic(diff) ;
break ;
default:
diff = 0.0 ;
n->SetHeuristic(diff) ;
}
}
else if (SEARCH_TYPE == DIJKSTRA)
{
HEURISTIC = ZERO ;
SEARCH_TYPE = ASTAR ;
UpdateNode(n) ;
}
}
#endif // SEARCH_H_