-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsim_one_dim.cpp
301 lines (259 loc) · 9.51 KB
/
sim_one_dim.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <random>
#include <string>
#include <vector>
double birth_rate = 6e-3;
double death_rate = 1e-5;
double birth_variance = 2e-2;
double death_variance = 1e-2;
std::vector<double> birth_kernel;
std::vector<double> death_kernel;
int wall = 0;
class Cell {
private:
long population, column;
double x;
public:
Cell() {};
Cell(int Population, double X)
: population(Population), x(X) {};
long get_population() const {
return population;
}
double get_coordinates() const { // FIXME: for many dimensions need a vector
return x;
}
long get_indices() const { // FIXME: for many dimensions need a vector
return column;
}
void set_population(long Population) {
population = Population;
}
void set_coordinates(double X) { // FIXME: for many dimensions need a vector
x = X;
}
void set_indices(long Column) { // FIXME: for many dimensions need a vector
column = Column;
}
void print(std::ostream &out) const {
out << std::setw(3) << population << std::endl;
}
friend std::ostream &operator<<(std::ostream &out, const Cell &cell) {
cell.print(out);
return out;
}
};
class Grid {
private:
long population, discretization;
double width; // FIXME: for many dimensions need a vector
double cell_width; // FIXME: for many dimensions need a vector
std::vector<Cell> cells;
public:
Grid() {};
Grid(long Init_population, long Discretization, double Width)
:population(Init_population), discretization(Discretization), width(Width) {
cell_width = width / (double)discretization;
cells = std::vector<Cell>(discretization);
for (long i = 0; i != cells.size(); ++i) {
cells[i].set_coordinates(width / discretization * i);
cells[i].set_indices(i);
cells[i].set_population(0);
}
for (long i = 0; i != population; ++i) {
long index = (((long)rand() << 32) + rand()) % discretization; // Слабоумие и слабоумие
cells[index].set_population(cells[index].get_population() + 1);
}
}
long get_population() const {
return population;
}
void set_population(long new_population) {
population = new_population;
}
long get_discretization() const {
return discretization;
}
double get_size() const {
return width;
}
double get_cell_size() const {
return cell_width;
}
std::vector<Cell> get_cells() const {
return cells;
}
Cell & operator[] (int x) { // setter
return cells[x];
}
Cell operator[] (int x) const { // getter
return cells[x];
}
};
inline double pow_int(double x, long p) {
double res = x;
--p;
for (long i = 0; i != p; ++i) {
res *= x;
}
return res;
}
double distance(const Cell &from, const Cell &to) { // FIXME: for many dimensions need a vector
return std::abs(from.get_coordinates() - to.get_coordinates());
}
// counting birth and death kernels (currently Gaussian)
std::vector<double> precompute_kernel(int type, Grid &grid) {
std::vector<double> result;
long max_distance = ceil(3.0 * birth_variance / grid.get_cell_size()); // 3 sigma rule
double variance, rate;
if (type == 0) {
variance = birth_variance;
rate = birth_rate;
} else if (type == 1) {
variance = death_variance;
rate = death_rate;
} else {
throw std::invalid_argument( "Invalid type" );
}
for (long i = 0; i <= max_distance; ++i) {
result.push_back(
rate / (sqrt(2 * M_PI) * variance) * std::exp(-pow_int(distance(grid[0], grid[i]), 2) / (2 * variance * variance)) // 1 / var убрать
);
}
return result;
}
std::pair<long, long> count_interval_for_cell(long cell, Grid &grid, int type, int wall) {
double max_distance;
if (type == 0) {
max_distance = 3 * birth_variance; // 3 sigma rule
} else if (type == 1) {
max_distance = 3 * death_variance; // 3 sigma rule
} else {
throw std::invalid_argument( "Invalid type" );
}
long border_x = ceil(max_distance / grid.get_cell_size());
std::pair<long, long> result;
if (wall == 0) {
result.first = (cell - border_x <= 0 ? 0 : cell - border_x);
result.second = (cell + border_x >= grid.get_discretization() ? grid.get_discretization() : cell + border_x);
} else {
throw std::invalid_argument( "Invalid wall" );
}
return result;
}
// life cycle of the grid
void iteration(Grid & grid) {
std::vector<double> nobirth_matrix(grid.get_discretization(), 1);
std::vector<double> nodeath_matrix(grid.get_discretization(), 1);
std::pair<long, long> cur_interval;
for (long i = 0; i != grid.get_discretization(); ++i) {
// here we try to birth from i-th cell into j cell
if (grid[i].get_population()) {
cur_interval = count_interval_for_cell(i, grid, 0, wall);
for (long j = cur_interval.first; j != cur_interval.second; ++j) {
if (i == j) continue;
double nobirth_prob = pow_int((1 - birth_kernel[std::abs(i - j)] * grid.get_cell_size()),
grid[i].get_population()); // * cell_size instead of integration
nobirth_matrix[grid[j].get_indices()] *= nobirth_prob;
}
}
}
for (long i = 0; i != grid.get_discretization(); ++i) {
// here we try to influence from i-th cell into j cell and induce death
// in j-th cell
if (grid[i].get_population()) {
cur_interval = count_interval_for_cell(i, grid, 1, wall);
for (long j = cur_interval.first; j != cur_interval.second; ++j) {
if (j == i) continue;
double nodeath_prob = pow_int((1 - death_kernel[std::abs(i - j)]),
grid[i].get_population()); // * cell_size instead of integration
nodeath_matrix[grid[j].get_indices()] *= nodeath_prob;
}
}
}
for (long i = 0; i != grid.get_discretization(); ++i) {
if (grid[i].get_population() && ((float)rand() / RAND_MAX >= nodeath_matrix[i] + 1e-10)) {
grid[i].set_population(grid[i].get_population() - 1);
grid.set_population(grid.get_population() - 1);
}
if ((float)rand() / RAND_MAX >= nobirth_matrix[i] + 1e-10) {
grid[i].set_population(grid[i].get_population() + 1);
grid.set_population(grid.get_population() + 1);
}
}
}
int main(int argc, char ** argv) {
std::string usage_string = "Usage: " + std::string(argv[0]) + " size discretization iterations initial_population birth_rate death_rate birth_variance death_variance [seed]";
if (argc > 10 || argc < 9) {
std::cerr << "Wrong number of arguments\n" << usage_string << std::endl;
return 1;
}
char *endptr;
long size = strtol(argv[1], &endptr, 10);
if (!*argv[1] || *endptr) {
std::cerr << "Wrong size: " << argv[1] << std::endl << usage_string << std::endl;
return 1;
}
long discretization = strtol(argv[2], &endptr, 10);
if (!*argv[2] || *endptr) {
std::cerr << "Wrong discretization: " << argv[2] << std::endl << usage_string << std::endl;
return 1;
}
long iterations = strtol(argv[3], &endptr, 10);
if (!*argv[3] || *endptr) {
std::cerr << "Wrong number of iterations: " << argv[3] << std::endl << usage_string << std::endl;
return 1;
}
long init_population = strtol(argv[4], &endptr, 10);
if (!*argv[4] || *endptr) {
std::cerr << "Wrong initial population: " << argv[4] << std::endl;
return 1;
}
birth_rate = strtod(argv[5], &endptr);
if (!*argv[5] || *endptr) {
std::cerr << "Wrong birth_rate: " << argv[5] << std::endl;
return 1;
}
death_rate = strtod(argv[6], &endptr);
if (!*argv[6] || *endptr) {
std::cerr << "Wrong death_rate: " << argv[6] << std::endl;
return 1;
}
birth_variance = strtod(argv[7], &endptr);
if (!*argv[7] || *endptr) {
std::cerr << "Wrong birth_variance: " << argv[7] << std::endl;
return 1;
}
death_variance = strtod(argv[8], &endptr);
if (!*argv[8] || *endptr) {
std::cerr << "Wrong death_variance: " << argv[8] << std::endl;
return 1;
}
if (argc < 10) {
srand(time(NULL));
} else {
long seed = strtol(argv[9], &endptr, 10);
if (!*argv[9] || *endptr) {
std::cerr << "Using time for seed" << argv[9] << std::endl;
srand(time(NULL));
} else {
srand(seed);
}
}
Grid grid(init_population, discretization, size);
birth_kernel = precompute_kernel(0, grid);
death_kernel = precompute_kernel(1, grid);
for (int i = 0; i != iterations; ++i) {
std::cout << i << " " << grid.get_population() << " ";
// for (int j = 0; j < grid.get_discretization(); ++j) {
// std::cout << " " << grid[j].get_population();
// }
std::cout << std::endl;
iteration(grid);
}
}