-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathant_sim.cpp
More file actions
198 lines (168 loc) · 5.91 KB
/
ant_sim.cpp
File metadata and controls
198 lines (168 loc) · 5.91 KB
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
#include "ant_sim.h"
#include "sim_cell_data.h"
void AntSimulator::setup(Grid<SimCellData> grid) {
reset();
m_grid = grid;
}
void AntSimulator::initialize() {
reset();
// Find a place to spawn the nest
do {
m_nestX = m_rng() % m_grid.getCols();
m_nestY = m_rng() % m_grid.getRows();
} while (m_grid.getCell(m_nestX, m_nestY).getData().getType() ==
SimCellData::Type::ROCK);
SimCellData tmp = m_grid.getCell(m_nestX, m_nestY).getData();
tmp.setType(SimCellData::Type::NEST);
m_grid.setCell(m_nestX, m_nestY, tmp);
emit gridReady(m_grid);
emit initialized();
}
void AntSimulator::step() {
// Spawn ants
if (m_ants.size() < m_maxAnts) {
m_ants.push_back(Ant(m_nestX, m_nestY,
std::pair<int, int>(m_rng() % 3 - 1, m_rng() % 3 - 1),
m_maxAntSteps));
}
// Kill excess ants
while (m_ants.size() > m_maxAnts) {
int victimIndex = m_rng() % m_ants.size();
const Ant &toRemove = m_ants[victimIndex];
SimCellData data =
m_grid.getCell(toRemove.getX(), toRemove.getY()).getData();
data.setType(SimCellData::Type::FLOOR);
m_grid.setCell(toRemove.getX(), toRemove.getY(), data);
m_ants.erase(m_ants.begin() + victimIndex);
}
// Update nest pheromone
std::vector<Cell<SimCellData>> nestArea =
m_grid.getNeumannNeighbourhood(m_nestX, m_nestY, 2);
for (Cell<SimCellData> cell : nestArea) {
SimCellData data = cell.getData();
data.incrementHomePheromone(1.0f, 0, 0);
m_grid.setCell(cell.getX(), cell.getY(), data);
}
// Simulate pheromone evaporation
for (int x = 0; x < m_grid.getCols(); x++) {
for (int y = 0; y < m_grid.getRows(); y++) {
SimCellData tmp = m_grid.getCell(x, y).getData();
tmp.decrementPheromones(m_phDecay);
m_grid.setCell(x, y, tmp);
}
}
// Move ants
for (Ant &ant : m_ants) {
std::vector<Cell<SimCellData>> neighbourhood =
m_grid.getDirectionalNeighbourhood(ant.getX(), ant.getY(),
ant.getDirection());
// We don't consider occupied cells
std::erase_if(neighbourhood, [&](Cell<SimCellData> cell) {
return cell.getData().getType() == SimCellData::Type::ROCK ||
cell.getData().getType() == SimCellData::Type::ANT ||
(ant.hasFood() &&
cell.getData().getType() == SimCellData::Type::FOOD);
});
// No suitable neighbouring cells to move to
if (neighbourhood.empty()) {
ant.invert();
continue;
}
Cell<SimCellData> destination = ant.pickDestination(neighbourhood, m_rng);
// Restore the previous cell
SimCellData tmp = m_grid.getCell(ant.getX(), ant.getY()).getData();
if (ant.getX() == m_nestX && ant.getY() == m_nestY)
tmp.setType(SimCellData::Type::NEST);
else {
tmp.setType(SimCellData::Type::FLOOR);
}
m_grid.setCell(ant.getX(), ant.getY(), tmp);
spreadPheromone(ant);
// Update the ant
ant.move(destination.getX(), destination.getY());
if (!ant.hasFood() &&
destination.getData().getType() == SimCellData::Type::FOOD) {
ant.pickUpFood();
ant.invert();
ant.returnHome();
} else if (destination.getData().getType() == SimCellData::Type::NEST) {
if (ant.hasFood()) {
m_deliveredFood++;
emit updateFoodCount(m_deliveredFood, m_totalFood);
ant.dropFood();
}
ant.invert();
ant.seekFood();
}
// Update the grid
tmp = m_grid.getCell(destination.getX(), destination.getY()).getData();
tmp.setType(SimCellData::Type::ANT);
m_grid.setCell(destination.getX(), destination.getY(), tmp);
}
emit gridReady(m_grid);
}
void AntSimulator::spreadPheromone(Ant ant) {
std::vector<Cell<SimCellData>> neighbourhood =
m_grid.getNeumannNeighbourhood(ant.getX(), ant.getY(), m_phSpread);
neighbourhood.push_back(m_grid.getCell(ant.getX(), ant.getY()));
for (Cell<SimCellData> cell : neighbourhood) {
SimCellData data = cell.getData();
// Pheromone strength decreases with distance from the source
int distFromSource =
m_grid.manhattanDist(cell.getX(), cell.getY(), ant.getX(), ant.getY());
if (ant.getMode() == Ant::RETURN && ant.hasFood())
data.incrementFoodPheromone(m_phStrength, distFromSource,
ant.getTraveledDistance());
else if (ant.getMode() == Ant::SEEK)
data.incrementHomePheromone(m_phStrength, distFromSource,
ant.getTraveledDistance());
m_grid.setCell(cell.getX(), cell.getY(), data);
}
}
void AntSimulator::onCellClicked(int x, int y) {
if (x < 0 || x >= m_grid.getCols() || y < 0 || y >= m_grid.getRows())
return;
std::vector<Cell<SimCellData>> neighbourhood =
m_grid.getNeumannNeighbourhood(x, y, 2);
neighbourhood.push_back(m_grid.getCell(x, y));
// Place food
for (Cell<SimCellData> cell : neighbourhood) {
SimCellData data = cell.getData();
if (data.getType() == SimCellData::FLOOR) {
data.setType(SimCellData::Type::FOOD);
m_grid.setCell(cell.getX(), cell.getY(), data);
m_totalFood++;
emit updateFoodCount(m_deliveredFood, m_totalFood);
}
}
emit gridReady(m_grid);
}
void AntSimulator::setMaxAntSteps(int m) {
m_maxAntSteps = m;
for (Ant &ant : m_ants) {
ant.setMaxSteps(m);
}
}
void AntSimulator::reset() {
m_nestX = -1;
m_nestY = -1;
m_ants.clear();
// Clears pheromones
for (int x = 0; x < m_grid.getCols(); x++) {
for (int y = 0; y < m_grid.getRows(); y++) {
SimCellData tmp = m_grid.getCell(x, y).getData();
if (tmp.getType() == SimCellData::Type::ANT ||
tmp.getType() == SimCellData::Type::NEST ||
tmp.getType() == SimCellData::Type::FOOD)
tmp.setType(SimCellData::Type::FLOOR);
tmp.clearPheromones();
m_grid.setCell(x, y, tmp);
}
}
}
void AntSimulator::resetParams() {
m_maxAnts = 20;
m_phStrength = 1.0f;
m_phSpread = 2;
m_phDecay = 0.01f;
}