Update Python version and refine Jupyter Notebook formatting

- Bump Python version from 3.11 to 3.13 in .python-version file.
- Reset execution counts to null in Jupyter Notebook for reproducibility.
- Improve code readability by adjusting comments and formatting in the notebook.
- Change the policy definition to use numpy.ndarray for better clarity.
- Modify pyproject.toml to enable E501 rule for line length management.
This commit is contained in:
2026-01-06 11:07:31 +01:00
parent 06bc1f28a9
commit 6eecdd6ab3
3 changed files with 51 additions and 48 deletions

View File

@@ -1 +1 @@
3.11
3.13

View File

@@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": null,
"id": "100d1e0d",
"metadata": {},
"outputs": [],
@@ -42,7 +42,9 @@
"np.set_printoptions(\n",
" precision=3,\n",
" suppress=True,\n",
") # (not mandatory) This line is for limiting floats to 3 decimal places, avoiding scientific notation (like 1.23e-04) for small numbers.\n",
")\n",
"# (not mandatory) This line is for limiting floats to 3 decimal places,\n",
"# avoiding scientific notation (like 1.23e-04) for small numbers.\n",
"\n",
"# For reproducibility\n",
"rng = np.random.default_rng(seed=42) # This line creates a random number generator."
@@ -229,7 +231,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": null,
"id": "a1258de4",
"metadata": {},
"outputs": [
@@ -249,18 +251,17 @@
"pos_to_state = {} # (i,j) -> s\n",
"\n",
"start_state = None # will store the state index of start state\n",
"goal_states = [] # will store the state index of goal state # We use a list in case there are multiple goals\n",
"trap_states = [] # will store the state index of trap state # We use a list in case there are multiple traps\n",
"goal_states = [] # will store the state index of goal state\n",
"trap_states = [] # will store the state index of trap state\n",
"\n",
"s = 0\n",
"for i in range(n_rows): # i = row index\n",
" for j in range(n_cols): # j = column index\n",
" cell = maze_str[i][j] # cell = the character at that position (S, ., #, etc.)\n",
"\n",
" if (\n",
" cell in FREE\n",
" ): # FREE contains: free cells \".\", start cell \"S\", goal cell \"G\" and trap cell \"X\"\n",
" # Walls # are ignored, they are not MDP states.\n",
" if cell in FREE:\n",
" # FREE contains: free cells \".\", start cell \"S\", goal cell \"G\" and trap cell \"X\"\n",
" # Walls # are ignored, they are not MDP states.\n",
" state_to_pos[s] = (i, j)\n",
" pos_to_state[(i, j)] = s\n",
"\n",
@@ -365,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": null,
"id": "fc61ceef-217c-47f4-8eba-0353369210db",
"metadata": {},
"outputs": [
@@ -398,7 +399,7 @@
" for (\n",
" s,\n",
" (i, j),\n",
" ) in state_to_pos.items(): # Calling .items() returns a list-like sequence of (key, value) pairs in the dictionary.\n",
" ) in state_to_pos.items():\n",
" cell = maze_str[i][j]\n",
"\n",
" if cell == \"S\":\n",
@@ -517,7 +518,7 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": null,
"id": "4b06da5e-bc63-48e5-a336-37bce952443d",
"metadata": {},
"outputs": [],
@@ -967,7 +968,7 @@
},
{
"cell_type": "code",
"execution_count": 91,
"execution_count": null,
"id": "2fffe0b7",
"metadata": {},
"outputs": [],
@@ -1109,7 +1110,7 @@
},
{
"cell_type": "code",
"execution_count": 94,
"execution_count": null,
"id": "4c428327",
"metadata": {},
"outputs": [
@@ -1125,7 +1126,7 @@
}
],
"source": [
"def plot_values(V: np.ndarray, title=\"Value function\") -> None:\n",
"def plot_values(V: np.ndarray, title: str = \"Value function\") -> None:\n",
" \"\"\"Plot the value function V on the maze as a heatmap.\"\"\"\n",
" grid_values = np.full(\n",
" (n_rows, n_cols),\n",
@@ -1149,7 +1150,7 @@
" # For each state:\n",
" # Place the text label at (column j, row i).\n",
" # Display value to two decimals.\n",
" # Use white text so its visible on the heatmap.\n",
" # Use white text so it's visible on the heatmap.\n",
" # Center the text inside each cell.\n",
"\n",
" for s, (i, j) in state_to_pos.items():\n",
@@ -1183,12 +1184,12 @@
},
{
"cell_type": "code",
"execution_count": 95,
"execution_count": null,
"id": "c1ab67f0-bd5e-4ffe-b655-aec030401b78",
"metadata": {},
"outputs": [],
"source": [
"def plot_policy(policy: np.ndarray, title=\"Policy\") -> None:\n",
"def plot_policy(policy: np.ndarray, title: str =\"Policy\") -> None:\n",
" \"\"\"Plot the given policy on the maze.\"\"\"\n",
" _fig, ax = plt.subplots()\n",
" # draw walls as dark cells\n",
@@ -1253,7 +1254,7 @@
" ax.set_yticks(np.arange(-0.5, n_rows, 1))\n",
" ax.set_xticklabels([])\n",
" ax.set_yticklabels([])\n",
" ax.grid(True)\n",
" ax.grid(visible=True)\n",
" ax.set_title(title)\n",
" plt.show()"
]
@@ -1297,7 +1298,7 @@
},
{
"cell_type": "code",
"execution_count": 97,
"execution_count": null,
"id": "929707e6-3022-4d86-96cc-12f251f890a9",
"metadata": {},
"outputs": [
@@ -1323,35 +1324,37 @@
}
],
"source": [
"my_policy = [\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_DOWN,\n",
" A_DOWN, # First row\n",
" A_UP,\n",
" A_DOWN,\n",
" A_DOWN,\n",
" A_LEFT, # Second row\n",
" A_UP,\n",
" A_RIGHT,\n",
" A_DOWN, # Third row\n",
" A_UP,\n",
" A_LEFT,\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_RIGHT, # Fourth row\n",
" A_UP,\n",
" A_LEFT,\n",
" A_DOWN,\n",
" A_RIGHT,\n",
" A_UP, # Fifth row\n",
"]\n",
"my_policy = np.ndarray(\n",
" [\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_DOWN,\n",
" A_DOWN, # First row\n",
" A_UP,\n",
" A_DOWN,\n",
" A_DOWN,\n",
" A_LEFT, # Second row\n",
" A_UP,\n",
" A_RIGHT,\n",
" A_DOWN, # Third row\n",
" A_UP,\n",
" A_LEFT,\n",
" A_RIGHT,\n",
" A_RIGHT,\n",
" A_RIGHT, # Fourth row\n",
" A_UP,\n",
" A_LEFT,\n",
" A_DOWN,\n",
" A_RIGHT,\n",
" A_UP, # Fifth row\n",
" ],\n",
")\n",
"\n",
"V_my_policy = policy_evaluation(policy=my_policy, P=P, R=R, gamma=gamma)\n",
"\n",
"plot_values(V=V_my_policy, title=\"Value function: my policy\")\n",
"plot_policy(policy=my_policy, title=\"My policy\")"
"plot_policy(policy=my_policy, title=\"My policy\")\n"
]
},
{
@@ -1400,7 +1403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.9"
"version": "3.11.12"
}
},
"nbformat": 4,

View File

@@ -51,7 +51,7 @@ select = ["ALL"]
# Désactiver certaines règles
ignore = [
# "E501", # line too long, géré par le formatter
"E501", # line too long, géré par le formatter
"E402", # Imports in top of file
"T201", # Print
"N806",