-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_fuzzy.py
268 lines (216 loc) · 8.52 KB
/
binary_fuzzy.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""This module tries to implement the boolean version of the earlier model in fuzzy logic"""
import numpy as np
import matplotlib.pyplot as plt
import skfuzzy as fuzz
def find_index(arr, value):
for i in xrange(arr.size):
if( arr[i] == value):
return i
return -1
def find_closest(arr, value):
dif = abs(arr[0] - value)
min_dif = dif
index = 0
for i in xrange(arr.size):
dif = abs(arr[i] - value)
if dif < min_dif:
min_dif = dif
index = i
return index
"""def compute_initial_egfr(egf, hrg, egfr):
'''Rules---
If egf is high or hrg is high then egfr is high
if egf is low and hrg is low then egfr is low'''
#Membership function for egf
egf_high = fuzz.gaussmf(egf, 1, 0.25)
egf_low = fuzz.gaussmf(egf, 0, 0.25)
'''plt.plot(egf,egf_low)
plt.show()'''
#Membership function for hrg and egfr
'''Technically they should have been calcultated using fuzz.someMf but since they are same to save computation we do this'''
hrg_high = egf_high
hrg_low = egf_low
egfr_high = egf_high
egfr_low = egf_low
#Antecedents
a1_egfr = egf_high # Again here np.fmax should have been used but we save computation
a2_egfr = egf_low
#Consequents
c1_egfr = fuzz.relation_min(a1_egfr, egfr_high)
c2_egfr = fuzz.relation_min(a2_egfr, egfr_low)
c_egfr_combined = np.fmax(c1_egfr, c2_egfr)
return c_egfr_combined
def compute_initial_raf(egfr, akt, raf):
'''Rules---
If egfr is high or akt is high then raf is high
if egfr is low and akt is low then raf is low'''
#Membership function for egf
egfr_high = fuzz.gaussmf(egfr, 1, 0.25)
egfr_low = fuzz.gaussmf(egfr, 0, 0.25)
#Membership function for akt and raf
'''Technically they should have been calcultated using fuzz.someMf but since they are same to save computation we do this'''
akt_high = egfr_high
akt_low = egfr_low
raf_high = egfr_high
raf_low = egfr_low
#Antecedents
a1_raf = egfr_high # Again here np.fmax should have been used but we save computation
a2_raf = egfr_low
#Consequents
c1_raf = fuzz.relation_min(a1_raf, raf_high)
c2_raf = fuzz.relation_min(a2_raf, raf_low)
c_raf_combined = np.fmax(c1_raf, c2_raf)
return c_raf_combined
def compute_initial_pi3k(egfr, erk, pi3k):
'''Rules ---
if egfr is high and erk is low then pi3k is high
if egfr is low or erk is high then pi3k is low'''
#Memebership function for egfr
egfr_high = fuzz.gaussmf(egfr, 1, 0.25)
egfr_low = fuzz.gaussmf(egfr, 0, 0.25)
#Membership function for akt and raf
'''Technically they should have been calcultated using fuzz.someMf but since they are same to save computation we do this'''
erk_high = egfr_high
erk_low = egfr_low
pi3k_high = egfr_high
pi3k_low = egfr_low
#Antecedents
a1_pi3k = np.fmin(egfr_high, erk_low)
a2_pi3k = np.fmax(egfr_low, erk_high)
#Consequents
c1_pi3k = fuzz.relation_min(a1_pi3k, pi3k_high)
c2_pi3k = fuzz.relation_min(a2_pi3k, pi3k_low)
c_pi3k_combined = np.fmin(c1_pi3k, c2_pi3k)
return c_pi3k_combined"""
def eval_membership_functions(initial_values):
'''Technically they should have been calcultated using fuzz.someMf but since they are same to save computation we do this'''
egf_high = fuzz.gaussmf(initial_values[0], 1, 0.25)
egf_low = fuzz.gaussmf(initial_values[1], 0, 0.25)
hrg_high = egf_high
hrg_low = egf_low
egfr_high = egf_high
egfr_low = egf_low
erk_high = egfr_high
erk_low = egfr_low
pi3k_high = egfr_high
pi3k_low = egfr_low
akt_high = egfr_high
akt_low = egfr_low
raf_high = egfr_high
raf_low = egfr_low
return ((egf_low, egf_high), (hrg_low, hrg_high), (egfr_low, egfr_high), (raf_low, raf_high), (pi3k_low, pi3k_high), (erk_low, erk_high), (akt_low, akt_high))
def compute_egfr(egf_value, hrg_value, initial_values, mfs ):
"""Rules---
If egf is high or hrg is high then egfr is high
if egf is low and hrg is low then egfr is low"""
a1_1 = mfs[0][1][initial_values[0] == egf_value] #egf_high[egf == egf_value]
a1_2 = mfs[1][1][ initial_values[1] == hrg_value ] #hrg_high[hrg == hrg_value]
if( a1_1.size == 0):
a1_1 = mfs[0][1][ find_closest(initial_values[0], egf_value)]
if( a1_2.size == 0):
a1_2 = mfs[1][1][ find_closest(initial_values[1], hrg_value)]
a1 = max( a1_1, a1_2 )
c1 = np.fmin(np.linspace(a1,a1,100), mfs[2][1])
a2_1 = mfs[0][0][initial_values[0] == egf_value] #egf_low[egf == egf_value]
a2_2 = mfs[1][0][initial_values[1] == hrg_value] #hrg_low[hrg == hrg_value]
if( a2_1.size == 0):
a2_1 = mfs[0][0][ find_closest(initial_values[0], egf_value)]
if( a2_2.size == 0):
a2_2 = mfs[1][0][ find_closest(initial_values[1], hrg_value)]
a2 = min(a2_1, a2_2)
c2 = np.fmin(np.linspace(a2,a2,100), mfs[2][0])
c_com = np.fmax(c1, c2)
a = fuzz.defuzz(initial_values[2], c_com, 'lom')
return a
def compute_pi3k(egfr_value, erk_value, initial_values, mfs):
"""Rules ---
if egfr is high and erk is low then pi3k is high
if egfr is low or erk is high then pi3k is low"""
a1_1 = mfs[0][1][initial_values[0] == egfr_value] #egfr_high[egfr == egfr_value]
a1_2 = mfs[1][0][ initial_values[1] == erk_value] #erk_low[erk == erk_value]
if( a1_1.size == 0):
a1_1 = mfs[0][1][ find_closest(initial_values[0], egfr_value)]
if( a1_2.size == 0):
a1_2 = mfs[1][0][ find_closest(initial_values[1], erk_value)]
a1 = min(a1_1 , a1_2)
c1 = np.fmin( np.linspace(a1, a1, 100), mfs[2][1])
a2_1 = mfs[0][0][ initial_values[0] == egfr_value] #egfr_low[egfr == egfr_value]
a2_2 = mfs[1][1][ initial_values[1] == erk_value] #erk_high[erk == erk_value]
if( a2_1.size == 0):
a2_1 = mfs[0][0][ find_closest(initial_values[0], egfr_value)]
if( a2_2.size == 0):
a2_2 = mfs[1][1][ find_closest(initial_values[1], erk_value)]
a2 = max(a2_1 , a2_2)
c2 = np.fmin( np.linspace(a2, a2, 100), mfs[2][0] )
c_com = np.fmax(c1, c2)
return fuzz.defuzz(initial_values[2], c_com, 'lom')
def compute_raf(egfr_value, akt_value, initial_values, mfs):
"""Rules---
If egfr is high or akt is high then raf is high
if egfr is low and akt is low then raf is low"""
a1_1 = mfs[0][1][initial_values[0] == egfr_value] #egfr_high[egfr == egfr_value]
a1_2 = mfs[1][1][initial_values[1] == akt_value] #akt_high[akt == akt_value]
if( a1_1.size == 0):
a1_1 = mfs[0][1][ find_closest(initial_values[0], egfr_value)]
if( a1_2.size == 0):
a1_2 = mfs[1][1][ find_closest(initial_values[1], akt_value)]
a1 = max( a1_1 , a1_2 )
#print egfr_value
c1 = np.fmin( np.linspace(a1, a1, 100), mfs[2][1])
a2_1 = mfs[0][0][initial_values[0] == egfr_value] #egfr_low[egfr == egfr_value]
a2_2 = mfs[1][0][initial_values[1] == akt_value] #akt_low[akt == akt_value]
if( a2_1.size == 0):
a2_1 = mfs[0][0][ find_closest(initial_values[0], egfr_value)]
if( a2_2.size == 0):
a2_2 = mfs[1][0][ find_closest(initial_values[1], akt_value)]
a2 = min(a2_1 ,a2_2 )
c2 = np.fmin( np.linspace(a2, a2, 100), mfs[2][0])
c_com = np.fmax(c1, c2)
return fuzz.defuzz(initial_values[2], c_com, 'lom')
def rules(initial_cond, (initial_values, mfs)):
y = np.copy(initial_cond)
y[2] = compute_egfr(initial_cond[0], initial_cond[1], (initial_values[0], initial_values[1], initial_values[2]), (mfs[0], mfs[1], mfs[2]))
y[3] = compute_raf(initial_cond[2], initial_cond[6], (initial_values[2], initial_values[6], initial_values[3]), (mfs[2], mfs[6], mfs[3]))
y[4] = compute_pi3k(initial_cond[2], initial_cond[5], (initial_values[2], initial_values[5], initial_values[4]), (mfs[2], mfs[5], mfs[4]))
y[5] = initial_cond[3]
y[6] = initial_cond[4]
return y
def main():
#Universal sets
#c_egfr_aggregated = compute_initial_egfr(egf, hrg, egfr)
#c_raf_aggregated = compute_initial_raf(egfr, akt, raf)
#c_pi3k_aggregated = compute_initial_pi3k(egfr, erk, pi3k)
initial_cond = np.array([1, 1, 0, 0, 0, 0, 0], dtype = "float64")
time_stop = 10
y = np.copy(initial_cond)
step = 1
egf = np.linspace(0,1,100)
hrg = egf
egfr = egf
akt = egf
raf = egf
pi3k = egf
erk = egf
vals = (egf, hrg, egfr, raf, pi3k, erk, akt)
mfs = eval_membership_functions(vals)
while(step < time_stop + 1):
#global vals
if(step == 1):
temp = rules(y, (vals, mfs))
else:
temp = rules( y[ step - 1, : ], (vals, mfs))
y = np.vstack((y, temp))
step += 1
print y
plt.title("Synch")
lines = plt.plot(np.arange(time_stop + 1), y[:,5], np.arange(time_stop + 1), y[:,6])
plt.setp(lines[0],antialiased = False,color ='#000000',linewidth = 2, marker = "o", markeredgecolor = 'green', label = "erk")
plt.setp(lines[1],antialiased = False,color ='red',linewidth = 2,linestyle = '--', marker = 'D', markeredgecolor = 'blue', markerfacecolor = 'none', label = "akt")
plt.legend(loc='upper right')
plt.xlabel('Time')
plt.ylabel('Species')
plt.axis([0,11,-0.05,1.2])
plt.grid(True)
plt.show()
if __name__ == "__main__":
main()