-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
403 lines (360 loc) · 10.8 KB
/
script.js
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Una simulación sencilla del proceso de invasión de Nicotiana glauca en un ecosistema de cardones y tabaibas desarrollada para alumnado de 4º de ESO.
Semidan Robaina Estevez, 2021
*/
let suelo, glauca, cardon, tabaiba, plantImg, images;
let gridElements = [];
let plants = [];
let plantTypeData;
let squareSideLength;
let numberOfSquares;
const squaresPerSide = 20;
let canvasSideLength = document.getElementById("cnv_container").offsetWidth;
const backgroundColor = getComputedStyle(document.documentElement)
.getPropertyValue("--backgroundColor");
const fontColor = getComputedStyle(document.documentElement)
.getPropertyValue("--fontColor");
let sim_started, sim_reseted;
let initialized = false;
let n_plants_init = {
glauca: 1,
cardon: 5,
tabaiba: 5
};
let plantParameters = {
glauca: {
energy_gain_rate: 0.25,
reproductive_energy_threshold: 2,
max_life: 20,
life_dev: 5,
seed_success: 0.01,
max_seed_production: 300,
seed_production_dev: 10,
max_seed_dispersal: 2,
toxicity_to_neighbors: 0.005,
life_loss_rate: 1,
},
cardon: {
energy_gain_rate: 0.20,
reproductive_energy_threshold: 2,
max_life: 20,
life_dev: 5,
seed_success: 0.01,
max_seed_production: 200,
seed_production_dev: 10,
max_seed_dispersal: 2,
toxicity_to_neighbors: 0.005,
life_loss_rate: 1,
},
tabaiba: {
energy_gain_rate: 0.20,
reproductive_energy_threshold: 1.7,
max_life: 20,
life_dev: 5,
seed_success: 0.01,
max_seed_production: 200,
seed_production_dev: 10,
max_seed_dispersal: 2,
toxicity_to_neighbors: 0.005,
life_loss_rate: 1,
},
suelo: {
energy_gain_rate: 0.25,
reproductive_energy_threshold: 2,
max_life: 20,
life_dev: 5,
seed_success: 0.01,
max_seed_production: 100,
seed_production_dev: 10,
max_seed_dispersal: 0,
toxicity_to_neighbors: 0.005,
life_loss_rate: 1,
}
};
function preload() {
suelo = loadImage("imgs/suelo.png");
glauca = loadImage("imgs/glauca.png");
cardon = loadImage("imgs/cardon.png");
tabaiba = loadImage("imgs/tabaiba.png");
setSliderDefaultValues();
}
function setup() {
images = {suelo: suelo, glauca: glauca, cardon: cardon, tabaiba:tabaiba};
plantTypeData = {glauca: [], tabaiba: [], cardon: []};
numberOfSquares = squaresPerSide ** 2;
squareSideLength = canvasSideLength / squaresPerSide;
let cnv = createCanvas(canvasSideLength, canvasSideLength);
cnv.parent('cnv_container');
frameRate(4);
for (let i = 0; i < (squaresPerSide - 0); i++) {
for (let j = 0; j < (squaresPerSide - 0); j++) {
gridElements.push({
x: i * squareSideLength,
y: j * squareSideLength,
coord: [i, j],
});
}
}
for (let n = 0; n < numberOfSquares; n++) {
let plantPos = gridElements[n];
plants[n] = new Plant(id="suelo", img=images["suelo"], pos=plantPos,
plantParameters["suelo"]);
}
// populate grid with seed plants
let plantIDs = ["cardon", "tabaiba", "glauca"];
for (let plantID of plantIDs) {
let randSample = getRandomSample(
0, numberOfSquares, n_plants_init[plantID]);
for (rand_idx of randSample) {
plants[rand_idx].id = plantID;
plants[rand_idx].img = images[plantID];
plants[rand_idx].p = plantParameters[plantID];
}
}
for (plant of plants) {
plant.initialize();
plant.show();
}
// Initialize interface
if (initialized) {
setSliderDefaultValues();
}
plotDataFraction(plantTypeData);
sim_started = false;
sim_reseted = false;
}
function draw() {
if (!sim_started) {
} else {
background('black');
for (plant of plants) {
plant.updateState();
if (plant.id !== "suelo" & plant.reproduce === true) {
reproducePlant(plant);
}
}
for (plant of plants) {
plant.show();
}
let plant_type_count = countPlantTypes(plants);
plantTypeData.glauca.push(plant_type_count.glauca);
plantTypeData.cardon.push(plant_type_count.cardon);
plantTypeData.tabaiba.push(plant_type_count.tabaiba);
plotDataFraction(plantTypeData);
if (plant_type_count.glauca === numberOfSquares) {
noLoop();
}
}
console.log(plantParameters["glauca"]);
}
class Plant {
constructor(id, img, pos, params) {
this.id = id;
this.img = img;
this.pos = pos;
this.p = params;
this.energy = 0;
this.life = 0;
this.seeds = 0;
this.reproduce = false;
}
initialize() {
this.life = getRandomInt(this.p.max_life - this.p.life_dev, this.p.max_life + this.p.life_dev);
this.energy = 0;
this.seeds = 0;
this.reproduce = false;
}
updateState() {
this.energy += this.p.energy_gain_rate;
if (this.life <= 0) {
this.img = suelo;
this.id = "suelo";
this.seeds = 0;
this.energy = 0;
this.p = plantParameters["suelo"];
}
if (this.energy >= this.p.reproductive_energy_threshold) {
this.seeds = getRandomInt(
this.p.max_seed_production - this.p.seed_production_dev,
this.p.max_seed_production + this.p.seed_production_dev
);
this.energy = 0;
}
if (Math.round(this.seeds * this.p.seed_success) > 0) {
this.reproduce = true;
} else {
this.reproduce = false;
}
this.life -= this.p.life_loss_rate;
}
show() {
image(
this.img, this.pos.x, this.pos.y, squareSideLength, squareSideLength);
}
}
function reproducePlant(plant) {
let available_spots = plants.filter(
spot => (spot.id === "suelo") & (computeDistance(spot.pos.coord, plant.pos.coord) <= plant.p.max_seed_dispersal)
);
if (available_spots.length > 0) {
let successful_seeds = Math.round(plant.seeds * plant.p.seed_success);
let random_idxs = getRandomSample(0, available_spots.length - 1,
Math.min(available_spots.length, successful_seeds));
for (let i=0; i<random_idxs.length; i++) {
available_spots[i].id = plant.id;
available_spots[i].img = plant.img;
available_spots[i].p = plantParameters[plant.id];
available_spots[i].initialize();
}
}
// Plant gets depleted of seeds and energy once reproduces
plant.reproduce = false;
plant.seeds = 0;
}
function computeDistance([a, b], [c, d]) {
return Math.max(Math.abs(a - c), Math.abs(b - d))
}
function countPlantTypes(plants) {
let plant_types = {glauca:0, tabaiba:0, cardon:0};
for (plant of plants) {
if (plant.id === "glauca") {
plant_types.glauca += 1;
} else if (plant.id === "cardon") {
plant_types.cardon += 1;
} else if (plant.id === "tabaiba") {
plant_types.tabaiba += 1;
}
}
return plant_types
}
function plotDataFraction(data) {
let plant_fraction = {glauca:[], tabaiba:[], cardon:[]};
for (let i=0; i<data.glauca.length; i++) {
let total = data.glauca[i] + data.tabaiba[i] + data.cardon[i];
plant_fraction.glauca.push(100 * (data.glauca[i]/total));
plant_fraction.tabaiba.push(100 * (data.tabaiba[i]/total));
plant_fraction.cardon.push(100 * (data.cardon[i]/total));
}
let x_array = [...Array(data.glauca.length).keys()];
let glauca_fraction = {
x: x_array,
y: plant_fraction.glauca,
name: "Tabaco moro",
showlegend: true,
line: {
color: "rgb(219, 185, 5)"
}
};
let cardon_fraction = {
x: x_array,
y: plant_fraction.cardon,
name: "Cardón",
showlegend: true,
line: {
color: "rgb(8, 184, 10)"
}
};
let tabaiba_fraction = {
x: x_array,
y: plant_fraction.tabaiba,
name: "Tabaiba",
showlegend: true,
line: {
color: "rgb(227, 44, 177)"
}
};
plot_data = [glauca_fraction, cardon_fraction, tabaiba_fraction];
let layout = {
title: `Evolución de poblaciones`,
mode: "lines",
font: {
color: fontColor
},
xaxis: {
title: "Tiempo"
// tickfont: {
// size: 10
// }
},
yaxis: {title: "Frecuencia (%)"},
plot_bgcolor: backgroundColor,
paper_bgcolor: backgroundColor,
legend: {
x: 1,
xanchor: 'right',
y: 1
},
// margin: {
// l: 0.5,
// r: 2
// t: 0,
// b: 0
// }
};
let config = {responsive: true};
Plotly.newPlot("plot_container", plot_data, layout);//, config);
}
// CONTROLS
function startSimulation() {
let button = document.getElementById("start-button");
// sim_started = !sim_started;
if (!sim_started) {
button.innerHTML = "<i class='fas fa-pause'></i>";
} else {
button.innerHTML = "<i class='fas fa-play'></i>";
}
sim_started = !sim_started;
}
function resetSimulation() {
initialized = false;
let button = document.getElementById("start-button");
button.innerHTML = "<i class='fas fa-play'></i>";
setup();
}
function setSliderDefaultValues() {
document.getElementById('ninit_slider_text').innerHTML = n_plants_init.glauca;
document.getElementById('dispersal_slider_text').innerHTML = plantParameters.glauca.max_seed_dispersal;
document.getElementById('seed_production_slider_text').innerHTML = plantParameters.glauca.max_seed_production;
document.getElementById('rep_eficiency_slider_text').innerHTML = plantParameters.glauca.energy_gain_rate;
}
function updateNinitSliderText(value) {
document.getElementById('ninit_slider_text').innerHTML = value;
n_plants_init.glauca = int(value);
resetSimulation();
}
function updateDispersalSliderText(value) {
document.getElementById('dispersal_slider_text').innerHTML = value;
plantParameters.glauca.max_seed_dispersal = int(value);
resetSimulation();
}
function updateSeedProductionSliderText(value) {
document.getElementById('seed_production_slider_text').innerHTML = value;
plantParameters.glauca.max_seed_production = int(value);
resetSimulation();
}
function updateRepEficiencySliderText(value) {
document.getElementById('rep_eficiency_slider_text').innerHTML = value;
plantParameters.glauca.energy_gain_rate = float(value);
resetSimulation();
}
// function updateSize(){
// resizeCanvas(windowWidth, windowHeight);
// }
function getRandomInt(min, max) {
return Math.floor((max - min) * Math.random() + min);
}
function getRandomSample(minInt, maxInt, size) {
/* Draw random sample of specified size, without repetition,
from a sequence of numbers between minInt and maxInt
*/
let numbers = []
for (i = minInt; i < maxInt + 1; i++) {
numbers.push(i);
}
randomSample = [];
for (let i = 0; i < size; i++) {
let sampledNumber = numbers.splice(
Math.floor(Math.random() * numbers.length), 1)[0];
randomSample.push(sampledNumber);
}
return randomSample
}