如何在不手动编写障碍的情况下将障碍分布到我的网格?

用户11791503

我正在研究 A 星算法,正如我下面的代码所示,网格是手动编写的,我正在考虑制作一个 100* 100 大小的网格。因此,手动编写它们会非常糟糕。我需要将起点放在 (0,0) 位置,将目标点放在 (99,99) 位置。

我正在尝试使用下面的这一行制作网格

grid1 = [[0 for i in range(100)]for j in range(100)]

但是如何在不触及起点和目标点的位置的情况下随机或不随机地为该网格分配障碍物?

这是在我的代码下面:

from __future__ import print_function
import random

grid = [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 1, 0]]

'''
heuristic = [[9, 8, 7, 6, 5, 4],
             [8, 7, 6, 5, 4, 3],
             [7, 6, 5, 4, 3, 2],
             [6, 5, 4, 3, 2, 1],
             [5, 4, 3, 2, 1, 0]]'''

init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x] 
cost = 1

drone_h = 60

#the cost map which pushes the path closer to the goal
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])):            
        heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])

        #if grid[i][j] == 1:
            #heuristic[i][j] = 99 #added extra penalty in the heuristic map
print(heuristic)
elevation = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])): 
        if grid[i][j] == 1:
            elevation[i][j] = random.randint(1,100)
        else:
            elevation[i][j] = 0



#the actions we can take
delta = [[-1, 0 ], # go up
         [ 0, -1], # go left
         [ 1, 0 ], # go down
         [ 0, 1 ]] # go right


#function to search the path
def search(grid,init,goal,cost,heuristic):

    closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
    closed[init[0]][init[1]] = 1
    action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid

    x = init[0]
    y = init[1]
    g = 0

    f = g + heuristic[init[0]][init[0]] + elevation[init[0]][init[0]]
    cell = [[f, g, x, y]]

    found = False  # flag that is set when search is complete
    resign = False # flag set if we can't find expand

    while not found and not resign:
        if len(cell) == 0:
            resign = True
            return "FAIL"
        else:
            cell.sort()#to choose the least costliest action so as to move closer to the goal
            cell.reverse()
            next = cell.pop()
            x = next[2]
            y = next[3]
            g = next[1]
            f = next[0]


            if x == goal[0] and y == goal[1]:
                found = True
            else:
                for i in range(len(delta)):#to try out different valid actions
                    x2 = x + delta[i][0]
                    y2 = y + delta[i][1]
                    if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
                        if closed[x2][y2] == 0 and grid[x2][y2] == 0 and elevation[x2][y2] < drone_h :
                            g2 = g + cost
                            f2 = g2 + heuristic[x2][y2] + elevation[x2][y2]
                            cell.append([f2, g2, x2, y2])
                            closed[x2][y2] = 1
                            action[x2][y2] = i
    invpath = []
    x = goal[0]
    y = goal[1]
    invpath.append([x, y])#we get the reverse path from here
    while x != init[0] or y != init[1]:
        x2 = x - delta[action[x][y]][0]
        y2 = y - delta[action[x][y]][1]
        x = x2
        y = y2
        invpath.append([x, y])

    path = []
    for i in range(len(invpath)):
        path.append(invpath[len(invpath) - 1 - i])
    print("ACTION MAP")
    for i in range(len(action)):
        print(action[i])

    return path

a = search(grid,init,goal,cost,heuristic)
for i in range(len(a)):
    print(a[i])
约弗雷夫

您可以随机分配网格,然后确保起点和终点都没有障碍物。对于相邻的字段,您可以对这两个字段执行相同的操作。

import random
grid1 = [[random.randint(0,1) for i in range(100)]for j in range(100)]

# clear starting and end point of potential obstacles
grid1[0][0] = 0
grid1[99][99] = 0

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在不手动设置限制的情况下使用ggplot2不对称地扩展轴?

来自分类Dev

如何在不手动卸载较旧应用程序的情况下更新最新版本

来自分类Dev

如何在不手动定义宽度的情况下居中放置div

来自分类Dev

如何在不部署的情况下将文件上传到我的Google App Engine项目?

来自分类Dev

我如何才能在不经过障碍物的情况下检测出从A点到B点的最短路径?

来自分类Dev

Sitecore:如何在不丢失数据的情况下将字段移动到我的基本模板之一

来自分类Dev

如何在不手动修改文件的情况下编辑部署?

来自分类Dev

我可以在不手动输入base :: Method()的情况下递归地在每个库中调用method吗?

来自分类Dev

如何在不手动关闭选项卡的情况下保持Firefox的可用性?

来自分类Dev

如何在没有手动设置的情况下将绑定属性作为参数传递

来自分类Dev

