evonet 0.1.0a0.dev1__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.0a0.dev1/LICENSE +21 -0
- evonet-0.1.0a0.dev1/PKG-INFO +61 -0
- evonet-0.1.0a0.dev1/README.md +12 -0
- evonet-0.1.0a0.dev1/evonet/__init__.py +0 -0
- evonet-0.1.0a0.dev1/evonet/activation.py +186 -0
- evonet-0.1.0a0.dev1/evonet/connection.py +55 -0
- evonet-0.1.0a0.dev1/evonet/core.py +117 -0
- evonet-0.1.0a0.dev1/evonet/io.py +0 -0
- evonet-0.1.0a0.dev1/evonet/mutation.py +59 -0
- evonet-0.1.0a0.dev1/evonet/neuron.py +50 -0
- evonet-0.1.0a0.dev1/evonet/types.py +16 -0
- evonet-0.1.0a0.dev1/evonet/utils.py +0 -0
- evonet-0.1.0a0.dev1/evonet/visualize.py +0 -0
- evonet-0.1.0a0.dev1/evonet.egg-info/PKG-INFO +61 -0
- evonet-0.1.0a0.dev1/evonet.egg-info/SOURCES.txt +21 -0
- evonet-0.1.0a0.dev1/evonet.egg-info/dependency_links.txt +1 -0
- evonet-0.1.0a0.dev1/evonet.egg-info/requires.txt +13 -0
- evonet-0.1.0a0.dev1/evonet.egg-info/top_level.txt +1 -0
- evonet-0.1.0a0.dev1/pyproject.toml +81 -0
- evonet-0.1.0a0.dev1/setup.cfg +8 -0
- evonet-0.1.0a0.dev1/tests/test_activation.py +30 -0
- evonet-0.1.0a0.dev1/tests/test_core.py +42 -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,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: evonet
|
|
3
|
+
Version: 0.1.0a0.dev1
|
|
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
|
+
Provides-Extra: dev
|
|
42
|
+
Requires-Dist: mypy; extra == "dev"
|
|
43
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
44
|
+
Provides-Extra: docs
|
|
45
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
46
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
47
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
|
|
50
|
+
# EvoNet
|
|
51
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
52
|
+
[](LICENSE)
|
|
53
|
+
[](https://github.com/EvoLib/evo-net)
|
|
54
|
+
|
|
55
|
+
**EvoNet** is a modular, evolvable neural network core designed for integration with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
56
|
+
It supports dynamic topologies, recurrent connections, and is optimized for mutation, crossover, and structural evolution.
|
|
57
|
+
|
|
58
|
+
## 🪪 License
|
|
59
|
+
|
|
60
|
+
This project is licensed under the [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
61
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
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, evolvable neural network core designed for integration with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
7
|
+
It supports dynamic topologies, recurrent connections, and is optimized for mutation, crossover, and structural evolution.
|
|
8
|
+
|
|
9
|
+
## 🪪 License
|
|
10
|
+
|
|
11
|
+
This project is licensed under the [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
12
|
+
|
|
File without changes
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Activation functions for evolvable neural networks.
|
|
4
|
+
|
|
5
|
+
Provides scalar functions such as ReLU, Tanh, Sigmoid, as well as a softmax
|
|
6
|
+
implementation and a function registry (ACTIVATIONS).
|
|
7
|
+
|
|
8
|
+
These functions are stateless and operate on scalars unless otherwise noted.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from typing import Callable, List, Tuple, Union
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
Scalar = Union[int, float]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Common Activation Functions
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def tanh(x: Scalar) -> float:
|
|
22
|
+
"""Hyperbolic tangent activation: [-inf, inf] --> [-1, 1]."""
|
|
23
|
+
return float(np.tanh(x))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def ntanh(x: Scalar) -> float:
|
|
27
|
+
"""Normalized tanh: (tanh(x) + 1) / 2 --> [0, 1]."""
|
|
28
|
+
return (np.tanh(x) + 1) / 2
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def sigmoid(x: Scalar) -> float:
|
|
32
|
+
"""Sigmoid/logistic function: [-inf, inf] --> [0, 1]."""
|
|
33
|
+
x = np.clip(float(x), -500, 500)
|
|
34
|
+
return 1 / (1 + np.exp(-x))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def relu(x: Scalar) -> float:
|
|
38
|
+
"""ReLU: max(0, x)."""
|
|
39
|
+
return max(0.0, float(x))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def relu_max1(x: Scalar) -> float:
|
|
43
|
+
"""ReLU clipped to [0, 1]."""
|
|
44
|
+
return min(max(0.0, float(x)), 1.0)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def leaky_relu(x: Scalar, alpha: float = 0.01) -> float:
|
|
48
|
+
"""Leaky ReLU: x if x ≥ 0 else alpha * x."""
|
|
49
|
+
x = float(x)
|
|
50
|
+
return x if x >= 0 else alpha * x
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def elu(x: Scalar, alpha: float = 1.0) -> float:
|
|
54
|
+
"""Exponential Linear Unit."""
|
|
55
|
+
x = float(x)
|
|
56
|
+
return x if x >= 0 else alpha * (np.exp(x) - 1)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def selu(x: Scalar, alpha: float = 1.67326324, scale: float = 1.05070098) -> float:
|
|
60
|
+
"""Scaled Exponential Linear Unit (SELU)."""
|
|
61
|
+
x = float(x)
|
|
62
|
+
return scale * x if x >= 0 else scale * alpha * (np.exp(x) - 1)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def gaussian(x: Scalar) -> float:
|
|
66
|
+
"""Gaussian bell function: exp(-x^2)."""
|
|
67
|
+
x = float(x)
|
|
68
|
+
if np.abs(x) > 38:
|
|
69
|
+
return 0.0
|
|
70
|
+
return np.exp(-(x**2))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# --- Threshold Functions ---
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def binary(x: Scalar) -> float:
|
|
77
|
+
"""Step function: 1 if x > 0 else 0."""
|
|
78
|
+
return 1.0 if x > 0 else 0.0
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def signum(x: Scalar) -> float:
|
|
82
|
+
"""Sign function: 1, -1 or 0 depending on sign of x."""
|
|
83
|
+
return 1.0 if x > 0 else -1.0 if x < 0 else 0.0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# --- Linear Variants ---
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def linear(x: Scalar) -> float:
|
|
90
|
+
"""Linear identity: returns x."""
|
|
91
|
+
return float(x)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def linear_max1(x: Scalar) -> float:
|
|
95
|
+
"""Linear function clipped to [-1, 1]."""
|
|
96
|
+
return min(max(-1.0, float(x)), 1.0)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def invert(x: Scalar) -> float:
|
|
100
|
+
"""Returns -x."""
|
|
101
|
+
return -float(x)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def null(_: Scalar = 0) -> float:
|
|
105
|
+
"""Always returns 0.0."""
|
|
106
|
+
return 0.0
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# Modern Functions
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def swish(x: Scalar) -> float:
|
|
113
|
+
"""Swish activation: x * sigmoid(x). Smooth and non-monotonic."""
|
|
114
|
+
return float(x) * sigmoid(x)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def mish(x: Scalar) -> float:
|
|
118
|
+
"""Mish activation: x * tanh(softplus(x))."""
|
|
119
|
+
return float(x) * np.tanh(np.log1p(np.exp(x)))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def softplus(x: Scalar) -> float:
|
|
123
|
+
"""Smooth ReLU approximation: log(1 + exp(x))."""
|
|
124
|
+
return np.log1p(np.exp(float(x)))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def softsign(x: Scalar) -> float:
|
|
128
|
+
"""Smooth alternative to tanh: x / (1 + |x|)."""
|
|
129
|
+
return float(x) / (1 + abs(float(x)))
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def hard_sigmoid(x: Scalar) -> float:
|
|
133
|
+
"""Piecewise linear approximation of sigmoid."""
|
|
134
|
+
return min(max(0.0, 0.2 * float(x) + 0.5), 1.0)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# Special Functions
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def softmax(values: Union[List[float], Tuple[float], np.ndarray]) -> np.ndarray:
|
|
141
|
+
"""
|
|
142
|
+
Applies softmax over a list of values.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
values: Array-like input with ≥2 values.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
np.ndarray: Normalized softmax output summing to 1.
|
|
149
|
+
"""
|
|
150
|
+
arr = np.array(values, dtype=float)
|
|
151
|
+
if arr.size < 2:
|
|
152
|
+
raise ValueError("Softmax input must have at least two values")
|
|
153
|
+
exp_x = np.exp(arr - np.max(arr)) # For numerical stability
|
|
154
|
+
return exp_x / np.sum(exp_x)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# --- Registry ---
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def random_function() -> str:
|
|
161
|
+
"""Returns a random activation function name from the registry."""
|
|
162
|
+
return np.random.choice(list(ACTIVATIONS.keys()))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
ACTIVATIONS: dict[str, Callable] = {
|
|
166
|
+
"tanh": tanh,
|
|
167
|
+
"ntanh": ntanh,
|
|
168
|
+
"sigmoid": sigmoid,
|
|
169
|
+
"relu": relu,
|
|
170
|
+
"relu_max1": relu_max1,
|
|
171
|
+
"leaky_relu": leaky_relu,
|
|
172
|
+
"elu": elu,
|
|
173
|
+
"selu": selu,
|
|
174
|
+
"gaussian": gaussian,
|
|
175
|
+
"binary": binary,
|
|
176
|
+
"signum": signum,
|
|
177
|
+
"linear": linear,
|
|
178
|
+
"linear_max1": linear_max1,
|
|
179
|
+
"invert": invert,
|
|
180
|
+
"null": null,
|
|
181
|
+
"swish": swish,
|
|
182
|
+
"mish": mish,
|
|
183
|
+
"softplus": softplus,
|
|
184
|
+
"softsign": softsign,
|
|
185
|
+
"hard_sigmoid": hard_sigmoid,
|
|
186
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Connection between neurons in evolvable neural network.
|
|
4
|
+
|
|
5
|
+
A connection links a source neuron to a target neuron with a weight. Supports optional
|
|
6
|
+
connection types for future use (e.g. inhibitory).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from evonet.types 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 signal.
|
|
27
|
+
delay (int): Optional delay in steps (not yet used).
|
|
28
|
+
type (ConnectionType): Type of the connection (e.g. excitatory).
|
|
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
|
+
self.source = source
|
|
40
|
+
self.target = target
|
|
41
|
+
self.weight = weight
|
|
42
|
+
self.delay = delay
|
|
43
|
+
self.type: ConnectionType = conn_type
|
|
44
|
+
|
|
45
|
+
def get_signal(self) -> float:
|
|
46
|
+
"""Computes the weighted signal from the source neuron."""
|
|
47
|
+
return self.source.output * self.weight
|
|
48
|
+
|
|
49
|
+
def __repr__(self) -> str:
|
|
50
|
+
type_str = self.type.name.lower()
|
|
51
|
+
return (
|
|
52
|
+
f"<Conn {self.source.id[:4]} "
|
|
53
|
+
f"-> {self.target.id[:4]} "
|
|
54
|
+
f"w={self.weight:.2f} type={type_str}>"
|
|
55
|
+
)
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Core class for evolvable neural networks.
|
|
4
|
+
|
|
5
|
+
Manages neurons, connections, and forward computation. Prepares mutation, crossover, and
|
|
6
|
+
export interfaces.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
from evonet.connection import Connection
|
|
14
|
+
from evonet.neuron import Neuron
|
|
15
|
+
from evonet.types import NeuronRole
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Nnet:
|
|
19
|
+
"""
|
|
20
|
+
Evolvable neural network with explicit topology.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
neurons (list[Neuron]): All neurons in the network.
|
|
24
|
+
connections (list[Connection]): All directed, weighted edges.
|
|
25
|
+
input_neurons (list[Neuron]): Subset of neurons used as input nodes.
|
|
26
|
+
output_neurons (list[Neuron]): Subset of neurons used as output nodes.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self) -> None:
|
|
30
|
+
self.neurons: list[Neuron] = []
|
|
31
|
+
self.connections: list[Connection] = []
|
|
32
|
+
self.input_neurons: list[Neuron] = []
|
|
33
|
+
self.output_neurons: list[Neuron] = []
|
|
34
|
+
|
|
35
|
+
def add_neuron(self, neuron: Neuron, role: NeuronRole = NeuronRole.HIDDEN) -> None:
|
|
36
|
+
"""Adds a neuron to the network and assigns its functional role."""
|
|
37
|
+
self.neurons.append(neuron)
|
|
38
|
+
if role == NeuronRole.INPUT:
|
|
39
|
+
self.input_neurons.append(neuron)
|
|
40
|
+
elif role == NeuronRole.OUTPUT:
|
|
41
|
+
self.output_neurons.append(neuron)
|
|
42
|
+
|
|
43
|
+
def add_connection(self, conn: Connection) -> None:
|
|
44
|
+
"""Adds a connection and updates neuron references."""
|
|
45
|
+
self.connections.append(conn)
|
|
46
|
+
conn.target.incoming.append(conn)
|
|
47
|
+
conn.source.outgoing.append(conn)
|
|
48
|
+
|
|
49
|
+
def reset(self) -> None:
|
|
50
|
+
"""Resets output values of all neurons (before forward pass)."""
|
|
51
|
+
for neuron in self.neurons:
|
|
52
|
+
neuron.reset()
|
|
53
|
+
|
|
54
|
+
def calc(self, input_values: list[float]) -> list[float]:
|
|
55
|
+
"""
|
|
56
|
+
Forward pass through the network.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
input_values: values to assign to input neurons
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
outputs from output neurons (after activation)
|
|
63
|
+
"""
|
|
64
|
+
assert len(input_values) == len(self.input_neurons), "Input size mismatch"
|
|
65
|
+
self.reset()
|
|
66
|
+
|
|
67
|
+
# Assign inputs
|
|
68
|
+
for i, value in enumerate(input_values):
|
|
69
|
+
self.input_neurons[i].output = float(value)
|
|
70
|
+
|
|
71
|
+
# Topological forward computation (assumes acyclic)
|
|
72
|
+
visited = set(self.input_neurons)
|
|
73
|
+
queue = [n for n in self.neurons if n not in visited]
|
|
74
|
+
|
|
75
|
+
while queue:
|
|
76
|
+
progressed = False
|
|
77
|
+
for neuron in queue[:]:
|
|
78
|
+
if all(src.source in visited for src in neuron.incoming):
|
|
79
|
+
total = sum(c.get_signal() for c in neuron.incoming) + neuron.bias
|
|
80
|
+
neuron.output = neuron.activation(total)
|
|
81
|
+
visited.add(neuron)
|
|
82
|
+
queue.remove(neuron)
|
|
83
|
+
progressed = True
|
|
84
|
+
if not progressed:
|
|
85
|
+
break # Prevent infinite loop
|
|
86
|
+
|
|
87
|
+
return [n.output for n in self.output_neurons]
|
|
88
|
+
|
|
89
|
+
def __repr__(self) -> str:
|
|
90
|
+
return (
|
|
91
|
+
f"<Nnet | {len(self.neurons)} neurons, "
|
|
92
|
+
f"{len(self.connections)} connections>"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def get_weights(self) -> np.ndarray:
|
|
96
|
+
"""Returns all connection weights as a flat NumPy array."""
|
|
97
|
+
import numpy as np
|
|
98
|
+
|
|
99
|
+
return np.array([c.weight for c in self.connections], dtype=float)
|
|
100
|
+
|
|
101
|
+
def set_weights(self, vector: np.ndarray) -> None:
|
|
102
|
+
"""Assigns connection weights from a flat NumPy array."""
|
|
103
|
+
assert len(vector) == len(self.connections), "Weight vector length mismatch"
|
|
104
|
+
for i, c in enumerate(self.connections):
|
|
105
|
+
c.weight = float(vector[i])
|
|
106
|
+
|
|
107
|
+
def get_biases(self) -> np.ndarray:
|
|
108
|
+
"""Returns all neuron biases as a flat NumPy array."""
|
|
109
|
+
import numpy as np
|
|
110
|
+
|
|
111
|
+
return np.array([n.bias for n in self.neurons], dtype=float)
|
|
112
|
+
|
|
113
|
+
def set_biases(self, vector: np.ndarray) -> None:
|
|
114
|
+
"""Assigns neuron biases from a flat NumPy array."""
|
|
115
|
+
assert len(vector) == len(self.neurons), "Bias vector length mismatch"
|
|
116
|
+
for i, n in enumerate(self.neurons):
|
|
117
|
+
n.bias = float(vector[i])
|
|
File without changes
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Mutation operators for evolvable neural networks.
|
|
4
|
+
|
|
5
|
+
Includes mutations for weights, biases, and (optionally) activation functions. Structure
|
|
6
|
+
mutations will be implemented separately.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import random
|
|
10
|
+
|
|
11
|
+
from evonet.core import Nnet
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def mutate_weights(
|
|
15
|
+
net: Nnet,
|
|
16
|
+
std: float = 0.1,
|
|
17
|
+
mutation_rate: float = 1.0,
|
|
18
|
+
weight_min: float = -5.0,
|
|
19
|
+
weight_max: float = 5.0,
|
|
20
|
+
) -> None:
|
|
21
|
+
"""
|
|
22
|
+
Applies Gaussian noise to connection weights.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
net (Nnet): The network to mutate.
|
|
26
|
+
std (float): Standard deviation of noise.
|
|
27
|
+
mutation_rate (float): Probability per connection to mutate.
|
|
28
|
+
weight_min (float): Lower bound for weights.
|
|
29
|
+
weight_max (float): Upper bound for weights.
|
|
30
|
+
"""
|
|
31
|
+
for conn in net.connections:
|
|
32
|
+
if random.random() < mutation_rate:
|
|
33
|
+
noise = random.gauss(0.0, std)
|
|
34
|
+
conn.weight += noise
|
|
35
|
+
conn.weight = max(min(conn.weight, weight_max), weight_min)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def mutate_biases(
|
|
39
|
+
net: Nnet,
|
|
40
|
+
std: float = 0.1,
|
|
41
|
+
mutation_rate: float = 1.0,
|
|
42
|
+
bias_min: float = -5.0,
|
|
43
|
+
bias_max: float = 5.0,
|
|
44
|
+
) -> None:
|
|
45
|
+
"""
|
|
46
|
+
Applies Gaussian noise to neuron biases.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
net (Nnet): The network to mutate.
|
|
50
|
+
std (float): Standard deviation of noise.
|
|
51
|
+
mutation_rate (float): Probability per neuron to mutate.
|
|
52
|
+
bias_min (float): Lower bound for biases.
|
|
53
|
+
bias_max (float): Upper bound for biases.
|
|
54
|
+
"""
|
|
55
|
+
for neuron in net.neurons:
|
|
56
|
+
if random.random() < mutation_rate:
|
|
57
|
+
noise = random.gauss(0.0, std)
|
|
58
|
+
neuron.bias += noise
|
|
59
|
+
neuron.bias = max(min(neuron.bias, bias_max), bias_min)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Neuron definition for evolvable neural network.
|
|
4
|
+
|
|
5
|
+
Each neuron holds its activation function, input/output connections, bias value, and a
|
|
6
|
+
cached output from the last forward pass.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Callable
|
|
12
|
+
from uuid import uuid4
|
|
13
|
+
|
|
14
|
+
from evonet.activation import ACTIVATIONS
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Neuron:
|
|
18
|
+
"""
|
|
19
|
+
Represents a single neuron in the network.
|
|
20
|
+
|
|
21
|
+
Attributes:
|
|
22
|
+
id (str): Unique identifier for tracking and crossover.
|
|
23
|
+
activation_name (str): Name of the activation function.
|
|
24
|
+
bias (float): Bias value added to incoming inputs.
|
|
25
|
+
incoming (list): Incoming connections (to be filled externally).
|
|
26
|
+
outgoing (list): Outgoing connections (to be filled externally).
|
|
27
|
+
output (float): Cached result after activation.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, activation: str = "tanh", bias: float = 0.0) -> None:
|
|
31
|
+
if activation not in ACTIVATIONS:
|
|
32
|
+
raise ValueError(f"Unknown activation function: '{activation}'")
|
|
33
|
+
self.id: str = str(uuid4())
|
|
34
|
+
self.activation_name: str = activation
|
|
35
|
+
self.activation: Callable[[float], float] = ACTIVATIONS[activation]
|
|
36
|
+
self.bias: float = bias
|
|
37
|
+
self.incoming: list = []
|
|
38
|
+
self.outgoing: list = []
|
|
39
|
+
self.output: float = 0.0
|
|
40
|
+
|
|
41
|
+
def reset(self) -> None:
|
|
42
|
+
"""Clears output before each forward pass."""
|
|
43
|
+
self.output = 0.0
|
|
44
|
+
|
|
45
|
+
def __repr__(self) -> str:
|
|
46
|
+
return (
|
|
47
|
+
f"<Neuron id={self.id[:6]} "
|
|
48
|
+
f"act={self.activation_name} "
|
|
49
|
+
f"bias={self.bias:.2f}>"
|
|
50
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
from enum import Enum, auto
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NeuronRole(Enum):
|
|
6
|
+
INPUT = auto()
|
|
7
|
+
HIDDEN = auto()
|
|
8
|
+
OUTPUT = auto()
|
|
9
|
+
BIAS = auto()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ConnectionType(Enum):
|
|
13
|
+
STANDARD = auto()
|
|
14
|
+
INHIBITORY = auto()
|
|
15
|
+
EXCITATORY = auto()
|
|
16
|
+
MODULATORY = auto()
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: evonet
|
|
3
|
+
Version: 0.1.0a0.dev1
|
|
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
|
+
Provides-Extra: dev
|
|
42
|
+
Requires-Dist: mypy; extra == "dev"
|
|
43
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
44
|
+
Provides-Extra: docs
|
|
45
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
46
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
47
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
|
|
50
|
+
# EvoNet
|
|
51
|
+
[](https://github.com/EvoLib/evo-net/actions/workflows/ci.yml)
|
|
52
|
+
[](LICENSE)
|
|
53
|
+
[](https://github.com/EvoLib/evo-net)
|
|
54
|
+
|
|
55
|
+
**EvoNet** is a modular, evolvable neural network core designed for integration with [EvoLib](https://github.com/EvoLib/evo-lib).
|
|
56
|
+
It supports dynamic topologies, recurrent connections, and is optimized for mutation, crossover, and structural evolution.
|
|
57
|
+
|
|
58
|
+
## 🪪 License
|
|
59
|
+
|
|
60
|
+
This project is licensed under the [MIT License](https://github.com/EvoLib/evo-net/tree/main/LICENSE).
|
|
61
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.cfg
|
|
5
|
+
evonet/__init__.py
|
|
6
|
+
evonet/activation.py
|
|
7
|
+
evonet/connection.py
|
|
8
|
+
evonet/core.py
|
|
9
|
+
evonet/io.py
|
|
10
|
+
evonet/mutation.py
|
|
11
|
+
evonet/neuron.py
|
|
12
|
+
evonet/types.py
|
|
13
|
+
evonet/utils.py
|
|
14
|
+
evonet/visualize.py
|
|
15
|
+
evonet.egg-info/PKG-INFO
|
|
16
|
+
evonet.egg-info/SOURCES.txt
|
|
17
|
+
evonet.egg-info/dependency_links.txt
|
|
18
|
+
evonet.egg-info/requires.txt
|
|
19
|
+
evonet.egg-info/top_level.txt
|
|
20
|
+
tests/test_activation.py
|
|
21
|
+
tests/test_core.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
evonet
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "evonet"
|
|
7
|
+
version = "0.1.0adev1"
|
|
8
|
+
description = "Evolvable neural network core for integration with EvoLib"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "EvoLib", email = "evolib@dismail.de" }
|
|
11
|
+
]
|
|
12
|
+
license = { file = "LICENSE" }
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.10"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"numpy >=1.24",
|
|
17
|
+
"pyyaml >=6.0",
|
|
18
|
+
"pandas >=2.3.0",
|
|
19
|
+
"pydantic >= 2.7,<3.0"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 3 - Alpha",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = ["mypy","types-PyYAML"]
|
|
33
|
+
docs = [
|
|
34
|
+
"sphinx",
|
|
35
|
+
"sphinx-rtd-theme",
|
|
36
|
+
"myst-parser",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
where = ["."]
|
|
41
|
+
include = ["evonet*"]
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.package-data]
|
|
44
|
+
"evolib" = ["py.typed"]
|
|
45
|
+
|
|
46
|
+
# Tool-Konfigurationen
|
|
47
|
+
[tool.black]
|
|
48
|
+
line-length = 88
|
|
49
|
+
target-version = ["py311"]
|
|
50
|
+
|
|
51
|
+
[tool.isort]
|
|
52
|
+
profile = "black"
|
|
53
|
+
line_length = 88
|
|
54
|
+
|
|
55
|
+
[tool.mypy]
|
|
56
|
+
python_version = "3.12"
|
|
57
|
+
ignore_missing_imports = true
|
|
58
|
+
disallow_untyped_defs = true
|
|
59
|
+
check_untyped_defs = true
|
|
60
|
+
warn_unused_ignores = true
|
|
61
|
+
|
|
62
|
+
[tool.flake8]
|
|
63
|
+
max-line-length = 88
|
|
64
|
+
extend-ignore = ["E203", "W503"]
|
|
65
|
+
|
|
66
|
+
[tool.docformatter]
|
|
67
|
+
wrap-summaries = 88
|
|
68
|
+
wrap-descriptions = 88
|
|
69
|
+
pre-summary-newline = true
|
|
70
|
+
|
|
71
|
+
[tool.pylint]
|
|
72
|
+
max-line-length = 88
|
|
73
|
+
disable = [
|
|
74
|
+
"missing-docstring",
|
|
75
|
+
"invalid-name",
|
|
76
|
+
"too-few-public-methods",
|
|
77
|
+
"too-many-arguments",
|
|
78
|
+
"too-many-instance-attributes",
|
|
79
|
+
"too-many-locals"
|
|
80
|
+
]
|
|
81
|
+
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from evonet import activation
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.mark.parametrize("x", [-2.0, 0.0, 2.0])
|
|
8
|
+
def test_activation_outputs_finite(x: float) -> None:
|
|
9
|
+
"""All scalar activations should return a finite float."""
|
|
10
|
+
for name, fn in activation.ACTIVATIONS.items():
|
|
11
|
+
if name == "softmax":
|
|
12
|
+
continue # handled separately
|
|
13
|
+
y = fn(x)
|
|
14
|
+
assert isinstance(y, float), f"{name} did not return float"
|
|
15
|
+
assert np.isfinite(y), f"{name} returned non-finite value: {y}"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_softmax_sum_to_one() -> None:
|
|
19
|
+
"""Softmax output should sum to 1.0."""
|
|
20
|
+
input_vector = [1.0, 2.0, 3.0]
|
|
21
|
+
output = activation.softmax(input_vector)
|
|
22
|
+
assert isinstance(output, np.ndarray)
|
|
23
|
+
np.testing.assert_almost_equal(np.sum(output), 1.0, decimal=6)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_random_function_exists_in_registry() -> None:
|
|
27
|
+
"""random_function must return a valid name in the registry."""
|
|
28
|
+
for _ in range(10):
|
|
29
|
+
name = activation.random_function()
|
|
30
|
+
assert name in activation.ACTIVATIONS
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from evonet.connection import Connection
|
|
2
|
+
from evonet.core import Nnet
|
|
3
|
+
from evonet.neuron import Neuron
|
|
4
|
+
from evonet.types import NeuronRole
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_forward_pass_identity() -> None:
|
|
8
|
+
"""Testet ein einfaches Netz mit einem Input und einem Output."""
|
|
9
|
+
net = Nnet()
|
|
10
|
+
|
|
11
|
+
# Neuronen erstellen
|
|
12
|
+
n_input = Neuron(activation="linear")
|
|
13
|
+
n_output = Neuron(activation="linear")
|
|
14
|
+
|
|
15
|
+
# Netz aufbauen
|
|
16
|
+
net.add_neuron(n_input, role=NeuronRole.INPUT)
|
|
17
|
+
net.add_neuron(n_output, role=NeuronRole.OUTPUT)
|
|
18
|
+
net.add_connection(Connection(n_input, n_output, weight=1.0))
|
|
19
|
+
|
|
20
|
+
# Eingabe --> Ausgabe testen
|
|
21
|
+
x = [0.75]
|
|
22
|
+
y = net.calc(x)
|
|
23
|
+
|
|
24
|
+
assert isinstance(y, list)
|
|
25
|
+
assert len(y) == 1
|
|
26
|
+
assert abs(y[0] - 0.75) < 1e-6
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_forward_pass_with_bias() -> None:
|
|
30
|
+
"""Testet Netz mit Bias am Output-Neuron."""
|
|
31
|
+
net = Nnet()
|
|
32
|
+
|
|
33
|
+
n_input = Neuron(activation="linear")
|
|
34
|
+
n_output = Neuron(activation="linear", bias=0.5)
|
|
35
|
+
|
|
36
|
+
net.add_neuron(n_input, role=NeuronRole.INPUT)
|
|
37
|
+
net.add_neuron(n_output, role=NeuronRole.OUTPUT)
|
|
38
|
+
net.add_connection(Connection(n_input, n_output, weight=2.0))
|
|
39
|
+
|
|
40
|
+
y = net.calc([1.0])
|
|
41
|
+
|
|
42
|
+
assert abs(y[0] - 2.5) < 1e-6 # (1.0 * 2.0) + 0.5 = 2.5
|