-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstar.cpp
172 lines (126 loc) · 4.64 KB
/
Astar.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
This is a cpp program for implementing A* search algorithm based on
psuedocode available in wikipedia .See more at https://en.wikipedia.org/wiki/A*_search_algorithm
*/
#include "star_helper.h" //header file for helper functions like input and initialising data structures
/*this function prints the path from destination to source */
void construct_path(vector<int> cameFrom,int curIndex)
{
cout<<endl;
cout<<"path is:\n";
while(curIndex!=INF)
{
cout<<curIndex<<" ";
curIndex=cameFrom[curIndex]; //getting predecessor of node in shortest path
}
cout<<endl;
}
/* the search function*/
void AStar(int src,int dest,vector< list<int> > G,struct node plane[],int num)
{
/*
open_list is priority queue of nodes based on sum of distance measure 'g' and
heuristic estimate 'h'
my_pq() is a macro for defining the priority queue
*/
my_pq(open_list);
plane[src].op=true; //marking source node as in open list
plane[src].g=0; //distance from source to itself is zero
open_list.push(plane[src]); //adding source destination to the open list
vector<int> cameFrom; //vector for storing best predecessor in the path
cameFrom.resize(num,INF); //initializing vector with size=no. of nodes
/*
do until there is no node in priority queue
exit --->implies exhaustion of search
*/
while(!open_list.empty())
{
/*
the node which is closest or has lowest 'f'
where 'f=g+h' in the priority queue
*/
struct node current=open_list.top();
/*
destination node is reached in the priority queue
implies search is success
*/
if(current.index==dest)
{
construct_path(cameFrom,current.index); //routine for printing path from destination to source
return;
}
int cur_index=current.index;
plane[cur_index].op=false; //removing node from open list
open_list.pop();
plane[cur_index].cl=true; //adding the node to closed list
list<int>::iterator iter; //iterator for accessing vertices in adjacency list
for(iter=G[cur_index].begin();iter!=G[cur_index].end();iter++) {
int i=*iter;
if(plane[i].cl==true) continue; //if node is already in closed list (or evaluated)
int temp_g=current.g+distance(plane[cur_index],plane[i]); //sum of estimate and distance
if(plane[i].op==false) //if node is not in open list already
{
cameFrom[i]=cur_index; //updating predecessor of node in what would be a shortest path
plane[i].g=temp_g; //distance measure
plane[i].h=distance(plane[i],plane[dest]); //heuristic
open_list.push(plane[i]); //adding node to open list
}
else if(temp_g>=plane[i].g) continue; //adjacent vertex is not optimal to include in shortest path
}
/*
//below is a version for representation of graph using adjacency matrix
for(int i=0;i<num;i++)
{
if(G[cur_index][i]==1)
{
if(plane[i].cl==true) continue;
int temp_g=current.g+distance(plane[cur_index],plane[i]);
if(plane[i].op==false)
{
cameFrom[i]=cur_index;
plane[i].g=temp_g;
plane[i].h=distance(plane[i],plane[dest]);
open_list.push(plane[i]);
}
else if(temp_g>=plane[i].g) continue;
}
}*/
}
cout<<"\nNo Path\n";
}
int main()
{
/*
st-index of source vertex from nodes
en-destination index
num-total no. of vertices
*/
int st,en,num;
cout<<"enter no. of objects: ";
cin>>num;
node plane[num]; //nodes or vertices in the graph
fill_nodes(plane,num); //routine for taking coordinates input from file defined in "star_helper.h"
/*
representation for adjacency list where the graph is directed and weighted.
the weight of an edge is taken as the distance between them
*/
vector< list<int> > G;
/*
this is a version for taking input as adjacency matrix
//int **G;
//G=new int*[num];
//for(int i=0;i<num;i++) G[i]=new int[num];
*/
init_graph(G,num); //takes the adjacenct list input
cout<<"choose the distance measure:\n1.Euclidean\n2.Manhattan\n";
int inp_d;
cin>>inp_d; //choice for euclidean or manhattan distance measure
if(inp_d==1) euclidean=true;
else if(inp_d==2) manhattan=true;
cout<<"enter source index: ";
cin>>st;
cout<<"enter destination index: ";
cin>>en;
AStar(st,en,G,plane,num); // calling A-Star algorithm
return 0;
}