【发布时间】:2012-08-14 16:42:02
【问题描述】:
我正在尝试实现一个程序来解决n-puzzle problem。
我用 Java 编写了一个简单的实现,它的问题状态由表示图块的矩阵表征。我还能够自动生成给出起始状态的所有状态图。然后,在图表上,我可以执行 BFS 来找到目标状态的路径。
但问题是我的内存不足,我什至无法创建整个图表。
我尝试使用 2x2 瓷砖,它可以工作。还有一些 3x3(这取决于起始状态和图中的节点数)。但总的来说这种方式并不适合。
所以我尝试在运行时生成节点,同时搜索。它可以工作,但速度很慢(有时几分钟后它仍然没有结束,我终止了程序)。
顺便说一句:我只给出可解决的配置作为起始状态,我不创建重复的状态。
所以,我无法创建图表。这导致了我的主要问题:我必须实现 A* 算法并且我需要路径成本(即每个节点到起始状态的距离),但我认为我无法在运行时计算它。我需要整个图表,对吗?因为 A* 没有遵循图的 BFS 探索,所以我不知道如何估计每个节点的距离。因此,我不知道如何执行 A* 搜索。
有什么建议吗?
编辑
State:
private int[][] tiles;
private int pathDistance;
private int misplacedTiles;
private State parent;
public State(int[][] tiles) {
this.tiles = tiles;
pathDistance = 0;
misplacedTiles = estimateHammingDistance();
parent = null;
}
public ArrayList<State> findNext() {
ArrayList<State> next = new ArrayList<State>();
int[] coordZero = findCoordinates(0);
int[][] copy;
if(coordZero[1] + 1 < Solver.SIZE) {
copy = copyTiles();
int[] newCoord = {coordZero[0], coordZero[1] + 1};
switchValues(copy, coordZero, newCoord);
State newState = checkNewState(copy);
if(newState != null)
next.add(newState);
}
if(coordZero[1] - 1 >= 0) {
copy = copyTiles();
int[] newCoord = {coordZero[0], coordZero[1] - 1};
switchValues(copy, coordZero, newCoord);
State newState = checkNewState(copy);
if(newState != null)
next.add(newState);
}
if(coordZero[0] + 1 < Solver.SIZE) {
copy = copyTiles();
int[] newCoord = {coordZero[0] + 1, coordZero[1]};
switchValues(copy, coordZero, newCoord);
State newState = checkNewState(copy);
if(newState != null)
next.add(newState);
}
if(coordZero[0] - 1 >= 0) {
copy = copyTiles();
int[] newCoord = {coordZero[0] - 1, coordZero[1]};
switchValues(copy, coordZero, newCoord);
State newState = checkNewState(copy);
if(newState != null)
next.add(newState);
}
return next;
}
private State checkNewState(int[][] tiles) {
State newState = new State(tiles);
for(State s : Solver.states)
if(s.equals(newState))
return null;
return newState;
}
@Override
public boolean equals(Object obj) {
if(this == null || obj == null)
return false;
if (obj.getClass().equals(this.getClass())) {
for(int r = 0; r < tiles.length; r++) {
for(int c = 0; c < tiles[r].length; c++) {
if (((State)obj).getTiles()[r][c] != tiles[r][c])
return false;
}
}
return true;
}
return false;
}
Solver:
public static final HashSet<State> states = new HashSet<State>();
public static void main(String[] args) {
solve(new State(selectStartingBoard()));
}
public static State solve(State initialState) {
TreeSet<State> queue = new TreeSet<State>(new Comparator1());
queue.add(initialState);
states.add(initialState);
while(!queue.isEmpty()) {
State current = queue.pollFirst();
for(State s : current.findNext()) {
if(s.goalCheck()) {
s.setParent(current);
return s;
}
if(!states.contains(s)) {
s.setPathDistance(current.getPathDistance() + 1);
s.setParent(current);
states.add(s);
queue.add(s);
}
}
}
return null;
}
基本上这是我的工作:
- Solver 的 solve 有一个 SortedSet。元素 (States) 根据 Comparator1 进行排序,它计算 f(n) = g(n) + h(n),其中 g(n) 是路径成本,h(n) 是启发式(错位图块的数量)。
- 我给出了起始配置并寻找所有的后继者。
- 如果一个后继者还没有被访问过(即如果它不在全局集States 中),我将它添加到队列和States,将当前状态设置为其父状态,将parent's path + 1 设置为其路径成本.
- 出列并重复。
我认为它应该有效,因为:
- 我保留所有访问过的状态,所以我不会循环。
- 另外,不会有任何无用的优势,因为我会立即存储当前节点的后继节点。例如:如果从 AI 可以到 B 和 C,并且从 BI 也可以到 C,就不会有边 B->C(因为每条边的路径成本为 1,并且 A->B 比 A 便宜->B->C)。
- 每次我选择以最小f(n)扩展路径,符合A*。
但它不起作用。或者至少,几分钟后它仍然找不到解决方案(我认为在这种情况下需要很多时间)。
如果我在执行 A* 之前尝试创建树结构,我会用完构建它的内存。
编辑 2
这是我的启发式函数:
private int estimateManhattanDistance() {
int counter = 0;
int[] expectedCoord = new int[2];
int[] realCoord = new int[2];
for(int value = 1; value < Solver.SIZE * Solver.SIZE; value++) {
realCoord = findCoordinates(value);
expectedCoord[0] = (value - 1) / Solver.SIZE;
expectedCoord[1] = (value - 1) % Solver.SIZE;
counter += Math.abs(expectedCoord[0] - realCoord[0]) + Math.abs(expectedCoord[1] - realCoord[1]);
}
return counter;
}
private int estimateMisplacedTiles() {
int counter = 0;
int expectedTileValue = 1;
for(int i = 0; i < Solver.SIZE; i++)
for(int j = 0; j < Solver.SIZE; j++) {
if(tiles[i][j] != expectedTileValue)
if(expectedTileValue != Solver.ZERO)
counter++;
expectedTileValue++;
}
return counter;
}
如果我使用简单的贪心算法,它们都可以工作(使用曼哈顿距离真的很快(大约 500 次迭代才能找到解决方案),而错位瓷砖的数量大约需要 10k 次迭代)。如果我使用 A*(也评估路径成本),它真的很慢。
比较器是这样的:
public int compare(State o1, State o2) {
if(o1.getPathDistance() + o1.getManhattanDistance() >= o2.getPathDistance() + o2.getManhattanDistance())
return 1;
else
return -1;
}
编辑 3
出了点小错误。我修复了它,现在 A* 可以工作了。或者至少,对于 3x3,如果仅通过 700 次迭代就可以找到最佳解决方案。对于 4x4,它仍然太慢。我将尝试使用 IDA*,但有一个问题:使用 A* 需要多长时间才能找到解决方案?分钟?小时?我把它放了10分钟,它并没有结束。
【问题讨论】:
-
可能是这样的:en.wikipedia.org/wiki/IDA* ?这是 A* 和深度限制搜索 - 您不需要存储您访问过的状态。
-
有一件事要说:tl;dr
-
@nhahtdh 我知道 IDA*,但我必须使用 A*(用于大学作业)。关于存储:起初我需要将它们全部存储以创建图形,这是我内存不足的时候。我什至无法运行 A*!
-
@Simon:不确定,但是 9! (实际上是其中的一半)只有〜360k。您需要找到一种有效存储状态的方案(尽可能接近 360k,或者如果可能的话 180k)。 15(或 16)拼图问题(4x4)不能用纯 A* 解决。
-
@nhahtdh 你确定吗?我在谷歌上看了一下,似乎是有可能的。此外,我必须做的分配清楚地表明“实施 A* 来解决它”。关于存储,我认为我的表述非常简单有效。也许有什么问题,但我不这么认为(使用 2x2 板它可以工作)。我会尽快发布代码。
标签: java search artificial-intelligence graph-algorithm sliding-tile-puzzle