Tron - Week 4 beginner project

    Tron - Week 4 beginner project

    Welcome back to week 4!

    By AI Club on 10/7/2024
    0

    Welcome to Week 4 of our Tron AI journey! This week, we're diving into the fascinating world of Deep Reinforcement Learning (DRL) by tackling a classic problem: balancing a pole on a moving cart. Don't worry if this sounds complex - we'll break it down step by step, using everyday examples to explain the concepts.

    Introduction to Deep Reinforcement Learning

    Imagine you're teaching your little sister to ride a bike. You can't ride the bike for her, but you can give her tips and encouragement. When she keeps the bike upright, you cheer. When she wobbles or falls, you give her advice on how to improve. Over time, through trial and error and your feedback, she learns to balance and ride smoothly.

    This is the essence of Reinforcement Learning:

    1. Your sister is the Agent - the learner trying to master a skill.

    2. The bike and the world around her is the Environment - the area in which she's operating

    3. The position of the bike, its speed, the angle of lean etc., is the State - the current situation.

    4. Turning the handlebars, pedaling, leaning, etc., are Actions - things she can do to affect her state.

    5. Your cheers or advice are the Reward - feedback on how well she's doing.

    The "Deep" in Deep Reinforcement Learning comes from using Deep Neural Networks - think of these as a really smart, trainable calculator that can recognize patterns and make decisions.

    The Cart-Pole Problem: A Balancing Act

    Now, let's look at our specific challenge: the Cart-Pole problem. Imagine you're at a carnival, and there's a game where you need to balance a broomstick on your palm. The broomstick is the pole, and your hand is the cart. You can move your hand left or right to keep the broomstick upright.

    In our AI version:

    • The Agent is our AI program.

    • The Environment is a simulated world with a cart and a pole.

    • The State includes the cart's position, its speed, the pole's angle, and how fast the pole is tipping.

    • The Actions are moving the cart left or right.

    • The Reward is +1 for each time step the pole stays upright.

    Building Our AI: Step by Step

    Let's break down each part of our code and understand what it does.

    Step 1: Setting Up the Environment

    Before we can teach our AI to balance a pole, we need to create a world for it to practice in. In the real world, we might build a physical cart with a pole. But in the world of AI, we can create a simulated environment on our computer. This is where the gym library comes in handy.

    What is Gym?

    gym is a toolkit for developing and comparing reinforcement learning algorithms. It provides a wide variety of simulated environments, from simple text-based games to complex physics simulations. Think of it as a massive virtual playground where AIs can learn and practice different skills.

    In our case, we'll be using the Cart-Pole environment, which simulates a cart that can move left or right, with a pole balanced on top of it.

    Let's break down the code step by step:

    Download the following file.

    cart_pole_env.py

    Let's explain each line

    "import gym": This line imports the gym library, giving us access to all its pre-built environments and tools.

    "from gym.wrappers import FlattenObservation": Gym provides "wrappers" that can modify the behavior of environments.

    FlattenObservation is one such wrapper. We'll explain its purpose soon.

    "def create_env(render_mode=None))": This line defines a function named create_env. The render_mode parameter is optional (that's what =None means) and determines how the environment will be displayed.

    env = gym.make('CartPole-v1', render_mode=render_mode)": This line creates our Cart-Pole environment. gym.make() is like a factory that produces environments. 'CartPole-v1' is the specific environment we want. The 'v1' means it's version 1 of the Cart-Pole environment.

    "return FlattenObservation(env)": This line wraps our environment with FlattenObservation, but why do we need this?

    Understanding FlattenObservation

    In reinforcement learning, the AI needs to understand the current state of the environment to make decisions. This state information is often represented as a list of numbers.

    Some environments provide this state information in a complex format, like a list of lists or a dictionary. FlattenObservation takes this complex format and "flattens" it into a simple list of numbers. This makes it easier for our AI to process the information.

    Real-world analogy: Imagine you're teaching someone to ride a bike. Instead of giving them separate pieces of information like "the bike is leaning 5 degrees left, you're going 2 mph, the handlebars are turned 10 degrees right", you might simplify it to "you're about to fall to the left, pedal faster and turn right a bit". That's what FlattenObservation does - it simplifies the information for our AI.

    Why This Matters.

    By setting up the environment this way, we're creating a consistent, simplified world for our AI to learn in. It's like creating a safe, flat area for a child to practice riding a bike, with clear markers and simple instructions. This setup allows our AI to focus on learning the core task (balancing the pole) without getting confused by complex environmental details.

    In the next step, we'll create our AI agent that will learn to navigate this environment and balance the pole!

    Step 2: Creating Our AI Agent

    Now that we have our environment set up, it's time to create our AI agent - the "brain" that will learn to balance the pole. We'll use a technique called Deep Q-Network (DQN), which combines deep learning (the "deep" part) with Q-learning (a type of reinforcement learning).

    Let's break this down step by step:

    Download the following file:

    dqn_agent.py

    Let's explain the packages we're importing:

    • numpy (imported as np): A library for numerical computations. We'll use it for handling arrays and mathematical operations.

    • random: Used for generating random numbers, which is important for exploration in reinforcement learning.

    • deque from collections:

    • tensorflow: A powerful machine learning library. We're using it as the backend for Keras.

    • models, layers, and optimizers from keras: These are the building blocks we'll use to create our neural network.

    Explaining our DQNAgent class:

    • The __init__ method sets up our agent with all the necessary attributes.

    • state_size and action_size: These define the "shape" of our problem - how much information the agent receives about the world, and how many different actions it can take.

    • memory: This is like the agent's diary. It can store up to 2000 experiences that the agent can learn from later.

    • gamma: This is the "discount rate". It determines how much the agent cares about future rewards compared to immediate rewards. A value of 0.95 means it cares quite a bit about the future.

    • epsilon, epsilon_min, and epsilon_decay: These control the agent's exploration vs. exploitation behavior. We'll explain this more later.

    • learning_rate: This determines how quickly the agent updates its knowledge based on new information.

    • model and target_model: These are the neural networks that the agent will use to make decisions and learn. We use two models to make the learning process more stable.

    Real-world analogy: Imagine you're setting up a new student to learn a skill. You give them a notebook (memory), teach them to value long-term success (gamma), encourage them to try new things but also to rely on what they know (epsilon), and give them a brain to process information (model and target_model).

    Next, let's look at the _build_model method. This method creates a neural network.

    • It's a "Sequential" model, which means the layers are stacked one after another.

    • It has three "Dense" layers. Dense layers are fully connected, meaning each neuron in one layer is connected to every neuron in the next layer.

    • The first two layers have 24 neurons each and use the 'relu' activation function, which helps the network learn complex patterns.

    • The last layer has as many neurons as there are possible actions, allowing the network to estimate the value of each action.

    • We compile the model with mean squared error (mse) as the loss function and Adam as the optimizer.

    Real-world analogy: This is like creating a brain for our AI. The layers are like different levels of understanding, from basic recognition to complex decision-making.

    The remember method is simple but crucial. This method stores a single experience in the agent's memory. Each experience includes:

    • The current state

    • The action taken

    • The reward received

    • The next state

    • Whether the episode is done

    Real-world analogy: This is like writing in a diary after each practice session, noting what you did, what happened, and how it turned out.

    The act method determines what action the agent will take. The method implements an "epsilon-greedy" strategy.

    • With probability epsilon, the agent chooses a random action (exploration).

    • Otherwise, it chooses the action that its model predicts will be best (exploitation).

    • As training progresses, epsilon decreases, so the agent explores less and exploits more.

    Real-world analogy: This is like deciding whether to try a new technique or stick with what you know when learning a skill. At first, you try lots of new things, but as you get better, you rely more on what you've learned works well.

    The replay method is where the actual learning happens. This method does the following:

    1. Samples a batch of experiences from memory.

    2. Calculates the target Q-values using the Bellman equation.

    3. Updates the model to better predict these target Q-values.

    4. Decreases the exploration rate (epsilon) over time.

    Real-world analogy: This is like reviewing your diary entries, figuring out what techniques worked best in different situations, and updating your strategy based on this review.

    Finally, we have methods to save and load the model's weights. These methods allow us to save our agent's learned knowledge and reload it later, so we don't have to retrain from scratch every time.

    Real-world analogy: This is like writing down everything you've learned in a book, so you can quickly refresh your memory later instead of having to relearn everything from the beginning.

    In the next step, we'll put all of this together and actually train our agent to balance the pole!

    Step 3: Putting It All Together

    Now that we have our environment (Step 1) and our AI agent (Step 2), it's time to bring everything together and actually train our AI to balance the pole. We'll do this in our main script.

    Download the following file:

    main.py

    Here's what each import does:

    • numpy (as np): For numerical operations.

    • create_env from cart_pole_env: The function we created in Step 1 to set up our Cart-Pole environment.

    • DQNAgent from dqn_agent:

    • time: Our AI agent class from Step 2.

    • time: We'll use this to add small delays in our visualization.

    • os and tensorflow: These are used to suppress some warning messages that TensorFlow might produce.

    The last two lines are just telling TensorFlow to be quiet and not print out a bunch of messages that might confuse us.

    The process_state function is a utility that ensures our state data is in the right format for our agent to use.

    The visualize_agent function is more complex, it does the following.

    1. Resets the environment to start a new episode.

    2. Runs the episode, letting the agent make decisions at each step.

    3. Renders the environment so we can see what's happening.

    4. Keeps track of how long the episode lasts and what the total reward is.

    5. Adds small delays so the visualization isn't too fast for us to see.

    Real-world analogy: This is like setting up a camera to record a student's bike-riding attempt, and then playing back the recording in slow motion so we can see exactly what happened.

    The main training loop is where the magic happens.

    1. We create our environment and our agent, setting up the size of the state and action spaces.

    2. We set up our training parameters:

      1. batch_size = 32: This is how many experiences the agent will learn from at once.

      2. EPISODES = 1000: This is how many full games (episodes) the agent will play.

    3. We enter a big loop that runs for each episode:

      • We reset the environment to start a new game.

      • We run the game for up to 500 time steps or until it's over.

      • At each step, the agent chooses an action, we apply it to the environment, and we store this experience.

      • If the agent has enough memories, it learns from a batch of them.

      • We keep track of the total reward.

    4. After each episode:

      • We update the agent's target model (this helps stabilize learning).

      • We print out information about how the episode went.

      • Every 5 episodes, we visualize the agent's performance.

      • Every 50 episodes, we save the agent's learned knowledge.

    Real-world analogy: This is like setting up a series of bike-riding lessons. Each episode is one lesson. During each lesson, the student (our agent) tries to ride the bike many times. After each attempt, they remember what happened. When they have enough memories, they reflect on them to try to improve. After each lesson, we make a note of how they did. Every so often, we record a video of their attempt, and periodically we write down everything they've learned so far.

    Running The Simulation

    To run this simulation:

    • Make sure you have the required libraries installed. You can do this with pip: pip install gym numpy tensorflow

    • Put all three Python files (cart_pole_env.py, dqn_agent.py, and main.py) in the same directory.

    • Run the main script: python main.py

    • Watch as your AI learns to balance the pole!

    As the simulation runs, you'll see output like this:

    Episode: 0/1000, Score: 17, Total Reward: 17.0, Epsilon: 0.98

    Episode: 1/1000, Score: 23, Total Reward: 23.0, Epsilon: 0.97
    Episode: 2/1000, Score: 15, Total Reward: 15.0, Epsilon: 0.96

    • Episode: the current training session number.

    • Score: How many steps the pole remained balanced.

    • Total Reward: The total reward received (in this case, equal to the score).

    • Epsilon: The exploration rate. It starts high (lots of random actions) and decreases over time (more calculated actions).

    Every 5 episodes, you'll see a visualization of how the AI is performing. At first, it might drop the pole quickly, but over time, it should learn to keep it balanced for longer periods.

    Congratulations! You've just built and trained an AI to balance a pole on a cart. This is a great introduction to the world of Deep Reinforcement Learning. Remember, learning AI is a journey. It's okay if some concepts are still fuzzy - they'll become clearer with practice and experience. Keep experimenting, asking questions, and most importantly, have fun with it!

    Conclusion and Next Steps

    Congratulations! You've just built an AI that can learn to balance a pole on a cart. This is a great introduction to the world of Deep Reinforcement Learning.

    Remember, learning AI is a journey. It's okay if some concepts are still fuzzy - they'll become clearer with practice and experience. Keep experimenting, asking questions, and most importantly, have fun with it!

    Bonus Challenges

    If you're feeling adventurous, here are some ways to extend your Cart-Pole AI:

    1. Try modifying the reward structure. What happens if you give a bigger penalty for dropping the pole?

    2. Experiment with different neural network architectures. What if you add more layers or change the number of neurons?

    Comments