<jupyter_start><jupyter_text>Building an AI: Stage 1 Lunar LanderWelcome to this tutorial. We are going to learn how to teach and AI to land a space craft! On February 6th 2018 SpaceX Falcon Heavy took off.  Later the Falcon Heavy side boosters landed simultaneously at Kennedy Space Center. Click the link below to see the video of the boosters landing. Its amazing! [![Falcon Heavy Landing](https://img.youtube.com/vi/u0-pfzKbh2k/0.jpg)](https://www.youtube.com/watch?v=u0-pfzKbh2k)  Start by importing things we will needWe are going to use **OpenAI Gym** to create our Lunar Lander. Follow this link to learn more. http://gym.openai.com/OpenAI is a company founded by <NAME> and others. Their mission is to ensure *“AI benefits all of humanity”* The **gym** library helps us build our AI system. Don't worry too much about this this is just setup. Copy the code below to your workbook cell. ```pythonimport gymfrom gymclass import Notebook%matplotlib inline```Select the cell below by clicking on it and hit 'Run' to test it.<jupyter_code>import gym
from gymclass import Notebook
%matplotlib inline<jupyter_output><empty_output><jupyter_text>If nothing happens but a litte number appears then it worked!```In [*]  wait, the code is still running. In [1]  IT WORKED move to the next step !``` Theory of Environment, Agent, Observation and ActionThe environment is a virtual world that we wil use to train our agent. The environment we will use in this case is a 2D Lunar Lander simulation. The Agent is another word for AI. In this case our Agent will make decisions about what to do, what **action** to take, based on the information it receives from the **observations**. ![What we need](images/EnvironmentAndAgent.png)  Setup the environment Copy the code below to your workbook cell.```pythonenv = gym.make("LunarLander-v2")```<jupyter_code>env = gym.make("LunarLander-v2")<jupyter_output><empty_output><jupyter_text>Running the environment (without an agent)Lets write a simple program to run the environment. The environment is lie a movie. It works one video frame at a time, or one step at a time. Each step we can receive observations and then take an action the environment moves forward on step. We are going to use a **for loop** in this case and run the environment for 500 frames. This is enough time for the simulation to run and complete. Explaining how the environment runs.![What we need](images/EnvironmentCodeExplain.png)Copy the code below to your workbook cell.```pythonNotebook.setup()observation = env.reset()steps = 500for t in range(steps):    Notebook.record(env)    action = 1    observation, reward, done, info = env.step(action)    if done:        breakNotebook.replay() ```Remember we have no agent yet. The action is set to **0 = Nothing** so the action is to do nothing. What do you think will happen to our Lunar Lander?<jupyter_code>Notebook.setup()
observation = env.reset()
steps = 500
for t in range(steps):
    Notebook.record(env)
    action = 0
    observation, reward, done, info = env.step(action)
    if done:
        break
Notebook.replay()<jupyter_output><empty_output><jupyter_text>Lets have some fun! You see the code above with ```Pythonaction = 0```Try changeing the value of the action to something else 0, 1, 2 or 3 and see what happens. ![What actions do](images/ThrusterActions.png)Once you change the value, Run the code block above again.  Put the cursor in the code clock and click run!  Lets see what happens if we randomly fire rockets. Do you think we will still crash?<jupyter_code>import random 
Notebook.setup()
observation = env.reset()
steps = 500
for t in range(steps):
    Notebook.record(env)
    # Now we made it randomly fire rockets
    action = random.randint(1, 3)
    observation, reward, done, info = env.step(action)
    if done:
        break
Notebook.replay()<jupyter_output><empty_output><jupyter_text>What have we learned?1. We have learned to program a little python in 'Jupyter Notebooks' 2. We have imported libraries we need including the **gym** library3. We have setup the Lunar Lander environment4. We have run the simulation and we have seen what happens when we change **actions**5. We know that if we randomly take actions its still not good.  No AI Agent just yet... **Go to Stage 2 workbook to start to build the agent**<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07799954117916953, 'fraction_numerical': 0.009864647855012617, 'mean_word_length': 4.57544757033248, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '114576', 'license': 'MIT', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1207, 'n_words': 598, 'path': 'Stage 1 - Lunar Lander.ipynb', 'repo_name': 'GameBridgeAI/LunarLander', 'size': '4362'}