fuzzylogic icon indicating copy to clipboard operation
fuzzylogic copied to clipboard

Center of gravity not working for singleton function

Open fbous opened this issue 3 years ago • 4 comments

The center_of_gravity method for sets created with the singleton function does not return the correct centre of gravity.

Steps to reproduce:

>>> from fuzzylogic.classes import D
>>> from fuzzylogic.functions import singleton
>>> D = Domain("D", 0, 1)
>>> D.s = singleton(0.5)
>>> D.s.center_of_gravity  # expected 0.5
0

This cannot work reliably since the centre of gravity is calculated by sampling over the domain and chances are high that the support of the singleton function is never hit. Since the centre of gravity is trivially known for singleton sets, perhaps a feasible approach would be to implement singleton as a subclass of Set with the center_of_gravity method returning the stored value from the constructor?

fbous avatar Aug 12 '22 10:08 fbous

Hmmmm... hold on a second. You created a domain with step-size 1, which constructs an array with values of memberships at those steps, so it's no surprise that 0.5 is never hit.. so, works as intended, so far.

What you are actually proposing is the ability to manipulate the layout of the array more directly and add specific sample-points, I guess. Ideally, singleton should add its value to the domain on assignment somehow, yes? That should be feasible.

amogorkon avatar Aug 12 '22 10:08 amogorkon

Maybe that example was not the best because of the step-size was inappropriate anyway. Here is another example:

>>> from fuzzylogic.classes import Domain
>>> from fuzzylogic.functions import singleton
>>> D = Domain("D", 0, 1000)
>>> D.s1 = singleton(500)
>>> D.s1.center_of_gravity  # expected 500
500.0
>>> D.s2 = singleton(500.1)
>>> D.s2.center_of_gravity  # expected 500.1
0

What you are actually proposing is the ability to manipulate the layout of the array more directly and add specific sample-points, I guess. Ideally, singleton should add its value to the domain on assignment somehow, yes? That should be feasible.

This could solve the problem. However we need to consider how exactly singleton sets should interact with non-singleton sets. If we union a singleton set with a non-null set, should the singleton influence the centre of gravity at all or not? I think strictly speaking it should not so in that case the extra sample point should be removed again.

Another possibility would be to adjust the width of the singleton function to the step-size, such that it always includes exactly one sample point.

fbous avatar Aug 12 '22 14:08 fbous

Hi, thank you for this nice package.

I am very new and I am not coming from an informatics background. Thus, I may be wrong, but isnt the center_of_gravity in the following line a function and not an attribute? While I am exploring your package I get an error saying that you cannot multiply a function with a float. In my local version I changed the line to say: center_of_gravity() and it works (though I expected a different result)

https://github.com/Python-Fuzzylogic/fuzzylogic/blob/1edc8fc37829594f9b73879114d0ffe3342691d9/src/fuzzylogic/classes.py#L447

born2bwater avatar Jul 03 '24 15:07 born2bwater

Hi and thank you :-)

You're absolutely correct. Seems I missed to test that properly when I updated the functionality and added rudimentary caching.

However, since you're new I'll point out (so you don't learn the wrong lesson from my mistake) that methods (functions attached to a class) and attributes are not as distinct in python as you may think. In python, you can replace an attribute behind the scene by a function call. This feature of the language is called a "property". It works nicely for simple lookups and as I recall, the center_of_gravity used to be such a property before I realized that the underlying call can be quite computationally heavy. It is recommended to avoid hiding such heavy calls behind properties in general because users of the lib would rightfully assume light-weight operations when they access attributes, so they might make heavy use of such calls, leading to unexpected performance problems.

amogorkon avatar Jul 03 '24 17:07 amogorkon