-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar_helper.h
135 lines (122 loc) · 2.95 KB
/
star_helper.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
#include<bits/stdc++.h>
using namespace std;
#define INF INT_MAX
struct node
{
int x; // x co-ordinate of point or node
int y; // y co-ordinate
int z; // z co-ordinate
int g; //distance from source to this point
int h; //heuristic or distance estimate from this point to destination
bool cl; //true if evaluated or in closed list
bool op; //true if in open list or being evaluated
int index; //index of point in graph
node()
{
x=INF;
y=INF;
z=INF;
g=INF;
h=INF;
cl=false;
op=false;
index=INF;
}
};
bool manhattan=false; //true if distance measure for heuristic is manhattan
bool euclidean=false; //true if distance measure for heuristic is euclidean
struct mycomparator //comparator
{
bool operator()(node const& l,node const& r) //comparison function for priority queue
{
if(l.g+l.h>r.g+r.h) return true;
else if(l.g+l.h==r.g+r.h) return l.g>r.g;
return false;
}
};
/*
macro for defining priority queue with priority based on sum of 'g' and 'h'
*/
#define my_pq(l_name) priority_queue<node, vector<node>,mycomparator> l_name
void fill_nodes(struct node plane[],int num)
{
fstream fin;
int xco,yco;
fin.open("coord_input.txt"); //input file for co-ordinates of vertices
for(int i=0;i<num;i++)
{
fin>>xco>>yco;
plane[i].x=xco;
plane[i].y=yco;
plane[i].index=i;
}
fin.close();
}
void init_graph(vector< list<int> > &G,int num)
{
fstream fin;
fin.open("graph_input.txt"); //input file for representation of graph (adjacency list or adjacency matrix)
int adjLen,vert;
for(int i=0;i<num;i++)
{
fin>>adjLen;
list<int> list_;
for(int j=0;j<adjLen;j++){
fin>>vert;
list_.push_back(vert);
}
G.push_back(list_);
//for(int j=0;j<num;j++) fin>>G[i][j];
}
fin.close();
}
void print_graph(int **G,int num)
{
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++) cout<<G[i][j]<<" ";
cout<<endl;
}
}
void print_graph(vector< list<int> > G)
{
for(int i=0;i<G.size();i++)
{
cout<<i;
list<int>::iterator iter;
for(iter=G[i].begin();iter!=G[i].end();iter++)
{
cout<<" " <<*iter;
}
cout<<endl;
}
}
void print_nodes(struct node plane[],int num)
{
cout<<endl;
for(int i=0;i<num;i++)
{
cout<<plane[i].x<<" "<<plane[i].y<<endl;
}
cout<<endl;
}
int euclidean_distance(node a,node b)
{
return (int)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int manhattan_distance(node a,node b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
}
int distance(node a,node b) //calls distance measure as chosen
{
if(euclidean==true) return euclidean_distance(a,b);
if(manhattan==true) return manhattan_distance(a,b);
}
void calculate_h(node plane[],int num,int dest) //calculates heuristic from the current point to destination
{
for(int i=0;i<num;i++)
{
plane[i].h=distance(plane[i],plane[dest]);
}
}