【问题标题】:Path finding: A star not same length from A to B than from B to A寻路:一颗星从 A 到 B 的长度与从 B 到 A 的长度不同
【发布时间】:2016-11-22 05:12:38
【问题描述】:

我正在为 8 谜题实现具有曼哈顿距离的 A 星算法。 [解决方案是螺旋形式]

1 2 3
8 0 4
7 6 5

在某些情况下,从 A 到 B 的步数与从 B 到 A 的步数不同。

我认为这是因为它不会在打开列表中选择相同的状态,当它们具有相同的成本时,因此不会扩展相同的节点。

来自

 7 6 4 
 1 0 8 
 2 3 5



 (A -> B)

 7 6 4 
 1 8 0 
 2 3 5 

 (B -> A)

 7 6 4 
 1 3 8 
 2 0 5

两者使用曼哈顿距离具有相同的值。 我应该探索所有具有相同价值的路径吗? 还是我应该改变启发式以进行某种决胜局?

这里是代码的相关部分

 def solve(self):
    cost = 0
    priority = 0
    self.parents[str(self.start)] = (None, 0, 0)
    open = p.pr() #priority queue
    open.add(0, self.start, cost)
    while open:
       current = open.get()
       if current == self.goal:
        return self.print_solution(current)
       parent = self.parents[str(current)]
       cost = self.parents[str(current)][2] + 1
       for new_state in self.get_next_states(current):
         if str(new_state[0]) not in self.parents or cost < self.parents[str(new_state[0])][2]:
           priority = self.f(new_state) + cost
           open.add(priority, new_state[0], cost)
           self.parents[str(new_state[0])] = (current, priority, cost)

【问题讨论】:

  • 我不确定我是否理解您在示例中所说的 A -&gt; BB -&gt; A 的意思。您是否正在从目标运行搜索到您显示的“发件人”位置?其他图表与这些搜索有何关系?我看不出您的 A* 代码有任何明显错误,但由于它不是完全独立的,我实际上无法运行它来查看是否有任何细微的错误。只要它仍然是admissible,启发式的关系就不应该成为问题。如果有多个解决方案,它可能会找到任何解决方案,但它们的长度都相同。
  • @Blckknght ,是的,这就是我的意思。从目标到我展示的“从”位置。所以我不需要找到管理关系的方法,因为曼哈顿距离是可以接受的,那么我的代码有问题吗?我会清理代码并发布项目的github链接。
  • 我尝试使用 f = 0 而不是曼哈顿距离,并且我尝试删除 if current == self.goal: return self.print_solution(current) 以获得所有可能的解决方案。两者都没有改变。我认为问题在于问题中发布的功能,可能在self.parents中。这是完整的代码:github.com/Sequoya42/automatic-waddle

标签: python-3.x a-star sliding-tile-puzzle


【解决方案1】:

在浪费了这么多时间以多种不同的方式重写我的“解决”函数之后,一无所获, 终于找到问题了。

 def get_next_states(self, mtx, direction):
    n = self.n
    pos = mtx.index(0)
    if  direction != 1 and pos < self.length and (pos + 1) % n: 
      yield (self.swap(pos, pos + 1, mtx),pos, 3)
    if  direction != 2 and pos < self.length - self.n:
      yield (self.swap(pos, pos + n, mtx),pos, 4)
    if  direction != 3 and pos > 0 and pos % n:
     yield (self.swap(pos, pos - 1, mtx),pos, 1)
    if  direction != 4 and pos > n - 1:
     yield (self.swap(pos, pos - n, mtx),pos, 2)

它在这个函数中。最后一个 if 曾经是“if 4 and pos > n:” 所以有未探索的状态.. “-1”需要 2 天

它会教我做更多的单元测试

【讨论】:

    猜你喜欢
    • 2023-01-14
    • 2023-03-10
    • 2021-10-06
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多