我正在研究一个A-Star算法来解决8道难题。我的大部分算法都失败了,但我在输出方面遇到了问题。它可以很好地解决这个问题[0,1,2,3,8,4,6,7,5],但当涉及到更复杂的问题时,例如[6,4,0,1,5,3,7,8,2],它一直在同一条路径上循环,似乎没有找到解决方案。如果你能给我指导或指出我哪里出错了,请帮忙。非常感谢。

代码如下:

from math import sqrt
import time
#fring- to keep the nodes 
#expand: the function to expand one node at a time 
    #heuristic: calculate the cheapest cost
    #f()- total cost 
    #h()- the number index to the goal 
#expanded_nodes- have all the visited 
startime = time.time_ns()
def astar(puzzle):
    
    #intializing the variables 
    cost = 0
    node = Node(puzzle,cost)
    loop = True
 
    #the possible nodes  
    fringe = []  
    #After expanding the list 
    expanded = []

    # maybe need it keep it for now 
    visit = set() #keep track of all the visit 
    
    #the goal state
    goal = [0,1,2,3,4,5,6,7,8] 
    
    #possible combination move
    possible_move = [[1,3],[0,2,4],[1,5],[0,4,6],[1,3,5,7],[2,4,8],[3,7],[4,6,8],[5,7]]
   
   
   
    #intialization of the first node 
    fringe = [node]
    #print('state:'+str(node.state))
    
    
 
    
    #start the loop 
    while loop:
        print('\nthe state of this game position is:\n ' + str(node.state) +"\n\n")
        
        #find the lowest cost in the fringe then expand the lowest cost node
        min_cost = fringe[0].cost 
        min_node = fringe[0]
        
        for node in fringe[:]:
            if node.cost < min_cost:
                min_cost = node.cost
                min_node = node
                
        #append the node that was just expaneded into the expanded list but keep the list not expaneded in fringe 
        expanded.append(min_node)
        print('the min node '+str(min_node.state)+'\nthe cheapest cost: '+ str(min_cost))
       
        #removes the min cost node from fringe  
        for node in fringe[:]:
            if node.state == min_node.state:
                fringe.remove(node)
        
        #when there is a solution in the expanded 
        for node in expanded:
            if node.state == goal:
                loop = False
       
        #traverse the nodes that was expanded and add the children to the fringe 
        for node in expanded[:]:
          
            #append all the successor/children and set the children's parent to fringe 
            blank = node.state.index(8)
            print('the index of the blank is '+ str(blank))
            print('\n')
            possible_pos = possible_move[blank]
            print('possible pos '+ str(possible_pos))
                
            for i in possible_pos:
                    
                    print('\n')
                    possible_sw = node.state[:]
                    print('index swap = '+ str(i))
                    possible_sw[blank] = possible_sw[i]
                    possible_sw[i] = 8
                    print('the child node is ' + str(possible_sw))
                    node.cost = manhattan(possible_sw, goal)
                    fringe.append(Node(possible_sw,node.cost,node))
                    print('the cost this node state: '+ str(node.cost)) 
        expanded.pop(0)

    #finding the solution  
    move = 0
    while node.parent:
        
        expanded.append(node.state.index(8))
        node = node.parent
        move += 1
    print('moves made '+ str(move))
   
    

    expanded.reverse()
    print('moves list '+ str(expanded))
    endtime = time.time_ns()
    executionTime = ( endtime - startime)
    print('Execution time in ns: ' + str(executionTime))
    return expanded     

#Try the Manhattan Distance
def manhattan(a, b):
        return sum(abs(val1-val2) for val1, val2 in zip(a,b))

class Node:
    def __init__(self,state,cost,parent = None):
        self.parent = parent
        self.state = state
        self.cost = cost
        self.children = []


#test case 
p = [0, 1, 2, 3, 4, 5, 6, 8, 7]
p = [0, 1, 2, 3, 8, 4, 6, 7, 5]
#p= [6, 4, 0, 1, 5, 3, 7, 8, 2]
#p = [1, 8, 2, 0, 3, 5, 6, 4, 7]
print("++++++++++A*++++++++++++")
astar(p)

