-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectivityGraph.cpp
More file actions
310 lines (253 loc) · 6.97 KB
/
ConnectivityGraph.cpp
File metadata and controls
310 lines (253 loc) · 6.97 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "ConnectivityGraph.h"
#include "Link.h"
#include <sstream>
#include <fstream>
#include <map>
#include <chrono>
using namespace std;
ConnectivityGraph::ConnectivityGraph() : fictiveId(-1)
{
}
ConnectivityGraph::~ConnectivityGraph()
{
}
float ConnectivityGraph::pathLength(const std::list<int>& path)
{
return prGr.pathLength(path);
}
void ConnectivityGraph::insertElem(TPrimitive e)
{
prGr.insertElem(e);
if (e.component_) //inserting component
hypGr.insertElem(HyperPrimitive(e.id_, HyperPrimitive::COMPONENT));
else //inserting element (need to insert in a set)
if (!hypGr.contain(e.set_))
hypGr.insertElem(HyperPrimitive(e.set_, HyperPrimitive::SET));
}
#define PENALTY 1000000000
#define SUPER_PENALTY 10000000000
void ConnectivityGraph::insertLink(const Link& lk)
{
// prGr.insertLink(lk);
auto eFromPtr = prGr.getElemPtr(lk.from);
auto eToPtr = prGr.getElemPtr(lk.to);
if (eFromPtr->component_ && eToPtr->component_)
{
hypGr.insertLink(Link(eFromPtr->id_, eToPtr->id_, PENALTY));
prGr.insertLink(lk);
}
else if (eFromPtr->component_)
{
hypGr.insertLink(Link(eFromPtr->id_, eToPtr->set_, PENALTY));
auto hpPtr = hypGr.getElemPtr(eToPtr->set_);
if (hpPtr != NULL)
hpPtr->cnt_++;
prGr.insertLink(lk);
}
else if (eToPtr->component_)
{
hypGr.insertLink(Link(eFromPtr->set_, eToPtr->id_, PENALTY));
auto hpPtr = hypGr.getElemPtr(eFromPtr->set_);
if (hpPtr != NULL)
hpPtr->cnt_++;
prGr.insertLink(lk);
}
else if (eFromPtr->set_ != eToPtr->set_) //link between 2 different sets
//creating fictive
{
int id = fictiveId;
--fictiveId;
TPrimitive pr(-1, -1, id, true, -1);
insertElem(pr);
Link lk1(id, lk.from, SUPER_PENALTY);
Link lk2(id, lk.to, SUPER_PENALTY);
prGr.insertLink(lk1);
prGr.insertLink(lk2);
hypGr.insertLink(Link(eFromPtr->set_, id, SUPER_PENALTY));
auto hpPtr = hypGr.getElemPtr(eFromPtr->set_);
if (hpPtr != NULL)
hpPtr->cnt_++;
hypGr.insertLink(Link(eToPtr->set_, id, SUPER_PENALTY));
hpPtr = hypGr.getElemPtr(eToPtr->set_);
if (hpPtr != NULL)
hpPtr->cnt_++;
prGr.insertLink(lk1);
prGr.insertLink(lk2);
}
else
prGr.insertLink(lk);
}
void ConnectivityGraph::removeElement(int id)
{
TPrimitive* prPtr = prGr.getElemPtr(id);
if (prPtr->component_)
{
hypGr.remove(id);
prGr.remove(id);
}
else
{
HyperPrimitive* hypPrPrt = hypGr.getElemPtr(prPtr->set_);
int hypId = hypPrPrt->id_;
hypPrPrt->cnt_--;
if(hypPrPrt->cnt_ == 0)
hypGr.remove(hypId);
prGr.remove(id);
}
}
void ConnectivityGraph::insertLinks(const std::list<Link>& lk)
{
prGr.insertLinks(lk);
for (auto& it : lk)
insertLink(it);
}
void printPath(const list<int>& path)
{
for (auto& it : path)
cout << it << " --> ";
cout << endl;
}
std::list<int> ConnectivityGraph::findPath(int start, int goal)
{ //start & goal are not the components but elements in some set
TPrimitive* sPrim = prGr.getElemPtr(start);
TPrimitive* gPrim = prGr.getElemPtr(goal);
if (sPrim->set_ == gPrim->set_)
return prGr.findPathSameSet(start, goal);
else
{
auto t_start = std::chrono::high_resolution_clock::now();
list<int> hypPth = hypGr.findPath(gPrim->set_, sPrim->set_);
auto t_end = std::chrono::high_resolution_clock::now();
cout << "duration hyper path = " <<
std::chrono::duration<double, std::milli>(t_end - t_start).count() <<
" ms." << endl;
list<int> pth;
cout << "Hyper path:" << endl;
printPath(hypPth);
//searching the path from start set element to component
auto hypPthIt = hypPth.begin();
hypPthIt++;
t_start = std::chrono::high_resolution_clock::now();
list<int> outPth = prGr.findPath(*hypPthIt, start, sPrim->set_);
t_end = std::chrono::high_resolution_clock::now();
cout << "duration out path = " <<
std::chrono::duration<double, std::milli>(t_end - t_start).count() <<
" ms." << endl;
cout << "Out path:" << endl;
// printPath(outPth);
//loop searching paths from component to component trough the set
hypPth.pop_front();
while (hypPth.size() > 2)
{
hypPthIt = hypPth.begin();
int comFrom = *(hypPthIt);
++hypPthIt;
int setThrough = *(hypPthIt);
++hypPthIt;
int comTo = *(hypPthIt);
cout << comTo << " <-- " << comFrom << endl;
t_start = std::chrono::high_resolution_clock::now();
pth = prGr.findPath(comTo, comFrom, setThrough);
t_end = std::chrono::high_resolution_clock::now();
cout << "duration path = " <<
std::chrono::duration<double, std::milli>(t_end - t_start).count() <<
" ms." << endl;
// printPath(pth);
for (auto& it : pth)
outPth.push_back(it);
pth.clear();
hypPth.pop_front();
hypPth.pop_front();
}
//searching the path from last component to goal set element
if (!hypPth.empty())
{
hypPthIt = hypPth.begin();
t_start = std::chrono::high_resolution_clock::now();
pth = prGr.findPath(goal, *hypPthIt, gPrim->set_);
t_end = std::chrono::high_resolution_clock::now();
cout << "duration in path = " <<
std::chrono::duration<double, std::milli>(t_end - t_start).count() <<
" ms." << endl;
// printPath(pth);
if (!pth.empty())
{
pth.pop_front();
for (auto& it : pth)
outPth.push_back(it);
}
else
cout << "Connectivity corruption" << endl;
}
return outPth;
}
return std::list<int>();
}
void ConnectivityGraph::read(string filename)
{
fstream fstr;
stringstream sstr;
string s;
int id, x, y, set, comp;
int lid;
map<int, TPrimitive> primitives;
list<Link> links;
map<TPrimitive, list<Link> > componentLinks;
fstr.open(filename);
while (!fstr.eof())
{
getline(fstr, s);
getline(fstr, s);
sstr.clear();
sstr << s;
sstr >> id >> x >> y >> set >> comp;
if (comp)
set = -1;
TPrimitive primitive(x, y, id, static_cast<bool>(comp), set);
primitives.insert(pair<int, TPrimitive>(id, primitive));
sstr.clear();
getline(fstr, s);
if (!s.empty())
{
sstr << s;
while (!sstr.eof())
{
sstr >> lid;
if (lid != id)
links.push_back(Link(id, lid, 0.0));
}
}
}
fstr.close();
// componentLinks.insert(pair <TPrimitive, list<Link> >(primitive, list<Link>()));
//set Link weights as euclidian length
for (auto& it : links)
{
auto fromIt = primitives.find(it.from);
auto toIt = primitives.find(it.to);
if (fromIt == primitives.end())
continue;
if (fromIt == primitives.end())
continue;
const TPrimitive& from = fromIt->second;
const TPrimitive& to = toIt->second;
float dx = static_cast<float>(from.x_ - to.x_);
float dy = static_cast<float>(from.y_ - to.y_);
it.weight = sqrtf(dx*dx + dy*dy);
}
auto t_start = std::chrono::high_resolution_clock::now();
for (auto& it : primitives)
insertElem(it.second);
for (auto& it : links)
insertLink(it);
auto t_end = std::chrono::high_resolution_clock::now();
cout << "duration of load = " <<
std::chrono::duration<double, std::milli>(t_end - t_start).count() <<
" ms." << endl;
// prGr.print();
// prGr.toGraphize("D:/dev/graph.txt");
// cout << endl << endl;
hypGr.toGraphize("./hyp_graph.txt");
// hypGr.print();
// cout << endl << endl;
}