Center of gravity not working for singleton function
fbous opened this issue · 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?
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.
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.