Compare commits

2 Commits

Author SHA1 Message Date
Jan Mrna
7cd1b06550 Add CreateMaze method for Map 2025-09-20 17:28:20 +02:00
Jan Mrna
8656736ea4 Increase map size, add labels 2025-09-20 17:19:10 +02:00
2 changed files with 36 additions and 5 deletions

View File

@@ -14,6 +14,7 @@
- [x] GBFS
- [x] A*
- [x] performance measurement: time/visited nodes
- [ ] finalize the script and copy back to the jupyter notebook
- [ ] finish text on the page
- [x] create a dedicated python script
- [ ] C++

View File

@@ -8,6 +8,7 @@
import matplotlib.pyplot as plt
import numpy as np
import time
import random
from typing import Optional, NewType, Any
from abc import ABC, abstractmethod
from queue import Queue, PriorityQueue
@@ -85,6 +86,33 @@ class Map:
self._visited_nodes += 1
return self.GetPointCost(point)
def CreateMaze(self, wall_probability: float = 0.3) -> None:
"""
Note: generated with Grok
Generate a simple maze on the map.
- Borders are set as walls (cost 1000).
- Internal cells are randomly set to 1 (path) or 1000 (wall) based on wall_probability.
Args:
wall_probability (float): Probability (0-1) that an internal cell becomes a wall.
"""
rows, cols = self.array.shape
# Set borders to walls (cost 1000)
self.array[0, :] = 1000 # Top row
self.array[-1, :] = 1000 # Bottom row
self.array[:, 0] = 1000 # Left column
self.array[:, -1] = 1000 # Right column
# Set internal cells randomly
for y in range(1, rows - 1): # Skip borders
for x in range(1, cols - 1):
if random.random() < wall_probability:
self.array[y, x] = 1000 # Wall
else:
self.array[y, x] = 1 # Normal tile
#
# Drawing utilities
#
@@ -117,6 +145,7 @@ class Visualizer:
self._axes.plot(xs, ys, 'o-', color=color, label=label)
self._axes.plot(xs[0], ys[0], 'o', color='lime', markersize=8) # starting point
self._axes.plot(xs[-1], ys[-1], 'o', color='magenta', markersize=8) # end point
self._axes.legend()
#
@@ -395,10 +424,11 @@ class A_star(PathFinderBase):
def main():
# Define the map and start/stop points
m = Map(15,10)
m.Randomize()
starting_point: Point2D = Point2D((1,1))
end_point: Point2D = Point2D((5,5))
m = Map(30,20)
#m.Randomize()
m.CreateMaze()
starting_point: Point2D = Point2D((29,19))
end_point: Point2D = Point2D((1,1))
path_finder_classes: list[type[PathFinderBase]] = [
DFS,
@@ -419,7 +449,7 @@ def main():
if path is not None:
cost = m.GetPathCost(path)
print(f"{path_finder.name:24}: took {elapsed_time/1e6:.3f} ms, visited {visited_nodes} nodes, cost {cost:.2f}")
v.DrawPath(path)
v.DrawPath(path, label=path_finder.name)
else:
print(f"{path_finder.name}: No path found")