Hello, my name is Nizar Tejani, and in these lessons you will learn about OpenAI Gym, QLearn, and DeepQ networks. And in this lesson, you will learn about OpenAI Gym. So far, we have discussed supervised machine learning and unsupervised machine learning. And now let's discuss another type. Reinforcement learning is a machine learning subfield that focuses on enabling an agent to learn optimal decision-making strategies by interacting with an environment. This interaction involves the agent taking actions in a specific state to achieve a goal while receiving feedback in the form of rewards. Key components of reinforcement learning include the agent, the environment, states, actions, rewards, and policies. The primary objective is to find the optimal policy that guides the agent to maximize its cumulative reward over time. Common reinforcement learning algorithms include QLearning and DeepQ networks, among others. Now OpenAI Gym is an essential tool in the world of reinforcement learning. It's an open-source platform and toolkit that serves as a standardized testing ground for developing and evaluating reinforcement learning algorithms. Gym provides a diverse array of environments, ranging from basic grid-world scenarios to complex physics simulations. These environments enable researchers and developers to benchmark an experiment with the reinforcement learning algorithms effectively. OpenAI Gym offers a consistent and simple API for interacting with these environments, making it possible to individuals of various skill levels. Researchers can leverage Gym's reproducibility features, which include standardized environments and evaluation metrics to compare and replicate results across the different reinforcement learning experiments. Additionally, it's highly extensible, allowing users to create custom environments tailored to their specific problem. Now OpenAI Gym is called Gym because it provides a framework for developing and testing reinforcement learning algorithms in a way that is analogous to how Gymnasiums or fitness centers provide a space for physical exercise and training. The term Gym is used metaphorically to emphasize that OpenAI Gym is a place where you can exercise and train your reinforcement learning agents. You have access to a variety of environments that represent different reinforcement learning problems and challenges. These environments allow you to train and evaluate your reinforcement learning agents, helping them become more capable and skilled in solving complex tasks. Alright, let's switch gears and take a look at a code example to see OpenAI Gym in action. As you can see from the first line of code, we import the OpenAI Gym library, which provides a wide range of reinforcement learning environments for testing and developing reinforcement learning algorithms. Now by default, this library is not installed in your Python environment, so you will need to open a terminal window and run the pip install Gym command to be able to use this library. I already have it installed so I can continue with our code. The next line of code we create a cart poll V1 environment using the GymMake method. The cart poll V1 environment represents the classic cart poll problem where you need to balance a poll on a moving cart. OpenAI Gym provides a wide range of common environments that cover various reinforcement learning tasks. These environments are categorized into different categories based on their characteristics and complexity. For example, we have Box2D. These environments use the Box2D physics engine and offer a variety of physics-based challenges. We also have Atari. These environments use the arcade learning environment and are based on classic Atari 2600 games like Pong and Space Invaders. We also have robotics, which are environments that involve robotic control and manipulation like controlling a robotic arm to reach a target. So these are just a few examples of the common environments available in OpenAI Gym. There are many more environments, each with its own unique characteristics and challenges. You can explore and use these environments to develop, test and evaluate reinforcement learning algorithms. Alright, let's continue with our next line of code and define some variables. These lines initialize two variables to keep track of the total reward accumulated over episodes and the total number of steps taken in all episodes. Then this variable defines the number of episodes. And in this example, we are going to run 10 episodes in the cart poll environment. Next we have a loop that iterates over the specified number of episodes. At the beginning of each episode, we reset the environment by calling the nth reset method. This initializes the cart and poll to their starting positions. And we initialize that done variable to false to indicate that the episode is not yet finished. Next we have a while loop, which continues until the episode is done. It's the main control loop for each episode. Then this line renders the environment, which is optional, but it allows you to visualize what's happening. You can comment this line out if you don't want to see the visualization. Next line of code sets the action variable to zero. In this code example, we use a very simple policy. We always take action zero, which means pushing the cart to the left. In a more complex policy, the cart will be moving left and right randomly. Next line of code steps the environment by applying the chosen action. It returns the next state, the reward obtained, a flag indicating if the episode is done, and additional information, which we ignore by using the underscore character. Then the next two lines update the total reward and total steps with the reward obtained in the current step. Next we update the current state with the next state to continue the loop. Then after all the episodes are completed, we should close the environment to release any resources. And finally, the code prints the total reward accumulated over the specified number of episodes and the average reward per step. This helps to evaluate the agent's performance in balancing the poll. Now, this was a very simple example of OpenAI Gym. The code uses a very basic policy in which the cart always moves to the left, and it doesn't implement a reinforcement learning algorithm. It's more of a baseline example to demonstrate how to set up and interact with an OpenAI Gym environment. In practice, though, you would use reinforcement learning algorithms to develop a policy that learns to balance the poll effectively. So let's run this code and see what we get back. The output for the provided code is the total reward accumulated over the specified number of episodes and the average reward per step. So our total reward here is the cumulative sum of rewards obtained by the agent over all 10 episodes. In this case, the agent collected a total reward of 233 across those episodes. This value is an indicator of how well the agent is performing. A higher total reward suggests better performance, of course. Then we see the average reward per step, which is the total reward divided by the total number of steps taken by the agent throughout the 10 episodes. It represents the average reward earned for each action taken by the agent. This metric gives you an idea of the efficiency of the agent's actions. A higher average reward per step indicates that the agent is making good decisions. Now in the context of the cart poll environment, the goal is to balance the poll on a moving cart for as long as possible. The output values indicate how well the agent performed in this task. In this specific run, the agent received a total reward of 233, which is considered a reasonable performance. The average reward per step is approximately 2.65, suggesting that, on average, the agent is earning positive rewards for each step it takes, which is a good sign. Keep in mind that these values can vary each time you run the code due to the randomness in the environment and the agent's behavior. So in this lesson, you learned about OpenAI Gym, and in the next lesson, you will learn about QLearn. Thanks for watching.