This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
73 lines (60 loc) · 1.73 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import noise
import numpy as np
from loguru import logger
import matplotlib.pyplot as plt
from core.agent import Agent, Status
def create_world(size):
shape = (size, size)
scale = 100.0
octaves = 4
persistence = 0.5
lacunarity = 2.0
DEM = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
DEM[i][j] = noise.pnoise2(
i / scale,
j / scale,
octaves=octaves,
persistence=persistence,
lacunarity=lacunarity,
repeatx=size,
repeaty=size,
base=17,
)
DEM -= DEM.min()
DEM *= 60
DEM = np.flip(DEM)
return DEM
if __name__ == "__main__":
# ! Create the map
LOCAL_SIZE = 128
SIZE = LOCAL_SIZE * 4
DEM = create_world(SIZE)
# ! Create the Agent
transform = lambda pos, dem: dem
AGENT = Agent(
start=(5, 5),
goal=(500, 200),
local_size=LOCAL_SIZE,
world=DEM,
transform=transform,
recalculate_threshold=0.8,
)
while True:
poly = AGENT.get_poly()
status = AGENT.forward()
path = AGENT.get_path()
if status == Status.REACHED:
logger.success("Path reached")
break
# ! Display results
plt.imshow(DEM, cmap="terrain", extent=[0, SIZE, 0, SIZE], origin="lower")
plt.plot(path[:, 0], path[:, 1], linewidth=2, c="r")
plt.plot(*poly.exterior.xy, c="black", linewidth=2)
plt.show(block=False)
plt.pause(0.1)
plt.close()
plt.imshow(DEM, cmap="terrain", extent=[0, SIZE, 0, SIZE], origin="lower")
plt.plot(path[:, 0], path[:, 1], linewidth=2, c="r")
plt.show()