-
Notifications
You must be signed in to change notification settings - Fork 2
/
extention.py
195 lines (121 loc) · 3.89 KB
/
extention.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from memfuncs import MemFunc
import matplotlib.pyplot as plt
import numpy as np
from file import File
w = 2
INC = .1
class ExtentionOps:
def __init__(self,op):
opDict = {'add':np.add,
'sub':np.subtract,
'div':np.divide,
'mul':np.multiply,
'max':max,
'min':min,
'pow':pow}
self.opName = op
#Weird worka round for the complemnt
if op == "comp":
self.func = self.comp
elif op in opDict:
self.op = opDict[op]
self.func = self.extention
else:
raise ValueError("Invalid Operator")
#raise ValueError("Do not have that operator in AlphaOps")
def convertToDomain(self,A):
mem1 = MemFunc("trap",A)
newA = []
for i in np.arange(0,1.05,.05):
newA.append([i,self.round2(mem1.memFunc(i))])
return newA
def round2(self,val):
val = int(val * 100)
return (val / 100)
def round_to_05(self,n):
correction = 0.5 if n >= 0 else -0.5
return int( n/.05+correction ) * .05
def comp(self,A):
A = A[0]
if len(A) == 4:
A = self.convertToDomain(A)
out = [[],[]]
for a in A:
z = self.round_to_05(self.round2(1.0 - a[0]))
f = a[1]
try:
index = out[0].index(z)
out[1][index] = max(out[1][index],f)
except ValueError:
out[0].append(z)
out[1].append(f)
out = list(zip(out[0],out[1]))
out.sort(key=lambda x:x[0])
out1 = list(zip(*out))
A = np.array(A)
#[i[0] for i in sorted(enumerate(myList), key=lambda x:x[1])]
plt.title("Compliment")
plt.plot(out1[0],out1[1],c='y',linewidth=2)
plt.plot(A[:,0],A[:,1],c='k',linewidth=2)
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()
print(out)
return out
def extention(self, params):
A = params[0]
for i in range(1,len(params)):
B = params[i]
#Convert a membership function to the right domain the first time
if len(A) == 4:
A = self.convertToDomain(A)
if len(B) == 4:
B = self.convertToDomain(B)
# print("A:",A)
# print("B:",B)
out = [[],[]]
for a in A:
for b in B:
z = self.round2(self.op(a[0], b[0]))
try:
b[1]
except:
continue
f = min(a[1],b[1])
try:
index = out[0].index(z)
out[1][index] = max(out[1][index],f)
except ValueError:
out[0].append(z)
out[1].append(f)
out = list(zip(out[0],out[1]))
out.sort(key=lambda x:x[0])
B = np.array(B)
A = np.array(A)
out1 = np.array(list(zip(*out)))
plt.plot(A[:,0],A[:,1],c='b',linewidth=2)
plt.plot(B[:,0],B[:,1],c='g',linewidth=2)
plt.plot(out1[0],out1[1],c='y',linewidth=2)
plt.xlim([0,1])
plt.ylim([0,1])
plt.title(self.opName)
plt.show()
A = out
return A
# e = ExtentionOps("add")
# mem1 = MemFunc('tri',[.2,.2,.4])
# mem2 = MemFunc('tri',[.4,.6,.8])
# #mem2 = lambda x: 1 if x == 1 else 0
# A = []
# B = []
# for i in np.arange(0,1,.05):
# A.append([i,e.round2(mem1.memFunc(i))])
# B.append([i,e.round2(mem2.memFunc(i))])
# A = np.array(A)
# B = np.array(B)
# #A = [.2,.4,.4,.6]
# # B = [.4,.6,.6,.8]
# print("########")
# #p = e.comp(A)
# t = e.extention([A,B])
# #print(e.extention([p,t]))