Fuzzy set addition and squaring

866 views Asked by At

I'm working with fuzzy sets. I was wondering if there are any libraries available for Python? Namely, I'm having trouble adding 2 fuzzy sets and also squaring them. I am storing the fuzzy set in a Python dictionary, the key being the member element and the value is the membership value.

My sets are:

set_A = {'3':0.1, '4': 0.8, '5': 0.5}
set_B = {'6':0.6, '7': 0.2, '8': 0.7}

I want to find out set_A + set_B

and also set_A^2 + set_B^2

1

There are 1 answers

4
bendl On

I don't know for certain if there's not already a library for this, but here's quick and simple class that I think does what you expect:

class Fuzzy_Set:

    def __init__(self, set):
        self.set = set

    def __add__(self, other):
        retset = {}
        for item in set(self.set.keys()).union(set(other.set.keys())):
            retset[item] = self.set.get(item, 0) + other.set.get(item, 0)

        return retset

    def __pow__(self, power, modulo=None):
        if modulo:
            return {k:v**power%modulo for k, v in self.set.items()}
        else:
            return {k:v**power for k, v in self.set.items()}

    def __mod__(self, other):
        return pow(Fuzzy_Set(self.set), 1, other)



if __name__ == '__main__':
    s1 = Fuzzy_Set({'3':0.1, '4': 0.8, '5': 0.5})
    s2 = Fuzzy_Set({'5': .5, '6':0.6, '7': 0.2, '8': 0.7})
    print(s1 + s2)
    print(s1**2)
    print(Fuzzy_Set({'1': 1, '2': 2, '3': 3})%2)

This implements adding and exponentiation and modulo. The output of main is:

{'3': 0.1, '6': 0.6, '5': 1.0, '7': 0.2, '8': 0.7, '4': 0.8}
{'3': 0.010000000000000002, '4': 0.6400000000000001, '5': 0.25}
{'1': 1, '2': 0, '3': 1}