-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation.js
More file actions
38 lines (34 loc) · 1.48 KB
/
Copy pathsimulation.js
File metadata and controls
38 lines (34 loc) · 1.48 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
function mutateGrid() {
for (let row = 0; row < GRID_HEIGHT; row++) {
for (let col = 0; col < GRID_WIDTH; col++) {
if (!isLand(row, col)) {
continue;
}
const currentValue = fitnessValues[row][col];
const shift = generateNormalRandom(mutationAmount * mutationBias, mutationAmount);
fitnessValues[row][col] = Math.max(0, Math.min(100, currentValue + shift));
}
}
}
function invadeNeighbors() {
const attempts = attemptPerCell * GRID_HEIGHT * GRID_WIDTH;
for (let i = 0; i < attempts; i++) {
// Randomly pick a start location and a nearby end location.
const startX = Math.floor(Math.random() * GRID_WIDTH);
const startY = Math.floor(Math.random() * GRID_HEIGHT);
const endX = Math.round(startX + generateNormalRandom(0, dispersalAmount));
const endY = Math.round(startY + generateNormalRandom(0, dispersalAmount));
// Check if end position is within the grid bounds.
if (endX < 0 || endX >= GRID_WIDTH || endY < 0 || endY >= GRID_HEIGHT) {
continue;
}
// Check if start and end position are on land.
if (!isLand(startY, startX) || !isLand(endY, endX)) {
continue;
}
// Invade if the start location has higher fitness.
if (fitnessValues[startY][startX] > fitnessValues[endY][endX]) {
fitnessValues[endY][endX] = fitnessValues[startY][startX];
}
}
}