-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTour.hpp
101 lines (86 loc) · 1.5 KB
/
Tour.hpp
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
/*
* Tour.hpp
* Stores a candidate tour through all cities
*/
#ifndef TOUR_HPP
#define TOUR_HPP
#include <vector>
#include <algorithm>
#include "TourManager.hpp"
class Tour
{
public:
std::vector<City> tour;
int distance = 0;
TourManager tourManager;
Tour()
{
distance = 0;
}
Tour(TourManager IntourManager)
{
tourManager = IntourManager;
for (int i = 0; i < tourManager.numberOfCities(); i++)
{
tour.push_back(tourManager.getCity(i));
}
}
Tour(std::vector<City> &tour)
{
this->tour = tour;
}
std::vector<City> &getTour()
{
return tour;
}
void generateIndividual()
{
std::random_shuffle(tour.begin(), tour.end());
}
City getCity(int tourPosition)
{
return tour[tourPosition];
}
void setCity(int tourPosition, City city)
{
tour[tourPosition] = city;
distance = 0;
}
int getDistance()
{
if (distance == 0)
{
int tourDistance = 0;
for (int cityIndex = 0; cityIndex < tourSize(); cityIndex++)
{
City fromCity = getCity(cityIndex);
City destinationCity;
if (cityIndex + 1 < tourSize())
{
destinationCity = getCity(cityIndex + 1);
}
else
{
destinationCity = getCity(0);
}
tourDistance += fromCity.distanceTo(destinationCity);
}
distance = tourDistance;
}
return distance;
}
int tourSize()
{
return tour.size();
}
std::string toString()
{
std::string geneString = "|";
for (int i = 0; i < tourSize(); i++)
{
geneString += getCity(i).toString() + "|";
}
return geneString;
}
};
#endif