-
Notifications
You must be signed in to change notification settings - Fork 0
/
ant_sim.h
192 lines (156 loc) · 4.23 KB
/
ant_sim.h
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
#ifndef ANT_SIM_H
#define ANT_SIM_H
#include "ant.h"
#include "grid.h"
#include "sim_cell_data.h"
#include <QObject>
/*
* Simulates the behaviour of a colony of ants foraging for food.
*/
class AntSimulator : public QObject {
Q_OBJECT
public:
/*
* Construct the simulator with the specified seed for random number
* generation.
*/
AntSimulator(int seed = 0) : m_seed(seed), m_rng(seed){};
/*
* Set the simulation grid.
*/
void setup(Grid<SimCellData> grid);
/*
* Returns the total amount of food in the environment.
*/
int getTotalFood() const { return m_totalFood; }
/*
* Returns the amount of food that was brought back to the nest.
*/
int getDeliveredFood() const { return m_deliveredFood; }
/*
* Returns the number of steps each ant can perform before aborting the
* search.
*/
int getMaxAntSteps() { return m_maxAntSteps; }
/*
* Returns the valid range of values for the number of maximum ant steps.
*/
std::pair<int, int> getMaxAntStepsRange() { return {0, 300}; }
/*
* Returns the number of ants to be simulated.
*/
int getMaxAnts() { return m_maxAnts; }
/*
* Returns the valid range of values for the number of ants.
*/
std::pair<int, int> getMaxAntsRange() { return {0, 100}; }
/*
* Returns the pheromone strength parameter.
*/
int getPhStrength() { return m_phStrength * 100; }
/*
* Returns the valid range of values for the number of ants.
*/
std::pair<int, int> getPhStrengthRange() { return {0, 100}; }
/*
* Returns the pheromone spread parameter.
*/
int getPhSpread() { return m_phSpread; }
/*
* Returns the valid range of values for the pheromone spread parameter.
*/
std::pair<int, int> getPhSpreadRange() { return {0, 5}; }
/*
* Returns the pheromone decay parameter.
*/
int getPhDecay() { return m_phDecay * 100; }
/*
* Returns the valid range of values for the pheromone decay parameter.
*/
std::pair<int, int> getPhDecayRange() { return {0, 100}; }
/*
* Sets the pheromone strength to v/100.
*/
void setPhStrength(int v) {
if (v < 0 || v > 100)
return;
m_phStrength = (float)v / 100;
}
/*
* Sets the pheromone strength to `v`.
*/
void setPhSpread(int v) {
if (v < 0 || v > 5)
return;
m_phSpread = v;
}
/*
* Sets the pheromone decay rate to v/100.
*/
void setPhDecay(int v) {
if (v < 0 || v > 100)
return;
m_phDecay = (float)v / 100;
}
/*
* Sets the number of ants to simulate.
*/
void setMaxAnts(int n) { m_maxAnts = n; }
/*
* Sets each ants' maximum number of search steps.
*/
void setMaxAntSteps(int n);
/*
* Resets all parameters to their default value.
*/
void resetParams();
public slots:
/*
* Sets up the initial state of the population.
*/
void initialize();
/*
* Performs one step of the simulation.
*/
void step();
/*
* Spreads pheromone from the specified ant.
*/
void spreadPheromone(Ant ant);
/*
* Places food on and around the clicked cell.
*/
void onCellClicked(int x, int y);
/*
* Resets the simulation-
*/
void reset();
signals:
/*
* Emitted when the simulation step is completed.
*/
void gridReady(Grid<SimCellData> grid);
/*
* Emitted when initialization is completed.
*/
void initialized();
/*
* Emitted when the food counters are updated.
*/
void updateFoodCount(int delivered, int total);
private:
Grid<SimCellData> m_grid; // Grid of the simulation
size_t m_maxAnts = 20; // Number of ants to simulate
int m_maxAntSteps = 150; // Number of steps before aborting the search
int m_deliveredFood = 0; // Amount of food delivered to the nest
int m_totalFood = 0; // Total food available in the environment
int m_nestX; // x coordinate of the nest
int m_nestY; // y coordinate of the nest
float m_phDecay = 0.01f; // Pheromone decay rate
float m_phStrength = 1.0f; // Pheromone starting strength
int m_phSpread = 2; // Pheromone spread radius
int m_seed; // Seed for the rng
std::default_random_engine m_rng; // Random number generator
std::vector<Ant> m_ants; // Vector to keep track of the ants
};
#endif // ANT_SIM_H