forked from ameya005/AntColonyAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantalShared.cpp
96 lines (71 loc) · 2.6 KB
/
antalShared.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
//Ant algorithms for Travelling salesman Problem
#include <stdio.h>
#include "common.h"
#include <complex>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <iomanip>
#include <omp.h>
using namespace std;
int simulateSharedAnts() {
int k;
int moving = 0;
#pragma omp parallel for \
reduction(+: moving)
for (k = 0; k < MAX_ANTS; k++) {
//checking if there are any more cities to visit
if (ants[k].pathIndex < MAX_CITIES) {
ants[k].nextCity = selectNextCity(k);
ants[k].tabu[ants[k].nextCity] = 1;
ants[k].path[ants[k].pathIndex++] = ants[k].nextCity;
ants[k].tourLength += dist[ants[k].curCity][ants[k].nextCity];
//handle last case->last city to first
if (ants[k].pathIndex == MAX_CITIES) {
ants[k].tourLength += dist[ants[k].path[MAX_CITIES - 1]][ants[k].path[0]];
}
ants[k].curCity = ants[k].nextCity;
moving++;
}
}
return moving;
}
int main() {
int curTime = 0;
cout << "S-ACO:";
cout << "MaxTime=" << MAX_TIME << endl;
srand(time(NULL));
std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
init();
std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
std::chrono::microseconds microRunTime
= std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime);
double runTime = microRunTime.count() / 1000000.0;
std::cout << std::setprecision( 8 )
<< "Init clock time = " << runTime << " seconds."
<< std::endl << std::flush;
startTime = std::chrono::system_clock::now();
omp_set_num_threads( 8 );
while (curTime++ < MAX_TIME) {
if (simulateSharedAnts() == 0) {
updateTrails();
if (curTime != MAX_TIME)
restartAnts();
cout << "\n Time is " << curTime << " (" << best << " km)";
}
}
endTime = std::chrono::system_clock::now();
microRunTime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime);
runTime = microRunTime.count() / 1000000.0;
std::cout << std::setprecision( 8 ) << endl
<< "Wall clock time = " << runTime << " seconds."
<< std::endl << std::flush;
cout << "\nSACO: Best tour = " << best << " km" << endl << endl << endl;
antType antBest;
antBest = ants[bestIndex];
for (int i = 0; i < MAX_CITIES; i++) {
cout << cities[antBest.path[i]].name << " > ";
}
emitDataFile(bestIndex);
return 0;
}