-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm4.cpp
More file actions
971 lines (753 loc) · 37.2 KB
/
m4.cpp
File metadata and controls
971 lines (753 loc) · 37.2 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
#include "m1.h"
#include "m2.h"
#include "m3.h"
#include "m4.h"
#include "Global.h"
#include "IntersectionNode.h"
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <queue>
#include <utility>
#include <queue>
#include <iostream>
#include <iterator>
#include <climits>
#include <boost/timer.hpp>
struct open_set_compare_ {
bool operator()(const std::pair<double, unsigned>& lhs, const std::pair<double, unsigned>& rhs) const {
return lhs.first > rhs.first;
}
};
std::vector<unsigned> traveling_courier(const std::vector<DeliveryInfo>& deliveries,
const std::vector<unsigned>& depots,
const float turn_penalty)
{
boost::timer t;
/*
movement: i -> j
i - right
j - down
- - - - -
|1|2|3|4|
- - - - -
|5|6|7|8|
- - - - -
*/
unsigned deliveries_size = deliveries.size();
std::vector<std::vector<double>> timeMap(2 * deliveries_size, std::vector<double>(2 * deliveries_size)); // Defaults to zero initial value
// 1. pickUp - pickUp
#pragma omp parallel for
for (unsigned i = 0; i < deliveries_size; ++i) {
for (unsigned j = 0; j < deliveries_size; ++j) {
if (i == j)
timeMap[i][j] = INT_MAX;
else if (deliveries[i].pickUp == deliveries[j].pickUp)
timeMap[i][j] = 0;
else {
double distance = find_distance_between_two_points(intersection_id_to_position[deliveries[i].pickUp],
intersection_id_to_position[deliveries[j].pickUp]);
//std::vector<unsigned> path = find_path_between_intersections(deliveries[i].pickUp, deliveries[j].pickUp, turn_penalty);
double travel_time_ = distance; //compute_path_travel_time(path, turn_penalty);
//intersectionsToPathVector[i][j] = path;
//if (!travel_time_)
// timeMap[i][j] = INT_MAX;
//else
timeMap[i][j] = travel_time_;
}
}
}
// 2. dropOff - pickUp / 3. pickUp - dropOff
#pragma omp parallel for
for (unsigned i = 0; i < deliveries_size; ++i) {
for (unsigned j = 0; j < deliveries_size; ++j) {
if (deliveries[i].dropOff == deliveries[j].pickUp)
timeMap[i + deliveries_size][j] = 0;
else {
double distance = find_distance_between_two_points(intersection_id_to_position[deliveries[i].dropOff],
intersection_id_to_position[deliveries[j].pickUp]);
//std::vector<unsigned> path = find_path_between_intersections(deliveries[i].dropOff, deliveries[j].pickUp, turn_penalty);
double travel_time_ = distance; //compute_path_travel_time(path, turn_penalty);
//intersectionsToPathVector[i + deliveries.size()][j] = path;
//if (!travel_time_)
// timeMap[i + deliveries.size()][j] = INT_MAX;
//else
// 2.
timeMap[i + deliveries_size][j] = travel_time_;
// 3.
timeMap[j][i + deliveries_size] = travel_time_;
}
}
}
// 3. pickUp - dropOff
//#pragma omp parallel for
// for (unsigned i = 0; i < deliveries_size; ++i)
// {
// for (unsigned j = 0; j < deliveries_size; ++j)
// {
// if (deliveries[i].pickUp == deliveries[j].dropOff)
// timeMap[i][j + deliveries_size] = 0;
// else
// {
// double distance = timeMap[j][i+deliveries_size];
// //double distance = find_distance_between_two_points(getIntersectionPosition(deliveries[i].pickUp), getIntersectionPosition(deliveries[j].dropOff));
//
// //std::vector<unsigned> path = find_path_between_intersections(deliveries[i].pickUp, deliveries[j].dropOff, turn_penalty);
// double travel_time_ = distance; //compute_path_travel_time(path, turn_penalty);
//
// //intersectionsToPathVector[i][j + deliveries.size()] = path;
//
// //if (!travel_time_)
// // timeMap[i][j + deliveries.size()] = INT_MAX;
// //else
// timeMap[i][j + deliveries_size] = travel_time_;
// }
// }
// }
// 4. dropOff - dropOff
#pragma omp parallel for
for (unsigned i = 0; i < deliveries_size; ++i) {
for (unsigned j = 0; j < deliveries_size; ++j) {
if (i == j)
timeMap[i + deliveries_size][j + deliveries_size] = INT_MAX;
else if (deliveries[i].dropOff == deliveries[j].dropOff)
timeMap[i + deliveries_size][j + deliveries_size] = 0;
else {
double distance = find_distance_between_two_points(intersection_id_to_position[deliveries[i].dropOff],
intersection_id_to_position[deliveries[j].dropOff]);
//std::vector<unsigned> path = find_path_between_intersections(deliveries[i].dropOff, deliveries[j].dropOff, turn_penalty);
double travel_time_ = distance; //compute_path_travel_time(path, turn_penalty);
//intersectionsToPathVector[i + deliveries.size()][j + deliveries.size()] = path;
//if (!travel_time_)
// timeMap[i + deliveries.size()][j + deliveries.size()] = INT_MAX;
//else
timeMap[i + deliveries_size][j + deliveries_size] = travel_time_;
}
}
}
std::vector<std::vector<unsigned>> paths;
std::vector<double> path_times;
for(unsigned i = 0; i < depots.size() && t.elapsed() < 0.96; ++i)
{
std::vector<unsigned> intersectionIDs;
std::vector<unsigned> intersection_index;
// vector of all the legal pickUps -- @ beginning is full (start at 0)
std::vector<bool> legalPickUps(deliveries_size, true);
// vector of all legal dropOffs -- @ beginning empty (start at deliveries.size())
std::vector<bool> legalDropOffs(2 * deliveries_size, false);
/************************************************/
/*************** GREEDY ALGORITHM ***************/
/************************************************/
//------------------------- 1. START AT RANDOM DEPOT -------------------------//
// start at random depot
//unsigned start_depot = depots[rand() % depots.size()];
unsigned start_depot = depots[i];
intersectionIDs.push_back(start_depot);
intersection_index.push_back(start_depot);
// first closest pickUp i index
unsigned first_pickup_index;
// go from RANDOM DEPOT to CLOSEST PICKUP
std::vector<unsigned> path = find_path_from_depot_to_first_pickUp(deliveries, start_depot, first_pickup_index, turn_penalty);
intersection_index.push_back(first_pickup_index);
// set current pickUp to the closest initial pickUp (intersection ID)
unsigned currentPickUp = deliveries[first_pickup_index].pickUp;
intersectionIDs.push_back(currentPickUp);
// get all legal drop offs corresponding to currentPickUp
legalDropOffs[first_pickup_index + deliveries_size] = true;
// update current legal pickUps
legalPickUps[first_pickup_index] = false;
// @ first pickUp intersection
unsigned currentLocation = currentPickUp;
unsigned currentLocationIndex = first_pickup_index;
//not only pick up s
while (1 /*std::find(legalPickUps.begin(), legalPickUps.end(), true) != legalPickUps.end() ||
std::find(legalDropOffs.begin(), legalDropOffs.end(), true) != legalDropOffs.end()*/) {
// search for minimum time (j-axis)
std::vector<double> timesFromCurrentLocation = timeMap[currentLocationIndex];
unsigned smallestIndex;
double smallestTime = INT_MAX;
for (unsigned j = 0; j < timesFromCurrentLocation.size(); ++j) // timesFromCurrentLocation.size() = 2 * deliveries.size()
{
if (j < deliveries_size) {
if (!legalPickUps[j]) continue; // invalid pickUp
}
else {
if (!legalDropOffs[j]) continue; // invalid dropOff
}
if (timesFromCurrentLocation[j] < smallestTime) {
smallestTime = timesFromCurrentLocation[j];
smallestIndex = j;
}
}
if (smallestTime == INT_MAX) break;
// //
// GOTO smallestIndex (smallest time) //
// //
// 1. goto dropOff
if (smallestIndex >= deliveries_size) {
unsigned drop_off_location = deliveries[smallestIndex % deliveries_size].dropOff;
std::vector<unsigned> vec1 = find_path_between_intersections(currentLocation, drop_off_location, turn_penalty);
if (vec1.size()) path.insert(path.end(), vec1.begin(), vec1.end());
intersectionIDs.push_back(deliveries[smallestIndex % deliveries.size()].dropOff);
// update legalDropOffs
legalDropOffs[smallestIndex] = false;
intersection_index.push_back(smallestIndex);
currentLocationIndex = smallestIndex;
currentLocation = drop_off_location;
}
// 2. goto pickUp
else {
unsigned pick_up_location = deliveries[smallestIndex].pickUp;
std::vector<unsigned> vec1 = find_path_between_intersections(currentLocation, pick_up_location, turn_penalty);
if (vec1.size()) path.insert(path.end(), vec1.begin(), vec1.end());
intersectionIDs.push_back(deliveries[smallestIndex].pickUp);
// update legalPickUps
legalPickUps[smallestIndex] = false;
// get all legal drop offs corresponding to currentPickUp
// update legalDropOffs
legalDropOffs[smallestIndex + deliveries_size] = true;
intersection_index.push_back(smallestIndex);
currentLocationIndex = smallestIndex;
currentLocation = pick_up_location;
}
}
unsigned intersection_id_end;
std::vector<unsigned> path_to_depot = find_path_from_last_dropOff_to_depots(depots, currentLocation, turn_penalty, intersection_id_end);
path.insert(path.end(), path_to_depot.begin(), path_to_depot.end());
intersectionIDs.push_back(intersection_id_end);
intersection_index.push_back(intersection_id_end);
//
//
//
//
//
paths.push_back(path);
path_times.push_back(compute_path_travel_time(path, turn_penalty));
}
auto it = std::min_element(path_times.begin(), path_times.end());
for (unsigned i = 0; i < path_times.size(); ++i)
if (path_times[i] == *it)
return paths[i];
//double elapsed_time = t.elapsed();
//double time_e;
//time_e =
//return path;
//------------------------------------------------------------------------//
//----- 2. dropOffsFromPickUps & pickUpsFromDropOffs -----//
// for (unsigned i = 0; i < dropOffsFromPickUps)
//
// // search for minimum time (j-axis)
// for (unsigned i = 0; i < timesFromCurrentPickUp.size(); ++i)
//------------------------------------------------------------------------//
//----- 2. dropOffsFromPickUps & pickUpsFromDropOffs -----//
/*
std::unordered_map<unsigned, std::vector<unsigned>> dropOffsFromPickUps; // pickUpIntersectionID -> <dropOffIntersectionID>
std::unordered_map<unsigned, std::vector<unsigned>> pickUpsFromDropOffs; // dropOffIntersectionID -> <pickUpIntersectionID>
std::unordered_map<unsigned, std::vector<unsigned>> corresponding;
for (unsigned i = 0; i < deliveries.size(); ++i) {
dropOffsFromPickUps[deliveries[i].pickUp].push_back(deliveries[i].dropOff);
pickUpsFromDropOffs[deliveries[i].dropOff].push_back(deliveries[i].pickUp);
//all_pick_up.push_back(deliveries[i].pickUp);
}
*/
// for (unsigned i = 0; i < 2*deliveries.size(); ++i) {
// corresponding[deliveries[i].pickUp].push_back(deliveries[i].dropOff);
// corresponding[deliveries[i].dropOff].push_back(deliveries[i].pickUp);
// //all_pick_up.push_back(deliveries[i].pickUp);
// }
//----- 3. delivery pickUp or dropOff ID -> "name" -----//
/*
std::unordered_map<unsigned, std::string> deliveriesIDtoName; // delivery pickUp || dropOff ID -> "name"
std::unordered_map<unsigned, std::string> depotsIDtoName; // depot ID's -> "name"
for(unsigned i = 0; i < deliveries.size(); ++i)
{
deliveriesIDtoName[deliveries[i].pickUp] = "pickUp";
deliveriesIDtoName[deliveries[i].dropOff] = "dropOff";
}
for(unsigned i = 0; i < deliveries.size(); ++i)
depotsIDtoName[depots[i]] = "depot";
*/
//----- 4. rtrees -----//
/*
rtree dropOffIntersections; // rtree for deliveries (dropOff)
rtree pickUpIntersections; // rtree for deliveries (pickUp)
rtree depotIntersections; // rtree for depots
for(unsigned i = 0; i < deliveries.size(); ++i)
{
std::vector<double> points_xyz_pickup = to_cartesian_3D(getIntersectionPosition(deliveries[i].pickUp));
point p1(points_xyz_pickup[0], points_xyz_pickup[1], points_xyz_pickup[2]);
std::vector<double> points_xyz_dropoff = to_cartesian_3D(getIntersectionPosition(deliveries[i].dropOff));
point p2(points_xyz_dropoff[0], points_xyz_dropoff[1], points_xyz_dropoff[2]);
pickUpIntersections.insert(std::make_pair(p1, deliveries[i].pickUp));
dropOffIntersections.insert(std::make_pair(p2, deliveries[i].dropOff));
}
for(unsigned i = 0; i < depots.size(); ++i)
{
std::vector<double> points_xyz = to_cartesian_3D(getIntersectionPosition(depots[i]));
point p(points_xyz[0], points_xyz[1], points_xyz[2]);
depotIntersections.insert(std::make_pair(p, depots[i]));
}
*/
/**************************************/
/********** GREEDY ALGORITHM **********/
/**************************************/
// start at first depot
/* unsigned current_pickUp = depots[0];
// get closest pickUp intersection to starting depot
std::vector<value> temp_result;
std::vector<double> points_xyz_temp = to_cartesian_3D(getIntersectionPosition(current_pickUp));
point my_pos_point(points_xyz_temp[0], points_xyz_temp[1], points_xyz_temp[2]);
pickUpIntersections.query(bgi::nearest(my_pos_point, 1), std::back_inserter(temp_result));
// goto the nearest pickup intersection from first depot
std::vector<unsigned> path = find_path_between_intersections(depots[0], temp_result[0].second, turn_penalty);
// if no path exists return' FIGURE THIS OUT LATER
if(!path.size()) return std::vector<unsigned>();
// remove pickUp intersection from tree
pickUpIntersections.remove(temp_result[0]);
current_pickUp = temp_result[0].second; // @ initial pickUp
while(1)
{
// get location(s) of dropOff intersections corresponding to current_pickUp
std::vector<unsigned> dropOffLocations = dropOffsFromPickUps[current_pickUp];
// goto each dropOff location
for(unsigned i = 0; i < dropOffLocations.size(); ++i)
{
std::vector<unsigned> vec1 = find_path_between_intersections(current_pickUp, dropOffLocations[i], turn_penalty);
path.insert(path.end(), vec1.begin(), vec1.end());
current_pickUp = dropOffLocations[i]; // @ end of loop contains last dropOff location
}
// search for next closest pickUp location with respect to current location
std::vector<value> result;
std::vector<double> points_xyz = to_cartesian_3D(getIntersectionPosition(current_pickUp));
point my_position_point (points_xyz[0], points_xyz[1], points_xyz[2]);
pickUpIntersections.query(bgi::nearest(my_position_point, 1), std::back_inserter(result));
// break if no more pickUp locations
if(!result.size()) break;
// remove pickUp location from rtree
pickUpIntersections.remove(result[0]);
// goto pickUp spot from current location
std::vector<unsigned> vec1 = find_path_between_intersections(current_pickUp, result[0].second, turn_penalty);
path.insert(path.end(), vec1.begin(), vec1.end());
// update current pickUp location
current_pickUp = result[0].second;
}
// return to first depot
std::vector<unsigned> vec1 = find_path_between_intersections(current_pickUp, depots[0], turn_penalty);
path.insert(path.end(), vec1.begin(), vec1.end());
return path;
*/
/***********************************************************************************************************/
/***********************************************************************************************************/
/********************************* NAIVE METHOD TO TEST FUNCTIONALITY **************************************/
/***********************************************************************************************************/
/***********************************************************************************************************/
/*
std::vector <unsigned> travel_path;
std::vector <unsigned> pickUp_skipped;
std::vector <unsigned> dropOff_skipped;
unsigned last_visited_intersection = depots[0];
//Loops through every pickup point and goes to first valid one
for (unsigned i = 0; i < deliveries.size(); i++)
{
std::vector <unsigned> pickUp_path = find_path_between_intersections(last_visited_intersection, deliveries[i].pickUp, turn_penalty);
//If path is valid
if (pickUp_path.size() != 0) {
copy_vector(pickUp_path, travel_path);
last_visited_intersection = deliveries[i].pickUp;
} else {
//Skips pickup for later visit
pickUp_skipped.push_back(deliveries[i].pickUp);
}
}
bool is_erased = false;
//keeps looping through skipped vector until all are visited
while (pickUp_skipped.size() != 0)
{
unsigned num_skipped_pickUp = pickUp_skipped.size();
//Loops through every pickup point that was skipped
for (unsigned i = 0; i < num_skipped_pickUp; i++) {
std::vector <unsigned> pickUp_path = find_path_between_intersections(last_visited_intersection, pickUp_skipped[i], turn_penalty);
//If path is valid
if (pickUp_path.size() != 0) {
copy_vector(pickUp_path, travel_path);
last_visited_intersection = pickUp_skipped[i];
pickUp_skipped.erase(pickUp_skipped.begin() + i);
is_erased = true;
}
}
//If no path was found
if (!is_erased)
return std::vector <unsigned>();
}
//Loops through every dropoff point and goes to first valid one
for (unsigned i = 0; i < deliveries.size(); i++) {
std::vector <unsigned> dropOff_path = find_path_between_intersections(last_visited_intersection, deliveries[i].dropOff, turn_penalty);
//If path is valid
if (dropOff_path.size() != 0) {
copy_vector(dropOff_path, travel_path);
last_visited_intersection = deliveries[i].dropOff;
} else {
//Skips dropoff for later visit
dropOff_skipped.push_back(deliveries[i].dropOff);
}
}
is_erased = false;
//keeps looping through skipped vector until all are visited
while (dropOff_skipped.size() != 0) {
unsigned num_skipped_dropOff = dropOff_skipped.size();
//Loops through every pickup point that was skipped
for (unsigned i = 0; i < num_skipped_dropOff; i++) {
std::vector <unsigned> dropOff_path = find_path_between_intersections(last_visited_intersection, dropOff_skipped[i], turn_penalty);
//If path is valid
if (dropOff_path.size() != 0) {
copy_vector(dropOff_path, travel_path);
last_visited_intersection = dropOff_skipped[i];
dropOff_skipped.erase(dropOff_skipped.begin() + i);
is_erased = true;
}
}
//If no path was found
if (!is_erased)
return std::vector <unsigned>();
}
std::vector <unsigned> return_path = find_path_between_intersections(last_visited_intersection, depots[0], turn_penalty);
copy_vector(return_path, travel_path);
return travel_path;
*/
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
double rand_double_in_range(double min, double max) {
double d = (double) rand() / RAND_MAX; // [0, 1]
return min + d * (max - min);
}
double P(double C_delta, double k, double T) {
return exp(-1 * C_delta / k * T);
}
//void simulated_annealing(std::vector<unsigned> initial_path, double turn_penalty, double high_temperature)
//{
// std::vector<unsigned> S = initial_path;
// double C = compute_path_travel_time(S, turn_penalty); // E.g. travel time
// double T = high_temperature; // big number
// double k = 1.38064852 * pow(10,-23);
// //bool solution_changing = true;
//
// while( /*solution_changing &&*/ T > 0 )
// {
// std::vector<unsigned> S_new = two_opt(S, turn_penalty); // perturb(S)
// double C_new = compute_path_travel_time(S_new, turn_penalty); // WAS: compute_path_travel_time(S, turn_penalty)
// double C_delta = C_new - C;
//
// if ( (C_new < C) || rand_double_in_range(0, 1) < P(C_delta, k, T) ) // Update solution
// {
// S = S_new;
// C = C_new;
// }
//
// --T; //T = reduceTemp(T);
// //solution_changing = is_solution_changing(S, S_new);
// }
//}
//Pushes vector elements from vector1 to vector2
void copy_vector(const std::vector<unsigned>& vector1, std::vector<unsigned>& vector2) {
unsigned vector1_size = vector1.size();
//Loops through all elements in vector1 and puts them in vector2
for (unsigned i = 0; i < vector1_size; ++i)
vector2.push_back(vector1[i]);
}
// if keys.size()>4, use two opt
/*
std::vector<unsigned> two_opt(const std::vector<DeliveryInfo>& deliveries, const float turn_penalty,
std::vector <int>& keys, std::vector<unsigned>& intersectionIDs) {
unsigned size = keys.size();
// unsigned improve = 0;
bool test_path_legal = false;
std::vector<unsigned> test_keys;
// generate two random number
while (!test_path_legal) {
bool tested = false;
test_path_legal = true;
unsigned first_random = (rand() % (size - 2) )+ 1;
unsigned second_random = (rand() % (size - 2) )+ 1;
while (first_random == second_random){
second_random = (rand() % (size - 2)) + 1;
}
// make sure second_random always > first random
if (second_random < first_random) {
unsigned temp = second_random;
second_random = first_random;
first_random = temp;
}
test_keys = two_opt_swap(first_random, second_random, keys);
for (unsigned i = first_random; i < = second_random; i++) {
for (unsigned j = first_random; j < = second_random; j++) {
if (keys[i] == keys[j] + deliveries.size()) {
test_path_legal = false;
tested = true;
break;
}
}
if (tested) break;
}
}
keys = test_keys;
std::vector<unsigned> intersection_id_new;
intersection_id_new.push_back(keys[0]);
for (unsigned i = 1; i < keys.size()-1; i++) {
if (keys[i] < deliveries.size())
intersection_id_new.push_back(deliveries[keys[i]].pickUp);
else
intersection_id_new.push_back(deliveries[keys[i] - deliveries.size()].dropOff);
}
intersection_id_new.push_back(keys[keys.size()-1]);
intersectionIDs = intersection_id_new;
return test_keys;
}
// std::vector<unsigned> test_keys = two_opt_swap(first_random, second_random, keys);
// if it is legal, construct the path
for(unsigned i=0; i< test_keys.size(); ++i){
std::vector<unsigned> path_segment = find_path_between_intersections(test_keys[i],test_keys[i+1],turn_penalty) ;
temp_path.insert(temp_path.end(), path_segment.begin(), path_segment.end());
}
double test_time = compute_path_travel_time(temp_path, turn_penalty);
if (test_time < time ) {
time = test_time;
shortest_path = temp_path;
}
*/
std::vector<unsigned> two_opt_swap(unsigned first_num, unsigned second_num, std::vector <int> keys) {
unsigned size = keys.size();
std::vector<unsigned> new_key;
// 1. take route[0] to route[i-1] and add them in order to new_route
for (unsigned a = 0; a <= first_num - 1; ++a) {
new_key.push_back(keys[a]);
}
// 2. take route[i] to route[k] and add them in reverse order to new_route
for (unsigned a = second_num; a >= first_num; a--) {
new_key.push_back(keys[a]);
}
// 3. take route[k+1] to end and add them in order to new_route
for (int a = second_num + 1; a < size; ++a) {
new_key.push_back(keys[a]);
}
// right now have a new vector of keys
}
std::vector<unsigned>
find_path_from_depot_to_first_pickUp(const std::vector<DeliveryInfo>& deliveries,
const unsigned depot,
unsigned &first_pickup_index,
const double turn_penalty) {
// priority queue to store nodes that are being preprocessed (openSet)
std::priority_queue<std::pair<double, unsigned>, std::vector<std::pair<double, unsigned>>, open_set_compare_> openSet;
// closed set (set of already evaluated nodes)
std::vector<bool> closedSet(getNumberOfIntersections(), false);
// "distance" vector contains times for each node
std::vector<double> dist(getNumberOfIntersections(), INT_MAX);
// Insert starting node in priority queue and initialize its time to 0
openSet.push(std::make_pair(0.0, depot));
dist[depot] = 0.0;
// best previous street seg
std::vector<unsigned> optimal_prior_streetseg(getNumberOfIntersections(), 0);
// best previous node
std::vector<unsigned> best_prior_node(getNumberOfIntersections(), 0);
bool found = false;
unsigned intersection_end_id;
//unsigned first_pick_up_index;
// set of nodeID's (all keys from cameFromStreetSegment[])
std::vector<bool> keys(getNumberOfIntersections(), false);
/***********************************************************************************/
/* Looping until priority queue becomes empty (or all distances are not finalized) */
/***********************************************************************************/
while (!openSet.empty() && !found) {
// Get top value (min distance) from set
unsigned current = openSet.top().second;
// if the current node == destination then break
//reaching the intersection of POI
for (unsigned i = 0; i < deliveries.size(); i++) {
if (current == deliveries[i].pickUp) {
found = true;
first_pickup_index = i;
intersection_end_id = deliveries[i].pickUp;
break;
}
}
if (found) break;
// Pop it off the queue
openSet.pop();
// skip node if it is already in closed set
if (closedSet[current])
continue;
// get all adjacent nodes to current node
std::vector<IntersectionNode> neighbor_nodes = adjacencyList[current];
if (current == depot) {
for (unsigned i = 0; i < neighbor_nodes.size(); ++i) {
// Get IntersectionNode object (nodeID, travelTImes, segmentIDs)
IntersectionNode n = neighbor_nodes[i];
std::vector<double> weights = n.travelTimes;
std::vector<unsigned> segments = n.streetSegments;
for (unsigned j = 0; j < weights.size(); ++j) {
// If there is shorter path to n through u.
if (dist[n.nodeID] > (dist[current] + weights[j])) {
// [nodeID] -> [best Street Segment ID, best previous node]
optimal_prior_streetseg[n.nodeID] = segments[j];
best_prior_node[n.nodeID] = current;
// add to keys vector
keys[n.nodeID] = true;
// Updating distance of n
dist[n.nodeID] = dist[current] + weights[j];
openSet.push(std::make_pair(dist[n.nodeID], n.nodeID));
}
}
}
} else {
for (unsigned i = 0; i < neighbor_nodes.size(); ++i) {
// Get IntersectionNode object (nodeID, travelTImes, segmentIDs)
IntersectionNode n = neighbor_nodes[i];
std::vector<double> weights = n.travelTimes;
std::vector<unsigned> segments = n.streetSegments;
unsigned previousSeg = optimal_prior_streetseg[current];
for (unsigned j = 0; j < weights.size(); ++j) {
// ADD TURN PENALTY TO WEIGHTS
if (street_segment_id_to_street_id[previousSeg] != street_segment_id_to_street_id[segments[j]])
weights[j] += turn_penalty;
// If there is shorter path to n through u.
if (dist[n.nodeID] > (dist[current] + weights[j])) {
// [nodeID] -> [best Street Segment ID, best previous node]
optimal_prior_streetseg[n.nodeID] = segments[j];
best_prior_node[n.nodeID] = current;
// add to keys vector
keys[n.nodeID] = true;
// Updating distance of n
dist[n.nodeID] = dist[current] + weights[j];
openSet.push(std::make_pair(dist[n.nodeID], n.nodeID));
}
}
}
}
// add current to the closedSet
closedSet[current] = true;
}
/****************************************/
/***** RECONSTRUCT THE SEGMENT PATH *****/
/****************************************/
unsigned key = intersection_end_id;
std::vector<unsigned> totalSegments;
totalSegments.push_back(optimal_prior_streetseg[key]);
while (keys[key]) {
key = best_prior_node[key];
if (key != depot) totalSegments.insert(totalSegments.begin(), optimal_prior_streetseg[key]);
}
/*assume that there is a way from depot to the first pick up all the time*/
// if(key != depot) return std::vector<unsigned>();
return totalSegments;
}
std::vector<unsigned>
find_path_from_last_dropOff_to_depots(const std::vector<unsigned>& depots,
unsigned end_dropOff,
const double turn_penalty,
unsigned &intersection_end_id) {
// priority queue to store nodes that are being preprocessed (openSet)
std::priority_queue<std::pair<double, unsigned>, std::vector<std::pair<double, unsigned>>, open_set_compare_> openSet;
// closed set (set of already evaluated nodes)
std::vector<bool> closedSet(getNumberOfIntersections(), false);
// "distance" vector contains times for each node
std::vector<double> dist(getNumberOfIntersections(), INT_MAX);
// Insert starting node in priority queue and initialize its time to 0
openSet.push(std::make_pair(0.0, end_dropOff));
dist[end_dropOff] = 0.0;
// best previous street seg
std::vector<unsigned> optimal_prior_streetseg(getNumberOfIntersections(), 0);
// best previous node
std::vector<unsigned> best_prior_node(getNumberOfIntersections(), 0);
bool found = false;
//unsigned intersection_end_id;
//unsigned first_pick_up_index;
// set of nodeID's (all keys from cameFromStreetSegment[])
std::vector<bool> keys(getNumberOfIntersections(), false);
/***********************************************************************************/
/* Looping until priority queue becomes empty (or all distances are not finalized) */
/***********************************************************************************/
while (!openSet.empty()&& !found) {
// Get top value (min distance) from set
unsigned current = openSet.top().second;
// if the current node == destination then break
//reaching the intersection of POI
for (unsigned i = 0; i < depots.size(); i++) {
if (current == depots[i]) {
found = true;
intersection_end_id = depots[i];
break;
}
}
if (found) break;
// Pop it off the queue
openSet.pop();
// skip node if it is already in closed set
if (closedSet[current])
continue;
// get all adjacent nodes to current node
std::vector<IntersectionNode> neighbor_nodes = adjacencyList[current];
if (current == end_dropOff) {
for (unsigned i = 0; i < neighbor_nodes.size(); ++i) {
// Get IntersectionNode object (nodeID, travelTImes, segmentIDs)
IntersectionNode n = neighbor_nodes[i];
std::vector<double> weights = n.travelTimes;
std::vector<unsigned> segments = n.streetSegments;
for (unsigned j = 0; j < weights.size(); ++j) {
// If there is shorter path to n through u.
if (dist[n.nodeID] > (dist[current] + weights[j])) {
// [nodeID] -> [best Street Segment ID, best previous node]
optimal_prior_streetseg[n.nodeID] = segments[j];
best_prior_node[n.nodeID] = current;
// add to keys vector
keys[n.nodeID] = true;
// Updating distance of n
dist[n.nodeID] = dist[current] + weights[j];
openSet.push(std::make_pair(dist[n.nodeID], n.nodeID));
}
}
}
} else {
for (unsigned i = 0; i < neighbor_nodes.size(); ++i) {
// Get IntersectionNode object (nodeID, travelTImes, segmentIDs)
IntersectionNode n = neighbor_nodes[i];
std::vector<double> weights = n.travelTimes;
std::vector<unsigned> segments = n.streetSegments;
unsigned previousSeg = optimal_prior_streetseg[current];
for (unsigned j = 0; j < weights.size(); ++j) {
// ADD TURN PENALTY TO WEIGHTS
if (street_segment_id_to_street_id[previousSeg] != street_segment_id_to_street_id[segments[j]])
weights[j] += turn_penalty;
// If there is shorter path to n through u.
if (dist[n.nodeID] > (dist[current] + weights[j])) {
// [nodeID] -> [best Street Segment ID, best previous node]
optimal_prior_streetseg[n.nodeID] = segments[j];
best_prior_node[n.nodeID] = current;
// add to keys vector
keys[n.nodeID] = true;
// Updating distance of n
dist[n.nodeID] = dist[current] + weights[j];
openSet.push(std::make_pair(dist[n.nodeID], n.nodeID));
}
}
}
}
// add current to the closedSet
closedSet[current] = true;
}
/****************************************/
/***** RECONSTRUCT THE SEGMENT PATH *****/
/****************************************/
unsigned key = intersection_end_id;
std::vector<unsigned> totalSegments;
totalSegments.push_back(optimal_prior_streetseg[key]);
while (keys[key]) {
key = best_prior_node[key];
if (key != end_dropOff) totalSegments.insert(totalSegments.begin(), optimal_prior_streetseg[key]);
}
/*assume that there is a way from depot to the first pick up all the time*/
// if(key != depot) return std::vector<unsigned>();
return totalSegments;
}