From e723b7ddf5bfdaf122fa6da92280261a077167e6 Mon Sep 17 00:00:00 2001 From: jparisu Date: Tue, 27 Aug 2024 17:34:01 +0200 Subject: [PATCH] Apply some changes and fixes Signed-off-by: jparisu --- src/sIArena/terrain/Terrain.py | 11 +++++++--- .../terrain/generator/MazeGenerator.py | 21 +++++++++++++++++-- src/sIArena/terrain/plot/plot_2D.py | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/sIArena/terrain/Terrain.py b/src/sIArena/terrain/Terrain.py index 8724698..16b21a4 100644 --- a/src/sIArena/terrain/Terrain.py +++ b/src/sIArena/terrain/Terrain.py @@ -136,7 +136,7 @@ def is_valid_path(self, path: Path) -> Tuple[bool, str]: return True, "Valid path" - def get_destination(self): + def get_destinations(self) -> List[Coordinate]: return None class Terrain (NoPathTerrain): @@ -184,8 +184,6 @@ def __init__( if self.destination[1] < 0 and self.destination[1] >= self.m: raise AttributeError(f"Destination column is out of bounds: {self.destination[1]}") - self.destinations = [destination] - def is_complete_path(self, path: Path) -> Tuple[bool, str]: """Returns True if the given path goes from the origin to the destination""" @@ -223,6 +221,9 @@ def __str__(self): return s + def get_destinations(self) -> List[Coordinate]: + return [self.destination] + class DestinationSetTerrain (NoPathTerrain): """ @@ -311,3 +312,7 @@ def __str__(self): s += "|\n" s += "+" + ("-" * (max_length + 3) + "+") * self.m + "\n" return s + + + def get_destinations(self) -> List[Coordinate]: + return self.destinations diff --git a/src/sIArena/terrain/generator/MazeGenerator.py b/src/sIArena/terrain/generator/MazeGenerator.py index d427939..3bac364 100644 --- a/src/sIArena/terrain/generator/MazeGenerator.py +++ b/src/sIArena/terrain/generator/MazeGenerator.py @@ -101,6 +101,22 @@ def visit(self): self.visited = True +def weigthed_distrubution(n, r=10): + + if n == 1: + return 0 + + choices = [i for i in range(n)] + weights = [1 for i in range(n)] + + x = 0 + for i in range(n): + weights[i] *= n-1 + weights[i] += x + x += r - 1 + + return random.choices(choices, weights=weights, k=1)[0] + class Maze: @@ -120,8 +136,9 @@ def __init__(self, n, m, start_point=None): while to_visit: - # Get a random tile from to_visit list - r = random.randint(0, len(to_visit) - 1) + # Get a random number from 0 to n being the last ones more probable + n = len(to_visit) + r = weigthed_distrubution(n) next_tile = to_visit.pop(r) if self.tile(next_tile).visited: diff --git a/src/sIArena/terrain/plot/plot_2D.py b/src/sIArena/terrain/plot/plot_2D.py index 8d37e25..d783b9b 100644 --- a/src/sIArena/terrain/plot/plot_2D.py +++ b/src/sIArena/terrain/plot/plot_2D.py @@ -24,7 +24,7 @@ def plot_terrain_2D( # Mark with red the origin and destination plt.plot(terrain.origin[1], terrain.origin[0], 'r+') - for dest in terrain.destinations: + for dest in terrain.get_destinations(): plt.plot(dest[1], dest[0], 'rx') # Set path legends if unset