-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_Log_ND_Set.cpp
265 lines (223 loc) · 7.75 KB
/
main_Log_ND_Set.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifndef PTHRESH
#define PTHRESH 0.60
#endif
enum searchType {ASTAR, DIJKSTRA} ; // BREADTH, DEPTH
enum heuristic {ZERO, MANHATTAN, EUCLIDEAN, RAGSCOMPARE} ;
enum pathOut {BEST,ALL} ;
searchType SEARCH_TYPE = ASTAR ;
heuristic HEURISTIC = ZERO ;
#include <iostream> // std::cout
#include <utility> // std::pair
#include <fstream> // std::ifstream, std::ofstream
#include <sstream> // std::stringstream
#include <string> // std::string
#include <random> // std::default_random_engine, std::uniform_real_distribution
#include <chrono> // std::chrono_system_clock::now
#include <algorithm> // std::count, std::find
#include <time.h> // time, clock, clock_t, CLOCKS_PER_SEC
#include <math.h> // pow, sqrt
#include <stdlib.h> // atof, atol, rand, srand
#include "RAGS.h"
#include "XY.h"
using namespace easymath ;
using std::cout ;
using std::cin ;
using std::pair ;
using std::ifstream ;
using std::ofstream ;
using std::stringstream ;
using std::string ;
using std::default_random_engine ;
using std::uniform_real_distribution ;
using std::normal_distribution ;
using std::count ;
using std::find ;
typedef pair<int, int> edge ;
typedef unsigned long int ULONG ;
#include "ExecutePlanners.h"
#include "GraphGeneration.h"
int main(){
int trialNum = 0 ;
int sigMax = 20 ;
int buffSize = 100 ;
char fileDir[buffSize] ;
sprintf(fileDir,"logs/maxSig_%d/pThresh_%.2f",sigMax,PTHRESH) ;
char configDir[buffSize] ;
sprintf(configDir,"config_files/maxSig_%d",sigMax) ;
char mkdir[buffSize] ;
sprintf(mkdir,"mkdir -p %s",fileDir) ;
system(mkdir) ;
sprintf(mkdir,"mkdir -p %s",configDir) ;
system(mkdir) ;
// Initialising graph parameters *****************************************************************
// Load vertex locations from txt file
cout << "Reading vertices from file: " ;
char vFile[buffSize] ;
sprintf(vFile,"%s/vertices%d.txt",configDir,trialNum) ;
ifstream verticesFile(vFile) ;
cout << vFile << "..." ;
if (!verticesFile.is_open()){
cout << "\nFile: " << vFile << " not found, skipping.\n" ;
exit(1) ;
}
vector<XY> vertices ;
vector<double> v(2) ;
string line ;
while (getline(verticesFile,line))
{
stringstream lineStream(line) ;
string cell ;
int i = 0 ;
while (getline(lineStream,cell,','))
{
v[i++] = atof(cell.c_str()) ;
}
vertices.push_back(XY(v[0],v[1])) ;
}
cout << "complete.\n" ;
// Load edge connections from txt file
cout << "Reading edges from file: " ;
char eFile[buffSize] ;
sprintf(eFile,"%s/edges%d.txt",configDir,trialNum) ;
ifstream edgesFile(eFile) ;
cout << eFile << "..." ;
if (!edgesFile.is_open()){
cout << "\nFile: " << eFile << " not found, skipping.\n" ;
exit(1) ;
}
vector<edge> edges ;
vector<double> e ;
vector< vector<double> > cost_distributions ;
vector<double> c(2) ;
while (getline(edgesFile,line))
{
stringstream lineStream(line) ;
string cell ;
e.clear() ;
while (getline(lineStream,cell,','))
e.push_back(atof(cell.c_str())) ;
edge temp((int)e[0],(int)e[1]) ;
c[0] = e[2] ;
c[1] = e[3] ;
edges.push_back(temp) ;
cost_distributions.push_back(c) ;
}
cout << "complete.\n" ;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator(seed);
// END: Initialising graph parameters ************************************************************
// START: Compute RAGS ND-set ********************************************************************
// Define planning task
XY start = vertices[0] ; // with random graph: vertices0.txt, edges0.txt
XY goal = vertices[vertices.size()-1] ; // with random graph: vertices0.txt, edges0.txt
// Write ND set to file
stringstream ndFileName ;
ndFileName << fileDir << "/ND_set" << trialNum << ".txt" ;
ofstream ndSetFile ;
ndSetFile.open(ndFileName.str().c_str(),std::ifstream::app) ;
// Create RAGS object
RAGS * testRAGS = new RAGS(vertices, edges, cost_distributions) ;
// Compute ND set
XY s ;
testRAGS->InitialiseNDSet(start,goal,s) ;
vector<Node *> NDSet = testRAGS->GetNDSet() ;
for (size_t i = 0; i < NDSet.size(); i++){
ndSetFile << NDSet[i]->GetVertex()->GetX() << "," << NDSet[i]->GetVertex()->GetY() << "\n" ;
Node * n = NDSet[i]->GetParent() ;
while (n){
ndSetFile << n->GetVertex()->GetX() << "," << n->GetVertex()->GetY() << "\n" ;
n = n->GetParent() ;
}
}
ndSetFile.close() ;
// // Display ND set
// for (size_t i = 0; i < testRAGS->GetNDSetSize(); i++)
// testRAGS->GetNDSet()[i]->DisplayPath() ;
// Write ND set to binary file
cout << "Writing ND-set to binaries...\n" ;
stringstream ndBinFileName ;
ndBinFileName << fileDir << "/ND_set" << trialNum << ".dat" ;
ofstream binaryNDSetFile ;
binaryNDSetFile.open(ndBinFileName.str().c_str(), std::ios::out | std::ios::binary) ;
testRAGS->NDSetWriteBinary(binaryNDSetFile) ;
binaryNDSetFile.close() ;
cout << "Writing complete.\n" ;
// Query for true edge costs and write to txt file
vector<double> true_costs ;
for (ULONG i = 0; i < edges.size(); i++){
normal_distribution<double> distribution(cost_distributions[i][0], cost_distributions[i][1]) ;
double c ;
while (true){
c = distribution(generator) ;
if (c > 0) // do not allow negative costs
break ;
}
true_costs.push_back(c) ;
}
// Write RAGS path file
cout << "Executing RAGS path on original RAGS object...\n" ;
stringstream pFileName ;
pFileName << fileDir << "/RAGS_path" << trialNum << ".txt" ;
ofstream pFile ;
pFile.open(pFileName.str().c_str(),std::ifstream::app) ;
// Initialise current location
XY curLoc = start ;
// Execute RAGS path
while (true){
pFile << curLoc.x << "," << curLoc.y << "\n" ;
cout << "Current vertex: (" << curLoc.x << "," << curLoc.y << ")\n" ;
if (curLoc == goal){
break ;
}
// Execute RAGS transition
XY nextLoc = testRAGS->SearchGraph(curLoc,goal,true_costs) ;
// Check transition was successful
if (curLoc == nextLoc){
cout << "Transition unsuccessful! Graph #" << trialNum << "\n" ;
exit(1) ;
}
curLoc = nextLoc ;
}
pFile.close() ;
delete testRAGS ;
// Create new RAGS object and read in stored ND set
cout << "Reading in ND-set to new RAGS object from binary files...\n" ;
std::ifstream readNDSetFile ;
readNDSetFile.open(ndBinFileName.str().c_str(), std::ios::in | std::ios::binary) ;
RAGS * newRAGS = new RAGS(vertices, edges, cost_distributions) ;
newRAGS->NDSetReadBinary(readNDSetFile) ; // read in ND set from binary file and write to itsNDSet
readNDSetFile.close() ;
cout << "Reading complete.\n" ;
// Write new RAGS path file
cout << "Executing RAGS path on new RAGS object...\n" ;
stringstream npFileName ;
npFileName << fileDir << "/RAGS_path_new" << trialNum << ".txt" ;
ofstream npFile ;
npFile.open(npFileName.str().c_str(),std::ifstream::app) ;
// // Display ND set
// for (size_t i = 0; i < newRAGS->GetNDSetSize(); i++)
// newRAGS->GetNDSet()[i]->DisplayPath() ;
// Initialise current location
curLoc = start ;
newRAGS->SetInitialVert(start) ;
// Execute RAGS path
while (true){
npFile << curLoc.x << "," << curLoc.y << "\n" ;
cout << "Current vertex: (" << curLoc.x << "," << curLoc.y << ")\n" ;
if (curLoc == goal){
break ;
}
// Execute RAGS transition
XY nextLoc = newRAGS->SearchGraph(curLoc,goal,true_costs) ;
// Check transition was successful
if (curLoc == nextLoc){
cout << "Transition unsuccessful! Graph #" << trialNum << "\n" ;
exit(1) ;
}
curLoc = nextLoc ;
}
npFile.close() ;
delete newRAGS ;
// END: Compute RAGS ND-set **********************************************************************
return 0 ;
}