-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.cpp
75 lines (64 loc) · 1.52 KB
/
Population.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
//
// Created by danny on 2018-11-09.
//
#include "Population.hpp"
/**
* Default constructor that fills tours vector with shuffled tours
*/
Population::Population() {
for (int i = 0; i < POPULATION_SIZE; ++i) {
Tour newTour(false);
newTour.shuffleCities();
tours.push_back(newTour);
}
baseDistance = getFittestTour().getTourDistance();
}
/**
* Getter for a tour at specified index
* @param index tour index in population
* @return tour at index
*/
Tour & Population::getTour(int index) {
return tours.at(static_cast<unsigned long>(index));
}
/**
* Iterates through vector of tours and returns the one with best fitness rating
* @return fittest tour
*/
Tour Population::getFittestTour() {
auto fittest = tours.at(0);
for (auto it = tours.begin() + 1; it < tours.end(); ++it) {
if (fittest.determineFitness() <= it->determineFitness()) {
fittest = *it;
}
}
return fittest;
}
/**
* Getter for base distance
* @return base distance
*/
int Population::getBaseDistance() {
return baseDistance;
}
/**
* Getter for populations vector of tours
* @return tours vector
*/
vector<Tour> &Population::getTours() {
return tours;
}
/**
* Getter for const value for the population's size
* @return population size
*/
int Population::getSize() {
return POPULATION_SIZE;
}
/**
* Setter for the populations vector of tours
* @param tours vector of tours
*/
void Population::setTours(const vector<Tour> &tours) {
Population::tours = tours;
}