Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Common Operations on Fuzzy Set with Example and Code

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

What is Fuzzy Set ?

Fuzzy refers to something that is unclear or vague . Hence, Fuzzy Set is a Set where every key is associated with value, which is between 0 to 1 based on the certainty .This value is often called as degree of membership. Fuzzy Set is denoted with a Tilde Sign on top of the normal Set notation.

Operations on Fuzzy Set with Code :

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 :
 

Python3




# Example to Demonstrate the
# Union of Two Fuzzy Sets
A = dict()
B = dict()
Y = dict()
 
A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}
B = {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}
 
print('The First Fuzzy Set is :', A)
print('The Second Fuzzy Set is :', B)
 
 
for A_key, B_key in zip(A, B):
    A_value = A[A_key]
    B_value = B[B_key]
 
    if A_value > B_value:
        Y[A_key] = A_value
    else:
        Y[B_key] = B_value
         
print('Fuzzy Set Union is :', Y)

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 :
 

Python3




# Example to Demonstrate
# Intersection of Two Fuzzy Sets
A = dict()
B = dict()
Y = dict()
 
A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}
B = {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}
 
print('The First Fuzzy Set is :', A)
print('The Second Fuzzy Set is :', B)
 
 
for A_key, B_key in zip(A, B):
    A_value = A[A_key]
    B_value = B[B_key]
 
    if A_value < B_value:
        Y[A_key] = A_value
    else:
        Y[B_key] = B_value
print('Fuzzy Set Intersection is :', Y)

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}

3. Complement :

Consider a Fuzzy Sets denoted by A  , then let’s consider Y be the Complement of it, then for every member of  A  , Y will be:

degree_of_membership(Y)= 1 - degree_of_membership(A)

EXAMPLE :

Python3




# Example to Demonstrate the
# Difference Between Two Fuzzy Sets
A = dict()
Y = dict()
 
A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}
 
print('The Fuzzy Set is :', A)
 
 
for A_key in A:
   Y[A_key]= 1-A[A_key]
         
print('Fuzzy Set Complement is :', Y)

Output

The Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
Fuzzy Set Complement is : {'a': 0.8, 'b': 0.7, 'c': 0.4, 'd': 0.4}

4. Difference :  
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), 1- degree_of_membership(B)) 

EXAMPLE :

Python3




# Example to Demonstrate the
# Difference Between Two Fuzzy Sets
A = dict()
B = dict()
Y = dict()
 
A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}
B = {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}
 
print('The First Fuzzy Set is :', A)
print('The Second Fuzzy Set is :', B)
 
 
for A_key, B_key in zip(A, B):
    A_value = A[A_key]
    B_value = B[B_key]
    B_value = 1 - B_value
 
    if A_value < B_value:
        Y[A_key] = A_value
    else:
        Y[B_key] = B_value
         
print('Fuzzy Set Difference is :', Y)

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 Fuzzy Sets

Python3




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]
       
      print('Result of UNION operation :',self.union_AB)
       
   
  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
       
      print('Result of INTERSECTION operation :\n\t\t',self.intersection_AB)
      self.change_intersection = True
 
  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]
 
      print('Result of COMPLEMENT on ',self.Aname,' operation :',self.complement_A)
      print('Result of COMPLEMENT on ',self.Aname,' operation :',self.complement_B)
 
      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
 
    print('\nSet ',self.Aname,' :',self.A)
    print('Set ',self.Bname,' :',self.B,end='')
 
    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)


My Personal Notes arrow_drop_up
Last Updated : 08 Mar, 2022
Like Article
Save Article
Similar Reads
Related Tutorials