The Wayback Machine - https://web.archive.org/web/20240822111754/https://www.geeksforgeeks.org/q-learning-in-python/
Open In App

Q-Learning

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Reinforcement Learning is a paradigm of the Learning Process in which a learning agent learns, over time, to behave optimally in a certain environment by interacting continuously in the environment. The agent during its course of learning experiences various situations in the environment it is in. These are called states. The agent while being in that state may choose from a set of allowable actions which may fetch different rewards (or penalties). Over time, The learning agent learns to maximize these rewards to behave optimally at any given state it is in. Q-learning is a basic form of Reinforcement Learning that uses Q-values (also called action values) to iteratively improve the behavior of the learning agent.

This example helps us to better understand reinforcement learning.

Q-Learning

Q-Learning

Q-learning in Reinforcement Learning

Q-learning is a popular model-free reinforcement learning algorithm used in machine learning and artificial intelligence applications. It falls under the category of temporal difference learning techniques, in which an agent picks up new information by observing results, interacting with the environment, and getting feedback in the form of rewards.

Key Components of Q-learning

  1. Q-Values or Action-Values: Q-values are defined for states and actions. [Tex]Q(S, A) [/Tex] is an estimation of how good is it to take the action A at the state S . This estimation of [Tex]Q(S, A) [/Tex] will be iteratively computed using the TD- Update rule which we will see in the upcoming sections.
  2. Rewards and Episodes: An agent throughout its lifetime starts from a start state, and makes several transitions from its current state to a next state based on its choice of action and also the environment the agent is interacting in. At every step of transition, the agent from a state takes an action, observes a reward from the environment, and then transits to another state. If at any point in time, the agent ends up in one of the terminating states that means there are no further transitions possible. This is said to be the completion of an episode.
  3. Temporal Difference or TD-Update: The Temporal Difference or TD-Update rule can be represented as follows:
    [Tex]Q(S,A)\leftarrow Q(S,A) + \alpha (R + \gamma Q({S}’,{A}’) – Q(S,A)) [/Tex]This update rule to estimate the value of Q is applied at every time step of the agent’s interaction with the environment. The terms used are explained below:
    • S – Current State of the agent.
    • A – Current Action Picked according to some policy.
    • S’ – Next State where the agent ends up.
    • A’ – Next best action to be picked using current Q-value estimation, i.e. pick the action with the maximum Q-value in the next state.
    • R – Current Reward observed from the environment in Response of current action.
    • [Tex]\gamma [/Tex](>0 and <=1) : Discounting Factor for Future Rewards. Future rewards are less valuable than current rewards so they must be discounted. Since Q-value is an estimation of expected rewards from a state, discounting rule applies here as well.
    • [Tex]\alpha [/Tex]: Step length taken to update the estimation of Q(S, A).
  4. Selecting the Course of Action with Ïĩ-greedy policy: A simple method for selecting an action to take based on the current estimates of the Q-value is the Ïĩ-greedy policy. This is how it operates:

Superior Q-Value Action (Exploitation):

  • With a probability of 1−Ïĩ, representing the majority of cases,
  • Select the action with the highest Q-value at the moment.
  • In this instance of exploitation, the agent chooses the course of action that, given its current understanding, it feels is optimal.

Exploration through Random Action:

  • With probability Ïĩ, occasionally,
  • Rather than selecting the course of action with the highest Q-value,
  • Select any action at random, irrespective of Q-values.
  • In order to learn about the possible benefits of new actions, the agent engages in a type of exploration.

How does Q-Learning Works?

Q-learning models engage in an iterative process where various components collaborate to train the model. This iterative procedure encompasses the agent exploring the environment and continuously updating the model based on this exploration. The key components of Q-learning include:

  • Agents: Entities that operate within an environment, making decisions and taking actions.
  1. States: Variables that identify an agent’s current position in the environment.
  2. Actions: Operations undertaken by the agent in specific states.
  3. Rewards: Positive or negative responses provided to the agent based on its actions.
  4. Episodes: Instances where an agent concludes its actions, marking the end of an episode.
  5. Q-values: Metrics used to evaluate actions at specific states.

There are two methods for determining Q-values:

Temporal Difference: Calculated by comparing the current state and action values with the previous ones.

Bellman’s Equation: A recursive formula invented by Richard Bellman in 1957, used to calculate the value of a given state and determine its optimal position. It provides a recursive formula for calculating the value of a given state in a Markov Decision Process (MDP) and is particularly influential in the context of Q-learning and optimal decision-making.

The Equation is expressed as :

