-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interface.h
1457 lines (1282 loc) · 42.2 KB
/
Interface.h
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
//
// Created by Hosein on 4/6/2023.
//
#ifndef HW_1_INTERFACE_H
#define HW_1_INTERFACE_H
#include <time.h>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <random>
#include <chrono>
#include <cmath>
#include <cstdlib>
#define MAX 20
#define MAXS 130
using namespace std;
class File;
class Vertex;
class TOP;
class SA;
class GRASP;
class TabuItem;
static vector<Vertex> vertexVector;
static vector<int> cantVisit;
void add_to_vertex_vector(Vertex v);
class Vertex{
protected:
int i; //vertex number
float x; //x coordinate
float y; //y coordinate
float d; //service duration
float profit; //profit of the duration
float opening_time;
float closing_time;
int nothing;
public:
Vertex(int a){
nothing = a;
}
Vertex(int i_v, float x_v, float y_v, float d_v, float profit_v, float openingTime_v, float closingTime_v){
i = i_v;
x = x_v;
y = y_v;
d = d_v;
profit = profit_v;
opening_time = openingTime_v;
closing_time = closingTime_v;
}
int getI() const {
return i;
}
float getX() const {
return x;
}
float getY() const {
return y;
}
float getD() const {
return d;
}
float getProfit() const {
return profit;
}
float getOpeningTime() const {
return opening_time;
}
float getClosingTime() const {
return closing_time;
}
};
class File {
protected:
int N, V;
string fileName;
public:
File(string file_name){
fileName = file_name;
}
int get_N(){
return N;
}
int get_V(){
return V;
}
void set_N(int n){
N = n;
}
void set_V(int v){
V = v;
}
void read_file() {
string firstPath = "C:\\HOSEIN\\jozve and tamrin\\8th Semester\\ADA\\HW 1\\Instances\\";
string format = ".txt";
string filePath = firstPath + fileName + format;
ifstream new_file;
// Open a file to perform a write operation using a file object.
cout << filePath << '\n';
new_file.open(filePath);
if (!new_file) {
cout << "Can't open file!" << '\n';
exit(1);
}
if (new_file.is_open()) {
string sa;
int lineCount = 1;
// Read data from the file object and put it into a string.
while (getline(new_file, sa)) {
if (lineCount == 2) {
lineCount++;
continue;
}
split_lines(sa, lineCount);
lineCount++;
}
new_file.close();
}
}
void read_file1() {
string firstPath = "C:\\HOSEIN\\jozve and tamrin\\8th Semester\\ADA\\HW 1\\Instances\\";
string format = ".txt";
string filePath = firstPath + fileName + format;
ifstream new_file;
// Open a file to perform a write operation using a file object.
cout << filePath << '\n';
new_file.open(filePath);
if (!new_file) {
cout << "Can't open file!" << '\n';
exit(1);
}
if (new_file.is_open()) {
string sa;
int lineCount = 3;
// Read data from the file object and put it into a string.
while (getline(new_file, sa)) {
if (lineCount == 2) {
lineCount++;
continue;
}
split_lines1(sa, lineCount);
lineCount++;
}
new_file.close();
}
}
void split_lines(string line, int lineCount){
float array[MAX];
fill_n(array,MAX,-1);
if(lineCount == 1){
stringstream lineStream(line);
int index = 0;
while (lineStream.good() && index < MAX)
{
lineStream >> array[index];
index ++;
}
set_V(array[1]);
set_N(array[2]);
}
else{
stringstream lineStream(line);
int index = 0;
int row = lineCount - 3;
while (lineStream.good() && index < MAX)
{
lineStream >> array[index];
index ++;
}
create_vertex(array);
}
}
void split_lines1(string line, int lineCount){
float array[MAX];
fill_n(array,MAXS,-1);
if(lineCount == 1){
stringstream lineStream(line);
int index = 0;
while (lineStream.good() && index < MAX)
{
lineStream >> array[index];
index ++;
}
set_V(array[1]);
set_N(array[2]);
}
else{
stringstream lineStream(line);
int index = 0;
int row = lineCount - 3;
while (lineStream.good() && index < MAX)
{
lineStream >> array[index];
index ++;
}
// create_vertex(array);
for(int j = 0; j < MAXS; j++){
cout<<array[j]<<" ";
}
cout<<"\n";
}
}
void create_vertex(float array[]){
int i = array[0];
float x = array[1];
float y = array[2];
float d = array[3];
float profit = array[4];
int c_index = not_minus1_index(array);
float opening_time = array[c_index - 1];
float closing_time = array[c_index];
Vertex vertex(i,x,y,d,profit,opening_time,closing_time);
add_to_vertex_vector(vertex);
}
int not_minus1_index(float array[]) {
for (int i = MAX - 1; i > -1; i--) {
if (array[i] != -1)
return (i);
}
}
};
class TOP{
protected:
int N;
int V;
float time;
float profit;
vector<int> finalSolution;
int nothing;
public:
TOP(int a ){ //Temp Constructor
nothing = a;
time = 0;
profit = 0;
}
const vector<int> &getFinalSolution() const {
return finalSolution;
}
TOP(int n, int v){
N = n;
V = v;
time = 0;
profit = 0;
}
float getProfit() const {
return profit;
}
void addProfit(float profit){
TOP::profit += profit;
}
void setTime(float time) {
TOP::time = time;
}
void addTime(float time){
TOP::time += time;
}
float distance_time(Vertex current, Vertex next){
int x1 = current.getX();
int y1 = current.getY();
int x2 = next.getX();
int y2 = next.getY();
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
void calculate_solution(vector<int>solution , int check){
profit = 0;
float travel_Time = 0;
int length = solution.size();
int visitedNodes = 0;
int index = 0;
Vertex current = vertexVector[0];
Vertex head = vertexVector[0];
Vertex next = vertexVector[0];
while (index < length){
if(solution[index] < 0){
setTime(0); // New path
index++;
continue;
}
else{
current = vertexVector[solution[index]];
int visitResult = canBeVisited(current);
if(visitResult == 0){
head = current; // Current holder
// cout<<"We visit "<<current.getI()<<'\n';
// cout<<"Time Before visit "<<time<<'\n';
addTime(current.getD());
addProfit(current.getProfit());
// cout<<"Time after visit "<<time<<'\n';
if(solution[index] != 0){
visitedNodes++;
}
}
else if(visitResult == -1){
// cout<<"We can't visit "<<current.getI()<<'\n';
// cout<<"Time in this node "<<time<<'\n';
addTime(-1 * travel_Time);
}
else{
head = current;
// cout<<"We can visit "<<current.getI()<<"But after "<<visitResult<<'\n';
addTime(visitResult);
// cout<<"Time Before visit "<<time<<'\n';
addTime(current.getD());
addProfit(current.getProfit());
// cout<<"Time after visit "<<time<<'\n';
if(solution[index] != 0){
visitedNodes++;
}
}
if(index + 1 != length){
next = vertexVector[solution[index + 1]];
travel_Time = distance_time(head, next);
// cout<<"Travel time from "<<head.getI()<<" to "<<next.getI()<<" "<<travel_Time<<'\n';
addTime(travel_Time);
}
}
index++;
}
if(check == 1){
cout<<"We visit "<<visitedNodes<<" nodes with profit "<<profit<<'\n';
}
}
void calculate_solution_final(vector<int>solution , int check){
profit = 0;
float travel_Time = 0;
int length = solution.size();
int visitedNodes = 0;
int index = 0;
Vertex current = vertexVector[0];
Vertex head = vertexVector[0];
Vertex next = vertexVector[0];
while (index < length){
if(solution[index] < 0){
setTime(0); // New path
finalSolution.push_back(solution[index]);
index++;
continue;
}
else{
current = vertexVector[solution[index]];
int visitResult = canBeVisited(current);
if(visitResult == 0){
head = current; // Current holder
cout<<"We visit "<<current.getI()<<'\n';
cout<<"Time Before visit "<<time<<'\n';
addTime(current.getD());
addProfit(current.getProfit());
cout<<"Time after visit "<<time<<'\n';
finalSolution.push_back(current.getI());
if(solution[index] != 0){
visitedNodes++;
}
}
else if(visitResult == -1){
cout<<"We can't visit "<<current.getI()<<'\n';
cout<<"Time in this node "<<time<<'\n';
addTime(-1 * travel_Time);
}
else{
head = current;
cout<<"We can visit "<<current.getI()<<"But after "<<visitResult<<'\n';
addTime(visitResult);
cout<<"Time Before visit "<<time<<'\n';
addTime(current.getD());
addProfit(current.getProfit());
finalSolution.push_back(current.getI());
cout<<"Time after visit "<<time<<'\n';
if(solution[index] != 0){
visitedNodes++;
}
}
if(index + 1 != length){
next = vertexVector[solution[index + 1]];
travel_Time = distance_time(head, next);
cout<<"Travel time from "<<head.getI()<<" to "<<next.getI()<<" "<<travel_Time<<'\n';
addTime(travel_Time);
}
}
index++;
}
if(check == 1){
cout<<"We visit "<<visitedNodes<<" nodes with profit "<<profit<<'\n';
}
}
int canBeVisited(Vertex v){
if (time >= v.getOpeningTime() && time <= v.getClosingTime())
return 0;
else if (time < v.getOpeningTime())
return ceill(v.getOpeningTime() - time);
else{
return -1;
}
}
vector<int> random_solution_generator(){
vector<int> numbers;
unsigned num = chrono::system_clock::now().time_since_epoch().count();
for(int i = 1; i < N+1; i++)
numbers.push_back(i);
shuffle(numbers.begin(), numbers.end(),default_random_engine(num));
vector<int> solution;
solution.push_back(0); // We start from 0
solution.push_back(numbers[0]);
float nTest = N;
float vTest = V;
int division = ceil(nTest/vTest);
for (int i = 1; i<N; i++){
if ((i % division)==0){
solution.push_back(0); //Each path ends to 0
solution.push_back(-1); // To separate path
solution.push_back(0); // next path start
}
solution.push_back(numbers[i]);
}
solution.push_back(0); //Last part
return solution;
}
vector<int> grasp_solution_generator() {
vector<int> solution;
solution.push_back(0); // We start from 0
solution.push_back(0);
float nTest = N;
float vTest = V;
int division = ceil(nTest/vTest);
int pathN = -1;
for (int i = 1; i<N; i++){
if ((i % division)==0){
solution.push_back(0); //Each path ends to 0
solution.push_back(pathN); // To separate path
solution.push_back(0); // next path start
pathN --;
}
solution.push_back(0);
}
solution.push_back(0); //Last part
return solution;
}
};
class SA{
protected:
int maxIterations;
float t0;
float alpha;
float noImprove;
int B;
int N;
int V;
vector<int> solution;
int fBest;
public:
SA(vector<int> solution_ , float t0_ , float alpha_ , float noImprove_, int B_, int N_, int V_){
solution = solution_;
alpha = alpha_;
t0 = t0_;
B = B_;
N = N_;
V = V_;
fBest = 0;
noImprove = noImprove_;
maxIterations = (N_ + V_ -1) * B_;
}
vector<int> swapRandom(vector<int> mySolution){
vector<int> newSolution = mySolution;
int randomIndex1 = vertexGenerator(mySolution);
int randomIndex2 = vertexGenerator(mySolution);
int tmp = newSolution[randomIndex1];
newSolution[randomIndex1] = newSolution[randomIndex2];
newSolution[randomIndex2] = tmp;
return newSolution;
}
bool checkVertex(int v1){
if(v1 == 0 || v1 == -1){
return false;
}
return true;
}
int vertexGenerator(vector<int> mySolution){
int length = mySolution.size();
int randomIndex = (rand() % length);
int vertex = mySolution[randomIndex];
while (!checkVertex(vertex)){
randomIndex = (rand() % length);
vertex = mySolution[randomIndex];
}
return randomIndex;
}
vector<int> insertionRandom(vector<int> mySolution){
vector<int> newSolution = mySolution;
int randomIndex1 = vertexGenerator(mySolution);
int randomIndex2 = vertexGenerator(mySolution);
newSolution.erase(newSolution.begin() + randomIndex1);
if(randomIndex2 > randomIndex1){
auto position = newSolution.begin() + randomIndex2 - 1;
newSolution.insert(position , mySolution[randomIndex1]);
}
else{
auto position = newSolution.begin() + randomIndex2;
newSolution.insert(position , mySolution[randomIndex1]);
}
return newSolution;
}
int random_0_1(){
return (rand()%100);
}
float calculate(vector<int> someSolution , int check){
TOP top(0);
top.calculate_solution(someSolution, check);
return top.getProfit();
}
void calculateFinal(vector<int> someSolution , int check){
TOP top(0);
top.calculate_solution_final(someSolution, check);
cout<<top.getProfit()<<" Final S"<<'\n';
vector<int> finalS = top.getFinalSolution();
cout<<finalS.size()<<" size"<<'\n';
for (int i = 0; i < finalS.size(); i++){
cout<<finalS[i]<<",";
}
cout<<'\n';
}
vector<int> swapVector(vector<int> vector, int index1, int index2){
int tmp = vector[index1];
vector[index1] = vector[index2];
vector[index2] = tmp;
return vector;
}
vector<int> insertionVector(vector<int> vector1, int index1 , int index2){
vector<int> newVector = vector1;
newVector.erase(newVector.begin() + index1);
if(index2 > index1){
auto position = newVector.begin() + index2 - 1;
newVector.insert(position , vector1[index1]);
}
else{
auto position = newVector.begin() + index2;
newVector.insert(position , vector1[index1]);
}
return newVector;
}
bool localSearchSwap(){
vector<int> tempSolution = solution;
int solutionLength = tempSolution.size();
float bestProfit = calculate(solution , 0);
int bestI;
int bestJ;
int flag = 0;
for(int i = 0; i < solutionLength; i++){
if(tempSolution[i] == 0 || tempSolution[i] == -1){ //We don't want to swap these
continue;
}
for(int j = i; j < solutionLength; j++){
if(tempSolution[j] == 0 || tempSolution[j] == -1){
continue;
}
tempSolution = swapVector(tempSolution, i, j);
float localProfit = calculate(tempSolution , 0);
if(localProfit > bestProfit){
flag = 1;
bestProfit = localProfit;
bestI = i;
bestJ = j;
}
tempSolution.clear();
tempSolution = solution;
}
}
if (flag == 1) {
solution = swapVector(solution, bestI, bestJ);
return true;
}
return false;
}
bool localSearchInsertion(){
vector<int> tempSolution = solution;
int solutionLength = tempSolution.size();
float bestProfit = calculate(solution , 0);
int bestI;
int bestJ;
int flag = 0;
for(int i = 0; i < solutionLength; i++){
if(tempSolution[i] == 0 || tempSolution[i] == -1){ //We don't want to mess with these
continue;
}
for(int j = i; j < solutionLength; j++){
if(tempSolution[j] == 0 || tempSolution[j] == -1){
continue;
}
tempSolution = insertionVector(tempSolution, i, j);
float localProfit = calculate(tempSolution , 0);
if(localProfit > bestProfit){
flag = 1;
bestProfit = localProfit;
bestI = i;
bestJ = j;
}
tempSolution.clear();
tempSolution = solution;
}
}
if (flag == 1) {
solution = insertionVector(solution, bestI, bestJ);
return true;
}
return false;
}
void ssaAlgorithm(){
// Initial Part!
srand(time(0));
vector<int> currentSolution = solution;
float t = t0;
fBest = calculate(solution , 1);
int localNoImprove = 0;
// Initial Part!
vector<int> newSolution; // Y
while(localNoImprove < noImprove){
int iteration = 0;
while(iteration < maxIterations){
iteration++; // Inc iteration
float currentProfit = calculate(currentSolution , 0); // OBJ(X)
int p = random_0_1();
if(p > 50){
newSolution = insertionRandom(currentSolution);
}
else{
newSolution = swapRandom(currentSolution);
}
float newProfit = calculate(newSolution , 0);
float delta = newProfit - currentProfit;
if(delta > 0){
}
else{
double x = delta / t;
double prob = exp(x) * 100;
float r = random_0_1();
if (prob < r){
continue;
}
}
currentSolution.clear();
currentSolution = newSolution;
if(newProfit > fBest){
solution = currentSolution;
fBest = newProfit;
}
newSolution.clear();
}
t = t * alpha;
// Local search
bool isSwapImprove = localSearchSwap();
bool isInsertionImprove = localSearchInsertion();
if(!isSwapImprove && !isInsertionImprove){
localNoImprove++;
}
}
}
float printFinalSolution(){
cout<<"***************** Final Solution *****************"<<'\n';
for(int k = 0; k<solution.size(); k++){
cout<<solution[k]<<' ';
}
cout<<'\n';
float finalProfit = calculate(solution, 1);
cout<<"*****************************************************"<<'\n';
calculateFinal(solution, 1);
return finalProfit;
}
void fsaAlgorithm(int seconds){
// Initial Part!
srand(time(0));
vector<int> currentSolution = solution;
float t = t0;
fBest = calculate(solution , 1);
// int localNoImprove = 0;
// Initial Part!
vector<int> newSolution; // Y
auto Start = std::chrono::high_resolution_clock::now();
while(1){
int iteration = 0;
auto End = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> Elapsed = End - Start;
if (Elapsed.count() >= seconds * 1000)
break;
while(iteration < maxIterations){
iteration++; // Inc iteration
float currentProfit = calculate(currentSolution , 0); // OBJ(X)
int p = random_0_1();
if(p > 50){
newSolution = insertionRandom(currentSolution);
}
else{
newSolution = swapRandom(currentSolution);
}
float newProfit = calculate(newSolution , 0);
float delta = newProfit - currentProfit;
if(delta > 0){
}
else{
double x = delta / t;
double prob = exp(x) * 100;
float r = random_0_1();
if (prob < r){
continue;
}
}
currentSolution.clear();
currentSolution = newSolution;
if(newProfit > fBest){
solution = currentSolution;
fBest = newProfit;
}
newSolution.clear();
}
t = t * alpha;
// Local search
bool isSwapImprove = localSearchSwap();
bool isInsertionImprove = localSearchInsertion();
}
}
};
class GRASP{
protected:
int N;
int V;
int RCL_LEN;
int maxIteration;
vector<int> FirstSolution;
vector<int> mainSolution;
vector<int> BestSolution;
int maxLocalI;
int fBest;
public:
GRASP(int n_, int v_, int rcl_len, int maxI, int maxLI, vector<int> FS){
N = n_;
V = v_;
RCL_LEN = rcl_len;
maxIteration = maxI;
fBest = 0;
FirstSolution = FS;
maxLocalI = maxLI;
}
struct sortByProfitStructure
{
inline bool operator() (const Vertex& v1, const Vertex& v2)
{
return (v1.getProfit() > v2.getProfit());
}
};
struct sortByTravelTimeStructure{
inline bool operator() (const Vertex& v1, const Vertex& v2){
TOP top(0);
Vertex initial = vertexVector[0];
float v1TravelTime = top.distance_time(initial, v1);
float v2TravelTime = top.distance_time(initial, v2);
return (v1TravelTime < v2TravelTime);
}
};
vector<int> sortByProfit(){
vector<Vertex> tmpV = vertexVector;
sort(tmpV.begin(), tmpV.end(), sortByProfitStructure());
vector<int> sortedByProfit;
for(int i = 0; i < tmpV.size() - 1; i++){
sortedByProfit.push_back(tmpV[i].getI());
}
return sortedByProfit;
}
vector<int> sortByTravelTime(){
vector<Vertex> tmpV = vertexVector;
sort(tmpV.begin(), tmpV.end(), sortByTravelTimeStructure());
TOP top(0);
vector<int> sortedByTT;
for(int i = 1; i< tmpV.size(); i++){
sortedByTT.push_back(tmpV[i].getI());
}
return sortedByTT;
}
int randomZeroRCL(int size){
int randomNumber = rand() % size;
return randomNumber;
}
int randomZero100(){
int randomNumber = rand() % 100;
return randomNumber;
}
vector<int> addToRCL( vector<int> sortedV){
vector<int> RCL;
int index = 0;
for(int & vertexId : sortedV){
if(index < RCL_LEN){
RCL.push_back(vertexId);
index++;
}
else{
break;
}
}
return RCL;
}
float calculate(vector<int> someSolution , int check){
TOP top(0);
top.calculate_solution(someSolution, check);
return top.getProfit();
}
void calculateFinal(vector<int> someSolution , int check){
TOP top(0);
top.calculate_solution_final(someSolution, check);
cout<<top.getProfit()<<" Final S"<<'\n';
vector<int> finalS = top.getFinalSolution();
cout<<finalS.size()<<" size"<<'\n';
for (int i = 0; i < finalS.size(); i++){
cout<<finalS[i]<<" ";
}
cout<<'\n';
cout<< "For check"<<'\n';
for (int i = 0; i < finalS.size(); i++){
cout<<finalS[i]<<",";
}
cout<<'\n';
}
void constructSolution(){
vector<int> greedyVector;
mainSolution = FirstSolution;
int chooseGreedy = randomZero100();
if (chooseGreedy > 50){
greedyVector = sortByTravelTime();
} else{
greedyVector = sortByProfit();
}
for(int i = 0; i < N; i++){ //Construct Path
vector<int> RCL = addToRCL(greedyVector);
int index = randomZeroRCL(RCL.size());
int selectedCandidate = RCL[index];
mainSolution = choosingBestPath(mainSolution, selectedCandidate);
RCL.erase(RCL.begin() + index); //Delete from RCL
greedyVector.erase(greedyVector.begin() + index); //Delete From vector
}
// printVector("Constructed Solution", mainSolution);
// calculate(mainSolution, 1);
}
void printVector(string name, vector<int> v){
cout<<name<<'\n';
for(int & element: v){
cout<<element<<" ";
}
cout<<'\n';
}
vector<int> choosingBestPath(vector<int> baseSolution, int SC){
float bestProfit = calculate(baseSolution,0);
int bestI = 0;
int ableToadI = 0;
int flag = 0;
for(int i = 0 ; i< V; i++){
vector<int> tmp = baseSolution;
int startIndex = 1 + ((i) * 3) + (i * ceill(N/V));
int lastIndex = startIndex + ceill(N/V);
for (int j = startIndex ; j < lastIndex ; j++){
if(tmp[j] == 0){
tmp[j] = SC;
ableToadI = i;
break;
}
continue;
}
float currentProfit = 0;
currentProfit = calculate(tmp, 0);
if(currentProfit > bestProfit){
bestProfit = currentProfit;
bestI = i;
flag = 1;
}
else{