-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellMap.cpp
More file actions
281 lines (260 loc) · 9.3 KB
/
cellMap.cpp
File metadata and controls
281 lines (260 loc) · 9.3 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
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
#include "cellMap.h"
#include "iostream" //For Debug
#include <QTime>
cell::cellMap::cellMap(int _width, int _height)
: width(_width), height(_height)
{
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
cget(i, j).init(EMPTY, NOTHING);
}
}
//Initial possibility of mutation
evolution = 0.1;
speed = NORMAL_SPEED;
manualeng.seed(std::chrono::system_clock::now().time_since_epoch().count());
loadMap();
}
void cell::cellMap::loadMap(double producer_freq, double consumer_freq, double highConsumer_freq, int seed)
{
double sum_freq = highConsumer_freq + consumer_freq + producer_freq;
if (producer_freq < 0 || consumer_freq < 0 || highConsumer_freq < 0 || sum_freq > 1)
{
return;
}
engine.seed((seed<0)? std::chrono::system_clock::now().time_since_epoch().count() : seed);
std::uniform_real_distribution<double> distribution(0, 1);
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
double freq = distribution(engine);
if (freq < highConsumer_freq)
{
cget(i, j).init(LIVE, HIGH_CONSUMER);
}
else if (freq < highConsumer_freq + consumer_freq)
{
cget(i, j).init(LIVE, CONSUMER);
}
else if (freq < sum_freq)
{
cget(i, j).init(LIVE, PRODUCER);
}
else
{
cget(i, j).init(EMPTY, NOTHING);
}
}
}
}
std::vector<cell::cell> cell::cellMap::count(int my_x, int my_y, int my_range, int ob_type, int ob_state, bool all=false)
{
std::vector<cell> countVector;
countVector.clear();
for (int i = my_x - my_range; i <= my_x + my_range; ++i)
{
for (int j = my_y - my_range; j <= my_y + my_range; ++j)
{
if (0 <= i && i < width && 0 <= j && j < height)
{
if ((cget(i, j).getType() == ob_type && cget(i, j).getState() == ob_state)
&& (i != my_x && j != my_y))
//Don't count itself
{
if(all)
{
countVector.push_back(array[i][j]);
}
//check if fertile
else if (cget(i, j).getProduceAge() <= cget(i, j).getAge())
{
countVector.push_back(array[i][j]);
}
}
}
}
}
return countVector;
}
void cell::cellMap::burn(int x, int y)
{
if (cget(x, y).getState() == NOTHING)
{
//count HIGH_CONSUMER
std::vector<cell> countVector = count(x, y, HIGH_CONSUMER_RANGE, HIGH_CONSUMER, LIVE);
if (countVector.size() < HIGH_CONSUMER_LN)
{
countVector.clear();
//count CONSUMER
countVector = count(x, y, CONSUMER_RANGE, CONSUMER, LIVE);
if (countVector.size() < CONSUMER_LN)
{
countVector.clear();
//count PRODUCER
countVector = count(x, y, PRODUCER_RANGE, PRODUCER, LIVE);
if (countVector.size() < PRODUCER_LN)
{
countVector.clear();
return;
}
}
}
std::uniform_int_distribution<int> distribution(0, countVector.size());
int motherIndex = distribution(engine);
//vegetative propagation
cget(x, y).copy(countVector[motherIndex]);
std::uniform_real_distribution<double> distribution2(0, 1);
std::uniform_int_distribution<int> distribution3(0, 5);
std::uniform_int_distribution<int> distribution4(0, 4);
std::uniform_int_distribution<int> distribution5(1, 4);
std::uniform_int_distribution<int> distribution6(0, 100);
std::uniform_int_distribution<int> distribution7(0, 10);
std::uniform_int_distribution<int> distribution8(0, 30);
std::uniform_int_distribution<int> distribution9(10, 50);
if (distribution2(engine) < evolution)
//possibility of mutation
{
//one of these attributes will be variable
switch (distribution3(engine))
{
case 0:
cget(x, y).setDeadNumber(distribution4(engine));
break;
case 1:
cget(x, y).setRange(distribution5(engine));
break;
case 2:
cget(x, y).setAgeLimit(distribution6(engine));
break;
case 3:
cget(x, y).setAfterDeadLimit(distribution7(engine));
break;
case 4:
cget(x, y).setStarvingTimeLimit(distribution8(engine));
break;
case 5:
cget(x, y).setProduceAge(distribution9(engine));
break;
default:
break;
}
}
}
}
bool cell::cellMap::eat(cell& op1, cell& op2)
{
//if(op2.getState() == DEAD) return true;
if ((op1.getType() == HIGH_CONSUMER && op2.getType() == CONSUMER)
|| (op1.getType() == CONSUMER && op2.getType() == PRODUCER))
{
return true;
}
return false;
}
void cell::cellMap::move(cell& op, int x,int y)
{
cget(x,y).copy(op);
cget(x,y).setAge(op.getAge());
cget(x,y).setStarvingTime(op.getStarvingTime());
op.init();
}
void cell::cellMap::exist(int x, int y)
{
if (visited[x][y]) return;
if (cget(x, y).getState() == DEAD)
{
if (cget(x, y).getAfterDead() >= cget(x, y).getAfterDeadLimit())
{
cget(x, y).init();
}
else
{
cget(x, y).setAfterDead(cget(x, y).getAfterDead() + 1);
}
}
else if (cget(x, y).getState() == LIVE)
{
//Reach its age limit
if (cget(x, y).getAge() >= cget(x, y).getAgeLimit())
{
cget(x, y).setState(DEAD);
}
else
{
//Grown age
cget(x, y).setAge(cget(x, y).getAge() + 1);
//find all prey
if (cget(x, y).getType() != PRODUCER)
{
int full = 0;
for (int i = x - cget(x, y).getRange(); i <= x + cget(x, y).getRange(); ++i)
{
for (int j = y - cget(x, y).getRange(); j <= y + cget(x, y).getRange(); ++j)
{
if (0 <= i && i < width && 0 <= j && j < height)
{
if (eat(cget(x, y), cget(i, j)))
{
cget(i, j).init();
++full;
}
}
}
}
if (full > 0)
{
//Addition starvation for no prey
int temp = cget(x, y).getStarvingTime() - full;
cget(x, y).setStarvingTime((temp>=0)?temp:0);
visited[x][y] = true;
}
else
{
cget(x, y).setStarvingTime(cget(x, y).getStarvingTime() + 1);
int my_range=cget(x,y).getRange();
std::vector<std::pair<int,int> > target;
for (int i = x + my_range; i >=x - my_range; --i)
{
for (int j = y + my_range; j >= y - my_range; --j)
{
if (0 <= i && i < width && 0 <= j && j < height)
{
if(cget(x,y).getType()==CONSUMER)
{
if ((cget(i, j).getType() == NOTHING)&& (i != x && j != y))
{
target.push_back(std::make_pair(i,j));
}
}
else if(cget(x, y).getType()==HIGH_CONSUMER)
{
if (cget(i, j).getType() == NOTHING || cget(i, j).getType() == PRODUCER || cget(i, j).getType() == CONSUMER)
{
target.push_back(std::make_pair(i,j));
}
}
}
}
}
if (!target.empty())
{
std::uniform_int_distribution<> dist(0,target.size() - 1);
auto it = target.begin() + dist(engine);
move(cget(x,y),it->first,it->second);
x = it->first; y = it->second;
}
visited[x][y] = true;
}
if(cget(x, y).getStarvingTime() > cget(x, y).getStarvingTimeLimit()) cget(x, y).setState(DEAD);
//check if competition is happened
if ((count(x, y, cget(x, y).getRange(), cget(x, y).getType(), LIVE, true).size()) >= cget(x, y).getDeadNumber())
{
cget(x, y).setState(DEAD);
}
}
}
}
}