[Tex]Q(s,a) = R(s,a) + \gamma \;\; max_a Q(s’,a)[/Tex]

Where,

  • Q(s,a) is the Q-value for a given state-action pair
  • R(s,a) is the immediate reward for taking action a in state s.
  • gamma is the discount factor, representing the importance of future rewards.
  • maxa​Q(sâ€ē,a) is the maximum Q-value for the next state â€ēsâ€ē and all possible actions.

Bellman’s equation is crucial in reinforcement learning as it helps in evaluating the long-term expected rewards associated with different actions in a given state. It forms the basis for Q-learning algorithms, guiding agents to learn optimal policies through iterative updates based on observed experiences.

What is Q-table?

The Q-table functions as a repository of rewards associated with optimal actions for each state in a given environment. It serves as a guide for the agent, indicating which actions are likely to yield positive outcomes in various scenarios.

Each row in the Q-table corresponds to a distinct situation the agent might face, while the columns represent the available actions. Through interactions with the environment and the receipt of rewards or penalties, the Q-table is dynamically updated to capture the model’s evolving understanding.

Reinforcement learning aims to enhance performance by refining the Q-table, enabling the agent to make informed decisions. As the Q-table undergoes continuous updates with more feedback, it becomes a more accurate resource, empowering the agent to make optimal choices and achieve superior results.

Crucially, the Q-table is closely tied to the Q-function, a mathematical expression that considers the current state and action, generating outputs that include anticipated future rewards for that specific state-action pair. By consulting the Q-table, the agent can retrieve expected future rewards, guiding it toward optimized decision-making and states.

Implementation of Q-Learning

Defining Enviroment and parameters

Python

import numpy as np # Define the environment n_states = 16 # Number of states in the grid world n_actions = 4 # Number of possible actions (up, down, left, right) goal_state = 15 # Goal state # Initialize Q-table with zeros Q_table = np.zeros((n_states, n_actions)) # Define parameters learning_rate = 0.8 discount_factor = 0.95 exploration_prob = 0.2 epochs = 1000

n this Q-learning implementation, a grid world environment is defined with 16 states, and agents can take 4 possible actions: up, down, left, and right. The goal is to reach state 15. The Q-table, initialized with zeros, serves as a memory to store Q-values for state-action pairs.

The learning parameters include a learning rate of 0.8, a discount factor of 0.95, an exploration probability of 0.2, and a total of 1000 training epochs. The learning rate influences the weight given to new information, the discount factor adjusts the importance of future rewards, and the exploration probability determines the likelihood of the agent exploring new actions versus exploiting known actions.

Throughout the training epochs, the agent explores the environment, updating Q-values based on received rewards and future expectations, ultimately learning a strategy to navigate the grid world towards the goal state.

Implement Q-Algorithm

Python

# Q-learning algorithm for epoch in range(epochs): current_state = np.random.randint(0, n_states) # Start from a random state while current_state != goal_state: # Choose action with epsilon-greedy strategy if np.random.rand() < exploration_prob: action = np.random.randint(0, n_actions) # Explore else: action = np.argmax(Q_table[current_state]) # Exploit # Simulate the environment (move to the next state) # For simplicity, move to the next state next_state = (current_state + 1) % n_states # Define a simple reward function (1 if the goal state is reached, 0 otherwise) reward = 1 if next_state == goal_state else 0 # Update Q-value using the Q-learning update rule Q_table[current_state, action] += learning_rate * \ (reward + discount_factor * np.max(Q_table[next_state]) - Q_table[current_state, action]) current_state = next_state # Move to the next state # After training, the Q-table represents the learned Q-values print("Learned Q-table:") print(Q_table)

Output:

Learned Q-table:
[[0.48767498 0.48377358 0.48751874 0.48377357]
[0.51252074 0.51317781 0.51334071 0.51334208]
[0.54036009 0.5403255 0.54018713 0.54036009]
[0.56880009 0.56880009 0.56880008 0.56880009]
[0.59873694 0.59873694 0.59873694 0.59873694]
[0.63024941 0.63024941 0.63024941 0.63024941]
[0.66342043 0.66342043 0.66342043 0.66342043]
[0.6983373 0.6983373 0.6983373 0.6983373 ]
[0.73509189 0.73509189 0.73509189 0.73509189]
[0.77378094 0.77378094 0.77378094 0.77378094]
[0.81450625 0.81450625 0.81450625 0.81450625]
[0.857375 0.857375 0.857375 0.857375 ]
[0.9025 0.9025 0.9025 0.9025 ]
[0.95 0.95 0.95 0.95 ]
[1. 1. 1. 1. ]
[0. 0. 0. 0. ]]

