diff --git a/earthmover-research/.gitignore b/earthmover-research/.gitignore new file mode 100644 index 0000000..87620ac --- /dev/null +++ b/earthmover-research/.gitignore @@ -0,0 +1 @@ +.ipynb_checkpoints/ diff --git a/earthmover-research/lunar-lander/Lunar Landing Reinforcement Learning.ipynb b/earthmover-research/lunar-lander/Lunar Landing Reinforcement Learning.ipynb new file mode 100644 index 0000000..bd54130 --- /dev/null +++ b/earthmover-research/lunar-lander/Lunar Landing Reinforcement Learning.ipynb @@ -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 +} diff --git a/earthmover-research/lunar-lander/lunar_lander.gif b/earthmover-research/lunar-lander/lunar_lander.gif new file mode 100644 index 0000000..342e41d Binary files /dev/null and b/earthmover-research/lunar-lander/lunar_lander.gif differ diff --git a/earthmover-research/lunar-lander/lunar_lander_multiple_episodes.gif b/earthmover-research/lunar-lander/lunar_lander_multiple_episodes.gif new file mode 100644 index 0000000..21ab9b7 Binary files /dev/null and b/earthmover-research/lunar-lander/lunar_lander_multiple_episodes.gif differ