在不使用“ .insert” /不手动重新排序的情况下添加列时,如何在数据表中的特定位置插入列?

来自分类Dev

如何在不手动指定PK的情况下仅将唯一数据加载到我的oracle表中?

来自分类Dev

如何在不手动复制和粘贴的情况下遍历python生成的列表?

来自分类Dev

我可以在不使用Javascript手动添加网格项的情况下使网格表现出所需的行为吗?

来自分类Dev

如何在不手动在SQL中手动键入列名的情况下透视表

来自分类Dev

Windows上的ROUTE-如何在不手动设置IP的情况下访问路由器配置页面

来自分类Dev

如何在不手动关闭选项卡的情况下保持Firefox的可用性?

来自分类Dev

如何在不破坏任何其他应用程序的情况下将手动安装的python用于个人程序?

来自分类Dev

如何在不手动设置每个属性的情况下更新数据库中的实体对象?

来自分类Dev

在不手动刷新asp.net的情况下如何在gridview中更新数据?

来自分类Dev

如何在不手动计算偏移的情况下以编程方式堆叠视图?

来自分类Dev

如何在不手动输入的情况下让tmux打开一组窗格?

来自分类Dev

如何在不运行`fc-cache -f`的情况下使所有应用程序都可以访问手动安装的字体?

来自分类Dev

如何在不使用C#手动更新ID的情况下将依赖LINQ插入SQL实体?

来自分类Dev

Sitecore:如何在不丢失数据的情况下将字段移动到我的基本模板之一

来自分类Dev

如何在不选择“项目”的情况下创建新问题(手动选择 project_id)?

来自分类Dev

如何在不手动列出查询中的所有值的情况下使用 SQL 创建分发表?

来自分类Dev

如何在不导入“library_name”的情况下将代码从库放置到我的项目中

来自分类Dev

如何修复angular 8中的变化检测方法。如何在不手动编写的情况下立即实现这一点,每次检测变化

Related 相关文章

  1. 1

    如何在不手动设置限制的情况下使用ggplot2不对称地扩展轴?

  2. 2

    如何在不手动卸载较旧应用程序的情况下更新最新版本

  3. 3

    如何在不手动定义宽度的情况下居中放置div

  4. 4

    如何在不部署的情况下将文件上传到我的Google App Engine项目?

  5. 5

    我如何才能在不经过障碍物的情况下检测出从A点到B点的最短路径?

  6. 6

    Sitecore:如何在不丢失数据的情况下将字段移动到我的基本模板之一

  7. 7

    如何在不手动修改文件的情况下编辑部署?

  8. 8

    我可以在不手动输入base :: Method()的情况下递归地在每个库中调用method吗?

  9. 9

    如何在不手动关闭选项卡的情况下保持Firefox的可用性?

  10. 10

    如何在没有手动设置的情况下将绑定属性作为参数传递

  11. 11

    在不使用“ .insert” /不手动重新排序的情况下添加列时,如何在数据表中的特定位置插入列?

  12. 12

    如何在不手动指定PK的情况下仅将唯一数据加载到我的oracle表中?

  13. 13

    如何在不手动复制和粘贴的情况下遍历python生成的列表?

  14. 14

    我可以在不使用Javascript手动添加网格项的情况下使网格表现出所需的行为吗?

  15. 15

    如何在不手动在SQL中手动键入列名的情况下透视表

  16. 16

    Windows上的ROUTE-如何在不手动设置IP的情况下访问路由器配置页面

  17. 17

    如何在不手动关闭选项卡的情况下保持Firefox的可用性?

  18. 18

    如何在不破坏任何其他应用程序的情况下将手动安装的python用于个人程序?

  19. 19

    如何在不手动设置每个属性的情况下更新数据库中的实体对象?

  20. 20

    在不手动刷新asp.net的情况下如何在gridview中更新数据?

  21. 21

    如何在不手动计算偏移的情况下以编程方式堆叠视图?

  22. 22

    如何在不手动输入的情况下让tmux打开一组窗格?

  23. 23

    如何在不运行`fc-cache -f`的情况下使所有应用程序都可以访问手动安装的字体?

  24. 24

    如何在不使用C#手动更新ID的情况下将依赖LINQ插入SQL实体?

  25. 25

    Sitecore:如何在不丢失数据的情况下将字段移动到我的基本模板之一

  26. 26

    如何在不选择“项目”的情况下创建新问题(手动选择 project_id)?

  27. 27

    如何在不手动列出查询中的所有值的情况下使用 SQL 创建分发表?

  28. 28

    如何在不导入“library_name”的情况下将代码从库放置到我的项目中

  29. 29

    如何修复angular 8中的变化检测方法。如何在不手动编写的情况下立即实现这一点,每次检测变化

热门标签

归档