3

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

Sumanth
  • 31
  • 3
  • 6
    So what would the result be in your example? – L3viathan Aug 29 '17 at 14:19
  • Output for the sum is ​{'0':​ ​0.7,​ ​'1':​ ​0.6,​ ​'2':​ ​0.5} @L3viathan – Sumanth Aug 29 '17 at 14:23
  • For squaring, I used a different fuzzy set, C = {'-1':1, '0':0.4, '1':0.2, '2':0.5} D = {'-1':0.5, '0':0.8, '1':1, '4':0.4} I want to find C^2 + D^2? Required output is ​{'2':​ ​1,​ ​'1':​ ​0.8,​ ​'17':​ ​0.4,​ ​'0':​ ​0.4,​ ​'16':​ ​0.4,​ ​'5':​ ​0.5,​ ​'4':​ ​0.5, '20':​ ​0.4} Thanks in advance. @L3viathan – Sumanth Aug 29 '17 at 14:24
  • What meaning do the "member elements" have, then? Why don't you store it in a list if you throw away the keys afterwards? – L3viathan Aug 29 '17 at 14:24
  • `dict`s are not ordered, so are you sorting on key to add? Or how else are you relating `'3'` to `'6'`? – AChampion Aug 29 '17 at 14:24
  • use dict.items()! @AChampion – Sumanth Aug 29 '17 at 14:27
  • @MaheshK order is not guaranteed with `dict`s so you could get completely different results depending on what is in the `dict`. – AChampion Aug 29 '17 at 14:28
  • ...unless you are using Python 3.6, in which case order is retained (despite still not being guaranteed) – L3viathan Aug 29 '17 at 14:29
  • 2
    Your example output does not show that and http://reference.wolfram.com/legacy/applications/fuzzylogic/Manual/9.html shows element wise addition of the sets. Python has unordered sets and dicts - bruh! – AChampion Aug 29 '17 at 14:32
  • I suggest you write your own code, SO isn't a code writing service, I'm more than happy to help when you show what you have attempted, as that will perhaps show how you get to your expected output. – AChampion Aug 29 '17 at 14:43

1 Answers1

1

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}
bendl
  • 1,583
  • 1
  • 18
  • 41
  • How exactly is modulo defined in this context? Modulo of each individual element or something different? – bendl Aug 29 '17 at 14:37
  • each individual element..@bendl – Sumanth Aug 29 '17 at 14:39
  • This doesn't produce the suggested output for addition: `​{'0':​ ​0.7,​ ​'1':​ ​0.6,​ ​'2':​ ​0.5}` – AChampion Aug 29 '17 at 14:42
  • I missed that comment... @MaheshK if you have extra information about the question, please edit it into the question rather than commenting. I now have no idea what you mean by a fuzzy set. That output makes no sense – bendl Aug 29 '17 at 14:46