Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个8字谜游戏中用Python计算曼哈顿距离_Python_Search_Puzzle_A Star - Fatal编程技术网

在一个8字谜游戏中用Python计算曼哈顿距离

在一个8字谜游戏中用Python计算曼哈顿距离,python,search,puzzle,a-star,Python,Search,Puzzle,A Star,我试图用Python为一个简单的8字谜游戏编写一个简单的a*解算器。 我用以下方式表达了我的比赛目标: goal = [[1, 2, 3], [8, 0, 4], [7, 6, 5]] 我的问题是,我不知道如何为我的目标编写一个简单的曼哈顿距离启发式。我知道它应该被定义为一般状态和目标状态之间距离的总和。我想我应该编写如下代码: def manhattan_distance(state): distance = 0 for x in xrang

我试图用Python为一个简单的8字谜游戏编写一个简单的a*解算器。 我用以下方式表达了我的比赛目标:

goal = [[1, 2, 3],
        [8, 0, 4], 
        [7, 6, 5]]
我的问题是,我不知道如何为我的目标编写一个简单的曼哈顿距离启发式。我知道它应该被定义为一般状态和目标状态之间距离的总和。我想我应该编写如下代码:

def manhattan_distance(state):
    distance = 0
    for x in xrange(3):
        for y in xrange(3):
            value = state[x][y]
            x_value = x
            y_value = y
            x_goal = ...?
            y_goal = ...?
            distance += abs(x_value - x_goal) + abs(y_value - y_goal)
    return distance
我的问题是,在目标状态下,我没有一个明确的工件坐标表示,因此我不知道如何为电路板的“值”工件定义“x_目标”和“y_目标”。我试着用除法和模运算,但这很难

你能给我一些提示来定义我的“x_目标”和“y_目标”变量吗


谢谢

曼哈顿距离是道路上的出租车距离,类似于曼哈顿的距离。你的公式是对的

distance += abs(x_value - x_goal) + abs(y_value - y_goal)
其中,
x\u值,y\u值
是您所在的位置,
x\u目标,y\u目标
是您想要去的位置

使用此启发式:当前位置中每个“12346578”的索引定义的点与目标中每个“12346578”的索引定义的点之间的mhd

def h(self, node):
    """Heuristic for 8 puzzle: returns sum for each tile of manhattan
    distance between it's position in node's state and goal"""
    sum = 0
    for c in '12345678':
        sum =+ mhd(node.state.index(c), self.goal.index(c))
    return sum

我还没试过。也许link会有所帮助。

我遇到了与您完全相同的问题,我通过编写一个不同的函数来解决它,该函数接受您已有的表示,并将其转换为您确定的表示(值/坐标对词典)


这样,你就能两全其美。当您想将网格视为网格时,可以使用原始列表表单,但如果您只需要快速查找曼哈顿距离函数的值所在位置,则可以使用您创建的新词典。

您可能可以使用它

def manhatan_dist(board,goal_stat):
    #Manhattan priority function. The sum of the Manhattan distances 
    #(sum of the vertical and horizontal distance) from the blocks to their goal positions, 
    #plus the number of moves made so far to get to the search node.
    import math
    b = board
    g = goal_stat

    manh_dist = 0
    for i in range (0,3,1):
        for j in range (0,3,1):
            bij = b[i][j]
            i_b = i
            j_b = j

            i_g, j_g = value_index(g,bij) 

            manh_dist += (math.fabs(i_g - i_b) + math.fabs(j_g - j_b))

    return manh_dist

您可以找到的大多数pythonic实现

假设

0112

3 4 5

678

是目标状态。。。 以及

1 5 3

4 2 6

7 8 9

是最终状态

initial_state = [1,5,3,4,2,6,7,8,0]
goal_state = [0,1,2,3,4,5,6,7,8]
def calculateManhattan(initial_state):
    initial_config = initial_state
    manDict = 0
    for i,item in enumerate(initial_config):
        prev_row,prev_col = int(i/ 3) , i % 3
        goal_row,goal_col = int(item /3),item % 3
        manDict += abs(prev_row-goal_row) + abs(prev_col - goal_col)
    return manDict

我不知道该怎么解释。它只是工作。享受吧D

使用除数和模运算符是正确的。在编写任何代码之前,试着在纸上算出公式。我见过其他使用除法和模运算的实现,但它们以不同的方式定义目标状态。我的问题是,我在目标状态的第二行和第三行的元素之间找不到任何共同点……如何:重新标记这些片段,使目标为012345678(更容易思考)。解决(课本)。还原原始标签。我已将目标状态的表示形式更改为标签及其坐标的字典。我不知道是否有更好的解决方案,但现在它起作用了。无论如何谢谢你!我知道这是可行的,但像这样的方法比我试图编码的方法复杂得多……什么是
value\u index
initial_state = [1,5,3,4,2,6,7,8,0]
goal_state = [0,1,2,3,4,5,6,7,8]
def calculateManhattan(initial_state):
    initial_config = initial_state
    manDict = 0
    for i,item in enumerate(initial_config):
        prev_row,prev_col = int(i/ 3) , i % 3
        goal_row,goal_col = int(item /3),item % 3
        manDict += abs(prev_row-goal_row) + abs(prev_col - goal_col)
    return manDict