private SearchResult rbfs(Problem p, Node n, double node_f, double fLimit,
int recursiveDepth) {
setMaxRecursiveDepth(recursiveDepth);
// if problem.GOAL-TEST(node.STATE) then return SOLUTION(node)
if (SearchUtils.isGoalState(p, n)) {
return new SearchResult(n, fLimit);
}
// successors <- []
// for each action in problem.ACTION(node.STATE) do
// add CHILD-NODE(problem, node, action) into successors
List<Node> successors = expandNode(n, p);
// if successors is empty then return failure, infinity
if (0 == successors.Count) {
return new SearchResult(null, INFINITY);
}
double[] f = new double[successors.Count];
// for each s in successors do
// update f with value from previous search, if any
int size = successors.Count;
for (int s = 0; s < size; s++) {
// s.f <- max(s.g + s.h, node.f)
f[s] = Math.max(evaluationFunction.f(successors.get(s)), node_f);
}
// repeat
while (true) {
// best <- the lowest f-value node in successors
int bestIndex = getBestFValueIndex(f);
// if best.f > f_limit then return failure, best.f
if (f[bestIndex] > fLimit) {
return new SearchResult(null, f[bestIndex]);
}
// if best.f > f_limit then return failure, best.f
int altIndex = getNextBestFValueIndex(f, bestIndex);
// result, best.f <- RBFS(problem, best, min(f_limit, alternative))
SearchResult sr = rbfs(p, successors.get(bestIndex), f[bestIndex],
Math.min(fLimit, f[altIndex]), recursiveDepth + 1);
f[bestIndex] = sr.getFCostLimit();
// if result != failure then return result
if (sr.getOutcome() == SearchResult.SearchOutcome.SOLUTION_FOUND) {
return sr;
}
}
}