Repository navigation
Expand file tree
/
Copy pathGraph.h
More file actions
2383 lines (2047 loc) · 79 KB
/
Copy pathGraph.h
File metadata and controls
2383 lines (2047 loc) · 79 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef PROJ_GRAPH_H_
#define PROJ_GRAPH_H_
#include <vector>
#include <queue>
#include <numeric>
#include <limits>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <graphviewer.h>
#include <sstream>
#include <string>
#include <cmath>
#include "MutablePriorityQueue.h"
#include "Utils.h"
constexpr auto INF = std::numeric_limits<double>::max();
constexpr auto UINF = std::numeric_limits<unsigned>::max();
constexpr double LATLNG_FACTOR = 100000.0;
class Van;
template<class T>
class Cluster;
template<class T>
class Vertex;
template<class T>
class Edge;
template<class T>
class Graph;
/*
* ================================================================================================
* Class Vertex
* ================================================================================================
*/
template<class T>
class Vertex {
private:
Vertex(T in, double lat, double lng, unsigned hour, unsigned tolerance, unsigned max_tolerance,
unsigned quantity = 0);
Vertex(T in, double lat, double lng, unsigned quantity = 0);
/// unique identifier of the vertex
T info;
/// latitude (or x) position of the vertex
double lat;
/// longitude (or y) position of the vertex
double lng;
/// outgoing edges of the vertex
std::vector<Edge<T> *> outgoing;
/// incoming edges of the vertex
std::vector<Edge<T> *> incoming;
/// all edges of the vertex
std::vector<Edge<T> *> adj;
/// used for path finding
bool visited = false;
Vertex<T> *path = NULL;
double dist = INF;
/// required by MutablePriorityQueue
int queueIndex = 0;
/// hour that the client wants the delivery (minutes from midnight)
unsigned hour = 0;
/// tolerance for the delivery, after the hour, from which a delay is counted
unsigned tolerance = 0;
/// maximum tolerance for the delivery
unsigned max_tolerance = 0;
/// quantity of breads in the order
unsigned quantity = 0;
/// used for path finding, the hour that the client was visited
unsigned visited_at;
void addEdge(Edge<T> *e);
bool operator<(Vertex<T> &vertex) const; // required by MutablePriorityQueue
public:
T getInfo() const;
double getDist() { return dist; }
std::vector<Edge<T> *> getIncoming() const;
std::vector<Edge<T> *> getOutgoing() const;
double getLat() const { return lat; }
void setLat(double lat) { Vertex::lat = lat; }
double getLng() const { return lng; }
void setLng(double lng) {
Vertex::lng = lng;
}
unsigned int getHour() const {
return hour;
}
void setHour(unsigned int hour) {
Vertex::hour = hour;
}
unsigned int getTolerance() const {
return tolerance;
}
void setTolerance(unsigned int tolerance) {
Vertex::tolerance = tolerance;
}
unsigned int getMaxTolerance() const {
return max_tolerance;
}
void setMaxTolerance(unsigned int maxTolerance) {
max_tolerance = maxTolerance;
}
unsigned int getQuantity() const {
return quantity;
}
void setQuantity(unsigned int quantity) {
Vertex::quantity = quantity;
}
unsigned int getVisitedAt() const {
return visited_at;
}
void setVisitedAt(unsigned int visitedAt) {
visited_at = visitedAt;
}
void setTimes(unsigned hour, unsigned tolerance, unsigned max_tolerance) {
this->hour = hour;
this->tolerance = tolerance;
this->max_tolerance = max_tolerance;
}
void removeEdge(Edge<T> *e);
friend class Graph<T>;
friend class MutablePriorityQueue<Vertex<T>>;
};
template<class T>
Vertex<T>::Vertex(T in, double lat, double lng, unsigned quantity): info(in), lat(lat), lng(lng), quantity(quantity) {}
template<class T>
Vertex<T>::Vertex(T in, double lat, double lng, unsigned hour, unsigned tolerance, unsigned max_tolerance,
unsigned quantity): info(in), lat(lat), lng(lng), hour(hour), tolerance(tolerance),
max_tolerance(max_tolerance), quantity(quantity) {}
/**
* Add outgoing edge to a vertex
*
* @param e pointer to the new edge
*/
template<class T>
void Vertex<T>::addEdge(Edge<T> *e) {
adj.push_back(e);
outgoing.push_back(e);
e->dest->incoming.push_back(e);
e->dest->adj.push_back(e);
}
/**
* Remove outgoing edge from a vertex
*
* @param e pointer to the edge to be removed
*/
template<class T>
void Vertex<T>::removeEdge(Edge<T> *e) {
for (auto edge = adj.begin(); edge < adj.end(); edge++) {
if (*edge == e) {
adj.erase(edge);
break;
}
}
for (auto edge = outgoing.begin(); edge < outgoing.end(); edge++) {
if (*edge == e) {
outgoing.erase(edge);
break;
}
}
for (auto edge = e->dest->incoming.begin(); edge < e->dest->incoming.end(); edge++) {
if (*edge == e) {
e->dest->incoming.erase(edge);
break;
}
}
delete e;
}
/**
* Compare two vertices (required by MutablePriorityQueue)
*
* @param vertex the vertex to compare to
* @return if the vertex dist is lower
*/
template<class T>
bool Vertex<T>::operator<(Vertex<T> &vertex) const {
return this->dist < vertex.dist;
}
/**
* Get the info of a Vertex
*
* @return the info of a Vertex
*/
template<class T>
T Vertex<T>::getInfo() const {
return this->info;
}
/**
* Get the incoming edges of a Vertex
*
* @return a vector of pointers to the incoming edges of a Vertex
*/
template<class T>
std::vector<Edge<T> *> Vertex<T>::getIncoming() const {
return this->incoming;
}
/**
* Get the outgoing edges of a Vertex
*
* @return a vector of pointers to the outgoing edges of a Vertex
*/
template<class T>
std::vector<Edge<T> *> Vertex<T>::getOutgoing() const {
return this->outgoing;
}
/*
* ================================================================================================
* Class Edge
* ================================================================================================
*/
template<class T>
class Edge {
/// the origin vertex
Vertex<T> *orig;
/// the destination vertex
Vertex<T> *dest;
/// the underlying path of an edge (for interest points graphs)
std::vector<T> complex_path;
/// the edge cost
double cost;
/// required to view Van Paths in GraphViewer
int vanID = 0;
public:
Edge(Vertex<T> *o, Vertex<T> *d, double cost, std::vector<T> complex_path);
Edge(Vertex<T> *o, Vertex<T> *d, double cost);
Edge(Vertex<T> *o, Vertex<T> *d, bool lat_long);
Vertex<T> *getOrigin() { return orig; }
Vertex<T> *getDest() { return dest; }
int getVanID() { return vanID; }
void setVanID(int vanID) { this->vanID = vanID; }
double getCost() const { return cost; }
void setCost(double cost) { this->cost = cost; }
//std::vector<Edge<T> *> getComplexPath();
std::vector<T> getComplexPath() {
return this->complex_path;
}
void setComplexPath(std::vector<Edge<T> *> complex_path) {
this->complex_path = complex_path;
}
friend class Graph<T>;
friend class Vertex<T>;
};
template<class T>
Edge<T>::Edge(Vertex<T> *o, Vertex<T> *d, double cost, std::vector<T> complex_path): orig(o), dest(d), cost(cost) {
this->complex_path = complex_path;
}
template<class T>
Edge<T>::Edge(Vertex<T> *o, Vertex<T> *d, double cost):
orig(o), dest(d), cost(cost) {}
template<class T>
Edge<T>::Edge(Vertex<T> *o, Vertex<T> *d, bool lat_long):
orig(o), dest(d) {
if (lat_long)
cost = calculateDistHaversine(this->orig->getLng(), this->orig->getLat(), this->dest->getLng(), this->dest->getLat());
else cost = calculateDist(this->orig->getLat(), this->orig->getLng(), this->dest->getLat(), this->dest->getLng());
}
/**
* For priority queues, compare clients by time
*/
template<class T>
class ClientCompare {
public:
bool operator()(const Vertex<T> *v1, const Vertex<T> *v2) {
return v1->getHour() + v1->getMaxTolerance() > v2->getHour() + v2->getMaxTolerance();;
}
};
/**
* For priority queues, compare clients by time
*/
template<class T>
class ClientArrivalCompare {
public:
bool operator()(Vertex<T> *v1, Vertex<T> *v2) {
return v1->getHour() + v1->getTolerance() > v2->getHour() + v2->getTolerance();;
}
};
/*
* ================================================================================================
* Class Graph
* ================================================================================================
*/
template<class T>
class Graph {
private:
/// list of vertices in the graph
std::vector<Vertex<T> *> vertexSet;
/// indicates if it is an interest points graph, suitable for the corresponding algorithms
bool ipGraph = false;
bool latLng = true;
unsigned start_time = 0;
double weight = 0.5;
unsigned visit_time = 5;
unsigned early_time = 10;
unsigned velocity = 50;
public:
void nearestNeighbour(const T &origin_info);
void nearestNeighbourTimes(const T &origin_info);
void dijkstraShortestPath(const T &origin);
void heldKarp(const T &origin);
double bruteForceTimes(const T &origin);
std::pair<double, std::vector<Vertex<T> *>>
bruteForceTimesRec(Vertex<T> *origin,
std::priority_queue<Vertex<T> *, std::vector<Vertex<T> *>, ClientCompare<T>>
client_queue,
std::vector<Vertex<T> *> path = std::vector<Vertex<T> *>()
);
std::vector<std::vector<T>> dfsConnectivity() const;
bool checkConectivity(std::vector<T> ids);
std::vector<T> dfs() const;
void dfsVisit(Vertex<T> *v, std::vector<T> &res, bool reverse = false) const;
Graph<T> generateInterestPointsGraph(std::vector<T> important_points);
Vertex<T> *findVertex(const T &inf) const;
Edge<T> *findEdge(const T &og, const T &dest) const;
Edge<T> *findEdge(const Vertex<T> *og, const Vertex<T> *dest) const;
std::vector<Vertex<T> *> getVertexSet() const;
unsigned int getStartTime() const;
void setStartTime(unsigned int startTime);
unsigned int getVisitTime() const;
bool getLatLng() const { return this->lat_lng; }
void setLatLng(bool lat_lng) { this->lat_lng = lat_lng; }
void setVisitTime(unsigned int visitTime);
unsigned int getEarlyTime() const;
void setEarlyTime(unsigned int earlyTime);
unsigned getTotalQuantity() const {unsigned totalQuantity = 0; for(Vertex<T> * v : vertexSet) totalQuantity += v->getQuantity(); return totalQuantity;};
Vertex<T> *addVertex(const T &in);
Vertex<T> *addVertex(const T &in, const double &lat, const double &lng, const unsigned &quantity = 0);
Vertex<T> *
addVertex(const T &in, const double &lat, const double &lng, const unsigned &hour, const unsigned &tolerance,
const unsigned &max_tolerance, const unsigned &quantity = 0);
Edge<T> *addEdge(const T &sourc, const T &dest, double capacity, double cost, double flow = 0);
Edge<T> *addEdge(const T &sourc, const T &dest, double cost, std::vector<T> complex_path);
Edge<T> *addEdge(const T &sourc, const T &dest, double cost);
Edge<T> *addEdge(const T &sourc, const T &dest, bool lat_lng);
std::pair<std::vector<T>, double> getPath(const T &origin, const T &dest) const;
unsigned getVelocity() { return this->velocity; }
void setVelocity(unsigned velocity) { this->velocity = velocity; }
void viewGraph();
void viewGraphIP(const Graph<T> &igraph);
void viewGraphPath(std::vector<T> path, std::vector<T> interest_points, bool remove_extra_nodes = false,
bool remove_extra_edges = false, bool show_w = false, bool show_nid = false, bool multiple_vans = true);
void viewGraphPathIP(Graph<T> igraph, std::vector<T> path, bool remove_extra_nodes = false,
bool remove_extra_edges = false, bool show_w = false, bool show_nid = false, bool multiple_vans = true);
void importGraph(std::string vertex_filename, std::string edges_filename, bool lat_lng);
void printTimes();
std::vector<T> getOverlapClients(const T &info);
std::vector<T> getOverlapClientsTravelling(const T &info);
std::vector<Vertex<T> *> getOverlapClientsTravelling(Vertex<T> *info);
std::vector<T> getPerfectOverlapClientsTravelling(const T &info);
std::vector<T> getPerfectOverlapClientsTravelling(std::vector<T> remaining_clients, const T &info);
double costFunctionStep(T og, std::vector<T> path, T new_element);
double costFunctionTotal(T og, std::vector<T> path);
double costFunctionTotal(Vertex<T> *og, std::vector<Vertex<T> *> path);
double costFunctionTotal(Vertex<T> *origin, unsigned visit_time, std::vector<Vertex<T> *> path, bool max_tolreance_cap);
std::vector<Cluster<T>> getClusters(const T &origin);
std::vector<std::pair<Van, std::vector<Vertex<T> *>>> dividingClustersBrute(std::vector<Van> vans, const T &origin);
std::vector<std::pair<Van, std::vector<Vertex<T> *>>>
dividingClustersGreedy(std::vector<Van> vans, const T &origin);
void followVansPath(std::vector<std::pair<Van, std::vector<Vertex<T> *>>> paths_dist, const T &og);
std::vector<T> vanPathtoViewPath(std::vector<std::pair<Van, std::vector<Vertex<T > *>>> van_path, const T &og);
double costFuntionMultipleVans(std::vector<std::pair<Van, std::vector<Vertex<T > *>>> van_path, const T &og);
};
template<class T>
Vertex<T> *Graph<T>::addVertex(const T &in) {
Vertex<T> *v = findVertex(in);
if (v != nullptr)
return v;
v = new Vertex<T>(in);
vertexSet.push_back(v);
return v;
}
template<class T>
Vertex<T> *Graph<T>::addVertex(const T &in, const double &lat, const double &lng, const unsigned &quantity) {
Vertex<T> *v = findVertex(in);
if (v != nullptr)
return v;
v = new Vertex<T>(in, lat, lng, quantity);
vertexSet.push_back(v);
return v;
}
/**
* Add a vertex to the graph
*
* @param in vertex id
* @param lat vertex lat (or x)
* @param lng vertex lng (or y)
* @param hour client hour of delivery
* @param tolerance client tolerance for delivery
* @param max_tolerance client maximum tolerance for delivery
* @param quantity the quantity of the delivery
* @return
*/
template<class T>
Vertex<T> *
Graph<T>::addVertex(const T &in, const double &lat, const double &lng, const unsigned &hour, const unsigned &tolerance,
const unsigned &max_tolerance, const unsigned &quantity) {
Vertex<T> *v = findVertex(in);
if (v != nullptr)
return v;
v = new Vertex<T>(in, lat, lng, hour, tolerance, max_tolerance, quantity);
vertexSet.push_back(v);
return v;
}
template<class T>
Edge<T> *Graph<T>::addEdge(const T &sourc, const T &dest, double capacity, double cost, double flow) {
auto s = findVertex(sourc);
auto d = findVertex(dest);
if (s == nullptr || d == nullptr)
return nullptr;
Edge<T> *e = new Edge<T>(s, d, capacity, cost, flow);
s->addEdge(e);
return e;
}
template<class T>
Edge<T> *Graph<T>::addEdge(const T &sourc, const T &dest, double cost, std::vector<T> complex_path) {
auto s = findVertex(sourc);
auto d = findVertex(dest);
if (s == nullptr || d == nullptr)
return nullptr;
Edge<T> *e = new Edge<T>(s, d, cost, complex_path);
s->addEdge(e);
return e;
}
template<class T>
Edge<T> *Graph<T>::addEdge(const T &sourc, const T &dest, double cost) {
auto s = findVertex(sourc);
auto d = findVertex(dest);
if (s == nullptr || d == nullptr)
return nullptr;
Edge<T> *e = new Edge<T>(s, d, cost);
s->addEdge(e);
return e;
}
template<class T>
Edge<T> *Graph<T>::addEdge(const T &sourc, const T &dest, bool lat_lng) {
auto s = findVertex(sourc);
auto d = findVertex(dest);
if (s == nullptr || d == nullptr)
return nullptr;
Edge<T> *e = new Edge<T>(s, d, lat_lng);
s->addEdge(e);
return e;
}
template<class T>
Vertex<T> *Graph<T>::findVertex(const T &inf) const {
for (auto v : vertexSet)
if (v->info == inf)
return v;
return nullptr;
}
template<class T>
Edge<T> *Graph<T>::findEdge(const T &og, const T &dest) const {
for (auto v : vertexSet) {
for (auto edge : v->getOutgoing()) {
if (edge->orig->getInfo() == og && edge->dest->getInfo() == dest) return edge;
}
}
return nullptr;
}
template<class T>
Edge<T> *Graph<T>::findEdge(const Vertex<T> *og, const Vertex<T> *dest) const {
for (auto out : og->outgoing) {
if (out->dest == dest)
return out;
}
return nullptr;
}
template<class T>
std::vector<Vertex<T> *> Graph<T>::getVertexSet() const {
return vertexSet;
}
template<class T>
unsigned int Graph<T>::getStartTime() const {
return start_time;
}
template<class T>
void Graph<T>::setStartTime(unsigned int startTime) {
start_time = startTime;
}
template<class T>
unsigned int Graph<T>::getVisitTime() const {
return visit_time;
}
template<class T>
void Graph<T>::setVisitTime(unsigned int visitTime) {
visit_time = visitTime;
}
template<class T>
unsigned int Graph<T>::getEarlyTime() const {
return early_time;
}
template<class T>
void Graph<T>::setEarlyTime(unsigned int earlyTime) {
early_time = earlyTime;
}
/*
* ================================================================================================
* DFS
* ================================================================================================
*/
template<class T>
std::vector<std::vector<T>> Graph<T>::dfsConnectivity() const {
std::vector<T> visit_order = dfs();
for (auto v : vertexSet) {
v->visited = false;
}
Vertex<T> *v;
std::vector<T> scc;
std::vector<std::vector<T>> res;
for (int i = visit_order.size() - 1; i >= 0; i--) {
v = findVertex(visit_order.at(i));
if (!v->visited) {
dfsVisit(v, scc, true);
res.push_back(scc);
scc.clear();
}
}
return res;
}
template<class T>
std::vector<T> Graph<T>::dfs() const {
std::vector<T> res;
for (auto v : vertexSet) {
v->visited = false;
}
for (auto v : vertexSet) {
if (!v->visited)
dfsVisit(v, res);
}
return res;
}
/*
* Auxiliary function that visits a vertex (v) and its adjacent not yet visited, recursively.
* Updates a parameter with the list of visited node contents.
*/
template<class T>
void Graph<T>::dfsVisit(Vertex<T> *v, std::vector<T> &res, bool reverse) const {
v->visited = true;
std::vector<Edge<T> *> edges;
if (reverse) {
for (auto e : v->incoming) {
if (!e->orig->visited)
dfsVisit(e->orig, res, reverse);
}
} else {
for (auto e : v->outgoing) {
if (!e->dest->visited)
dfsVisit(e->dest, res, reverse);
}
}
res.push_back(v->info);
}
/*
* ================================================================================================
* Shortest Path Problem
* ================================================================================================
*/
template<class T>
void Graph<T>::dijkstraShortestPath(const T &origin) {
for (Vertex<T> *vertex : vertexSet) {
vertex->dist = INF;
vertex->path = NULL;
vertex->queueIndex = 0;
}
MutablePriorityQueue<Vertex<T>> vertexQueue;
findVertex(origin)->dist = 0;
vertexQueue.insert(findVertex(origin));
while (!vertexQueue.empty()) {
Vertex<T> *v = vertexQueue.extractMin();
for (Edge<T> *edge : v->outgoing) {
double oldDist = edge->getDest()->dist;
if (edge->getDest()->dist > v->dist + edge->getCost()) {
edge->getDest()->dist = v->dist + edge->getCost();
edge->getDest()->path = v;
if (oldDist == INF)
vertexQueue.insert(edge->dest);
else
vertexQueue.decreaseKey(edge->dest);
}
}
}
}
template<class T>
std::pair<std::vector<T>, double> Graph<T>::getPath(const T &origin, const T &dest) const {
std::vector<T> path;
double total_dist = 0;
Vertex<T> *v = this->findVertex(dest);
if (v == nullptr || v->dist == INF) return std::pair<std::vector<T>, double>(path, total_dist);
while (true) {
path.push_back(v->info);
total_dist += v->dist;
v = v->path;
if (v == nullptr) {
return std::pair<std::vector<T>, double>(path, total_dist);
}
if (v->info == origin) {
path.push_back(v->info);
break;
}
}
std::reverse(path.begin(), path.end());
return std::pair<std::vector<T>, double>(path, total_dist);
}
template<class T>
Graph<T> Graph<T>::generateInterestPointsGraph(std::vector<T> important_points) {
Graph<T> result;
if (important_points.empty()) return result;
result.visit_time = this->visit_time;
result.early_time = this->early_time;
result.start_time = this->start_time;
result.velocity = this->velocity;
result.latLng = this->latLng;
std::cout << "Checking Connectivity..." << std::endl;
if (!checkConectivity(important_points)) {
std::cout << "Connectivity Invalid!!!" << std::endl;
return result;
}
std::cout << "Connectivity Good!" << std::endl;
for (int i = 0; i < important_points.size(); i++) {
Vertex<T> *v = findVertex(important_points[i]);
result.addVertex(important_points[i], v->getLat(), v->getLng(), v->getHour(), v->getTolerance(),
v->getMaxTolerance(), v->getQuantity());
}
if (important_points.size() == 1) return result;
for (int i = 0; i < important_points.size(); i++) {
T current_info = important_points[i];
//Graph<T> copy = complete_graph;
this->dijkstraShortestPath(current_info);
//copy.dijkstraShortestPath(current_info);
for (int j = 0; j < important_points.size(); j++) {
if (important_points[j] != current_info) {
std::vector<T> path = (getPath(current_info, important_points[j])).first;
double dist = findVertex(important_points[j])->getDist();
result.addEdge(current_info, important_points[j], dist, path);
}
}
}
return result;
}
class Van {
private:
unsigned id;
unsigned visit_time;
unsigned capacity;
public:
Van(unsigned id, unsigned visit_time, unsigned capacity=1) {
this->id = id;
this->visit_time = visit_time;
this->capacity = capacity;
}
unsigned getID() const { return this->id; }
unsigned getVisitTime() const { return this->visit_time; }
void setVisitTime(unsigned visit_time) { this->visit_time = visit_time; }
unsigned getCapacity() const { return this->capacity; }
void setCapacity(unsigned capacity) { this->capacity = capacity; }
};
class VanCompare {
public:
bool operator()(Van v1, Van v2) {
return v1.getVisitTime() > v2.getVisitTime();
}
};
template<class T>
class Cluster {
private:
Graph<T> * graph;
std::vector<T> info_set;
std::vector<Vertex<T> *> vertex_set;
public:
Cluster(Graph<T> * graph, std::vector<T> info_set) {
this->graph = graph;
this->info_set = info_set;
}
unsigned getSize() { return info_set.size(); }
std::vector<T> getInfoSet() { return info_set; }
std::vector<T> getVertexSet() { return vertex_set; }
void addInfo(T info) { info_set.push_back(info); }
void addVertex(Vertex<T> *v) {
vertex_set.push_back(v);
addInfo(v->info);
}
void PrintInfo(){
for(T info : info_set){
std::cout << info << " ";
}
std::cout << "END" << std::endl;
}
unsigned getTotalQuantity() const {
unsigned totalQuantity = 0;
for (T info : info_set){
Vertex<T> * v = graph->findVertex(info);
if(v == nullptr) return UINF;
totalQuantity += v->getQuantity();
}
return totalQuantity;
};
bool isInSet(T new_info) {
for (T info : info_set) {
if (new_info == info) return true;
}
return false;
}
std::pair<Vertex<T> *, unsigned> getArrivalHour() {
unsigned minHour = UINF;
Vertex<T> *selected_vertex = nullptr;
for (T info : info_set) {
Vertex<T> *vertex = graph->findVertex(info);
if (vertex != nullptr) {
unsigned vertex_mh = vertex->getHour() + vertex->getTolerance();
if (vertex_mh < minHour) {
minHour = vertex_mh;
selected_vertex = vertex;
}
}
}
return std::pair<Vertex<T> *, unsigned>(selected_vertex, minHour);
};
std::pair<Vertex<T> *, unsigned> getDepartureHour() {
unsigned maxHour = 0;
Vertex<T> *selected_vertex = nullptr;
for (T info : info_set) {
Vertex<T> *vertex = graph->findVertex(info);
if (vertex != nullptr) {
//unsigned vertex_mh = vertex->getHour() + vertex->getTolerance() + graph.getVisitTime();
unsigned vertex_mh = vertex->getHour() + vertex->getMaxTolerance() + graph->getVisitTime();
if (vertex_mh > maxHour) {
maxHour = vertex_mh;
selected_vertex = vertex;
}
}
}
return std::pair<Vertex<T> *, unsigned>(selected_vertex, maxHour);
};
Vertex<T> *getArrivalVertex() {
return getArrivalHour().first;
}
Vertex<T> *getDepartureVertex() {
return getDepartureHour().first;
}
unsigned getMaxArrivalHour() {
Vertex<T> *v = getArrivalHour().first;
return v->getHour() + v->getMaxTolerance();
}
double calculateClusterCost(Cluster<T> otherCluster);
double calculateOriginCost(const T &origin, bool to_origin = false);
void mergeCluster(Cluster<T> otherCluster);
bool hasCommon(Cluster<T> otherCluster);
std::vector<Vertex<T> *> exportVertices(){
std::priority_queue<Vertex<T> *, std::vector<Vertex<T> *>, ClientArrivalCompare<T>> vertex_queue;
std::vector<Vertex<T> *> result;
for(T info: info_set){
Vertex<T> * v = graph->findVertex(info);
if(v == nullptr) return {};
vertex_queue.push(v);
}
while(!vertex_queue.empty()){
result.push_back(vertex_queue.top());
vertex_queue.pop();
}
return result;
}
/*
Cluster<T> operator=(Cluster<T> otherCluster){
//this->info_set = otherCluster.info_set;
//return this;
return Cluster<T>(graph, otherCluster.getInfoSet());
}
*/
};
template<class T>
class ClusterCompareArrival {
public:
bool operator()(Cluster<T> c1, Cluster<T> c2) {
return c1.getArrivalHour().second > c2.getArrivalHour().second;
}
};
template<class T>
class ClusterCompareDeparture {
public:
bool operator()(Cluster<T> c1, Cluster<T> c2) {
return c1.getDepartureHour().second > c2.getDepartureHour().second;
}
};
template<class T>
class ClusterCompareSize {
public:
bool operator()(Cluster<T> c1, Cluster<T> c2) {
return c1.getSize() < c2.getSize();
}
};
template<class T>
void Cluster<T>::mergeCluster(Cluster<T> otherCluster) {
for (T info : otherCluster.info_set) {
if (!isInSet(info)) addInfo(info);
}
}
template<class T>
bool Cluster<T>::hasCommon(Cluster<T> otherCluster) {
for (T info : info_set) {
for (T other_info : otherCluster.getInfoSet()) {
if (other_info == info) return true;
}
}
return false;
}
template<class T>
double Cluster<T>::calculateClusterCost(Cluster<T> otherCluster) {
std::pair<Vertex<T> *, unsigned> departure = getDepartureHour();
std::pair<Vertex<T> *, unsigned> arrival = otherCluster.getArrivalHour();
if (arrival.first == nullptr || departure.first == nullptr) return INF;