-
Notifications
You must be signed in to change notification settings - Fork 0
/
defuzzifiers.py
105 lines (97 loc) · 3.65 KB
/
defuzzifiers.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
import scipy.integrate as integrate
import member_functions as mf
import numpy as np
def run_defuzzyfiers( agregation_func):
x = get_left_max(agregation_func)
vg, g, r, b, vb = mf.eval_chances_func(x)
print("using left-most max value:\n\
x = %s\n\
f(x) = {very good aproval chance = %s,\n\
good aproval chance = %s,\n\
regular aproval chance = %s,\n\
bad aproval chance = %s,\n\
very bad aproval chance = %s\n" %(str(x), str(vg), str(g), str(r), str(b), str(vb)))
x = get_right_max(agregation_func)
vg, g, r, b, vb = mf.eval_chances_func(x)
print("using right-most max value:\n\
x = %s\n\
f(x) = {very good aproval chance = %s,\n\
good aproval chance = %s,\n\
regular aproval chance = %s,\n\
bad aproval chance = %s,\n\
very bad aproval chance = %s\n" %(str(x), str(vg), str(g), str(r), str(b), str(vb)))
x = get_median_max(agregation_func)
vg, g, r, b, vb = mf.eval_chances_func(x)
print("using median max value:\n\
x = %s\n\
f(x) = {very good aproval chance = %s,\n\
good aproval chance = %s,\n\
regular aproval chance = %s,\n\
bad aproval chance = %s,\n\
very bad aproval chance = %s\n" %(str(x), str(vg), str(g), str(r), str(b), str(vb)))
x = centroid_method(agregation_func)
vg, g, r, b, vb = mf.eval_chances_func(x)
print("using centroid method value:\n\
x = %s\n\
f(x) = {very good aproval chance = %s,\n\
good aproval chance = %s,\n\
regular aproval chance = %s,\n\
bad aproval chance = %s,\n\
very bad aproval chance = %s\n" %(str(x), str(vg), str(g), str(r), str(b), str(vb)))
x = bisector_method(agregation_func)
vg, g, r, b, vb = mf.eval_chances_func(x)
print("using bisector method value:\n\
x = %s\n\
f(x) = {very good aproval chance = %s,\n\
good aproval chance = %s,\n\
regular aproval chance = %s,\n\
bad aproval chance = %s,\n\
very bad aproval chance = %s\n" %(str(x), str(vg), str(g), str(r), str(b), str(vb)))
# Max method
def get_left_max(f, precission = 0.01):
max_value = -1
max_x = 0
xs = np.arange(0, 100, precission)
for x in xs:
if f(x) > max_value:
max_value = f(x)
max_x = x
return max_x
def get_right_max( f, precission = 0.01):
max_value = -1
max_x = 0
xs = np.arange(0, 100, precission)
for x in xs:
if f(x) >= max_value:
max_value = f(x)
max_x = x
return max_x
def get_median_max( f, precission = 0.01):
xs = np.arange(0,100,precission)
max_value = -1
max_xs = []
for x in xs:
if f(x) > max_value:
max_xs = [x]
max_value = f(x)
elif f(x) == max_value:
max_xs.append(x)
return max_xs[int(len(max_xs)/2)]
# Centroid
def centroid_method( f):
f = f
def func(x):
return x*f(x)
return integrate.quad(func, 0, 100)[0]/integrate.quad(f, 0, 100)[0]
# Bisector
def bisector_method( f):
return _binary_search(f, 0, 100)
def _binary_search( f, a, b):
m = (b + a)/2
left_val = integrate.quad(f, 0, m)[0]
right_val = integrate.quad(f, m, 100)[0]
if abs(left_val - right_val) < 0.1:
return m
if left_val < right_val:
return _binary_search(f, m, b)
return _binary_search(f, a, m)