如何生成100张网格图?

SH_IQ

我建立了一个为寻路算法map生成obstacles, start point and endpoint randomly的。如下所示:

import random
import numpy as np

x = [[random.uniform(0,1) for i in range(50)]for j in range(50)]
grid = np.array([[0 for i in range(len(x[0]))]for j in range(len(x))])

for i in range(len(x)):
    for j in range(len(x[0])):
        if x[i][j] <= 0.7:
            grid[i][j] = 0
        else:
            grid[i][j] = 1

init = np.random.randint(0, high = 49, size = 2) #Start location 
goal = np.random.randint(0, high = 49, size = 2) #Our goal 

# clear starting and end point of potential obstacles
def clear_grid(grid, x, y):
    if x != 0 and y != 0:
        grid[x-1:x+2,y-1:y+2]=0
    elif x == 0 and y != 0:
        grid[x:x+2,y-1:y+2]=0
    elif x != 0 and y == 0:
        grid[x-1:x+2,y:y+2]=0
    elif x ==0 and y == 0:
        grid[x:x+2,y:y+2]=0

clear_grid(grid, init[0], init[1])
clear_grid(grid, goal[0], goal[1]) 

如上所示,这个片段会给我one environment(栅格地图)与random obstaclesstart pointendpoint我需要创建100 environments(网格图),这100个环境中的每一个都会给我随机的障碍,起点和终点。请给我任何帮助?

accdias

考虑到您要填充的规则(uniform(0,1)<= .7),这将为您提供100个具有随机零和一的50x50网格ones

from random import uniform
import numpy as np

grids = np.array([[[0 if uniform(0, 1) <= .7 else 1 for i in range(50)] for j in range(50)] for _ in range(100)])

上面的代码替换了下面的代码,并且还可以在一遍中为您提供100个网格:

import random
import numpy as np

x = [[random.uniform(0,1) for i in range(50)]for j in range(50)]
grid = np.array([[0 for i in range(len(x[0]))]for j in range(len(x))])

for i in range(len(x)):
    for j in range(len(x[0])):
        if x[i][j] <= 0.7:
            grid[i][j] = 0
        else:
            grid[i][j] = 1

这是概念证明:

>>> import numpy as np
>>> from random import uniform
>>> grids = np.array([[[0 if uniform(0, 1) <= .7 else 1 for i in range(50)] for j in range(50)] for _ in range(100)])
>>> grids
array([[[0, 1, 1, ..., 0, 1, 1],
        [1, 1, 0, ..., 0, 0, 0],
        [1, 1, 0, ..., 0, 1, 1],
        ...,
        [1, 0, 1, ..., 0, 0, 0],
        [0, 0, 1, ..., 0, 0, 0],
        [0, 1, 0, ..., 0, 0, 1]],

       [[0, 1, 1, ..., 0, 1, 0],
        [1, 0, 0, ..., 0, 1, 1],
        [1, 0, 0, ..., 0, 0, 0],
        ...,
        [1, 1, 0, ..., 0, 0, 0],
        [1, 1, 1, ..., 0, 1, 1],
        [0, 0, 1, ..., 1, 1, 0]],

       [[0, 1, 0, ..., 0, 1, 1],
        [0, 0, 1, ..., 1, 0, 0],
        [0, 0, 1, ..., 0, 0, 1],
        ...,
        [0, 1, 1, ..., 1, 0, 0],
        [0, 1, 0, ..., 0, 1, 1],
        [0, 1, 0, ..., 0, 1, 0]],

       ...,

       [[1, 0, 0, ..., 1, 1, 1],
        [0, 1, 1, ..., 1, 0, 0],
        [0, 0, 0, ..., 0, 1, 0],
        ...,
        [0, 1, 1, ..., 0, 0, 1],
        [0, 0, 1, ..., 0, 0, 0],
        [1, 0, 1, ..., 0, 1, 0]],

       [[1, 1, 1, ..., 1, 0, 1],
        [0, 0, 1, ..., 1, 0, 1],
        [0, 1, 1, ..., 1, 1, 1],
        ...,
        [0, 0, 0, ..., 0, 0, 1],
        [0, 0, 0, ..., 1, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       [[1, 1, 0, ..., 1, 1, 1],
        [1, 1, 0, ..., 0, 0, 1],
        [1, 1, 1, ..., 0, 1, 1],
        ...,
        [1, 0, 1, ..., 1, 0, 1],
        [1, 1, 0, ..., 0, 0, 0],
        [1, 0, 1, ..., 0, 0, 1]]])
>>>
>>> len(grids)
100
>>> len(grids[0])
50
>>> len(grids[0][0])
50
>>> 

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章