📜  模糊集的常见操作示例和代码

📅  最后修改于: 2022-05-13 01:54:59.750000             🧑  作者: Mango

模糊集的常见操作示例和代码

什么是模糊集?

模糊是指不清楚或含糊不清的东西。因此,模糊集是一个集合,其中每个键都与值相关联,基于确定性在 0 到 1 之间。这个值通常称为隶属度。模糊集在普通集符号之上用波浪号表示。

带代码的模糊集操作:

1.工会:

考虑由 A 和 B 表示的 2 个模糊集,然后让我们认为 Y 是它们的并集,那么对于 A 和 B 的每个成员,Y 将是:

degree_of_membership(Y)= max(degree_of_membership(A), degree_of_membership(B)) 

例子 :

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)


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)


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)


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)


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)


输出
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. 路口:

考虑由 A 和 B 表示的 2 个模糊集,然后让我们认为 Y 是它们的交集,那么对于 A 和 B 的每个成员,Y 将是:

degree_of_membership(Y)= min(degree_of_membership(A), degree_of_membership(B)) 

例子 :

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)
输出
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. 补充:

考虑一个由 A 表示的模糊集,然后让我们认为 Y 是它的补集,那么对于 A 的每个成员,Y 将是:

degree_of_membership(Y)= 1 - degree_of_membership(A)

例子 :

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)
输出
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.区别:
考虑由 A 和 B 表示的 2 个模糊集,然后让我们认为 Y 是它们的交集,那么对于 A 和 B 的每个成员,Y 将是:

degree_of_membership(Y)= min(degree_of_membership(A), 1- degree_of_membership(B)) 

例子 :

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)

输出

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}

类模糊集

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)