amusecode / amuse

Astrophysical Multipurpose Software Environment. This is the main repository for AMUSE
http://www.amusecode.org
Apache License 2.0
154 stars 98 forks source link

'nearest_neighbour' method should not return the same particle #978

Open rieder opened 1 year ago

rieder commented 1 year ago

Currently, the nearest_neighbour method can result in a particle finding itself as its nearest neighbour when checking against another particle set that contains (some of) the same particles:

from amuse.ic.plummer import new_plummer_model
p = new_plummer_model(10)
q = p.copy()
r = q.nearest_neighbour(p)
print(r.key == p.key)

returns

[ True,  True,  True,  True,  True,  True,  True,  True,  True,  True]

There should be an option (probably enabled by default) to ensure a particle with the same key is ignored.

rieder commented 1 year ago

Note that this is not the case when finding nearest neighbours in the same set:

r = q.nearest_neighbour()
print(r.key == p.key)

returns

[False, False, False, False, False, False, False, False, False, False]
stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 28 days if no further activity occurs. Thank you for your contributions.

LourensVeen commented 9 months ago

This sounds like an object/value issue. If particles are values, then two different Particle objects with the same properties should be considered to be identical. (As an example, calendar dates are values because November 29th is November 29th regardless of how many variables you have containing November 29th.) If Particles are objects, then two different Particle objects with the same properties are still different things. (People are objects, because two people with the same date of birth, name, residence, etc. are still two different people.) Objects have an identity independent of their properties, while values haven't.

It seems like Particles are considered objects here, and that makes sense to me actually. As a result, if you copy the particles, you get two different particles in the same place, and since a neighbour is a different particle that is close by, it gets returned. I'd say that that is correct, even if it isn't what you want.

The question is, what were you trying to do?

rieder commented 9 months ago

Maybe in some sense it's expected, but let me give another example. Say I have two lists of people:

Now, I want to see who in the bigger list is the closest neighbour to each of the people in the smaller list. I can't remove them from that list (since they might live close to one of the others), but as-is, the algorithm would return themselves as their closest neighbour, which is not desired. This is analogous to what I was attempting here.

rieder commented 9 months ago

It would be good to at least have an option to check if the nearest neighbour is not (a copy of) the object itself.