Skip to content

Commit

Permalink
Merge pull request #31 from BradenEverson/rl/research
Browse files Browse the repository at this point in the history
New Reinforcement Learning Branch
  • Loading branch information
BradenEverson authored Oct 31, 2024
2 parents 0087f62 + 1c4b7d7 commit 4a2f15c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions earthmover-research/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.ipynb_checkpoints/
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "01c32df2",
"metadata": {},
"source": [
"# Reinforcement Learning with Lunar Landing\n",
"This is a small sample code for the `LunarLander-v3` environment in OpenAI's Gym which is designed to simulate a reinforcement learning scenario where a agent must control a lunar lander to safely land on a designated landing pad."
]
},
{
"cell_type": "markdown",
"id": "86ca8de7",
"metadata": {},
"source": [
"### Library Imports\n",
"Here, we make the following library imports, where we manage to import the `gymansium` library, which gives us access to the environment. We import the `imageio` library which allows us to produce a `.gif` file."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "05c326ff",
"metadata": {},
"outputs": [],
"source": [
"import gymnasium as gym\n",
"import imageio"
]
},
{
"cell_type": "markdown",
"id": "d5772728",
"metadata": {},
"source": [
"### Creating a Environment\n",
"Here, we create a Gym environment using `gym.make()`. Although the `.make()` can accept multiple parameters, we can insert the lunar lander environment, as well as set the render mode, which allows us to create image arrays."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "72e3d14d",
"metadata": {},
"outputs": [],
"source": [
"env = gym.make(\"LunarLander-v3\", render_mode=\"rgb_array\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2fac2cd5",
"metadata": {},
"outputs": [],
"source": [
"def run_multiple_episodes(num_episodes=10, max_steps=200):\n",
" frames = [] \n",
"\n",
" for _ in range(num_episodes):\n",
" observation, info = env.reset()\n",
" episode_over = False\n",
" steps = 0\n",
"\n",
" while not episode_over and steps < max_steps:\n",
" frame = env.render() \n",
" frames.append(frame) \n",
" action = env.action_space.sample() \n",
" observation, reward, terminated, truncated, info = env.step(action)\n",
" episode_over = terminated or truncated\n",
" steps += 1 \n",
"\n",
" env.close()\n",
" return frames"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ca785b50",
"metadata": {},
"outputs": [],
"source": [
"frames = run_multiple_episodes(num_episodes=10) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "581bd473",
"metadata": {},
"outputs": [],
"source": [
"imageio.mimsave('lunar_lander_multiple_episodes.gif', frames, fps=50)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "RL-lunar-lander",
"language": "python",
"name": "rl-lunar-lander"
},
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4a2f15c

Please sign in to comment.