ai-22 / src / am / aua / search / BestFirstFrontier.java
BestFirstFrontier.java
Raw
package am.aua.search;

import java.util.Comparator;
import java.util.PriorityQueue;

public class BestFirstFrontier implements Frontier {
    private final NodeFunction f;
    private final PriorityQueue<Node> frontier;

    // statistics
    private int stretchFactor;

    public BestFirstFrontier (NodeFunction f) {
        this.f = f;
        frontier = new PriorityQueue<>(new BestFirstComparator());
    }

    @Override
    public void add(Node node) {
        node.value = f.eval(node);
        frontier.add(node);
        stretchFactor = Math.max(stretchFactor, frontier.size());
    }

    @Override
    public Node remove() {
        return frontier.remove();
    }

    @Override
    public void clear() {
        frontier.clear();
    }

    @Override
    public boolean isEmpty() {
        return frontier.isEmpty();
    }

    @Override
    public int stretchFactor() {
        return stretchFactor;
    }

    @Override
    public String toString() {
        return "Best First";
    }

    private static class BestFirstComparator implements Comparator<Node> {

        @Override
        public int compare(Node node1, Node node2) {
            return node1.value - node2.value;
        }
    }
}