-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuzzyfication.py
More file actions
78 lines (62 loc) · 2.76 KB
/
Fuzzyfication.py
File metadata and controls
78 lines (62 loc) · 2.76 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
from KnowledgeBase import Rulebase
class FuzzyInterface:
def __init__(self):
self.lv={}
self.inst_var={}
def addlv(self, quality,label,membership_params):
if quality not in self.lv.keys():
self.lv[quality]={}
self.lv[quality][label]=membership_params
else:
self.lv[quality][label]=membership_params
def fuzzify(self,x,quality, type_fun, switch=True):
massimo=0
variabile_linguistica=None
for attribute, lv in self.lv.items():
if attribute == quality:
for label, cordinate in lv.items():
match type_fun:
case 'triangular':
if quality not in self.inst_var.keys():
self.inst_var[quality]={}
a,b,c= cordinate
if x <= a:
calc=0
self.inst_var[quality][label] = calc
elif x<=b and x >= a:
calc=round((x-a)/(b-a),2)
self.inst_var[quality][label]=calc
elif x >= b and x <=c:
calc=round((c-x)/(c-b),2)
self.inst_var[quality][label]=calc
if x >= c:
calc=0
self.inst_var[quality][label] = calc
case 'trapezoidal':
a,b,c,d= cordinate
if x <= a:
calc=0
self.inst_var[quality][label] = calc
elif a <= x and x <= b:
calc=round((x-a)/(b-a),2)
self.inst_var[quality][label]=calc
elif b <= x and x<=c:
self.inst_var[quality][label] =1
elif c <= x and x<= d:
calc=round((d-x)/(d-c),2)
self.inst_var[quality][label]=calc
elif d <= x:
calc=0
self.inst_var[quality][label] = calc
if switch == False:
pass
else:
self.clean(quality)
def clean(self, qual):
counter=0
for quality, inputs in self.inst_var.items():
if qual == quality:
for label, value in sorted(inputs.items(), key=lambda x: x[1], reverse=True):
if counter >= 1:
self.inst_var[quality][label]=0
counter+=1