-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.py
119 lines (95 loc) · 3.23 KB
/
rules.py
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
from dataclasses import dataclass
import numpy as np
import cv2
import numba
from types import FunctionType
jit = numba.jit(nopython=True, parallel=True, fastmath=True)
def basic_convolution(field: np.ndarray, kernel: np.ndarray) -> np.ndarray:
return cv2.filter2D(field, -1, kernel, borderType=cv2.BORDER_ISOLATED).astype('float32')
@jit
def fast_inv_gaussian_activation(field: np.ndarray) -> np.ndarray:
return (-1/(np.exp(0.41 * np.square(field)))+1).astype('float32')
@jit
def fast_lowkurt_inv_gaussian_activation(field: np.ndarray) -> np.ndarray:
return (-1.01/(np.exp(0.4159 * np.square(field))+.02)+1)
@jit
def inv_gaussian_activation(field: np.ndarray) -> np.ndarray:
return (-1/np.exp2(0.6*np.square(field))+1).astype('float32')
def basic_intervention(field: np.ndarray, x: int, y: int) -> None:
field[x, y] = 1
def checkerboard_intervetion(field: np.ndarray, x: int, y: int) -> None:
field[x-5:x+5, y-5:y+5] = (
np.array([[1, 0]*5, [0, 1]*5] * 5)
* cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
)
@dataclass
class CallableRuleset:
kernel: np.ndarray
convolution: FunctionType
activation: FunctionType
intervention: FunctionType
steps: int = 1
initialization_percentage: float = 0.5
def __call__(self, field: np.ndarray) -> np.ndarray:
for _ in range(self.steps):
field = self.activation(self.convolution(field, self.kernel))
return field
classic = CallableRuleset(
kernel=np.array([
[1, 1, 1],
[1, 9, 1],
[1, 1, 1],
], dtype='uint8'),
convolution=basic_convolution,
activation=lambda field: ((field == 3) | (field == 11) | (field == 12)),
intervention=basic_intervention
)
slime_pulling_worms = CallableRuleset(
kernel=np.array([
[0.74, -0.946, 0.74],
[-0.946, -0.434, -0.946],
[0.74, -0.946, 0.74],
], dtype='float32'),
convolution=basic_convolution,
activation=inv_gaussian_activation,
steps=4,
intervention=checkerboard_intervetion
)
blood_pumping_worms = CallableRuleset(
kernel=np.array([
[0.742, -0.966, 0.742],
[-0.966, -0.45, -0.966],
[0.742, -0.966, 0.742],
], dtype='float32'),
convolution=basic_convolution,
activation=inv_gaussian_activation,
intervention=checkerboard_intervetion,
steps=4,
initialization_percentage=0.2
)
pipes = CallableRuleset(
kernel=np.array([
[-.2, -0.1, 0, .1, -.2],
[.1, 0, .55, .0, -.1],
[0, .55, 1.3, .55, 0],
[-.1, 0, .55, .0, .1],
[-.2, .1, 0, -.1, -.2],
], dtype='float32'),
convolution=basic_convolution,
activation=inv_gaussian_activation,
intervention=checkerboard_intervetion,
initialization_percentage=0.1
)
moving_rocks = CallableRuleset(
kernel=np.array([
[-.3, -.3, .1, .3, -.3],
[.3, .0, .55, .0, -.3],
[.1, .55, 1.2, .55, .1],
[-.3, .0, .55, .0, .3],
[-.6, .3, .1, -.3, -.3],
], dtype='float32'),
convolution=basic_convolution,
activation=inv_gaussian_activation,
intervention=checkerboard_intervetion,
initialization_percentage=0.2
)