Skip to content

Commit

Permalink
Stablize sorts used by path finders (#1805)
Browse files Browse the repository at this point in the history
* applied std:tie treat to priority queue for QTPFS
* add comment to avoid future confusion.
  • Loading branch information
lostsquirrel1 authored Dec 16, 2024
1 parent 63b8152 commit e4d0e47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
8 changes: 6 additions & 2 deletions rts/Sim/Path/HAPFS/PathDataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ struct PathNode {


/// functor to define node priority
/// This needs to guarantee that the sorting is stable.
struct lessCost {
inline bool operator() (const PathNode* x, const PathNode* y) const {
return (x->fCost == y->fCost) ? (x->gCost < y->gCost) : (x->fCost > y->fCost);
inline bool operator() (const PathNode* lhs, const PathNode* rhs) const {
// fCost == gCost + hCost.
// When fCosts are the same, prioritize the node closest the goal. Since we don't have hCost to hand, we know
// that hCost == fCost - gCost. This is why we invert the left/right side for the gCost comparison.
return std::tie(lhs->fCost, rhs->gCost, lhs->nodeNum) > std::tie(rhs->fCost, lhs->gCost, rhs->nodeNum);
}
};

Expand Down
19 changes: 11 additions & 8 deletions rts/Sim/Path/QTPFS/PathThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ namespace QTPFS {
};

struct SearchQueueNode {
bool operator < (const SearchQueueNode& n) const { return (heapPriority < n.heapPriority); }
bool operator > (const SearchQueueNode& n) const { return (heapPriority > n.heapPriority); }
bool operator == (const SearchQueueNode& n) const { return (heapPriority == n.heapPriority); }
bool operator <= (const SearchQueueNode& n) const { return (heapPriority <= n.heapPriority); }
bool operator >= (const SearchQueueNode& n) const { return (heapPriority >= n.heapPriority); }

SearchQueueNode(int index, float newPriorty)
: heapPriority(newPriorty)
, nodeIndex(index)
Expand All @@ -105,9 +99,18 @@ namespace QTPFS {
int nodeIndex;
};

/// Functor to define node priority.
/// Needs to guarantee stable ordering, even if the sorting algorithm itself is not stable.
struct ShouldMoveTowardsBottomOfPriorityQueue {
inline bool operator() (const SearchQueueNode& lhs, const SearchQueueNode& rhs) const {
return std::tie(lhs.heapPriority, lhs.nodeIndex) > std::tie(rhs.heapPriority, rhs.nodeIndex);
}
};


// Reminder that std::priority does comparisons to push element back to the bottom. So using
// std::greater here means the smallest value will be top()
typedef std::priority_queue<SearchQueueNode, std::vector<SearchQueueNode>, std::greater<SearchQueueNode>> SearchPriorityQueue;
// ShouldMoveTowardsBottomOfPriorityQueue here means the smallest value will be top()
typedef std::priority_queue<SearchQueueNode, std::vector<SearchQueueNode>, ShouldMoveTowardsBottomOfPriorityQueue> SearchPriorityQueue;

struct SearchThreadData {

Expand Down

0 comments on commit e4d0e47

Please sign in to comment.