Interface IHeuristicStrategy
- All Known Implementing Classes:
LinearHeuristicStrategy,SquaredHeuristicStrategy
A heuristic strategy provides two key functions:
- Computing the heuristic value (estimated cost) from a position to the target
- Calculating the actual transition cost between two adjacent positions
The heuristic function is crucial for guiding the A* algorithm towards the target efficiently. It must be admissible (never overestimate the actual cost) to guarantee optimal pathfinding results.
- Since:
- 5.3.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondoublecalculate(HeuristicContext heuristicContext) Calculates the heuristic value (estimated cost) from the current position to the target.doublecalculateTransitionCost(PathPosition from, PathPosition to) Calculates the actual transition cost between two adjacent positions.
-
Method Details
-
calculate
Calculates the heuristic value (estimated cost) from the current position to the target.This method computes an estimate of the remaining cost to reach the target from the current position. The heuristic should be admissible (never overestimate the actual cost) to ensure optimal pathfinding results.
Common heuristic functions include:
- Manhattan distance for grid-based movement
- Euclidean distance for free movement
- Octile distance for diagonal movement
- Combined metrics considering elevation and terrain
- Parameters:
heuristicContext- the context containing information about the current position, target position, and other relevant pathfinding data- Returns:
- the estimated cost from the current position to the target, must be non-negative
- Throws:
IllegalArgumentException- if the heuristic context is invalid- See Also:
-
calculateTransitionCost
Calculates the actual transition cost between two adjacent positions.This method determines the cost of moving from one position to another. The cost should reflect the actual difficulty or expense of the movement, taking into account factors such as:
- Distance between positions
- Terrain difficulty
- Elevation changes
- Movement penalties
The transition cost is used to calculate the G-cost (actual cost from start) in the A* algorithm.
- Parameters:
from- the starting position of the movementto- the destination position of the movement- Returns:
- the cost of moving from the 'from' position to the 'to' position, must be positive for valid movements
- Throws:
IllegalArgumentException- if either position is null or invalid- See Also:
-