Python A-Star 8谜题输出问题的更多相关文章

  1. XCode 3.2 Ruby和Python模板

    在xcode3.2下,我的ObjectiveCPython/Ruby项目仍然可以打开更新和编译,但是你无法创建新项目.鉴于xcode3.2中缺少ruby和python的所有痕迹(即创建项目并添加新的ruby/python文件),是否有一种简单的方法可以再次安装模板?我发现了一些关于将它们复制到某个文件夹的信息,但我似乎无法让它工作,我怀疑文件夹的位置已经改变为3.2.解决方法3.2中的应用程序模板

  2. Swift基本使用-函数和闭包(三)

    声明函数和其他脚本语言有相似的地方,比较明显的地方是声明函数的关键字swift也出现了Python中的组元,可以通过一个组元返回多个值。传递可变参数,函数以数组的形式获取参数swift中函数可以嵌套,被嵌套的函数可以访问外部函数的变量。可以通过函数的潜逃来重构过长或者太复杂的函数。

  3. 10 个Python中Pip的使用技巧分享

    众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。本文小编为大家总结了Python中Pip的使用技巧,需要的可以参考一下

  4. Swift、Go、Julia与R能否挑战 Python 的王者地位

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 红薯因 Swift 重写开源中国失败,貌似欲改用 Python

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  6. 你没看错:Swift可以直接调用Python函数库

    上周Perfect又推出了新一轮服务器端Swift增强函数库:Perfect-Python。对,你没看错,在服务器端Swift其实可以轻松从其他语种的函数库中直接拿来调用,不需要修改任何内容。以如下python脚本为例:Perfect-Python可以用下列方法封装并调用以上函数,您所需要注意的仅仅是其函数名称以及参数。

  7. Swift中的列表解析

    在Swift中完成这个的最简单的方法是什么?我在寻找类似的东西:从Swift2.x开始,有一些与你的Python样式列表解析相当的东西。(在这个意义上,它更像是Python的xrange。如果你想保持集合懒惰一路通过,只是这样说:与Python中的列表解析语法不同,Swift中的这些操作遵循与其他操作相同的语法。

  8. swift抛出终端的python错误

    每当我尝试启动与python相关的swift时,我都会收到错误.我该如何解决?

  9. 在Android上用Java嵌入Python

    解决方法看看this,它适用于J2SE,你可以尝试在Android上运行.

  10. 在android studio中使用python代码构建android应用程序

    我有一些python代码和它的机器人,我正在寻找一种方法来使用android项目中的那些python代码.有没有办法做到这一点!?解决方法有两种主要工具可供使用,它们彼此不同:>QPython>Kivy使用Kivy,大致相同的代码也可以部署到IOS.

随机推荐

  1. 如何扩展ATmega324PB微控制器的以下宏寄存器?

    我目前正在学习嵌入式,我有以下练习:展开以下宏寄存器:如果有人解决了这个问题,我将不胜感激,以便将来参考

  2. Python将ONNX运行时设置为返回张量而不是numpy数组

    在python中,我正在加载预定义的模型:然后我加载一些数据并运行它:到目前为止,它仍在正常工作,但我希望它默认返回Tensor列表,而不是numpy数组。我对ONNX和PyTorch都是新手,我觉得这是我在这里缺少的基本内容。这将使转换中的一些开销相同。

  3. 在macOS上的终端中使用Shell查找文件中的单词

    我有一个文本文件,其中有一行:我需要找到ID并将其提取到变量中。我想出了一个RexEx模式:但它似乎对我尝试过的任何东西都不起作用:grep、sed——不管怎样。我的一个尝试是:我为这样一个看似愚蠢的问题感到抱歉,但我在互联网上找不到任何东西:我在SO和SE上读了几十个类似的问题,并在谷歌上搜索了几个教程,但仍然无法找到答案。欢迎提供任何指导!

  4. react-chartjs-2甜甜圈图中只有标题未更新

    我正在使用react-chartjs-2在我的网站中实现甜甜圈图。下面是我用来呈现图表的代码。我将甜甜圈图的详细信息从父组件传递到子组件,所有道具都正确传递。当我在beforeDraw函数外部记录props.title时,它会记录正确的值,但当我在beforeDraw函数内部记录props.title时,它将记录标题的前一个值,从而呈现标题的前值。我在这里做错了什么?

  5. 如何在tkinter中使用Python生成器函数?

    生成器函数承诺使某些代码更易于编写。但我并不总是知道如何使用它们。假设我有一个斐波那契生成器函数fib(),我想要一个显示第一个结果的tkinter应用程序。当我点击“下一步”按钮时,它会显示第二个数字,依此类推。我如何构建应用程序来实现这一点?我可能需要在线程中运行生成器。但如何将其连接回GUI?

  6. 如何为每次提交将存储库历史记录拆分为一行?

    我正在尝试获取存储库的历史记录,但结果仅以单行文本的形式返回给我。

  7. 尝试在颤振项目上初始化Firebase时出错

    当尝试在我的颤振项目上初始化firebase时,我收到了这个错误有人知道我能做什么吗?应用程序分级Gradle插件Gradle项目颤振相关性我已经将firebase设置为Google文档已经在另一个模拟器上尝试过,已经尝试过创建一个全新的模拟器,已经在不同的设备上尝试过了,已经尝试了特定版本的firebase,已经尝试添加但没有任何效果,已经在youtube上看到了关于它的每一个视频,该应用程序在android和iOS两个平台上都抛出了这个错误

  8. 在unix中基于当前日期添加新列

    我试图在unix中基于时间戳列在最后一个单元格中添加一个状态列。我不确定如何继续。

  9. 麦克斯·蒙特利。我一直得到UncaughtReferenceError:当我在终端中写入node-v时,节点未定义

    如果这是您应该知道的,请确认:我已将所有shell更改为默认为zsh。当我在终端中写入node-v时,我一直收到“UncaughtReferenceError:nodeisnotdefined”。但它显示节点已安装。我是个新手,在这方面经验不足。

  10. 如何在前端单击按钮时调用后端中的函数?

    那么如何在后端添加一个新的端点,点击按钮调用这个函数。

返回
顶部