Fuzzy Logic Lab Final
Fuzzy Logic Lab Final
1. Union :
Consider 2 Fuzzy Sets denoted by A and B, then let’s
consider Y be the Union of them, then for every member of A
and B, Y will be:
degree_of_membership(Y)= max(degree_of_membership(A),
degree_of_membership(B))
EXAMPLE :
Output
The First Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
The Second Fuzzy Set is : {'a': 0.9, 'b': 0.9, 'c': 0.4, 'd': 0.5}
Fuzzy Set Union is : {'a': 0.9, 'b': 0.9, 'c': 0.6, 'd': 0.6}
2. Intersection :
Consider 2 Fuzzy Sets denoted by A and B, then let’s
consider Y be the Intersection of them, then for every
member of A and B, Y will be:
degree_of_membership(Y)= min(degree_of_membership(A),
degree_of_membership(B))
EXAMPLE :
# Example to Demonstrate
# Intersection of Two Fuzzy Sets
A = dict()
B = dict()
Y = dict()
Output
The First Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
The Second Fuzzy Set is : {'a': 0.9, 'b': 0.9, 'c': 0.4, 'd': 0.5}
Fuzzy Set Intersection is : {'a': 0.2, 'b': 0.3, 'c': 0.4, 'd': 0.5}
EXAMPLE :
Output
The First Fuzzy Set is : {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}
The Second Fuzzy Set is : {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}
Fuzzy Set Difference is : {"a": 0.1, "b": 0.1, "c": 0.6, "d": 0.5}
class FzSets:
def __init__(self):
self.A = dict()
self.B = dict()
self.complement_A = dict()
self.complement_B = dict()
self.union_AB = dict()
self.intersection_AB = dict()
self.differenceAB = dict()
self.differenceBA = dict()
self.change_union = False
self.change_intersection = False
self.change_complement = False
def __init__(self,A,nA,B,nB):
self.A = A
self.B = B
self.Aname = nA
self.Bname = nB
self.complement_A = dict()
self.complement_B = dict()
self.union_AB = dict()
self.intersection_AB = dict()
self.differenceAB = dict()
self.differenceBA = dict()
self.change_union = False
self.change_intersection = False
self.change_complement = False
def unionOp(self):
if self.change_union:
print('Result of UNION operation :',self.union_AB)
else:
#unionSet = set(self.A.keys()).union(self.B.keys())
sa = set(self.A.keys())
sb = set(self.B.keys())
intersectionSet = set(self.A.keys()).intersection(self.B.keys())
for i in intersectionSet:
self.union_AB[i] = max(self.A[i],self.B[i])
for i in sa-intersectionSet:
self.union_AB[i] = self.A[i]
for i in sb-intersectionSet:
self.union_AB[i] = self.B[i]
def intersectionOp(self):
if self.change_intersection:
print('Result of INTERSECTION operation :\n\t\t',self.intersection_AB)
else:
#unionSet = set(self.A.keys()).union(self.B.keys())
sa = set(self.A.keys())
sb = set(self.B.keys())
intersectionSet = set(self.A.keys()).intersection(self.B.keys())
for i in intersectionSet:
self.intersection_AB[i] = min(self.A[i],self.B[i])
for i in sa-intersectionSet:
self.intersection_AB[i] = 0.0
for i in sb-intersectionSet:
self.intersection_AB[i] = 0.0
def complementOp(self):
if self.change_complement:
print('Result of COMPLEMENT on ',self.Aname,' operation :',self.complement_A)
print('Result of COMPLEMENT on ',self.Bname,' operation :',self.complement_B)
else:
for i in self.A:
self.complement_A[i] = 1 - A[i]
for i in self.B:
self.complement_B[i] = 1 - B[i]
self.change_complement = True
def __oneMinustwo(self,L,R):
minus_d = dict()
Rcomp = dict()
for i in R:
Rcomp[i] = 1 - R[i]
sa = set(L.keys())
sb = set(R.keys())
intersectionSet = sa.intersection(sb) # min( A , complement(B) )
# l - r OR a - b
for i in intersectionSet:
minus_d[i] = min(L[i],Rcomp[i])
for i in sa-intersectionSet:
minus_d[i] = 0.0
for i in sb-intersectionSet:
minus_d[i] = 0.0
return minus_d
def AminusB(self):
self.differenceAB = self.__oneMinustwo(self.A,self.B)
print('Result of DIFFERENCE ',self.Aname,' | ',self.Bname,' operation
:\n\t\t',self.differenceAB)
def BminusA(self):
self.differenceBA = self.__oneMinustwo(self.B,self.A)
print('Result of DIFFERENCE ',self.Bname,' | ',self.Aname,' operation
:\n\t\t',self.differenceBA)
def change_Setz(self,A,B):
self.A = A
self.B = B
self.change_union = True
self.change_intersection = True
self.change_complement = True
print('\t\t\t Cache Reset')
def displaySets(self):
print('\nSet ',self.Aname,' :',self.A)
print('Set ',self.Bname,' :' ,self.B)
Ques 2. Implementation of Membership
functions
Membership functions
Membership functions for fuzzy logic, encoded and plotted in python.
• Pending function
• Lineal function
• Triangle function
• Trapezoidal function
• Generalized bell function
• Gaussian function
membershipFunctions.py
import numpy as np
import skfuzzy as fuzz
def getAxisValues(axis,stepSize):
return np.arange(axis['xmin'], axis['xmax'], stepSize)
pass
Ques 3
Fuzzy Logic Implementation
with Python
• Mamdani systems
• Sugeno
systems
Fuzzy rule-based systems evaluate linguistic if-then rules using
fuzzification, inference and composition. They often produce fuzzy
results that need to be converted to crisp output. Fuzzy results are
made clear by defuzzification. Also there are some defuzzification
methods;
• Center of Sums(COS)
• Weighted Average
• Maxima
Our system consists of 24 rule bases and has a MISO (Multi Input
Single Output) system structure consisting of 6 inputs - single outputs.
Input values of the person; age, blood pressure, cholesterol, blood
sugar, LDL and HDL values. The output value consists of the “risk”
Mamdani Inference Engine and the centroid of
area defuzzification method. We coded this project with Python and
used numpy, skfuzzy, matplotlib.
First of all we need define inputs and output variable ranges with
numpy.
Rule Base
We read global heart health articles for try to choose true rules. By the
way please not forget, these rules are just our choises.
Defuzzification (CoA)
Time to make crisp and sense something. We choose centroid of area
method for defuzzification.
Outputs of Model
The risk of heart disease is calculated by applying the necessary
procedures to the information received from the user. I tried our model
with these data and It calculated Coroner Heart Diagnosis value is
4.386973. So in this example, this people has no risk for Coroner Heart
Disease.
Here is output diagrams;