Skip to content

Instantly share code, notes, and snippets.

@jcbribeiro
Created May 15, 2018 11:02
Show Gist options
  • Select an option

  • Save jcbribeiro/3a423c94b6c49a6c15a246cb58242eb6 to your computer and use it in GitHub Desktop.

Select an option

Save jcbribeiro/3a423c94b6c49a6c15a246cb58242eb6 to your computer and use it in GitHub Desktop.
IDAStarSearch
@Override
public Solution search(Problem problem) {
statistics.reset();
stopped = false;
this.heuristic = problem.getHeuristic();
limit = heuristic.compute(problem.getInitialState());
Solution solution;
do {
solution = graphSearch(problem);
} while (solution == null);
return solution;
}
@Override
protected Solution graphSearch(Problem problem) {
newLimit = Double.POSITIVE_INFINITY;
frontier.clear();
frontier.add(new Node(problem.getInitialState()));
while (!frontier.isEmpty() && !stopped) {
Node n = frontier.poll();
if (problem.isGoal(n.getState())) {
return new Solution(problem, n);
}
List<State> successors = problem.executeActions(n.getState());
addSuccessorsToFrontier(successors, n);
computeStatistics(successors.size());
}
limit = newLimit;
return null;
}
@Override
public void addSuccessorsToFrontier(List<State> successors, Node parent) {
for (State successor : successors) {
double g = parent.getG() + successor.getAction().getCost();
if (!frontier.containsState(successor)) {
double f = g + heuristic.compute(successor);
if (f <= limit) {
Node node = new Node(successor, parent, g, f);
if (!node.isCycle()) {
frontier.add(node);
}
} else {
newLimit = Math.min(newLimit, f);
}
} else if (frontier.getNode(successor).getG() > g) {
frontier.removeNode(successor);
frontier.add(new Node(successor, parent, g, g + heuristic.compute(successor)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment