com.github.dieterdepaepe.jsearch.problem.npuzzle.ManhattanDistance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsearch-examples Show documentation
Show all versions of jsearch-examples Show documentation
Example problem sets for the JSearch library.
package com.github.dieterdepaepe.jsearch.problem.npuzzle;
import com.github.dieterdepaepe.jsearch.search.constructive.Heuristic;
import com.github.dieterdepaepe.jsearch.search.constructive.cost.IntegerCost;
/**
* Manhattan distance heuristic for N-Puzzle. It estimates the remaining cost as the total distance that each field
* in the puzzle still has to move, ignoring the limitation that fields may only move by switching with the empty field.
* @author Dieter De Paepe
*/
public class ManhattanDistance implements Heuristic {
@Override
public IntegerCost estimateRemainingCost(PuzzleSearchNode node, PuzzleEnvironment environment) {
SlidingPuzzle puzzle = environment.getPuzzle();
PuzzleFields targetState = environment.getTargetState();
PuzzleFields currentState = node.getPuzzleFields();
int estimate = 0;
for (int row = 0; row < puzzle.getDimension(); row++)
for (int column = 0; column < puzzle.getDimension(); column++) {
int fieldValue = puzzle.getValue(currentState, row, column);
if (fieldValue == puzzle.getEmptyFieldValue())
continue;
PuzzleIndex targetIndex = puzzle.findValue(targetState, fieldValue);
estimate += Math.abs(targetIndex.row - row) + Math.abs(targetIndex.column - column);
}
return IntegerCost.valueOf(estimate);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy