-
Notifications
You must be signed in to change notification settings - Fork 24
/
abstractions_funs.cpp
195 lines (146 loc) · 4.3 KB
/
abstractions_funs.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
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// This function allows to do convolutions on the edge elements of the
// matrix since it extracts elements from the opposite rows or columns
// to avoid losing dimensionality after convolution
int get_index(int M, int i)
{
if (i < 0)
return (M + i % M) % M;
if(i >= M)
return i % M;
return i;
}
// Sensor stage
Rcpp::DataFrame sensor (arma::mat envM,
Rcpp::DataFrame parF,
double FL,
double FR,
double RA,
double SO) {
int m = envM.n_rows;
int n = envM.n_cols;
int npart = parF.nrows();
Rcpp::IntegerVector x = parF["x"];
Rcpp::IntegerVector y = parF["y"];
Rcpp::NumericVector h = parF["h"];
// create a vector to store heading
Rcpp::NumericVector hnew(npart);
for(int i = 0; i < npart; i++) {
// Sensory stage
int Fx = get_index(m, (int)round(x[i] + SO * cos(h[i])));
int Fy = get_index(n, (int)round(y[i] + SO * sin(h[i])));
int FLx = get_index(m, (int)round(x[i] + SO * (cos(h[i] + FL))));
int FLy = get_index(n, (int)round(y[i] + SO * (sin(h[i] + FL))));
int FRx = get_index(m, (int)round(x[i] + SO * (cos(h[i] + FR))));
int FRy = get_index(n, (int)round(y[i] + SO * (sin(h[i] + FR))));
double F = envM(Fx, Fy);
double FL = envM(FLx, FLy);
double FR = envM(FRx, FRy);
if ((F > FL) && (F > FR))
{
hnew[i] = h[i];
}
else if ((F < FL) && (F < FR))
{
if ( rand() % 2 == 0 )
{
hnew[i] = h[i] + RA;
}
else
{
hnew[i] = h[i] - RA;
}
}
else if (FL < FR)
{
hnew[i] = h[i] - RA;
}
else if (FR < FL)
{
hnew[i] = h[i] + RA;
}
else
{
hnew[i] = h[i];
}
}
Rcpp::DataFrame dest = Rcpp::DataFrame::create(Rcpp::Named("x") = x,
Rcpp::Named("y") = y,
Rcpp::Named("h") = hnew);
return dest;
}
// Motor stage
Rcpp::DataFrame motor (Rcpp::DataFrame parF,
int n,
int m,
double SS){
int npart = parF.nrows();
Rcpp::IntegerVector x = parF["x"];
Rcpp::IntegerVector y = parF["y"];
Rcpp::NumericVector h = parF["h"];
// create the new columns
Rcpp::IntegerVector xnew(npart);
Rcpp::IntegerVector ynew(npart);
for(int i = 0; i < npart; i++) {
xnew[i] = get_index(n, (int)round(x[i] + SS * cos(h[i])));
ynew[i] = get_index(m, (int)round(y[i] + SS * sin(h[i])));
}
Rcpp::DataFrame dest = Rcpp::DataFrame::create(Rcpp::Named("x") = xnew,
Rcpp::Named("y") = ynew,
Rcpp::Named("h") = h);
return dest;
}
// Deposition
arma::mat deposition (Rcpp::DataFrame parF,
double depT,
arma::mat envM) {
int npart = parF.nrows();
Rcpp::IntegerVector x = parF["x"];
Rcpp::IntegerVector y = parF["y"];
for(int i = 0; i < npart; i++) {
envM(x[i], y[i]) = envM(x[i], y[i]) + depT;
}
return envM;
}
// Evaporation
arma::mat evaporate (arma::mat source,
double factor) {
int m = source.n_rows;
int n = source.n_cols;
arma::mat dest(m, n);
for (int y = 0; y < n; ++y) {
for (int x = 0; x < m; ++x) {
dest(x, y) = source(x, y) * (1 - factor);
}
}
return dest;
};
// Gather all into a funtion called physarum
// [[Rcpp::export]]
arma::mat physarum(arma::mat envM,
Rcpp::DataFrame parF,
double decayT,
double FL,
double FR,
double RA,
double SO,
int SS,
double depT,
int iters)
{
int m = envM.n_rows;
int n = envM.n_cols;
// envM = scale(envM);
for (int k = 0; k < iters; k++){
// Sensor stage
parF = sensor (envM, parF, FL, FR, RA, SO);
// Motor stage
parF = motor (parF, m, n, SS);
// Deposition stage
envM = deposition (parF, depT, envM);
// Evaporation
envM = evaporate(envM, decayT);
}
return envM;
};