Skip to content

Comments

Fix heuristic distance calculation bug in A* algorithm#2

Open
cyrusagent-staging[bot] wants to merge 1 commit intomainfrom
cyrustester/eng-3-find-a-small-improvement
Open

Fix heuristic distance calculation bug in A* algorithm#2
cyrusagent-staging[bot] wants to merge 1 commit intomainfrom
cyrustester/eng-3-find-a-small-improvement

Conversation

@cyrusagent-staging
Copy link

Summary

  • Fixed critical bug in distance_calculation() function where it always returned Manhattan distance regardless of the heuristic parameter
  • The function now properly returns Euclidean distance when requested

Problem

The distance_calculation() function in src/graph.cpp had a logic error where:

  1. When "euclidean" heuristic was specified, it would calculate Euclidean distance
  2. But then it would always overwrite that value with Manhattan distance before returning
  3. This meant the Euclidean heuristic never actually worked

Solution

Added an else block to ensure Manhattan distance is only calculated when the heuristic is not "euclidean". The function now:

  • Calculates and returns Euclidean distance when heuristic == "euclidean"
  • Calculates and returns Manhattan distance otherwise

Changes

File: src/graph.cpp (lines 101-108)

Before:

int Graph::distance_calculation(Node update_node, std::pair<int,int> start, 
                                std::pair<int,int> end, std::string heuristic) {
    if(heuristic == "euclidean") {
        update_node.h = distance_euclidean({update_node.x, update_node.y}, end);
    }
    return update_node.h = distance_manhattan({update_node.x, update_node.y}, end);
}

After:

int Graph::distance_calculation(Node update_node, std::pair<int,int> start, 
                                std::pair<int,int> end, std::string heuristic) {
    if(heuristic == "euclidean") {
        update_node.h = distance_euclidean({update_node.x, update_node.y}, end);
    } else {
        update_node.h = distance_manhattan({update_node.x, update_node.y}, end);
    }
    return update_node.h;
}

Impact

  • Critical: Fixes incorrect A* pathfinding behavior when Euclidean heuristic is selected
  • Severity: High - this bug causes the algorithm to use the wrong heuristic, potentially affecting path quality and performance

Test Plan

  • Build the project successfully
  • Run A* with "euclidean" heuristic and verify it uses Euclidean distance
  • Run A* with "manhattan" heuristic and verify it uses Manhattan distance
  • Compare path results between both heuristics to ensure they differ appropriately

Resolves ENG-3

🤖 Generated with Claude Code

The distance_calculation() function had a critical bug where it always returned Manhattan distance regardless of the heuristic parameter. When "euclidean" was specified, the function would calculate the Euclidean distance but then immediately overwrite it with Manhattan distance before returning.

This fix adds an else block to ensure that Manhattan distance is only calculated when the heuristic is not "euclidean", allowing both heuristics to work correctly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants