SortedSet not removing some elements
I am trying to implement the A* pathfinding algorithm, everything is good so far except one this, SortedSet not removing some element. It removes element, but some element is not removing and because of it the loop is going forever.
while (openNodes.Count > 0)
{
currentPoint = openNodes.Min;
currentPoint.SetIsSearched(true);
openNodes.Remove(currentPoint);
if (openNodes.Contains(currentPoint))
{
throw new Exception("MalFunctioning Detected...");
}
// Rest of the code
}
My WayPoint class implemented IComparable interface
public int CompareTo(WayPoint other)
{
// Index is the position in grid, grid contains the walkable and non walkable node,
// this class purpose is to hold data until shorted path found
// If both points are at same location they are same
if (other.Index.x == Index.x && other.Index.y == Index.y) return 0;
else if (F < other.F || F == other.F && H < other.H) return -1;
else return 1;
}
I'm sure nothing other than this is wrong with my code since, using List makes it run and finding path as expected. I'm so frustrated by this, it's appreciable if anyone can explain to me what and why this happening.
Well, comparison must be antisymmetric, i.e. if
then
for all
AandB. You break this rule; ifthisandotherare not equal (i.e.Index.xor/andIndex.yare not equal)in case
this.F == other.F && H == other.H1is returned and we havethis > other; if we swapthisandotherwe expect-1to be returned, but we sill get1.You, probably, want