-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAGV.cpp
434 lines (418 loc) · 12.6 KB
/
AGV.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "AGV.h"
AGV::AGV(string fileName)
{
ifstream file(fileName);
int numberOfVertices, numberOfEdges, numberOfVehicules;
vector<map<int, int>> guidePath;
A.clear();
file >> numberOfVertices >> numberOfEdges >> numberOfVehicules >> nh;
for (int i = 0; i < numberOfVertices; i++)
{
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
}
for (int i = 0; i < numberOfEdges; i++)
{
int from, to, count;
file >> from >> to >> count;
if (guidePath[from].find(to) == guidePath[from].end())
{
guidePath[from][to] += count;
}
else
{
guidePath[from].insert(pair<int, int>(to, count));
}
}
for (int i = 0; i < numberOfVehicules; i++)
{
int from, to, count;
file >> from >> to >> count;
if (A[from].find(to) == A[from].end())
{
A[from][to] += count;
}
else
{
A[from].insert(pair<int, int>(to, count));
}
}
file.close();
G = MultiGraph(guidePath, nh);
Ghat = G;
if (Ghat.AddVehicules(A))
cout << "All vehicules added" << endl;
else
cout << "Problem in adding vehicules" << endl;
}
AGV::AGV(string fileName, bool condensed)
{
ifstream file(fileName);
int numberOfVertices, numberOfUndirectedEdges, numberOfDirectedEdges;
vector<map<int, int>> guidePath;
A.clear();
file >> numberOfVertices >> numberOfUndirectedEdges >> numberOfDirectedEdges >> nh;
for (int i = 0; i < numberOfVertices; i++)
{
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
}
int verticesCount = numberOfVertices;
// read the capacities
for (int i = 0; i < numberOfVertices; i++)
{
int capacity = 0;
file >> capacity;
if (capacity == 1)
{
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
guidePath[i].insert(pair<int, int>(verticesCount, 2));
A[i].insert(pair<int, int>(verticesCount, 1));
verticesCount++;
}
else if (capacity > 1)
{
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
guidePath[i].insert(pair<int, int>(verticesCount, capacity));
verticesCount++;
}
}
// read the undirected edges
for (int i = 0; i < numberOfUndirectedEdges; i++)
{
int from, to, weight;
file >> from >> to >> weight;
if (weight == 1)
{
if (guidePath[from].find(to) == guidePath[from].end())
{
guidePath[from][to] += 1;
}
else
{
guidePath[from].insert(pair<int, int>(to, 1));
}
}
else if (weight > 1)
{
vector<int> IntermediateVertices;
for (int j = 1; j < weight; j++)
{
IntermediateVertices.push_back(verticesCount);
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
verticesCount++;
}
guidePath[from].insert(pair<int, int>(IntermediateVertices[0], 1));
guidePath[IntermediateVertices[weight - 2]].insert(pair<int, int>(to, 1));
for (int j = 0; j < weight - 2; j++)
{
guidePath[IntermediateVertices[j]].insert(pair<int, int>(IntermediateVertices[j + 1], 1));
}
}
}
// read the directed edges
for (int i = 0; i < numberOfDirectedEdges; i++)
{
int from, to, weight;
file >> from >> to >> weight;
if (weight == 1)
{
if (guidePath[from].find(to) == guidePath[from].end())
{
guidePath[from][to] += 1;
}
else
{
guidePath[from].insert(pair<int, int>(to, 1));
}
if (A[from].find(to) == A[from].end())
{
A[from][to] += 1;
}
else
{
A[from].insert(pair<int, int>(to, 1));
}
}
else if (weight > 1)
{
vector<int> IntermediateVertices;
for (int j = 1; j < weight; j++)
{
IntermediateVertices.push_back(verticesCount);
guidePath.push_back(map<int, int>());
A.push_back(map<int, int>());
verticesCount++;
}
guidePath[from].insert(pair<int, int>(IntermediateVertices[0], 1));
guidePath[IntermediateVertices[weight - 2]].insert(pair<int, int>(to, 1));
A[from].insert(pair<int, int>(IntermediateVertices[0], 1));
A[IntermediateVertices[weight - 2]].insert(pair<int, int>(to, 1));
for (int j = 0; j < weight - 2; j++)
{
guidePath[IntermediateVertices[j]].insert(pair<int, int>(IntermediateVertices[j + 1], 1));
A[IntermediateVertices[j]].insert(pair<int, int>(IntermediateVertices[j + 1], 1));
}
}
}
file.close();
G = MultiGraph(guidePath, nh);
Ghat = G;
if (Ghat.AddVehicules(A))
cout << "All vehicules added" << endl;
else
cout << "Problem in adding vehicules" << endl;
}
bool AGV::ExploredBefore(int _nodes, vector<int> _hash)
{
// This condition will be executed only once when the graph is checked for the first time
if (ExploredConfigurations.empty())
{
for (int i = 0; i < _nodes; i++)
ExploredConfigurations.push_back(vector<vector<int>>());
return false;
}
for (int i = 0; i < ExploredConfigurations[_nodes].size(); i++)
{
if (ExploredConfigurations[_nodes][i].size() == _hash.size())
{
if (std::equal(ExploredConfigurations[_nodes][i].begin(), ExploredConfigurations[_nodes][i].end(), _hash.begin()))
{
return true;
}
}
}
ExploredConfigurations[_nodes].push_back(_hash);
return false;
}
bool AGV::IsLive()
{
CondensedMultiGraph C_Ghat(&Ghat);
//priority_queue<CondensedMultiGraph, vector<CondensedMultiGraph>, less<vector<CondensedMultiGraph>::value_type>> pqueue;
//pqueue.push(C_Ghat);
stack<pair<CondensedMultiGraph, int>> STACK; //int is the level in the computation tree
STACK.push(make_pair(C_Ghat, 0));
// Implement main Algorithm here.
int i = 0;
int level;
while (!STACK.empty())
{
CondensedMultiGraph C = STACK.top().first;
level = STACK.top().second;
STACK.pop();
if (C.isSingleChained())
return true;
DirectedAcyclicMultiGraph G = DirectedAcyclicMultiGraph(&C);
if (G.IsTree())
{
cout << "Tree" << endl;
TreeLiveness TL = G;
if (TL.IsLive())
return true;
}
else
{
CondensedMultiGraph Cd;
bool terminate = false;
if (G.TerminalNodesCapacityLessThanAllInEdges())
{
// Do Nothing
cout << " nothing pushing to stack at level = " << level << endl;
}
else if (G.ExistAProducerMergerEdge(&Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack because there is a producer merger at level = " << level << endl;
}
else if (G.ExistAPathLeadingToNH(&Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack because there is a path leading to n_h at level = " << level << endl;
}
else
{
vector<int> apLevel;
vector<int> art = G.GetArticulationPoints(apLevel);
if (art.size() == 0) // run algorithm 4 without constructing the tree
{
Algorithm5 alg5;
if (alg5.OneStep(G, &Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 5 at level = " << level << endl;
}
else
{
// Do Nothing (Terminate)
cout << " nothing pushing to stack due to Algorithm 5 at level = " << level << endl;
}
}
else // construct the block-cutpoint tree and run algorithm 5
{
BlockCutpointTree BCT = BlockCutpointTree(&G, art, apLevel);
while (true)
{
int Block = BCT.GetNextBlockToProcess();
if (Block == -1) // finished processing and no more blocks could be explored
{
break;
}
int parentAP = BCT.ParentAP[Block];
BlockType type = BCT.Types[Block];
cout << "processing a block corresponding to case " << type << " of Algorithm 6 at level = " << level << endl;
if (type == BlockType::CASE_I)
{
int from = BCT.ParentAP[Block];
if (from == -1)
from = G.nh;
set<int> temp = BCT.Blocks[Block];
temp.erase(from);
int to = *temp.begin();
if (G.ExistAFeasibleMergerEdge(from, to, &Cd))
{
STACK.push(make_pair(Cd, level + 1));
}
else
{
//push nothing
cout << " nothing pushing to stack due to Algorithm 6 case I at level = " << level << endl;
}
break;
}
else if (type == BlockType::CASE_II)
{
// if it has a subtree that we could simulate
if (!BCT.Children[Block].empty())
{
int to = BCT.ParentAP[Block];
int from;
if (to == -1)
{
from = G.nh;
set<int> temp = BCT.Blocks[Block];
temp.erase(from);
to = *temp.begin();
}
else
{
set<int> temp = BCT.Blocks[Block];
temp.erase(to);
from = *temp.begin();
}
if (G.ExistAFeasibleMergerEdge(from, to, &Cd))
{
// DO SIMULATION CODE HERE
int capacity = G.capacities[parentAP];
int vertex = G.C->vertices[G.nodes[parentAP][0]][0];
set<int> sTreeNodes = BCT.GetSubTreeNodes(Block);
set<int> sTreeVertices;
for (set<int>::iterator itrs = sTreeNodes.begin(); itrs != sTreeNodes.end(); itrs++)
sTreeVertices.insert(G.C->vertices[G.nodes[*itrs][0]][0]);
Simulation sim(Cd, vertex, capacity, sTreeVertices);
if (sim.simulate(Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 6 Case II at level = " << level << endl;
break;
}
}
}
// mark block as processed and get another block.
BCT.Processed[Block] = true;
cout << " Marking block as processed due to Algorithm 6 case II at level = " << level << endl;
}
else if (type == BlockType::CASE_III)
{
Algorithm5 alg5;
BlockDirectedAcyclicMultiGraph_3 BDAG(&G, BCT.Blocks[Block], parentAP);
if (alg5.OneStep(BDAG, &Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 6 Case III at level = " << level << endl;
}
else
{
// Do Nothing (Terminate)
cout << " nothing pushing to stack due to Algorithm 6 Case III at level = " << level << endl;
}
break;
}
else if (type == BlockType::CASE_IV)
{
Algorithm5 alg5;
BlockDirectedAcyclicMultiGraph_4 BDAG(&G, BCT.Blocks[Block], parentAP);
bool simulate = false;
if (alg5.OneStep(BDAG, &Cd, simulate))
{
if (simulate)
{
// DO SIMULATION CODE HERE
int capacity = G.capacities[parentAP];
int vertex = G.C->vertices[G.nodes[parentAP][0]][0];
set<int> sTreeNodes = BCT.GetSubTreeNodes(Block);
set<int> sTreeVertices;
for (set<int>::iterator itrs = sTreeNodes.begin(); itrs != sTreeNodes.end(); itrs++)
sTreeVertices.insert(G.C->vertices[G.nodes[*itrs][0]][0]);
Simulation sim(Cd, vertex, capacity, sTreeVertices);
if (sim.simulate(Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 6 Case IV at level = " << level << endl;
break;
}
else
{
// mark block as processed and get another block if the simulation ends with decreasing the capacity of the parent ap
BCT.Processed[Block] = true;
cout << " Marking block as processed due to Algorithm 6 case IV at level = " << level << endl;
}
}
else
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 6 Case IV at level = " << level << endl;
break;
}
}
else
{
if (simulate)
{
BCT.Processed[Block] = true;
cout << " Marking block as processed due to Algorithm 6 case IV at level = " << level << endl;
}
else
{
// Do Nothing (Terminate)
cout << " nothing pushing to stack due to Algorithm 6 Case IV at level = " << level << endl;
break;
}
}
}
else // CASE V
{
Algorithm5 alg5;
BlockDirectedAcyclicMultiGraph BDAG(&G, BCT.Blocks[Block]);
if (alg5.OneStep(BDAG, &Cd))
{
STACK.push(make_pair(Cd, level + 1));
cout << " pushing to stack due to Algorithm 6 Case V at level = " << level << endl;
}
else
{
// Do Nothing (Terminate)
cout << " nothing pushing to stack due to Algorithm 6 Case 5 at level = " << level << endl;
}
break;
}
}
}
}
}
cout << "While cycle repeated " << ++i << " times, tree level = " << level << endl;
}
return false;
}