-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifs_operators_plot.py
215 lines (169 loc) · 6.42 KB
/
ifs_operators_plot.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
def infStd(*ifsets):
mus = np.minimum(*[ifset[0] for ifset in ifsets])
nus = np.maximum(*[ifset[1] for ifset in ifsets])
return (mus,nus)
def supStd(*ifsets):
mus = np.maximum(*[ifset[0] for ifset in ifsets])
nus = np.minimum(*[ifset[1] for ifset in ifsets])
return (mus,nus)
###
###
def infPi(*ifsets):
mus = np.minimum(*[ifset[0] for ifset in ifsets])
nus = np.minimum(*[ifset[1] for ifset in ifsets])
return (mus,nus)
def supPi(*ifsets):
mus = np.maximum(*[ifset[0] for ifset in ifsets])
nus = np.maximum(*[ifset[1] for ifset in ifsets])
if mus + nus > 1:
# The result is not a correct IFS
return None
else:
return (mus,nus)
##############
## Unary operators
##############
def neg(ifset):
return (ifset[1], ifset[0])
##############
## Inclusion operators
##############
import collections
def incl_j(musnus1, musnus2):
# Set theoretical inclusion indicator
holder = collections.OrderedDict([])
# [((1, 2), (3, 4)), ((1, 2), (3, 4)), ((1, 2), (3, 4))]
lst = list(zip(list(zip(musnus1[0], musnus1[1])),
list(zip(musnus2[0], musnus2[1]))))
def argwhere(filt):
indices = np.argwhere(np.array(list(map(filt, lst)))==True)
indices = [x[0] for x in indices]
return indices
#################################################################
def eq_(a, b):
return np.abs(a - b) < 1e-05
def cmp(cmp_, munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return cmp_(munu1[0], munu2[0]) and cmp_(munu1[1], munu2[1])
def eq(munu1_munu2):
return cmp(eq_, munu1_munu2)
holder['e_eq'] = argwhere(eq)
#################################################################
def lt(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] < munu2[0] and munu1[1] > munu2[1]
def gt(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] > munu2[0] and munu1[1] < munu2[1]
holder['e_0'] = argwhere(lt), argwhere(gt)
#################################################################
def lt_pi(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] < munu2[0] and munu1[1] < munu2[1]
def gt_pi(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] > munu2[0] and munu1[1] > munu2[1]
holder['e_pi'] = argwhere(lt_pi), argwhere(gt_pi)
#################################################################
def lt_square(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] < munu2[0] and eq_(munu1[1], munu2[1])
def gt_square(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return munu1[0] > munu2[0] and eq_(munu1[1], munu2[1])
holder['e_square'] = argwhere(lt_square), argwhere(gt_square)
#################################################################
def lt_diam(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return eq_(munu1[0], munu2[0]) and munu1[1] > munu2[1]
def gt_diam(munu1_munu2):
munu1, munu2 = munu1_munu2[0], munu1_munu2[1]
return eq_(munu1[0], munu2[0]) and munu1[1] < munu2[1]
holder['e_diam'] = argwhere(lt_diam), argwhere(gt_diam)
#################################################################
return holder
def incl_i(musnus1, musnus2):
# Set theoretical inclusion indicator
holder = incl_j(musnus1, musnus2)
result = collections.OrderedDict()
result['e_eq'] = len(holder['e_eq'])
for key, pair in holder.items():
if key != 'e_eq':
result[key] = len(pair[0]), len(pair[1])
return result
##############
## Topological operators
##############
def incGeneral(ifset, alpha, beta, gamma_a, gamma_b):
mus = ifset[0]
nus = ifset[1]
if not np.min(mus) < 1.0:
print('incorrect input incGeneral')
return ifset
# nsu = ifset[1]
if not gamma_a > 0.0:
mus_rst = mus
else:
mu_slope = 1 / (1-gamma_a)
mus_rst = map(lambda mu: mu if(mu >= alpha) else \
max(mu_slope*(mu - gamma_a*alpha), 0.0),
mus)
mus_rst = list(mus_rst)
nus_rst = map(lambda pair: pair[1] if pair[1] >= beta else \
min((1-gamma_b)*pair[1] + gamma_b*beta, 1-pair[0]),
zip(mus_rst,nus))
# print("in incGeneral \n")
return (np.asarray(list(mus_rst)), np.asarray(list(nus_rst)))
def clGeneral(ifset, alpha, beta, gamma_a, gamma_b):
# print("in clGeneral")
# print(neg(ifset))
result = neg(incGeneral(neg(ifset), beta, alpha, gamma_b, gamma_a))
return result
###############################
def incGeneral2(ifset,alpha0, beta0,
alpha, beta,
gamma_a, gamma_b):
mus = ifset[0]
nus = ifset[1]
if not np.min(mus) < 1.0:
return ifset
# nsu = ifset[1]
if not gamma_a > 0.0:
mus_rst = mus
else:
mu_slope = 1 / (1-gamma_a)
dAlpha = alpha - alpha0
dBeta = beta - beta0
def f_inc(mu):
if mu <= alpha0:
return mu
elif alpha0 < mu <= alpha0 + gamma_a*dAlpha:
return alpha0
elif alpha0 + gamma_a*dAlpha < mu <= alpha:
return mu_slope*(mu - alpha) + alpha
else:
return mu
def g_cl(nu):
if nu < beta0:
return nu
elif beta0 <= nu <= beta:
return (1 - gamma_b)*(nu - beta) + beta
else:
return nu
mus_rst = map(f_inc, mus)
mus_rst = list(mus_rst)
nus_rst = map(lambda pair: pair[1] if pair[1] >= beta else \
min(g_cl(pair[1]), 1-pair[0]),
zip(mus_rst,nus))
# print("in incGeneral \n")
return (np.asarray(list(mus_rst)), np.asarray(list(nus_rst)))
def clGeneral2(ifset, alpha0, beta0,
alpha, beta,
gamma_a, gamma_b):
# print("in clGeneral")
# print(neg(ifset))
result = neg(incGeneral2(neg(ifset),
beta0, alpha0,
beta, alpha, gamma_b, gamma_a))
return result