生成栅格地图数据集的代码
时间: 2024-05-07 20:21:25 浏览: 242
openLayers完整代码示例数据
以下是生成栅格地图数据集的Python代码示例:
```python
import numpy as np
import random
# 设置栅格地图大小
MAP_WIDTH = 10
MAP_HEIGHT = 10
# 随机生成障碍物
def generate_obstacle():
obstacle_map = np.zeros([MAP_HEIGHT, MAP_WIDTH])
for i in range(MAP_HEIGHT):
for j in range(MAP_WIDTH):
if random.random() < 0.3:
obstacle_map[i][j] = 1
return obstacle_map
# 生成起点和终点
def generate_start_goal(obstacle_map):
start = None
goal = None
while start is None:
x = random.randint(0, MAP_WIDTH-1)
y = random.randint(0, MAP_HEIGHT-1)
if obstacle_map[y][x] == 0:
start = (x, y)
while goal is None:
x = random.randint(0, MAP_WIDTH-1)
y = random.randint(0, MAP_HEIGHT-1)
if obstacle_map[y][x] == 0:
goal = (x, y)
return start, goal
# 生成栅格地图数据集
def generate_dataset(size):
dataset = []
for i in range(size):
obstacle_map = generate_obstacle()
start, goal = generate_start_goal(obstacle_map)
dataset.append((obstacle_map, start, goal))
return dataset
# 测试生成的数据
dataset = generate_dataset(10)
for i in range(len(dataset)):
print("Obstacle Map:\n", dataset[i][0])
print("Start Position:", dataset[i][1])
print("Goal Position:", dataset[i][2])
print("------------------------")
```
以上代码将生成一个大小为10的数据集,包含10个栅格地图数据,每个数据包含随机生成的障碍物、起点和终点。你可以根据需求更改地图大小、障碍物密度和数据集大小。
阅读全文