diff --git a/docs/conf.py b/docs/conf.py index 0133aa5..94ef88d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -15,3 +15,7 @@ # The spelling list filename. spelling_word_list_filename=['spelling/spelling_wordlist.txt'] + +# Change tab title +release = '1.0.0' +html_title = f"{project} {release} Documentation" diff --git a/docs/index.rst b/docs/index.rst index a0e0f00..6b33279 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,4 +1,8 @@ +##################### +sIArena Documentation +##################### + .. include:: /rst/titlepage.rst diff --git a/docs/rst/titlepage.rst b/docs/rst/titlepage.rst index cabca13..4fde11f 100644 --- a/docs/rst/titlepage.rst +++ b/docs/rst/titlepage.rst @@ -1,7 +1,7 @@ -############################## +****************************** IArena Searching Documentation -############################## +****************************** **IArena** is an **open source** project that aims to provide a **simple** and **easy to use** framework for playing games in a manual or **automatic** way. It is developed with educational and research purposes related with **computer science** and **artificial intelligence**. diff --git a/docs/spelling/spelling_wordlist.txt b/docs/spelling/spelling_wordlist.txt index 6d1d05b..174c74d 100644 --- a/docs/spelling/spelling_wordlist.txt +++ b/docs/spelling/spelling_wordlist.txt @@ -1,2 +1,3 @@ -IArena Colab +IArena +sIArena diff --git a/src/sIArena/measurements/measurements.py b/src/sIArena/measurements/measurements.py index c1ae468..423ce0e 100644 --- a/src/sIArena/measurements/measurements.py +++ b/src/sIArena/measurements/measurements.py @@ -1,4 +1,6 @@ import math +import threading +import time from typing import List, Tuple from sIArena.terrain.Terrain import Coordinate, Terrain, Path @@ -25,7 +27,9 @@ def measure_function( - The average time that the function has elapsed to find it """ - # A paused timer to measure time + # Function to return the result as a parameter + def func_wrapper(func, terrain, result): + result.append(func(terrain)) best_path_cost = math.inf best_path = None @@ -36,11 +40,27 @@ def measure_function( if debug: print(f"Running iteration {i}...") - # TODO add max time stop + # List for the result of the function + result = [] + + # Thread with timeout + thread = threading.Thread(target=func_wrapper, args=(search_function, terrain, result,)) + + # Start timer timer = Timer() - path = search_function(terrain) + + # Start thread + thread.start() + thread.join(timeout=max_seconds) + + # Store time times.append(timer.elapsed_s()) + if thread.is_alive(): + raise TimeoutError(f"Function {search_function.__name__} took more than {max_seconds} seconds to finish.") + else: + path = result[0] + if not terrain.is_full_path(path): raise ValueError(f"Found Incorrect path with function {search_function.__name__}: {path}") diff --git a/src/sIArena/terrain/plot/plot_2D.py b/src/sIArena/terrain/plot/plot_2D.py index cd3275a..be55b5f 100644 --- a/src/sIArena/terrain/plot/plot_2D.py +++ b/src/sIArena/terrain/plot/plot_2D.py @@ -7,6 +7,8 @@ def plot_terrain_2D( terrain: Terrain, paths: List[Path] = [], + paths_legends: List[str] = None, + use_cost_as_path_legend: bool = False, colors: List[str] = ['r', 'y', 'm', 'k', 'c', 'g', 'b'], cmap: str = 'terrain', title: str = 'Terrain', @@ -20,17 +22,30 @@ def plot_terrain_2D( plt.plot(terrain.origin[1], terrain.origin[0], 'r+') plt.plot(terrain.destination[1], terrain.destination[0], 'rx') + # Set path legends if unset + paths_legends_ = paths_legends + if paths_legends_ is None: + paths_legends_ = [""] + while len(paths_legends_) < len(paths): + paths_legends_.append("") + # Plot the paths for i, p in enumerate(paths): + if use_cost_as_path_legend: + paths_legends_[i] = f'{paths_legends_[i]} ({terrain.get_path_cost(p)})' plt.plot( [pos[1] for pos in p], [pos[0] for pos in p], - colors[i % len(colors)]) + colors[i % len(colors)], + label=paths_legends_[i],) plt.xlabel('row') plt.ylabel('col') plt.title(title) + if paths_legends or use_cost_as_path_legend: + plt.legend() + plt.colorbar() plt.show(block=False) plt.pause(0.001) diff --git a/src/sIArena/terrain/plot/plot_3D.py b/src/sIArena/terrain/plot/plot_3D.py index 355e57c..d8df9fe 100644 --- a/src/sIArena/terrain/plot/plot_3D.py +++ b/src/sIArena/terrain/plot/plot_3D.py @@ -58,5 +58,5 @@ def plot_terrain_3D( ax.set_xlabel('row') ax.set_ylabel('col') - fig.title(title) + fig.suptitle(title) plt.show() diff --git a/src/sIArena/utils/threading_utils.py b/src/sIArena/utils/threading_utils.py new file mode 100644 index 0000000..153d779 --- /dev/null +++ b/src/sIArena/utils/threading_utils.py @@ -0,0 +1,20 @@ +import threading + +def run_function_with_timeout(func, timeout): + + # Function to return the result as a parameter + def func_wrapper(result): + result.append(func()) + + # List for the result of the function + result = [] + + # Thread with timeout + thread = threading.Thread(target=func_wrapper, args=(result,)) + thread.start() + thread.join(timeout) + + if thread.is_alive(): + return None + else: + return result[0]