Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stablize sorts used by path finders #1805

Merged
merged 8 commits into from
Dec 16, 2024
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