A*算法求解迷宫寻路问题实验

  • 任务要求
  • 参考答案
  • 评论

任务描述

本关任务:编写一个使用 A* 搜索算法完成求解迷宫问题最优解的小程序。

相关知识

为了完成本关任务,你需要掌握:

1.A*算法的原理和步骤;

2.解决迷宫问题的思路。

A* 搜索算法简介

A* 搜索算法是一种启发式搜索算法。所谓启发式搜索算法,就是在盲目搜索算法中加入一个启发函数,在当前节点搜索完毕后,通过这个启发函数来进行计算,选择代价最少的节点作为下一步搜索的节点。通过这样的方式就能够找到最优解。DFSBFS这两种搜索方式都属于盲目的搜索方式,它不会在选择下一个节点的时候进行代价计算,而是按照一个固定的方式选择,这样在运气不好的情况,会对所有节点进行遍历。

A* 搜索算法的核心就在于如何设计一个好的启发函数,启发函数的表达形式为:f(n)=g(n)+h(n)。其中f(n)是每个节点可能的估值或者代价值,它由两部分组成:

  • g(n):它表示从起点到搜索点的代价;

  • h(n):它表示从搜索点到目标点的代价,h(n)设计的好坏,直接影响到该 A* 算法的效率。

A* 算法步骤

OPEN 表:保留所有已生成而未扩展的状态; CLOSED 表:记录已扩展过的状态。

A* 算法求解迷宫问题

迷宫问题指的是在一个n×m的迷宫里,入口坐标和出口坐标分别为(startx,starty)(endx,eny),每一个坐标点有两种可能:0或1,其中0表示该位置允许通过,1表示该位置不允许通过。求解目标是找到一个最短路径从入口到出口。

在采用A*算法对迷宫路径求解中,g(n)和h(n)函数都采用曼哈顿距离,曼哈顿距离代表两个点在标准坐标系上的绝对轴距总和。计算公式为:d(i,j)=∣x1−x2∣+∣y1−y2∣

即每次获取的当前通道块,都会对其到入口通道块和出口通道块的曼哈顿距离进行计算。因此,我们定义估价函数为:f(n)=g(n)+h(n),式中,g(n)为起点到n状态的最短路径代价的估计值,我们使用起点到n状态的曼哈顿距离表示;而h(n)是n状态到目的状态的最短路径代价的估计值,由于在搜索过程中,每次只走一步,我们姑且设置h(n)为1。综上,可得到估价函数的代码定义为:

 
  1. # 曼哈顿距离比较 f = g + h,h=1
  2. def getFunctionValue(self, end):
  3. dist = abs(self.x - end.x) + abs(self.y - end.y)
  4. return dist + 1

迷宫寻路问题的目标是:如何在最短的路径基础上从起始点到目的地。

输入:

 
  1. [[0, 0, 0, 0, 0],
  2. [1, 0, 1, 0, 1],
  3. [0, 0, 0, 0, 1],
  4. [0, 1, 0, 0, 0],
  5. [0, 0, 0, 1, 0]]

预期结果:最短路径为 8,具体如下,

 
  1. * * 0 0 0
  2. 1 * 1 0 1
  3. 0 * * 0 1
  4. 0 1 * * *
  5. 0 0 0 1 *

其中,*表示走过的路径。

编程要求

根据提示,在右侧 vscode 编辑器补充代码,参考寻路过程中向左、向右、向上三个方向的移动方式,完成向下的移动代码的编写,达到通过使用 A* 搜索算法完成求解迷宫寻路问题的最短路径。

编程路径:/data/workspace/myshixun/step1/test.py

测试说明

平台会对你编写的代码进行测试:

测试输入:

预期输出:

 
  1. Best Way:
  2. * * 0 0 0
  3. 1 * 1 0 1
  4. 0 * * 0 1
  5. 0 1 * * *
  6. 0 0 0 1 *
  7. Total steps is 8

代码部分

这里的往下走部分可以参考往上走和往右走的逻辑来仿写,毕竟有很多部分都是相同的就是要注意x轴和y轴的区别

import numpy as np


class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.f = 0

    def setF(self, f):
        self.f = f

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

        
    ########## Begin ##########
    # 曼哈顿距离比较  f = g + h,h=1
    def getFunctionValue(self, end):
        dist = abs(self.x - end.x) + abs(self.y - end.y)
        return dist + 1

    ########## Eed ##########



class State:
    def __init__(self, state, current_point=Point(0, 0), end_point=Point(0, 0)):
        self.state = state
        self.cP = current_point
        self.eP = end_point

    def __eq__(self, other):
        return self.cP == other.cP

    def setF(self, f):
        self.f = f

    def setCurrentPoint(self, x, y):
        self.cP.x = x
        self.cP.y = y

    def getCurPoint(self):
        return self.cP.x, self.cP.y


# 确定下一步的方法
def nextStep(map, openTable, closeTable, wrongTable):
    subPoints = []
    boarder = len(map.state) - 1
    # 获取当前所在的点
    x, y = map.getCurPoint()
    # 往左走
    if y > 0 and map.state[x][y - 1] == 0:
        p = Point(x, y - 1)
        if p not in closeTable and p not in wrongTable:
            # 添加到可以走的list
            openTable.append(p)
            # new point
            # 获取F函数值
            p.setF(p.getFunctionValue(map.eP))
            subPoints.append(p)
    # 往上走
    if x > 0 and map.state[x - 1][y] == 0:
        p = Point(x - 1, y)
        if p not in closeTable and p not in wrongTable:
            openTable.append(p)
            p.setF(p.getFunctionValue(map.eP))
            subPoints.append(p)

    # 任务:完成往下走的代码逻辑
    ########## Begin ##########
    if x < boarder and map.state[x + 1][y] == 0:
            p = Point(x + 1, y)
            if p not in closeTable and p not in wrongTable:
                openTable.append(p)
                p.setF(p.getFunctionValue(map.eP))
                subPoints.append(p)


    ########## Eed ##########

    # 往右走
    if y < boarder and map.state[x][y + 1] == 0:
        p = Point(x, y + 1)
        if p not in closeTable and p not in wrongTable:
            openTable.append(p)
            p.setF(p.getFunctionValue(map.eP))
            subPoints.append(p)
    #   根据F值排序,获取F值最近的
    subPoints.sort(key=compareF)
    if len(subPoints) < 1:
        # 防止走到死路无法回头情况
        wrongTable.append(Point(map.cP.x, map.cP.y))
        closeTable.remove(map.cP)
        next_point = closeTable[-1]
        map.cP.x, map.cP.y = next_point.x, next_point.y
    else:
        next_point = subPoints[0]
        map.cP.x, map.cP.y = next_point.x, next_point.y
        closeTable.append(next_point)
        openTable.remove(next_point)


# 迭代走下一步
def solve(map, openTable, closeTable, wrongTable):
    # start the loop
    while not map.cP == map.eP:
        nextStep(map, openTable, closeTable, wrongTable)


def compareF(p):
    return p.f


# 展示最后结果
def showInfo(map, path):
    for i in range(len(map.state)):
        for j in range(len(map.state)):
            if Point(i, j) in path:
                # 正确路径用‘*’表示
                print('*', end='  ')
            else:
                print(map.state[i, j], end='  ')
        print("\n")
    return


if __name__ == '__main__':
    # openList
    openTable = []
    # closeList
    closeTable = []
    # 走错路返回用的
    wrongTable = []
    state = np.array([[0, 0, 0, 0, 0], [1, 0, 1, 0, 1], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 0, 1, 0]])
    # 起点终点
    start_point = Point(0, 0)
    end_point = Point(4, 4)
    # 最终路径
    path = [start_point]
    Map = State(state, Point(0, 0), end_point)
    solve(Map, openTable, closeTable, wrongTable)
    print('Best Way:')
    path = path + closeTable
    showInfo(Map, path)
    print("Total steps is %d" % (len(path) - 1))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值