The Q-learning algorithm involves iterative training where the agent explores and updates its Q-table. It starts from a random state, selects actions via epsilon-greedy strategy, and simulates movements. A reward function grants a 1 for reaching the goal state. Q-values update using the Q-learning rule, combining received and expected rewards. This process continues until the agent learns optimal strategies. The final Q-table represents acquired state-action values after training.

Q-learning Advantages and Disadvantages

Advantages:

  • Long-term outcomes, which are exceedingly challenging to accomplish, are best achieved with this strategy.
  • This learning paradigm closely resembles how people learn. Consequently, it is almost ideal.
  • The model has the ability to fix mistakes made during training.
  • Once a model has fixed a mistake, there is virtually little probability that it will happen again.
  • It can produce the ideal model to address a certain issue.

Disadvantages:

  • Drawback of using actual samples. Think about the situation of robot learning, for instance. The hardware for robots is typically quite expensive, subject to deterioration, and in need of meticulous upkeep. The expense of fixing a robot system is high.
  • Instead of abandoning reinforcement learning altogether, we can combine it with other techniques to alleviate many of its difficulties. Deep learning and reinforcement learning are one common combo.

Q-learning Applications

Applications for Q-learning, a reinforcement learning algorithm, can be found in many different fields. Here are a few noteworthy instances:

Playing Games:

  • Atari Games: Classic Atari 2600 games can now be played with Q-learning. In games like Space Invaders and Breakout, Deep Q Networks (DQN), an extension of Q-learning that makes use of deep neural networks, has demonstrated superhuman performance.

Automation:

  • Robot Control: Q-learning is used in robotics to perform tasks like navigation and robot control. With Q-learning algorithms, robots can learn to navigate through environments, avoid obstacles, and maximise their movements.

Driverless Automobiles:

  • Traffic Management: Autonomous vehicle traffic management systems use Q-learning. It lessens congestion and enhances traffic flow overall by optimising route planning and traffic signal timings.

Finance:

  • Algorithmic Trading: The use of Q-learning to make trading decisions has been investigated in algorithmic trading. It makes it possible for automated agents to pick up the best strategies from past market data and adjust to shifting market conditions.

Health Care:

  • Personalized Treatment Plans: To make treatment plans more unique, Q-learning is used in the medical field. Through the use of patient data, agents are able to recommend personalized interventions that account for individual responses to various treatments.

Energy Management:

  • Smart Grids: Energy management systems for smart grids employ Q-learning. It aids in maximizing energy use, achieving supply and demand equilibrium, and enhancing the effectiveness of energy distribution.

Education:

  • Adaptive Learning Systems: Adaptive learning systems make use of Q-learning. These systems adjust the educational material and level of difficulty according to each student’s performance and learning style using Q-learning algorithms.

Recommendations Systems:

  • Content Recommendation: To customise content recommendations, recommendation systems use Q-learning. To increase user satisfaction, agents pick up on user preferences and modify recommendations accordingly.

Resources Management:

  • Network Resource Allocation: Allocating bandwidth in communication networks is one example of how network resource management uses Q-learning. It aids in resource allocation optimisation for improved network performance.

Space Travel:

  • Satellite Control: Autonomous satellite control is possible with Q-learning. Agents are trained in the best movements and activities for satellite operations in orbit.

Frequently Asked Questions (FAQs) on Q-Learning

Q. What is Q-learning?

A machine learning technique called Q-learning allows a model to learn iteratively and get better over time by making the right decisions. One kind of reinforcement learning is Q-learning.

Q. What is Reinforcement Learning?

A machine learning technique known as reinforcement learning uses feedback to teach an agent how to behave in a given environment by having it perform actions and observe the outcomes of those actions. The agent receives positive feedback for each action they take that goes well and negative feedback or a penalty for each action they take that goes wrong.

Q. What is a recommendations system?

A software programme that gives users recommendations or suggestions is called a recommendation system. These systems employ algorithms to examine user preferences and actions in order to make product, movie, or article recommendations.

Q. Why is Q-learning used?

Q-Learning is a Reinforcement learning policy designed to determine the optimal course of action based on the current state. It selects this action at random with the goal of getting the biggest reward.



Previous Article
Next Article

Similar Reads

