evonet 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- evonet-0.1.0/LICENSE +21 -0
- evonet-0.1.0/PKG-INFO +123 -0
- evonet-0.1.0/README.md +72 -0
- evonet-0.1.0/evonet/__init__.py +0 -0
- evonet-0.1.0/evonet/activation.py +207 -0
- evonet-0.1.0/evonet/connection.py +126 -0
- evonet-0.1.0/evonet/core.py +757 -0
- evonet-0.1.0/evonet/core_mit_plot_simple.py +645 -0
- evonet-0.1.0/evonet/enums.py +68 -0
- evonet-0.1.0/evonet/io.py +0 -0
- evonet-0.1.0/evonet/layer.py +26 -0
- evonet-0.1.0/evonet/mutation.py +352 -0
- evonet-0.1.0/evonet/neuron.py +134 -0
- evonet-0.1.0/evonet/serialization.py +180 -0
- evonet-0.1.0/evonet/utils.py +28 -0
- evonet-0.1.0/evonet/visualize.py +0 -0
- evonet-0.1.0/evonet.egg-info/PKG-INFO +123 -0
- evonet-0.1.0/evonet.egg-info/SOURCES.txt +26 -0
- evonet-0.1.0/evonet.egg-info/dependency_links.txt +1 -0
- evonet-0.1.0/evonet.egg-info/requires.txt +15 -0
- evonet-0.1.0/evonet.egg-info/top_level.txt +1 -0
- evonet-0.1.0/pyproject.toml +83 -0
- evonet-0.1.0/setup.cfg +8 -0
- evonet-0.1.0/tests/test_activation.py +30 -0
- evonet-0.1.0/tests/test_core.py +75 -0
- evonet-0.1.0/tests/test_nnet_io_and_forward.py +139 -0
- evonet-0.1.0/tests/test_recurrent_dynamics.py +192 -0
evonet-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 EvoLib
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
evonet-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: evonet
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Evolvable neural network core for integration with EvoLib
|
|
5
|
+
Author-email: EvoLib <evolib@dismail.de>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 EvoLib
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Classifier: Development Status :: 3 - Alpha
|
|
29
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
32
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
33
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
34
|
+
Requires-Python: >=3.10
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
License-File: LICENSE
|
|
37
|
+
Requires-Dist: numpy>=1.24
|
|
38
|
+
Requires-Dist: pyyaml>=6.0
|
|
39
|
+
Requires-Dist: pandas>=2.3.0
|
|
40
|
+
Requires-Dist: pydantic<3.0,>=2.7
|
|
41
|
+
Requires-Dist: graphviz>=0.20.1
|
|
42
|
+
Requires-Dist: matplotlib
|
|
43
|
+
Provides-Extra: dev
|
|
44
|
+
Requires-Dist: mypy; extra == "dev"
|
|
45
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
46
|
+
Provides-Extra: docs
|
|
47
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
48
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
49
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# EvoNet
|
|
53
|
+
|
|
54
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
55
|
+
[](LICENSE)
|
|
56
|
+
[](https://github.com/EvoLib/evo-net)
|
|
57
|
+
|
|
58
|
+
**EvoNet** is a modular and evolvable neural network core designed for integration
|
|
59
|
+
with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
60
|
+
It supports dynamic topologies, recurrent connections, per-neuron activation, and
|
|
61
|
+
structural evolution, with a strong emphasis on clarity and explicit behaviour.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Scope
|
|
66
|
+
|
|
67
|
+
EvoNet is not a state-of-the-art or general-purpose deep learning framework.
|
|
68
|
+
|
|
69
|
+
It does not aim to compete with libraries such as PyTorch, TensorFlow, or JAX in terms
|
|
70
|
+
of performance, scalability, or training algorithms. Backpropagation, GPU acceleration,
|
|
71
|
+
and highly optimised tensor operations are outside the scope of this project.
|
|
72
|
+
|
|
73
|
+
Instead, EvoNet is designed for evolutionary algorithms, structural mutation, and
|
|
74
|
+
exploratory research, with a focus on transparent and explicit implementations rather
|
|
75
|
+
than performance optimisation or feature completeness.
|
|
76
|
+
|
|
77
|
+
EvoNet should be understood as a conceptual and experimental model, not as a
|
|
78
|
+
production-grade neural-network engine.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Features
|
|
83
|
+
|
|
84
|
+
- **Explicit, layer-based topology** with support for skip connections, cycles,
|
|
85
|
+
and recurrent paths
|
|
86
|
+
- **Typed neuron roles and connection types** (`NeuronRole`, `ConnectionType`)
|
|
87
|
+
- **Topology-aware mutation operations**:
|
|
88
|
+
add/remove neurons and connections, mutate weights, change activations
|
|
89
|
+
- **Per-neuron activation functions**, configurable and evolvable
|
|
90
|
+
- **Explicit 1-step recurrent state model**, without iterative stabilisation passes
|
|
91
|
+
- **Runtime topology growth**, e.g. via `add_neuron` and `add_connection`
|
|
92
|
+
- **Debug-friendly architecture** with explicit neuron IDs, labels, roles,
|
|
93
|
+
and directional graphs
|
|
94
|
+
- **Designed for evolutionary integration**, not gradient-based training
|
|
95
|
+
- **Lightweight and extensible**: pure Python, NumPy-based, no hard dependencies
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
> ⚠️ **Project status: Alpha**
|
|
100
|
+
> Interfaces, APIs, and internal structure may change as the project evolves.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## Quick Example
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from evonet.core import Nnet
|
|
108
|
+
|
|
109
|
+
net = Nnet()
|
|
110
|
+
net.add_layer() # Input layer
|
|
111
|
+
net.add_layer() # Output layer
|
|
112
|
+
|
|
113
|
+
net.add_neuron(layer_idx=0, activation="linear", label="in")
|
|
114
|
+
net.add_neuron(layer_idx=1, activation="linear", bias=0.5, label="out")
|
|
115
|
+
|
|
116
|
+
print(net.calc([1.0]))
|
|
117
|
+
```
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT License - see [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
123
|
+
|
evonet-0.1.0/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# EvoNet
|
|
2
|
+
|
|
3
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://github.com/EvoLib/evo-net)
|
|
6
|
+
|
|
7
|
+
**EvoNet** is a modular and evolvable neural network core designed for integration
|
|
8
|
+
with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
9
|
+
It supports dynamic topologies, recurrent connections, per-neuron activation, and
|
|
10
|
+
structural evolution, with a strong emphasis on clarity and explicit behaviour.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Scope
|
|
15
|
+
|
|
16
|
+
EvoNet is not a state-of-the-art or general-purpose deep learning framework.
|
|
17
|
+
|
|
18
|
+
It does not aim to compete with libraries such as PyTorch, TensorFlow, or JAX in terms
|
|
19
|
+
of performance, scalability, or training algorithms. Backpropagation, GPU acceleration,
|
|
20
|
+
and highly optimised tensor operations are outside the scope of this project.
|
|
21
|
+
|
|
22
|
+
Instead, EvoNet is designed for evolutionary algorithms, structural mutation, and
|
|
23
|
+
exploratory research, with a focus on transparent and explicit implementations rather
|
|
24
|
+
than performance optimisation or feature completeness.
|
|
25
|
+
|
|
26
|
+
EvoNet should be understood as a conceptual and experimental model, not as a
|
|
27
|
+
production-grade neural-network engine.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Explicit, layer-based topology** with support for skip connections, cycles,
|
|
34
|
+
and recurrent paths
|
|
35
|
+
- **Typed neuron roles and connection types** (`NeuronRole`, `ConnectionType`)
|
|
36
|
+
- **Topology-aware mutation operations**:
|
|
37
|
+
add/remove neurons and connections, mutate weights, change activations
|
|
38
|
+
- **Per-neuron activation functions**, configurable and evolvable
|
|
39
|
+
- **Explicit 1-step recurrent state model**, without iterative stabilisation passes
|
|
40
|
+
- **Runtime topology growth**, e.g. via `add_neuron` and `add_connection`
|
|
41
|
+
- **Debug-friendly architecture** with explicit neuron IDs, labels, roles,
|
|
42
|
+
and directional graphs
|
|
43
|
+
- **Designed for evolutionary integration**, not gradient-based training
|
|
44
|
+
- **Lightweight and extensible**: pure Python, NumPy-based, no hard dependencies
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
> ⚠️ **Project status: Alpha**
|
|
49
|
+
> Interfaces, APIs, and internal structure may change as the project evolves.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Quick Example
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from evonet.core import Nnet
|
|
57
|
+
|
|
58
|
+
net = Nnet()
|
|
59
|
+
net.add_layer() # Input layer
|
|
60
|
+
net.add_layer() # Output layer
|
|
61
|
+
|
|
62
|
+
net.add_neuron(layer_idx=0, activation="linear", label="in")
|
|
63
|
+
net.add_neuron(layer_idx=1, activation="linear", bias=0.5, label="out")
|
|
64
|
+
|
|
65
|
+
print(net.calc([1.0]))
|
|
66
|
+
```
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT License - see [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
72
|
+
|
|
File without changes
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Activation functions for evolvable neural networks.
|
|
4
|
+
|
|
5
|
+
Includes standard nonlinearities (ReLU, Tanh, Sigmoid), linear and threshold functions,
|
|
6
|
+
modern smooth activations (Swish, Mish), and a softmax utility.
|
|
7
|
+
|
|
8
|
+
All functions are scalar-based and stateless, unless noted otherwise.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import random
|
|
12
|
+
from typing import Callable, List, Tuple, Union
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
|
|
16
|
+
Scalar = Union[int, float]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Common Activation Functions
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def tanh(x: Scalar) -> float:
|
|
23
|
+
"""Hyperbolic tangent activation: [-inf, inf] --> [-1, 1]."""
|
|
24
|
+
return float(np.tanh(x))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def ntanh(x: Scalar) -> float:
|
|
28
|
+
"""Normalized tanh: (tanh(x) + 1) / 2 --> [0, 1]."""
|
|
29
|
+
return (np.tanh(x) + 1) / 2
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def sigmoid(x: Scalar) -> float:
|
|
33
|
+
"""Sigmoid/logistic function: [-inf, inf] --> [0, 1]."""
|
|
34
|
+
x = np.clip(float(x), -500, 500)
|
|
35
|
+
return 1 / (1 + np.exp(-x))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def relu(x: Scalar) -> float:
|
|
39
|
+
"""ReLU: max(0, x)."""
|
|
40
|
+
return max(0.0, float(x))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def relu_max1(x: Scalar) -> float:
|
|
44
|
+
"""ReLU clipped to [0, 1]."""
|
|
45
|
+
return min(max(0.0, float(x)), 1.0)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def leaky_relu(x: Scalar, alpha: float = 0.01) -> float:
|
|
49
|
+
"""Leaky ReLU: x if x ≥ 0 else alpha * x."""
|
|
50
|
+
x = float(x)
|
|
51
|
+
return x if x >= 0 else alpha * x
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def elu(x: Scalar, alpha: float = 1.0) -> float:
|
|
55
|
+
"""Exponential Linear Unit."""
|
|
56
|
+
x = float(x)
|
|
57
|
+
return x if x >= 0 else alpha * (np.exp(x) - 1)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def selu(x: Scalar, alpha: float = 1.67326324, scale: float = 1.05070098) -> float:
|
|
61
|
+
"""Scaled Exponential Linear Unit (SELU)."""
|
|
62
|
+
x = float(x)
|
|
63
|
+
return scale * x if x >= 0 else scale * alpha * (np.exp(x) - 1)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def gaussian(x: Scalar) -> float:
|
|
67
|
+
"""Gaussian bell function: exp(-x^2)."""
|
|
68
|
+
x = float(x)
|
|
69
|
+
if np.abs(x) > 38:
|
|
70
|
+
return 0.0
|
|
71
|
+
return np.exp(-(x**2))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# Threshold Functions
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def binary(x: Scalar) -> float:
|
|
78
|
+
"""Step function: 1 if x > 0 else 0."""
|
|
79
|
+
return 1.0 if x > 0 else 0.0
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def signum(x: Scalar) -> float:
|
|
83
|
+
"""Sign function: 1, -1 or 0 depending on sign of x."""
|
|
84
|
+
return 1.0 if x > 0 else -1.0 if x < 0 else 0.0
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# Linear Variants
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def linear(x: Scalar) -> float:
|
|
91
|
+
"""Linear identity: returns x."""
|
|
92
|
+
return float(x)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def linear_max1(x: Scalar) -> float:
|
|
96
|
+
"""Linear function clipped to [-1, 1]."""
|
|
97
|
+
return min(max(-1.0, float(x)), 1.0)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def invert(x: Scalar) -> float:
|
|
101
|
+
"""Returns -x."""
|
|
102
|
+
return -float(x)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def null(_: Scalar = 0) -> float:
|
|
106
|
+
"""Always returns 0.0."""
|
|
107
|
+
return 0.0
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# Modern Functions
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def swish(x: Scalar) -> float:
|
|
114
|
+
"""Swish activation: x * sigmoid(x). Smooth and non-monotonic."""
|
|
115
|
+
return float(x) * sigmoid(x)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def mish(x: Scalar) -> float:
|
|
119
|
+
"""Mish activation: x * tanh(softplus(x))."""
|
|
120
|
+
return float(x) * np.tanh(np.log1p(np.exp(x)))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def softplus(x: Scalar) -> float:
|
|
124
|
+
"""Smooth ReLU approximation: log(1 + exp(x))."""
|
|
125
|
+
return np.log1p(np.exp(float(x)))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def softsign(x: Scalar) -> float:
|
|
129
|
+
"""Smooth alternative to tanh: x / (1 + |x|)."""
|
|
130
|
+
return float(x) / (1 + abs(float(x)))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def hard_sigmoid(x: Scalar) -> float:
|
|
134
|
+
"""Piecewise linear approximation of sigmoid."""
|
|
135
|
+
return min(max(0.0, 0.2 * float(x) + 0.5), 1.0)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
# Special Functions
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def softmax(values: Union[List[float], Tuple[float], np.ndarray]) -> np.ndarray:
|
|
142
|
+
"""
|
|
143
|
+
Applies softmax over a list of values.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
values: Array-like input with ≥2 values.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
np.ndarray: Normalized softmax output summing to 1.
|
|
150
|
+
"""
|
|
151
|
+
arr = np.array(values, dtype=float)
|
|
152
|
+
if arr.size < 2:
|
|
153
|
+
raise ValueError("Softmax input must have at least two values")
|
|
154
|
+
exp_x = np.exp(arr - np.max(arr)) # For numerical stability
|
|
155
|
+
return exp_x / np.sum(exp_x)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def random_function_name(activations: list[str] | None = None) -> str:
|
|
159
|
+
"""
|
|
160
|
+
Return a random activation function name from the registry.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
activations (list[str] | None): Optional subset of allowed function names.
|
|
164
|
+
If None, all registered activation names are considered.
|
|
165
|
+
|
|
166
|
+
Raises:
|
|
167
|
+
ValueError: If any name is not in the activation registry.
|
|
168
|
+
|
|
169
|
+
Returns:
|
|
170
|
+
str: Randomly selected activation function name.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
if activations is None:
|
|
174
|
+
activations = list(ACTIVATIONS.keys())
|
|
175
|
+
|
|
176
|
+
invalid = set(activations) - set(ACTIVATIONS.keys())
|
|
177
|
+
if invalid:
|
|
178
|
+
raise ValueError(f"Invalid activation functions: {invalid}")
|
|
179
|
+
|
|
180
|
+
return random.choice(activations)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# Registry
|
|
184
|
+
|
|
185
|
+
ACTIVATIONS: dict[str, Callable] = {
|
|
186
|
+
"tanh": tanh,
|
|
187
|
+
"ntanh": ntanh,
|
|
188
|
+
"sigmoid": sigmoid,
|
|
189
|
+
"relu": relu,
|
|
190
|
+
"relu_max1": relu_max1,
|
|
191
|
+
"leaky_relu": leaky_relu,
|
|
192
|
+
"elu": elu,
|
|
193
|
+
"selu": selu,
|
|
194
|
+
"gaussian": gaussian,
|
|
195
|
+
"binary": binary,
|
|
196
|
+
"signum": signum,
|
|
197
|
+
"linear": linear,
|
|
198
|
+
"linear_max1": linear_max1,
|
|
199
|
+
"invert": invert,
|
|
200
|
+
"null": null,
|
|
201
|
+
"swish": swish,
|
|
202
|
+
"mish": mish,
|
|
203
|
+
"softplus": softplus,
|
|
204
|
+
"softsign": softsign,
|
|
205
|
+
"hard_sigmoid": hard_sigmoid,
|
|
206
|
+
"softmax": softmax,
|
|
207
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Connection between neurons in an evolvable neural network.
|
|
4
|
+
|
|
5
|
+
A connection links a source neuron to a target neuron and transmits a weighted signal.
|
|
6
|
+
Supports optional connection types for specialized behaviors (e.g. inhibitory,
|
|
7
|
+
recurrent).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from collections import deque
|
|
11
|
+
from typing import TYPE_CHECKING, Deque, Optional
|
|
12
|
+
|
|
13
|
+
from evonet.enums import ConnectionType
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from evonet.neuron import Neuron
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Connection:
|
|
20
|
+
"""
|
|
21
|
+
Represents a directed, weighted connection between two neurons.
|
|
22
|
+
|
|
23
|
+
Attributes:
|
|
24
|
+
source (Neuron): The source neuron (presynaptic).
|
|
25
|
+
target (Neuron): The target neuron (postsynaptic).
|
|
26
|
+
weight (float): Multiplicative weight of the transmitted signal.
|
|
27
|
+
delay (int): Delay in discrete time steps (used for recurrent edges).
|
|
28
|
+
type (ConnectionType): Type of connection (e.g. standard, recurrent).
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
source: "Neuron",
|
|
34
|
+
target: "Neuron",
|
|
35
|
+
weight: float = 1.0,
|
|
36
|
+
delay: int = 0,
|
|
37
|
+
conn_type: ConnectionType = ConnectionType.STANDARD,
|
|
38
|
+
) -> None:
|
|
39
|
+
|
|
40
|
+
if delay < 0:
|
|
41
|
+
raise ValueError("delay must be >= 0")
|
|
42
|
+
|
|
43
|
+
if conn_type is not ConnectionType.RECURRENT:
|
|
44
|
+
delay = 0
|
|
45
|
+
|
|
46
|
+
# Normalize: recurrent implies delay >= 1
|
|
47
|
+
if conn_type is ConnectionType.RECURRENT and delay == 0:
|
|
48
|
+
delay = 1
|
|
49
|
+
|
|
50
|
+
self.source = source
|
|
51
|
+
self.target = target
|
|
52
|
+
self.weight = weight
|
|
53
|
+
self.delay = delay
|
|
54
|
+
self.type = conn_type
|
|
55
|
+
|
|
56
|
+
self._history: Optional[Deque[float]] = None
|
|
57
|
+
if self.type is ConnectionType.RECURRENT:
|
|
58
|
+
self._history = deque(maxlen=self.delay)
|
|
59
|
+
|
|
60
|
+
def set_delay(self, delay: int) -> None:
|
|
61
|
+
if self.type is not ConnectionType.RECURRENT:
|
|
62
|
+
raise ValueError("set_delay is only valid for recurrent connections")
|
|
63
|
+
if delay <= 0:
|
|
64
|
+
delay = 1
|
|
65
|
+
self.delay = int(delay)
|
|
66
|
+
self._history = deque(maxlen=self.delay)
|
|
67
|
+
|
|
68
|
+
def push_source_output(self, value: float) -> None:
|
|
69
|
+
"""
|
|
70
|
+
Push the current source output into the delay buffer.
|
|
71
|
+
|
|
72
|
+
This should be called once per time step after the network computed outputs.
|
|
73
|
+
"""
|
|
74
|
+
if self._history is None:
|
|
75
|
+
return
|
|
76
|
+
self._history.append(float(value))
|
|
77
|
+
|
|
78
|
+
def delayed_source_output(self) -> float:
|
|
79
|
+
"""
|
|
80
|
+
Return the delayed source output.
|
|
81
|
+
|
|
82
|
+
Semantics:
|
|
83
|
+
- delay == 0 -> no delay buffer used (caller decides what to do)
|
|
84
|
+
- delay > 0 -> returns output[t-delay] if available else 0.0
|
|
85
|
+
"""
|
|
86
|
+
if self.delay == 0:
|
|
87
|
+
return 0.0
|
|
88
|
+
|
|
89
|
+
if self._history is None:
|
|
90
|
+
return 0.0
|
|
91
|
+
|
|
92
|
+
if len(self._history) < self.delay:
|
|
93
|
+
return 0.0
|
|
94
|
+
|
|
95
|
+
return float(self._history[0])
|
|
96
|
+
|
|
97
|
+
def reset_buffer(self) -> None:
|
|
98
|
+
"""Clear the internal delay history buffer."""
|
|
99
|
+
if self._history is not None:
|
|
100
|
+
self._history.clear()
|
|
101
|
+
|
|
102
|
+
def get_signal(self) -> float:
|
|
103
|
+
"""
|
|
104
|
+
Return the weighted signal from the source neuron.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
float: source.output × weight
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
return self.source.output * self.weight
|
|
111
|
+
|
|
112
|
+
def __repr__(self) -> str:
|
|
113
|
+
"""
|
|
114
|
+
Return a concise string representation of the connection.
|
|
115
|
+
|
|
116
|
+
Example:
|
|
117
|
+
<Conn abc123 -> def456 w=0.85 d=3 type=standard>
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
type_str = self.type.name.lower()
|
|
121
|
+
return (
|
|
122
|
+
f"<Conn {self.source.id[:6]} "
|
|
123
|
+
f"-> {self.target.id[:6]} "
|
|
124
|
+
f"d={self.delay} "
|
|
125
|
+
f"w={self.weight:.2f} type={type_str}>"
|
|
126
|
+
)
|