evonet 0.1.0.dev23__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.dev23/LICENSE +21 -0
- evonet-0.1.0.dev23/PKG-INFO +98 -0
- evonet-0.1.0.dev23/README.md +47 -0
- evonet-0.1.0.dev23/evonet/__init__.py +0 -0
- evonet-0.1.0.dev23/evonet/activation.py +207 -0
- evonet-0.1.0.dev23/evonet/connection.py +70 -0
- evonet-0.1.0.dev23/evonet/core.py +681 -0
- evonet-0.1.0.dev23/evonet/core_mit_plot_simple.py +645 -0
- evonet-0.1.0.dev23/evonet/enums.py +68 -0
- evonet-0.1.0.dev23/evonet/io.py +0 -0
- evonet-0.1.0.dev23/evonet/layer.py +26 -0
- evonet-0.1.0.dev23/evonet/mutation.py +345 -0
- evonet-0.1.0.dev23/evonet/neuron.py +99 -0
- evonet-0.1.0.dev23/evonet/serialization.py +171 -0
- evonet-0.1.0.dev23/evonet/utils.py +28 -0
- evonet-0.1.0.dev23/evonet/visualize.py +0 -0
- evonet-0.1.0.dev23/evonet.egg-info/PKG-INFO +98 -0
- evonet-0.1.0.dev23/evonet.egg-info/SOURCES.txt +26 -0
- evonet-0.1.0.dev23/evonet.egg-info/dependency_links.txt +1 -0
- evonet-0.1.0.dev23/evonet.egg-info/requires.txt +15 -0
- evonet-0.1.0.dev23/evonet.egg-info/top_level.txt +1 -0
- evonet-0.1.0.dev23/pyproject.toml +83 -0
- evonet-0.1.0.dev23/setup.cfg +8 -0
- evonet-0.1.0.dev23/tests/test_activation.py +30 -0
- evonet-0.1.0.dev23/tests/test_core.py +75 -0
- evonet-0.1.0.dev23/tests/test_nnet_io_and_forward.py +139 -0
- evonet-0.1.0.dev23/tests/test_recurrent_dynamics.py +106 -0
|
@@ -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.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: evonet
|
|
3
|
+
Version: 0.1.0.dev23
|
|
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
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
54
|
+
[](LICENSE)
|
|
55
|
+
[](https://github.com/EvoLib/evo-net)
|
|
56
|
+
|
|
57
|
+
**EvoNet** is a modular and evolvable neural network core designed for integration with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
58
|
+
It supports dynamic topologies, recurrent connections, per-neuron activation, and structural evolution – with a strong emphasis on **clarity**, **transparency**, and **didactic value**.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Features
|
|
63
|
+
|
|
64
|
+
- **Layer-based but flexible** – allows skip connections, cycles, and recurrent paths
|
|
65
|
+
- **Typed neuron roles and connection types** (`NeuronRole`, `ConnectionType`)
|
|
66
|
+
- **Topology-aware mutation system** – add/remove neurons and connections, mutate weights, change activations
|
|
67
|
+
- **Per-neuron activation functions** – configurable, extensible, evolvable
|
|
68
|
+
- **1-step recurrent state logic** – avoids multi-pass stabilization
|
|
69
|
+
- **Topology can grow at runtime** – with `add_neuron`, `add_connection`, `split_connection`
|
|
70
|
+
- **Debug-friendly architecture** – explicit IDs, labels, roles, directional graphs
|
|
71
|
+
- **Designed for evolutionary learning** – mutation, crossover, speciation ready
|
|
72
|
+
- **Lightweight & extensible** – pure Python, NumPy-based, no hard dependencies
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
> ⚠️ **This project is in early development (alpha)**. Interfaces and structure may change.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Quick Example
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from evonet.core import Nnet
|
|
84
|
+
|
|
85
|
+
net = Nnet()
|
|
86
|
+
net.add_layer() # Input
|
|
87
|
+
net.add_layer() # Output
|
|
88
|
+
|
|
89
|
+
net.add_neuron(layer_idx=0, activation="linear", lable="in")
|
|
90
|
+
net.add_neuron(layer_idx=1, activation="linear", bias=0.5, lable="out", connect_layer=True)
|
|
91
|
+
|
|
92
|
+
print(net.calc([1.0]))
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT License - see [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
98
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# EvoNet
|
|
2
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://github.com/EvoLib/evo-net)
|
|
5
|
+
|
|
6
|
+
**EvoNet** is a modular and evolvable neural network core designed for integration with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
7
|
+
It supports dynamic topologies, recurrent connections, per-neuron activation, and structural evolution – with a strong emphasis on **clarity**, **transparency**, and **didactic value**.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Layer-based but flexible** – allows skip connections, cycles, and recurrent paths
|
|
14
|
+
- **Typed neuron roles and connection types** (`NeuronRole`, `ConnectionType`)
|
|
15
|
+
- **Topology-aware mutation system** – add/remove neurons and connections, mutate weights, change activations
|
|
16
|
+
- **Per-neuron activation functions** – configurable, extensible, evolvable
|
|
17
|
+
- **1-step recurrent state logic** – avoids multi-pass stabilization
|
|
18
|
+
- **Topology can grow at runtime** – with `add_neuron`, `add_connection`, `split_connection`
|
|
19
|
+
- **Debug-friendly architecture** – explicit IDs, labels, roles, directional graphs
|
|
20
|
+
- **Designed for evolutionary learning** – mutation, crossover, speciation ready
|
|
21
|
+
- **Lightweight & extensible** – pure Python, NumPy-based, no hard dependencies
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
> ⚠️ **This project is in early development (alpha)**. Interfaces and structure may change.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Quick Example
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from evonet.core import Nnet
|
|
33
|
+
|
|
34
|
+
net = Nnet()
|
|
35
|
+
net.add_layer() # Input
|
|
36
|
+
net.add_layer() # Output
|
|
37
|
+
|
|
38
|
+
net.add_neuron(layer_idx=0, activation="linear", lable="in")
|
|
39
|
+
net.add_neuron(layer_idx=1, activation="linear", bias=0.5, lable="out", connect_layer=True)
|
|
40
|
+
|
|
41
|
+
print(net.calc([1.0]))
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT License - see [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
47
|
+
|
|
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,70 @@
|
|
|
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
|
+
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
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): Optional delay in time steps (not yet implemented).
|
|
28
|
+
type (ConnectionType): Type of connection (e.g. standard, recurrent,
|
|
29
|
+
inhibitory).
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
source: "Neuron",
|
|
35
|
+
target: "Neuron",
|
|
36
|
+
weight: float = 1.0,
|
|
37
|
+
delay: int = 0,
|
|
38
|
+
conn_type: ConnectionType = ConnectionType.STANDARD,
|
|
39
|
+
) -> None:
|
|
40
|
+
|
|
41
|
+
self.source = source
|
|
42
|
+
self.target = target
|
|
43
|
+
self.weight = weight
|
|
44
|
+
self.delay = delay
|
|
45
|
+
self.type: ConnectionType = conn_type
|
|
46
|
+
|
|
47
|
+
def get_signal(self) -> float:
|
|
48
|
+
"""
|
|
49
|
+
Return the weighted signal from the source neuron.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
float: source.output × weight
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
return self.source.output * self.weight
|
|
56
|
+
|
|
57
|
+
def __repr__(self) -> str:
|
|
58
|
+
"""
|
|
59
|
+
Return a concise string representation of the connection.
|
|
60
|
+
|
|
61
|
+
Example:
|
|
62
|
+
<Conn abc123 -> def456 w=0.85 type=standard>
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
type_str = self.type.name.lower()
|
|
66
|
+
return (
|
|
67
|
+
f"<Conn {self.source.id[:6]} "
|
|
68
|
+
f"-> {self.target.id[:6]} "
|
|
69
|
+
f"w={self.weight:.2f} type={type_str}>"
|
|
70
|
+
)
|