/
activators.js
126 lines (110 loc) · 3 KB
/
activators.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
import React from 'react';
import { Grid } from 'react-sim';
import Model from './framed-model';
// helpers
function getTotalValue(grid, listOfCells) {
return listOfCells.reduce((total, [x, y]) => total + grid[y][x].value, 0);
}
export function update({ data, tick, params, complete }) {
let grid = JSON.parse(JSON.stringify(data));
let changes = 0;
const { height, width, w } = params;
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const { innerNeighbors, outerNeighbors } = data[row][col];
const activators = getTotalValue(data, innerNeighbors);
const inhibitors = getTotalValue(data, outerNeighbors);
const mutation = activators - w * inhibitors;
if (mutation > 0) {
grid[row][col].value = 1;
}
if (mutation < 0) {
grid[row][col].value = 0;
}
if (grid[row][col] !== data[row][col]) {
changes++;
}
// if mutation = 0, leave cell unchanged
}
}
if (changes < 0.01 * height * width) {
complete();
}
return grid;
}
export function initData(
{ height, width, density, innerRadius, outerRadius },
random = Math.random
) {
const data = [];
for (let row = 0; row < height; row++) {
const cells = [];
for (let col = 0; col < width; col++) {
const innerNeighbors = [];
const outerNeighbors = [];
const x0 = Math.max(col - outerRadius, 0);
const x1 = Math.min(col + outerRadius, width - 1);
const y0 = Math.max(row - outerRadius, 0);
const y1 = Math.min(row + outerRadius, height - 1);
for (let x = x0; x < x1; x++) {
for (let y = y0; y < y1; y++) {
const dist = Math.hypot(col - x, row - y);
if (dist > innerRadius && dist <= outerRadius) {
outerNeighbors.push([x, y]);
} else {
if (dist > 0 && dist <= innerRadius) {
innerNeighbors.push([x, y]);
}
}
}
}
const value = random() < density;
cells.push({ value, innerNeighbors, outerNeighbors });
}
data.push(cells);
}
return data;
}
export const ActivatorFrame = props => (
<Grid size={10} accessor={d => (d.value ? '#000' : 'none')} {...props} />
);
const Activators = props => (
<Model
auto="false"
controls={[
[
{
param: 'innerRadius',
minValue: 1,
maxValue: 10,
label: 'Inner Radius',
},
{
param: 'outerRadius',
minValue: 1,
maxValue: 10,
label: 'Outer Radius',
},
],
[
{ param: 'w', minValue: 0, maxValue: 1, label: 'weight', step: 0.01 },
{ param: 'density', minValue: 0, maxValue: 1, step: 0.1 },
],
]}
initData={initData}
initialParams={{
height: 35,
width: 35,
density: 0.5,
innerRadius: 3,
outerRadius: 6,
w: 0.35,
}}
updateData={update}
maxTime={200}
{...props}
>
<ActivatorFrame />
</Model>
);
export default Activators;