-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (99 loc) · 4.57 KB
/
main.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
#include "algorithms/graph.h"
#include "algorithms/Dijkstra.h"
#include "algorithms/A_Star.h"
#include "algorithms/GraphPartition.h"
#include "algorithms/GTree.h"
#include <random>
/**
* display the query information and query result
* @param testcaseID
* @param nodeS
* @param nodeT
* @param distance
* @param pathResult
*/
void displayResult(string algorithmName,unsigned int testcaseID,unsigned int nodeS,unsigned int nodeT,double distance,const vector<unsigned int> & pathResult,double time){
cout<<"algorithm: "<<algorithmName<<" "<<endl;
cout<<"testcase: "<<testcaseID<<". start node:"<<nodeS<<". end node: "<<nodeT<<endl;
cout<<"the shortest distance is:"<<distance<<endl;
cout<<"the path is :";
int pathsize=pathResult.size();
for(int i=0;i<pathsize;i++){
if(i!=pathsize-1){
cout<<pathResult[i]<<"->";
}else{
cout<<pathResult[i]<<endl;
}
}
cout<<"time used: "<<time<<" seconds."<<endl<<endl;
}
int main() {
// load road file (the road dataset is specified in file /algorithms/configure.h)
Graph* graph=new Graph();
cout<<"the number of nodes in graph is: "<<graph->graph_size<<endl;
Dijkstra dijkstra(graph);
A_Star aStar(graph);
vector<CoarsenGraph*> coarsenStage=vector<CoarsenGraph*>();
coarsening(graph,coarsenStage);
GTree gTree(coarsenStage,graph);
vector<vector<double>> timeComsumings=vector<vector<double>>(4);
srand((unsigned )time(NULL));
int testcaseNum=100;
for(int i=0;i<testcaseNum;i++){
unsigned int nodeS=rand()%graph->graph_size;
unsigned int nodeT=rand()%graph->graph_size;
// ======================= Dijkstra with priority queue =======================================================
clock_t start = 0, finish = 0;
start = clock();
vector<unsigned int> pathResult;
pathResult=vector<unsigned int> ();
//double distance= dijkstra.ShortestPathPriorityQueue(nodeS, nodeT, pathResult);
double distance=dijkstra.ShortestDistancePriorityQueue(nodeS,nodeT);
finish=clock();
double totaltime = (double) (finish - start) / CLOCKS_PER_SEC;
timeComsumings[0].push_back(totaltime);
displayResult("Dijkstra priority queue",i,nodeS,nodeT,distance,pathResult,totaltime);
// ============================================================================================================
// ======================= Dijkstra classic ===================================================================
start = 0, finish = 0;
start = clock();
pathResult=vector<unsigned int> ();
//distance= dijkstra.ShortestPath(nodeS, nodeT, pathResult);
distance=dijkstra.ShortestDistance(nodeS,nodeT);
finish=clock();
totaltime = (double) (finish - start) / CLOCKS_PER_SEC;
timeComsumings[1].push_back(totaltime);
displayResult("Dijkstra classic",i,nodeS,nodeT,distance,pathResult,totaltime);
//===========================================================================================================
// ======================= A * shortest with plane distance as heuristic ====================================
start = 0, finish = 0;
start = clock();
pathResult=vector<unsigned int> ();
//distance= aStar.ShortestPath(nodeS, nodeT, pathResult);
distance=aStar.ShortestDistance(nodeS,nodeT);
finish=clock();
totaltime = (double) (finish - start) / CLOCKS_PER_SEC;
timeComsumings[2].push_back(totaltime);
displayResult("A Star",i,nodeS,nodeT,distance,pathResult,totaltime);
//============================================================================================================
// ======================= G Tree shortest distance algorithm ===============================================
start = 0, finish = 0;
start = clock();
pathResult=vector<unsigned int> ();
// GTree has no shortest path version
distance=gTree.shortestDistance(nodeS,nodeT,graph);
finish=clock();
totaltime = (double) (finish - start) / CLOCKS_PER_SEC;
timeComsumings[3].push_back(totaltime);
displayResult("G Tree",i,nodeS,nodeT,distance,pathResult,totaltime);
//============================================================================================================
}
for(int i=0;i<4;i++){
cout<<"algorithm "<<i<<endl;
for(int j=0;j<testcaseNum;j++){
cout<<timeComsumings[i][j]<<",";
}
cout<<endl;
}
return 0;
}