Better search method when searching for longest route. Now shouldn't discard a longer route just because it starts at a lower altitude

This commit is contained in:
Martin Asprusten 2025-07-02 23:30:00 +02:00
parent e5a4e9d716
commit b3b3a5b652

View File

@ -100,7 +100,7 @@ struct CurrentAreaSearch {
std::vector<uint32_t> startNodes; std::vector<uint32_t> startNodes;
size_t startNodeCounter = 0; size_t startNodeCounter = 0;
std::vector<AreaSearchEntry> currentAreaSearchResults; std::vector<AreaSearchEntry> currentAreaSearchResults;
std::set<uint32_t> utilizedNodes; std::map<uint32_t, std::vector<uint32_t>> utilizedNodes;
}; };
// Data // Data
@ -801,18 +801,72 @@ AreaSearchResult continueAreaSearch() {
} }
} }
std::set<uint32_t> path; std::map<uint32_t, std::vector<uint32_t>> path;
path.insert(farthestNode); path[farthestNode] = std::vector<uint32_t>();
uint32_t currentNode = farthestNode; uint32_t currentNode = farthestNode;
while (result.previous.find(currentNode) != result.previous.end()) { while (result.previous.find(currentNode) != result.previous.end()) {
currentNode = result.previous.find(currentNode)->second; currentNode = result.previous.find(currentNode)->second;
path.insert(currentNode); path[currentNode] = std::vector<uint32_t>();
} }
// Check if our path overlaps too much with already travelled paths // Check if our path overlaps too much with already travelled paths
std::vector<uint32_t> overlap; std::map<uint32_t, std::vector<uint32_t>> overlap;
std::set_intersection(path.begin(), path.end(), currentAreaSearch.utilizedNodes.begin(), currentAreaSearch.utilizedNodes.end(), std::back_inserter(overlap)); auto inserterIterator = overlap.begin();
if (overlap.size() < 0.5 * path.size()) { std::set_intersection(
currentAreaSearch.utilizedNodes.begin(),
currentAreaSearch.utilizedNodes.end(),
path.begin(),
path.end(),
std::inserter(overlap, inserterIterator),
currentAreaSearch.utilizedNodes.value_comp()
);
std::map<uint32_t, uint32_t> overlapCounts;
for (auto it = overlap.begin(); it != overlap.end(); it++) {
for (auto startingNodeIt = it->second.begin(); startingNodeIt != it->second.end(); startingNodeIt++) {
uint32_t startingNode = *startingNodeIt;
if (overlapCounts.find(startingNode) == overlapCounts.end()) {
overlapCounts[startingNode] = 0;
}
overlapCounts[startingNode] = overlapCounts[startingNode] + 1;
}
}
auto overlapIterator = overlapCounts.begin();
while (overlapIterator != overlapCounts.end()) {
uint32_t overlapCount = overlapIterator->second;
if (overlapCount < path.size() * 0.5) {
overlapIterator = overlapCounts.erase(overlapIterator);
continue;
}
// In case we overlap more than 50% with another path, check if the current path is longer
uint32_t otherPath = overlapIterator->first;
bool isLongerThanOtherPath = true;
for (auto otherPathIterator = currentAreaSearch.currentAreaSearchResults.begin(); otherPathIterator != currentAreaSearch.currentAreaSearchResults.end(); otherPathIterator++) {
if (otherPathIterator->nodeId != otherPath) {
continue;
}
if (farthestDistance > otherPathIterator->longestRoute) {
currentAreaSearch.currentAreaSearchResults.erase(otherPathIterator);
break;
} else {
isLongerThanOtherPath = false;
break;
}
}
if (isLongerThanOtherPath) {
overlapIterator = overlapCounts.erase(overlapIterator);
continue;
}
// If we got here, we found another path that shares at least 50% of or route points, that is longer than the current route
break;
}
if (overlapCounts.size() == 0) {
AreaSearchEntry searchEntry; AreaSearchEntry searchEntry;
searchEntry.nodeId = currentStartNode; searchEntry.nodeId = currentStartNode;
searchEntry.positionX = startNode.position_x; searchEntry.positionX = startNode.position_x;
@ -829,8 +883,12 @@ AreaSearchResult continueAreaSearch() {
searchEntry searchEntry
); );
for (uint32_t pathNode : path) { for (std::pair<uint32_t, std::vector<uint32_t>> pathNodeEntry : path) {
currentAreaSearch.utilizedNodes.insert(pathNode); uint32_t pathNode = pathNodeEntry.first;
if (currentAreaSearch.utilizedNodes.find(pathNode) == currentAreaSearch.utilizedNodes.end()) {
currentAreaSearch.utilizedNodes[pathNode] = std::vector<uint32_t>();
}
currentAreaSearch.utilizedNodes[pathNode].push_back(currentStartNode);
} }
} }