-
Notifications
You must be signed in to change notification settings - Fork 0
/
ant.cpp
53 lines (45 loc) · 1.49 KB
/
ant.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
#include "ant.h"
void Ant::move(int x, int y) {
int dX = x - m_x;
int dY = y - m_y;
m_d = std::pair<int, int>(dX, dY);
m_traveledDistance++;
if (m_mode == SEEK) {
// The search is unfruitful, abort it and return home
if (m_traveledDistance >= m_maxSteps) {
m_mode = RETURN;
m_traveledDistance = 0;
invert();
}
}
m_x = x;
m_y = y;
}
Cell<SimCellData>
Ant::pickDestination(const std::vector<Cell<SimCellData>> &candidates,
std::default_random_engine &rng) {
// Choose the most appealing cell to move to
Cell<SimCellData> destination = candidates[rng() % candidates.size()];
for (Cell<SimCellData> neighbour : candidates) {
if (m_mode == RETURN) {
// When in return mode, the ant looks for the nest
if (neighbour.getData().getType() == SimCellData::Type::NEST) {
// We found the nest, stop searching
destination = neighbour;
break;
} else if (neighbour.getData().getHomePheromone() >
destination.getData().getHomePheromone())
destination = neighbour;
} else if (m_mode == SEEK) {
// When in seek mode, the ant looks for food
if (neighbour.getData().getType() == SimCellData::Type::FOOD) {
// We found food, stop searching
destination = neighbour;
break;
} else if (neighbour.getData().getFoodPheromone() >
destination.getData().getFoodPheromone())
destination = neighbour;
}
}
return destination;
}