From 153ab3878de5498a168afa4b456b1cf0918fb8d6 Mon Sep 17 00:00:00 2001 From: jparisu Date: Wed, 31 Jan 2024 18:04:48 +0100 Subject: [PATCH] Add final features and some docs Signed-off-by: jparisu --- docs/index.rst | 10 +- docs/src/elements/elements.rst | 14 ++ docs/src/elements/generation.rst | 10 ++ docs/src/elements/measure.rst | 10 ++ docs/src/elements/terrain.rst | 10 ++ docs/src/getting_started.rst | 20 --- docs/src/tldr.rst | 51 ++++++ docs/src/tutorial.rst | 10 ++ src/sIArena/measurements/measurements.py | 4 +- src/sIArena/terrain/Terrain.py | 4 + .../terrain/generator/FocusedGenerator.py | 2 - src/sIArena/terrain/generator/Generator.py | 10 +- .../terrain/generator/easy_generator.py | 30 ++++ src/sIArena/terrain/plot/cmap_allowed.txt | 166 ++++++++++++++++++ src/sIArena/terrain/plot/plot_2D.py | 5 +- src/sIArena/terrain/plot/plot_3D.py | 5 +- 16 files changed, 330 insertions(+), 31 deletions(-) create mode 100644 docs/src/elements/elements.rst create mode 100644 docs/src/elements/generation.rst create mode 100644 docs/src/elements/measure.rst create mode 100644 docs/src/elements/terrain.rst delete mode 100644 docs/src/getting_started.rst create mode 100644 docs/src/tldr.rst create mode 100644 docs/src/tutorial.rst create mode 100644 src/sIArena/terrain/generator/easy_generator.py create mode 100644 src/sIArena/terrain/plot/cmap_allowed.txt diff --git a/docs/index.rst b/docs/index.rst index a53bf22..3ab55fb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -6,5 +6,13 @@ :maxdepth: 2 :hidden: - /src/getting_started /src/installation + /src/tutorial + /src/tlnr + +.. toctree:: + :caption: Elements + :maxdepth: 2 + :hidden: + + /src/elements/elements diff --git a/docs/src/elements/elements.rst b/docs/src/elements/elements.rst new file mode 100644 index 0000000..915bffd --- /dev/null +++ b/docs/src/elements/elements.rst @@ -0,0 +1,14 @@ +.. _elements: + +######## +Elements +######## + +.. toctree:: + :caption: Project + :maxdepth: 2 + :hidden: + + terrain + measure + generation diff --git a/docs/src/elements/generation.rst b/docs/src/elements/generation.rst new file mode 100644 index 0000000..e9bfecc --- /dev/null +++ b/docs/src/elements/generation.rst @@ -0,0 +1,10 @@ +.. _elements_generation: + +################ +Generate Terrain +################ + +.. warning:: + + Coming soon. + TODO diff --git a/docs/src/elements/measure.rst b/docs/src/elements/measure.rst new file mode 100644 index 0000000..c55cadf --- /dev/null +++ b/docs/src/elements/measure.rst @@ -0,0 +1,10 @@ +.. _elements_measure: + +###################### +Path and Measure tools +###################### + +.. warning:: + + Coming soon. + TODO diff --git a/docs/src/elements/terrain.rst b/docs/src/elements/terrain.rst new file mode 100644 index 0000000..feb47cf --- /dev/null +++ b/docs/src/elements/terrain.rst @@ -0,0 +1,10 @@ +.. _elements_terrain: + +####### +Terrain +####### + +.. warning:: + + Coming soon. + TODO diff --git a/docs/src/getting_started.rst b/docs/src/getting_started.rst deleted file mode 100644 index 9b2261e..0000000 --- a/docs/src/getting_started.rst +++ /dev/null @@ -1,20 +0,0 @@ -.. _getting_started: - -############### -Getting Started -############### - -============ -Installation -============ - -Check the :ref:`installation guide `. - - -========= -Scenarios -========= - -.. warning:: - - Coming soon. diff --git a/docs/src/tldr.rst b/docs/src/tldr.rst new file mode 100644 index 0000000..b7040f0 --- /dev/null +++ b/docs/src/tldr.rst @@ -0,0 +1,51 @@ +.. _tldr: + +##### +TL;DR +##### + +================ +Project Overview +================ + +This project helps you to generate 2D terrains, that are matrix of integers, with a point of origin and a point of destination. +Over these terrains you have several features: + +- You can print it in the console +- You can plot it in a matplotlib 2D plot +- You can plot it in 3D with different angles +- **You can check the total path of a given path** +- You can even draw the paths in the plots previously mentioned + + +============ +Installation +============ + +Just use the following command in your notebook: + +.. code-block:: py + + !pip install git+https://github.com/jparisu/sIArena.git + +Check the :ref:`installation guide ` for more details. + + +========================= +How to generate a terrain +========================= + +.. warning:: + + Coming soon. + TODO + + +===================================== +How to write a path finding algorithm +===================================== + +.. warning:: + + Coming soon. + TODO diff --git a/docs/src/tutorial.rst b/docs/src/tutorial.rst new file mode 100644 index 0000000..16870c7 --- /dev/null +++ b/docs/src/tutorial.rst @@ -0,0 +1,10 @@ +.. _tutorial: + +######## +Tutorial +######## + +.. warning:: + + Coming soon. + TODO diff --git a/src/sIArena/measurements/measurements.py b/src/sIArena/measurements/measurements.py index 0509c20..c1ae468 100644 --- a/src/sIArena/measurements/measurements.py +++ b/src/sIArena/measurements/measurements.py @@ -8,7 +8,8 @@ def measure_function( search_function, terrain: Terrain, iterations: int = 1, - debug: bool = False + debug: bool = False, + max_seconds: float = 60*5 ) -> Tuple[int, float, Path]: """ This function uses a search function to calculate a path from the beggining to the end of a terrain. @@ -35,6 +36,7 @@ def measure_function( if debug: print(f"Running iteration {i}...") + # TODO add max time stop timer = Timer() path = search_function(terrain) times.append(timer.elapsed_s()) diff --git a/src/sIArena/terrain/Terrain.py b/src/sIArena/terrain/Terrain.py index 0cab919..d8a8cf2 100644 --- a/src/sIArena/terrain/Terrain.py +++ b/src/sIArena/terrain/Terrain.py @@ -43,9 +43,13 @@ def __init__( if self.origin is None: self.origin = (0, 0) + else: + self.origin = (origin[0], origin[1]) if self.destination is None: self.destination = (self.n - 1, self.m - 1) + else: + self.destination = (destination[0], destination[1]) # Check that the origin is valid if self.origin[0] < 0 and self.origin[0] >= self.n: diff --git a/src/sIArena/terrain/generator/FocusedGenerator.py b/src/sIArena/terrain/generator/FocusedGenerator.py index 862381c..85dad3c 100644 --- a/src/sIArena/terrain/generator/FocusedGenerator.py +++ b/src/sIArena/terrain/generator/FocusedGenerator.py @@ -44,8 +44,6 @@ def generate_random_matrix_( matrix[i][j] = FocusedGenerator.focused_value_generator( [matrix[i - 1][j], matrix[i][j - 1]], abruptness) - print(matrix) - return matrix diff --git a/src/sIArena/terrain/generator/Generator.py b/src/sIArena/terrain/generator/Generator.py index a165376..2d2e3fe 100644 --- a/src/sIArena/terrain/generator/Generator.py +++ b/src/sIArena/terrain/generator/Generator.py @@ -38,11 +38,15 @@ def generate_random_terrain( m = scalade(m, min_height, vmax) # Convert the matrix to integers using numpy - m = np.array(m, dtype=int) + final_m = np.zeros(m.shape, dtype=int) + for i in range(m.shape[0]): + for j in range(m.shape[1]): + final_m[i,j] = round(m[i,j]) - m *= min_step + final_m *= min_step + + return Terrain(final_m, origin=origin, destination=destination) - return Terrain(m, origin=origin, destination=destination) @pure_virtual def generate_random_matrix_( diff --git a/src/sIArena/terrain/generator/easy_generator.py b/src/sIArena/terrain/generator/easy_generator.py new file mode 100644 index 0000000..e2ebe97 --- /dev/null +++ b/src/sIArena/terrain/generator/easy_generator.py @@ -0,0 +1,30 @@ + +from sIArena.terrain.Terrain import Coordinate, Terrain +from sIArena.terrain.generator.Generator import TerrainGenerator +from sIArena.terrain.generator.PernilGenerator import PernilGenerator +from sIArena.terrain.generator.FocusedGenerator import FocusedGenerator + + +def generate_random_terrain( + generator_ctor: TerrainGenerator, + n: int, + m: int, + min_height: int = 0, + max_height: int = 99, + min_step: int = 1, + abruptness: float = 0.2, + seed: int = None, + origin: Coordinate = None, + destination: Coordinate = None + ) -> Terrain: + return generator_ctor().generate_random_terrain( + n=n, + m=m, + min_height=min_height, + max_height=max_height, + min_step=min_step, + abruptness=abruptness, + seed=seed, + origin=origin, + destination=destination + ) diff --git a/src/sIArena/terrain/plot/cmap_allowed.txt b/src/sIArena/terrain/plot/cmap_allowed.txt new file mode 100644 index 0000000..e65b003 --- /dev/null +++ b/src/sIArena/terrain/plot/cmap_allowed.txt @@ -0,0 +1,166 @@ +Accent +Accent_r +Blues +Blues_r +BrBG +BrBG_r +BuGn +BuGn_r +BuPu +BuPu_r +CMRmap +CMRmap_r +Dark2 +Dark2_r +GnBu +GnBu_r +Greens +Greens_r +Greys +Greys_r +OrRd +OrRd_r +Oranges +Oranges_r +PRGn +PRGn_r +Paired +Paired_r +Pastel1 +Pastel1_r +Pastel2 +Pastel2_r +PiYG +PiYG_r +PuBu +PuBuGn +PuBuGn_r +PuBu_r +PuOr +PuOr_r +PuRd +PuRd_r +Purples +Purples_r +RdBu +RdBu_r +RdGy +RdGy_r +RdPu +RdPu_r +RdYlBu +RdYlBu_r +RdYlGn +RdYlGn_r +Reds +Reds_r +Set1 +Set1_r +Set2 +Set2_r +Set3 +Set3_r +Spectral +Spectral_r +Wistia +Wistia_r +YlGn +YlGnBu +YlGnBu_r +YlGn_r +YlOrBr +YlOrBr_r +YlOrRd +YlOrRd_r +afmhot +afmhot_r +autumn +autumn_r +binary +binary_r +bone +bone_r +brg +brg_r +bwr +bwr_r +cividis +cividis_r +cool +cool_r +coolwarm +coolwarm_r +copper +copper_r +cubehelix +cubehelix_r +flag +flag_r +gist_earth +gist_earth_r +gist_gray +gist_gray_r +gist_heat +gist_heat_r +gist_ncar +gist_ncar_r +gist_rainbow +gist_rainbow_r +gist_stern +gist_stern_r +gist_yarg +gist_yarg_r +gnuplot +gnuplot2 +gnuplot2_r +gnuplot_r +gray +gray_r +hot +hot_r +hsv +hsv_r +inferno +inferno_r +jet +jet_r +magma +magma_r +nipy_spectral +nipy_spectral_r +ocean +ocean_r +pink +pink_r +plasma +plasma_r +prism +prism_r +rainbow +rainbow_r +seismic +seismic_r +spring +spring_r +summer +summer_r +tab10 +tab10_r +tab20 +tab20_r +tab20b +tab20b_r +tab20c +tab20c_r +terrain +terrain_r +turbo +turbo_r +twilight +twilight_r +twilight_shifted +twilight_shifted_r +viridis +viridis_r +winter +winter_r diff --git a/src/sIArena/terrain/plot/plot_2D.py b/src/sIArena/terrain/plot/plot_2D.py index 6cb2e75..17c9ca7 100644 --- a/src/sIArena/terrain/plot/plot_2D.py +++ b/src/sIArena/terrain/plot/plot_2D.py @@ -7,11 +7,12 @@ def plot_terrain_2D( terrain: Terrain, paths: List[Path] = [], - colors: List[str] = ['r', 'y', 'm', 'k', 'c', 'g', 'b']): + colors: List[str] = ['r', 'y', 'm', 'k', 'c', 'g', 'b'], + cmap: str = 'terrain'): """Plots the terrain and the given paths""" plt.clf() - plt.imshow(terrain.matrix, cmap='terrain') + plt.imshow(terrain.matrix, cmap=cmap) # Mark with red the origin and destination plt.plot(terrain.origin[1], terrain.origin[0], 'r+') diff --git a/src/sIArena/terrain/plot/plot_3D.py b/src/sIArena/terrain/plot/plot_3D.py index 91a50c2..42dbe5b 100644 --- a/src/sIArena/terrain/plot/plot_3D.py +++ b/src/sIArena/terrain/plot/plot_3D.py @@ -10,7 +10,8 @@ def plot_terrain_3D( terrain: Terrain, angles: List[tuple] = [(45, 45), (45, 225)], paths: List[Path] = [], - colors: List[str] = ['r', 'y', 'm', 'k', 'c', 'g', 'b']): + colors: List[str] = ['r', 'y', 'm', 'k', 'c', 'g', 'b'], + cmap: str = 'terrain'): """Plots the terrain and the given paths""" fig = plt.figure(figsize=(18, 6)) @@ -30,7 +31,7 @@ def plot_terrain_3D( Z = np_matrix[X, Y] # Plot the surface - ax.plot_surface(X, Y, Z, cmap='terrain') + ax.plot_surface(X, Y, Z, cmap=cmap) # Plot the origin point ax.plot([terrain.origin[0]], [terrain.origin[1]], [terrain[terrain.origin]], 'r+', markersize=5, zorder=6)