-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for Q learning (#11)
- Loading branch information
Showing
7 changed files
with
559 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from behavior_generation_lecture_python.mdp.mdp import (\n", | ||
" MDP,\n", | ||
" GridMDP,\n", | ||
" expected_utility_of_action,\n", | ||
" derive_policy,\n", | ||
" q_learning,\n", | ||
" GRID_MDP_DICT,\n", | ||
" HIGHWAY_MDP_DICT,\n", | ||
" LC_RIGHT_ACTION,\n", | ||
" STAY_IN_LANE_ACTION,\n", | ||
")\n", | ||
"from behavior_generation_lecture_python.utils.grid_plotting import (\n", | ||
" make_plot_grid_step_function,\n", | ||
" make_plot_policy_step_function,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## TOY EXAMPLE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grid_mdp = GridMDP(**GRID_MDP_DICT)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"computed_utility_history = q_learning(\n", | ||
" mdp=grid_mdp, alpha=0.1, epsilon=0.1, iterations=10000, return_history=True\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline\n", | ||
"plot_grid_step = make_plot_grid_step_function(\n", | ||
" columns=4, rows=3, U_over_time=computed_utility_history\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"mkdocs_flag = False # set to true if you are running the notebook locally\n", | ||
"if mkdocs_flag:\n", | ||
" import ipywidgets\n", | ||
" from IPython.display import display\n", | ||
"\n", | ||
" iteration_slider = ipywidgets.IntSlider(\n", | ||
" min=0, max=len(computed_utility_history) - 1, step=1, value=0\n", | ||
" )\n", | ||
" w = ipywidgets.interactive(plot_grid_step, iteration=iteration_slider)\n", | ||
" display(w)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_grid_step(1000)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## HIGHWAY EXAMPLE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if False:\n", | ||
" # we will change this to true later on, to see the effect\n", | ||
" HIGHWAY_MDP_DICT[\"transition_probabilities_per_action\"][LC_RIGHT_ACTION] = [\n", | ||
" (0.4, LC_RIGHT_ACTION),\n", | ||
" (0.6, STAY_IN_LANE_ACTION),\n", | ||
" ]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"highway_mdp = GridMDP(**HIGHWAY_MDP_DICT)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"utility_history_highway = q_learning(\n", | ||
" mdp=highway_mdp, alpha=0.1, epsilon=0.1, iterations=10000, return_history=True\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_grid_step_highway = make_plot_grid_step_function(\n", | ||
" columns=10, rows=4, U_over_time=utility_history_highway\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"if mkdocs_flag:\n", | ||
" iteration_slider = ipywidgets.IntSlider(\n", | ||
" min=0, max=len(utility_history_highway) - 1, step=1, value=0\n", | ||
" )\n", | ||
" w = ipywidgets.interactive(plot_grid_step_highway, iteration=iteration_slider)\n", | ||
" display(w)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_grid_step_highway(1000)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"policy_array = [\n", | ||
" derive_policy(highway_mdp, utility) for utility in utility_history_highway\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_policy_step_highway = make_plot_policy_step_function(\n", | ||
" columns=10, rows=4, policy_over_time=policy_array\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if mkdocs_flag:\n", | ||
" iteration_slider = ipywidgets.IntSlider(\n", | ||
" min=0, max=len(utility_history_highway) - 1, step=1, value=0\n", | ||
" )\n", | ||
" w = ipywidgets.interactive(plot_policy_step_highway, iteration=iteration_slider)\n", | ||
" display(w)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_policy_step_highway(1000)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.