首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AI:图搜索和带曼哈顿启发式的A*实现

AI:图搜索和带曼哈顿启发式的A*实现
EN

Stack Overflow用户
提问于 2013-05-21 15:43:45
回答 1查看 2K关注 0票数 1

弗洛伦萨大学AI课程的This is my project。我必须解决一个经典的游戏:滑动拼图与8和15个细胞。这是我的通用图搜索算法的实现:

代码语言:javascript
复制
public abstract class GraphSearch implements SearchAlgorithm {

protected Queue<Node> fringe;
protected HashSet<Node> closedList;

public GraphSearch() {
    fringe = createFringe();
    closedList = new HashSet<Node>(100);
}

protected abstract Queue<Node> createFringe();

public int getNodeExpanded() {
    return closedList.size();
}

@Override
public Solution search(Puzzle puzzle) {
    fringe.add(new Node(puzzle.getInitialState(), null, null));
    while (!fringe.isEmpty()) {
        Node selectedNode = fringe.poll();
        if (puzzle.getGoalTest().isGoalState(selectedNode.getState())) {
            return new Solution(selectedNode, getNodeExpanded());
        }
        closedList.add(selectedNode);
        LinkedList<Node> expansion = selectedNode.expandNode();
        for (Node n : expansion) {
            if (!closedList.contains(n) && !fringe.contains(n))
                fringe.add(n);
        }
    }
    return new Solution(null, getNodeExpanded());
}

}

这是我的A*代码:

代码语言:javascript
复制
public class AStar extends GraphSearch implements InformedSearch {

private Heuristic heuristic;

public AStar(Heuristic heuristic) {
    setHeuristic(heuristic);
}

public Heuristic getHeuristic() {
    return heuristic;
}

@Override
public void setHeuristic(Heuristic heuristic) {
    this.heuristic = heuristic;
}

@Override
protected Queue<Node> createFringe() {
    return new PriorityQueue<Node>(1000, new Comparator<Node>() {

        @Override
        public int compare(Node o1, Node o2) {
            o1.setH(heuristic.h(o1));
            o2.setH(heuristic.h(o2));
            o1.setF(o1.getG() + o1.getH());
            o2.setF(o2.getG() + o2.getH());
            if (o1.getF() < o2.getF())
                return -1;
            if (o1.getF() > o2.getF())
                return 1;
            return 0;
        }
    });
}

}

这是我在曼哈顿的启发式代码:

代码语言:javascript
复制
    @Override
public int h(Node n) {
    int distance = 0;
    ArrayList<Integer> board = n.getState().getBoard();
    int[][] multiBoard = new int[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            multiBoard[i][j] = board.get(i * N + j);
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int value = multiBoard[i][j];
            if (multiBoard[i][j] != 0) {
                int targetX = (value - 1) / N;
                int targetY = (value - 1) % N;
                distance += Math.abs(i - targetX) + Math.abs(j - targetY);
            }
        }
    }
    return distance;
}

现在,代码工作并找到了难题的解决方案(难题状态是N*N值的数组,GoalState是1, 2, 3, 4, 5, 6, 7, 8, 9 ,0,空单元格= 0),但是与其他程序(相同的算法和相同的启发式)相比,我的程序扩展了不同的节点数。我认为在一般的图形search...any想法有一个问题?:D谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-21 16:01:15

你的启发式计算是错误的。假设9位于棋盘的索引4处。您将其rowRight值计算为3而不是2。这将导致您的算法具有超最佳的性能。您的行右侧计算应为:

int rowRight = (valueFound - 1) / N;

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16664676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档