18
18
from itertools import permutations
19
19
from random import choice , randint
20
20
from statistics import median
21
- from typing import Callable
22
21
23
22
import numpy as np
24
23
25
- from .functions import R , S , constant , gauss , rectangular , sigmoid , singleton , step , trapezoid , triangular
24
+ from .classes import Array
25
+ from .functions import (
26
+ Membership ,
27
+ R ,
28
+ S ,
29
+ constant ,
30
+ gauss ,
31
+ rectangular ,
32
+ sigmoid ,
33
+ singleton ,
34
+ step ,
35
+ trapezoid ,
36
+ triangular ,
37
+ )
26
38
27
39
np .seterr (all = "raise" )
28
40
functions = [step , rectangular ]
33
45
argument4_functions = [trapezoid ]
34
46
35
47
36
- def normalize (target : np . ndarray , output_length : int = 16 ) -> np . ndarray :
48
+ def normalize (target : Array , output_length : int = 16 ) -> Array :
37
49
"""Normalize and interpolate a numpy array.
38
50
39
51
Return an array of output_length and normalized values.
40
52
"""
41
- min_val = np .min (target )
42
- max_val = np .max (target )
53
+ min_val = float ( np .min (target ) )
54
+ max_val = float ( np .max (target ) )
43
55
if min_val == max_val :
44
56
return np .ones (output_length )
45
57
normalized_array = (target - min_val ) / (max_val - min_val )
@@ -49,13 +61,12 @@ def normalize(target: np.ndarray, output_length: int = 16) -> np.ndarray:
49
61
return normalized_array
50
62
51
63
52
- def guess_function (target : np . ndarray ) -> Callable :
64
+ def guess_function (target : Array ) -> Membership :
53
65
normalized = normalize (target )
54
- # trivial case
55
66
return constant if np .all (normalized == 1 ) else singleton
56
67
57
68
58
- def fitness (func : Callable , target : np . ndarray , certainty : int | None = None ) -> float :
69
+ def fitness (func : Membership , target : Array , certainty : int | None = None ) -> float :
59
70
"""Compute the difference between the array and the function evaluated at the parameters.
60
71
61
72
if the error is 0, we have a perfect match: fitness -> 1
@@ -66,7 +77,7 @@ def fitness(func: Callable, target: np.ndarray, certainty: int | None = None) ->
66
77
return result if certainty is None else round (result , certainty )
67
78
68
79
69
- def seed_population (func : Callable , target : np . ndarray ) -> dict [tuple , float ]:
80
+ def seed_population (func : Membership , target : Array ) -> dict [tuple , float ]:
70
81
# create a random population of parameters
71
82
params = [p for p in inspect .signature (func ).parameters .values () if p .kind == p .POSITIONAL_OR_KEYWORD ]
72
83
seed_population = {}
@@ -106,7 +117,7 @@ def reproduce(parent1: tuple, parent2: tuple) -> tuple:
106
117
107
118
108
119
def guess_parameters (
109
- func : Callable , target : np . ndarray , precision : int | None = None , certainty : int | None = None
120
+ func : Membership , target : Array , precision : int | None = None , certainty : int | None = None
110
121
) -> tuple :
111
122
"""Find the best fitting parameters for a function, targetting an array.
112
123
@@ -188,7 +199,7 @@ def best() -> tuple:
188
199
return best ()
189
200
190
201
191
- def shave (target : np . ndarray , components : dict [Callable , tuple ]) -> np . ndarray :
202
+ def shave (target : Array , components : dict [Membership , tuple ]) -> Array :
192
203
"""Remove the membership functions from the target array."""
193
204
result = np .zeros_like (target )
194
205
for func , params in components .items ():
0 commit comments