jeudi 10 octobre 2019

How could I set the staring and ending points randomly in a grid that generate random obstacles?

I built a grid with random obstacles for pathfinding algorithm, but with fixed starting and ending points as shown in my snippet below:

import random
import numpy as np

#grid format
# 0 = navigable space
# 1 = occupied space

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 = [5,5]       #Start location 
goal = [45,45]     #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])

I need to generate also the starting and ending points randomly every time I run the code instead of making them fixed. How could I make it? Any assistance, please?.




Aucun commentaire:

Enregistrer un commentaire