Introduction to Multi-Task Learning(MTL) for Deep Learning
Multi-Task Learning (MTL) is a type of machine learning technique where a model is trained to perform multiple tasks simultaneously. In deep learning, MTL refers to training a neural network to perform multiple tasks by sharing some of the network's layers and parameters across tasks. In MTL, the goal is to improve the generalization performance of
6 min read
Artificial intelligence vs Machine Learning vs Deep Learning
Nowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are related to each other but not the same. Let's see
4 min read
Need of Data Structures and Algorithms for Deep Learning and Machine Learning
Deep Learning is a field that is heavily based on Mathematics and you need to have a good understanding of Data Structures and Algorithms to solve the mathematical problems optimally. Data Structures and Algorithms can be used to determine how a problem is represented internally or how the actual storage pattern works &amp; what is happening under
6 min read
Fusion Learning - The One Shot Federated Learning
Introduction Machine Learning has improved our lives significantly. Right from the intelligent chatbots to autonomous cars. The main ingredient which improves these models to perform beyond expectation is data. With the digitization and increased popularity of IoT, more and more people have devices that are generating immense amounts of quality dat
5 min read
Machine Learning - Learning VS Designing
In this article, we will learn about Learning and Designing and what are the main differences between them. In Machine learning, the term learning refers to any process by which a system improves performance by using experience and past data. It is kind of an iterative process and every time the system gets improved though one may not see a drastic
3 min read
Passive and Active learning in Machine Learning
Machine learning is a subfield of artificial intelligence that deals with the creation of algorithms that can learn and improve themselves without explicit programming. One of the most critical factors that contribute to the success of a machine learning model is the quality and quantity of data used to train it. Passive learning and active learnin
3 min read
Automated Machine Learning for Supervised Learning using R
Automated Machine Learning (AutoML) is an approach that aims to automate various stages of the machine learning process, making it easier for users with limited machine learning expertise to build high-performing models. AutoML is particularly useful in supervised learning, where you have labeled data and want to create models that can make predict
8 min read
Difference Between Machine Learning and Deep Learning
If you are interested in building your career in the IT industry then you must have come across the term Data Science which is a booming field in terms of technologies and job availability as well. In this article, we will learn about the two major fields in Data Science that are Machine Learning and Deep Learning. So, that you can choose which fie
6 min read
Meta-Learning in Machine Learning
Traditional machine learning requires a huge dataset that is specific to a particular task and wishes to train a model for regression or classification purposes using these datasets. That’s radically far from how humans take advantage of their past experiences to learn quickly a new task from only a handset of examples. What is Meta Learning?Meta-l
13 min read
Continual Learning in Machine Learning
As we know Machine Learning (ML) is a subfield of artificial intelligence that specializes in growing algorithms that learn from statistics and make predictions or choices without being explicitly programmed. It has revolutionized many industries by permitting computer systems to understand styles, make tips, and perform tasks that were soon consid
10 min read
Few-shot learning in Machine Learning
What is a Few-shot learning?Few-shot learning is a type of meta-learning process. It is a process in which a model possesses the capability to autonomously acquire knowledge and improve its performance through self-learning. It is a process like teaching the model to recognize things or do tasks, but instead of overwhelming it with a lot of example
8 min read
Can I Use Unsupervised Learning Followed by Supervised Learning?
Answer : Yes, you can use unsupervised learning to discover patterns or features and then apply supervised learning for prediction or classification tasks.Combining unsupervised learning with supervised learning is a powerful strategy that leverages the strengths of both approaches to enhance the performance of machine learning models. This combina
2 min read
Real-Life Examples of Supervised Learning and Unsupervised Learning
Two primary branches of machine learning, supervised learning and unsupervised learning, form the foundation of various applications. This article explores examples in both learnings, shedding light on diverse applications and showcasing the versatility of machine learning in addressing real-world challenges. Examples of Supervised Learning and Uns
6 min read
Collaborative Learning - Federated Learning
The field of data science has seen significant developments in the past five years. Traditional Machine Learning training relied on large datasets which were stored in centralized locations like data centers, and the goal was to get accurate predictions and generate insights that will profit us in the end. But, this approach came with challenges li
6 min read
What Is Meta-Learning in Machine Learning in R
In traditional machine learning, models are typically trained on a specific dataset for a specific task, and their performance is optimized for that particular task. However, in R Programming Language the focus is on building models that can leverage prior knowledge or experience to quickly adapt to new tasks with minimal additional training data.
7 min read
Types of Federated Learning in Machine Learning
Federated Learning is a powerful technique that allow a single machine to learn from many different source and converting the data into small pieces sending them to different Federated Learning (FL) is a decentralized of the machine learning paradigm that can enables to model training across various devices while preserving your data the data priva
5 min read
Machine Learning-based Recommendation Systems for E-learning
In today's digital age, e-learning platforms are transforming education by giving students unprecedented access to a wide range of courses and resources. Machine learning-based recommendation systems have emerged as critical tools for effectively navigating this vast amount of content. The article delves into the role of recommendation systems in e
9 min read
Batch (Offline) learning vs Online learning in Artificial Intelligence
Computer science has been defined as the science of utilizing technology to understand data, and Artificial Intelligence, especially machine learning, has offered precise approaches to analyzing and drawing inferences from data. There is a major classification of machine learning known as batch learning and the other one is called online learning.
7 min read
Advances in Meta-Learning: Learning to Learn
Meta-learning, or "learning to learn," is a fascinating and rapidly growing field within machine learning. This concept refers to models that can improve their learning process based on past experiences, adapting to new tasks with minimal data. Meta-learning aims to enhance the efficiency and effectiveness of machine-learning algorithms by leveragi
5 min read
Understanding PAC Learning: Theoretical Foundations and Practical Applications in Machine Learning
In the vast landscape of machine learning, understanding how algorithms learn from data is crucial. Probably Approximately Correct (PAC) learning stands as a cornerstone theory, offering insights into the fundamental question of how much data is needed for learning algorithms to reliably generalize to unseen instances. PAC learning provides a theor
8 min read
One Shot Learning in Machine Learning
One-shot learning is a machine learning paradigm aiming to recognize objects or patterns from a limited number of training examples, often just a single instance. Traditional machine learning models typically require large amounts of labeled data for high performance. Still, one-shot learning seeks to overcome this limitation by enabling models to
7 min read
Difference Between Artificial Intelligence vs Machine Learning vs Deep Learning
Artificial Intelligence is basically the mechanism to incorporate human intelligence into machines through a set of rules(algorithm). AI is a combination of two words: "Artificial" meaning something made by humans or non-natural things and "Intelligence" meaning the ability to understand or think accordingly. Another definition could be that "AI is
14 min read
Applications of Machine Learning
Machine learning is one of the most exciting technologies that one would have ever come across. As is evident from the name, it gives the computer that which makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect. Today, companies are using Machine Lear
5 min read
Demystifying Machine Learning
Machine Learning". Now that's a word that packs a punch! Machine learning is hot stuff these days! And why won’t it be? Almost every "enticing" new development in the field of Computer Science and Software Development, in general, has something related to machine learning behind the veils. Microsoft's Cortana - Machine Learning. Object and Face Rec
7 min read
How To Use Classification Machine Learning Algorithms in Weka ?
Weka tool is an open-source tool developed by students of Waikato university which stands for Waikato Environment for Knowledge Analysis having all inbuilt machine learning algorithms. It is used for solving real-life problems using data mining techniques. The tool was developed using the Java programming language so that it is platform-independent
3 min read
Support vector machine in Machine Learning
In this article, we are going to discuss the support vector machine in machine learning. We will also cover the advantages and disadvantages and application for the same. Let's discuss them one by one. Support Vector Machines : Support vector machine is a supervised learning system and is used for classification and regression problems. Support vec
9 min read
ML | Introduction to Data in Machine Learning
Data is a crucial component in the field of Machine Learning. It refers to the set of observations or measurements that can be used to train a machine-learning model. The quality and quantity of data available for training and testing play a significant role in determining the performance of a machine-learning model. Data can be in various forms su
10 min read
Decision Threshold In Machine Learning
What is Decision Threshold ? sklearn does not let us set the decision threshold directly, but it gives us the access to decision scores ( Decision function o/p ) that is used to make the prediction. We can select the best score from decision function output and set it as Decision Threshold value and consider all those Decision score values which ar
4 min read
Creating a simple machine learning model
Create a Linear Regression Model in Python using a randomly created data set. Linear Regression Model Linear regression geeks for geeks Generating the Training Set # python library to generate random numbers from random import randint # the limit within which random numbers are generated TRAIN_SET_LIMIT = 1000 # to create exactly 100 data items TRA
2 min read
How Amazon Uses Machine Learning?
In modern times, Amazon is everywhere!!! And while Machine Learning has long been a part of Amazon, now it seems that ML is everywhere! From Amazon Alexa to Amazon AWS to even Amazon Prime, everything uses Machine Learning. And these are only the more common items! In fact, Amazon is heavily invested in Machine Learning Research in almost all imagi
11 min read
three90RightbarBannerImg