-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmembership.py
More file actions
82 lines (67 loc) · 1.99 KB
/
membership.py
File metadata and controls
82 lines (67 loc) · 1.99 KB
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
import numpy as np
def determine_membership(x,abc, max, min, type='tri'):
assert(len(abc) ==3)
a,b,c = abc
if x <= min or x>= max:
return 1
if type == 'tri':
if a<x and x <=b:
return (x-a)/(b-a)
elif b<x and x < c:
return (c-x)/(c-b)
else:
return 0
elif type =='gaussian':
return a * np.exp(- ((x-b)**2) / (2*c**2))
elif type == 'semicircle':
r = c - b
y = np.sqrt(r**2 - (x-b)**2)
return y
else:
print('mf type not supported')
assert False
# if x < a or x > c:
# return 0
# elif x >= a and x <= b:
# return (b-x)/(b-a)
# elif x > b and x <= c:
# return (x-a)/(c-b)
def build_membership_function(universe, labels):
'''
adpated from skfuzzy
'''
num_labels = len(labels)
assert num_labels % 2 == 1
limits = [np.min(universe), np.max(universe)]
universe_range = limits[1] - limits[0]
widths = [universe_range/ ((num_labels-1)/2.)] * int(num_labels)
centers = np.linspace(limits[0], limits[1], num_labels)
abcs = [[c-w/2, c, c+w/2] for c,w in zip(centers, widths)]
dic = {}
for label, abc in zip(labels, abcs):
dic[label] = abc
# print(abcs)
# dic = {}
# for label, abc in zip(labels, abcs):
# mf = _trimf(universe,abc)
# dic[label] = mf
return dic
def _trimf(x, abc):
'''
adapted from skfuzzy
'''
assert len(abc) == 3
a,b,c = np.r_[abc]
assert a<=b and b <= c
y=np.zeros(len(x))
if a!=b:
idx = np.nonzero(np.logical_and(a<x, x<b))[0]
y[idx] = (x[idx] - a) / float(b-a)
if b!= c:
idx = np.nonzero(np.logical_and(b<x,x<c))[0]
y[idx] = (c-x[idx])/float(c-b)
idx = np.nonzero(x==b)
y[idx]=1
return y
if __name__ == '__main__':
print(build_membership_function(np.linspace(-2, 2, 10),list('abcdefg')))