AI Handbook
AI Handbook
AI Handbook
Applications of AI :
Virtual Assistants: AI powers virtual assistants like Siri and Alexa to understand
and respond to voice commands.
Recommendation Systems: AI algorithms suggest products or content based
on user preferences and behavior, as seen on Netflix or Amazon.
Image Recognition: AI identifies and categorizes objects in images, used in
applications like facial recognition and medical imaging.
Fraud Detection: AI detects unusual patterns in financial transactions to
identify potential fraud.
Autonomous Vehicles: AI enables self-driving cars to navigate and make
decisions in real time.
Chatbots: AI-driven chatbots handle customer service inquiries and provide
instant responses.
CHAPTER 2 : AGENTS
Agent : Agent is an entity that perceives its environment through sensors and
acts upon that environment through actuators.
Agent Examples:
Agents entities :
Environment: The part of the universe that is relevant for designing and
evaluating the agent’s actions.
Percept: The content sensed by the agent’s sensors at a specific time.
Percept Sequence: The complete history of all percepts an agent has
experienced.
Agent Function (f): A mapping from a percept sequence to an action that
determines the agent’s behavior.
Agent Program: The implementation of the agent function that executes the
mapping from percept sequences to actions.
Rationality:
Rationality in the context of artificial intelligence refers to the quality of an
agent that enables it to make decisions and take actions that maximize its
performance, given the knowledge it has about the environment.
Rational agent: take actions that are expected to maximize performance
measure
Performance Measure: In the context of rationality in artificial intelligence, a
performance measure is a quantitative criterion used to evaluate how well an
agent is performing its tasks or achieving its goals. It provides a standard by
which an agent’s actions and decisions can be assessed in terms of their
effectiveness and efficiency.
• Rationality at any given time depends on
• Performance measure (the criterion of success)
• Agent’s prior knowledge of the environment
• Available actions
• Percept sequence
Rationality ≠ Omniscience:
Rationality does not mean that an agent knows everything or can predict the
future. Instead, a rational agent makes the best possible decision based on the
information it has at the time. Omniscience would imply perfect knowledge
and foresight, which is unrealistic for any agent in a complex environment.
Do not expect the agent to always take the best action in hindsight:
Rationality doesn't require that the agent’s actions are always optimal after the
fact. An agent can make a rational decision given the information available at
the moment, but later, it may turn out that another action would have been
better due to unforeseen circumstances. Rational agents make decisions based
on what is reasonable, not perfect, at the time of action.
Nature of Environments:
The PEAS (Performance measure, Environment, Actuators, Sensors) description
for a self-driving car task environment would be:
Performance Measure: Safety (minimizing accidents and injuries),
efficiency (optimizing route and fuel consumption), comfort (smooth ride
and minimal passenger discomfort), and adherence to traffic laws.
Environment: The road network, traffic signals, other vehicles,
pedestrians, road conditions, and weather conditions.
Actuators: Steering wheel, brakes, accelerator, indicators, and possibly
other control mechanisms like horn and windshield wipers.
Sensors: Cameras, LIDAR, radar, GPS, ultrasonic sensors, and possibly
other sensors like temperature and humidity sensors.
Agent Program :
1) Table-Driven Agent :
Looks up in the table to find the action: Whenever the agent receives a
new percept, it checks the percept sequence and looks up the
corresponding action in the table.
How It Works
1. Predefined Table: The agent uses a table, known as the action table,
which contains a comprehensive mapping from percept sequences to
actions. Each entry in the table corresponds to a particular sequence of
percepts and specifies the appropriate action for that sequence.
3. Lookup Process: The agent looks up the current percept sequence in the
action table. The table contains predefined responses for all possible
percept sequences that the agent might encounter.
4. Action Selection: Based on the percept sequence found in the table, the
agent selects the corresponding action. This action is then executed
using the agent's actuators.
2) Reflex Agent :
A Reflex Agent acts based solely on the current percept, ignoring the history
of past percepts. It makes decisions by applying a set of condition-action
rules, often referred to as if-then rules. Here’s a breakdown of its
functioning:
No Percept History: Unlike table-driven agents, reflex agents do not store
or rely on a sequence of percepts. They respond only to the current
situation.
Condition-Action Rules: The agent operates by matching the current
percept with a predefined set of condition-action rules. If a condition is
met, it executes the corresponding action.
o Example: "If car ahead is too close, then brake."
Simple and Fast: Reflex agents are typically fast and efficient because
they don't need to store or process large amounts of data (like percept
sequences).
Works Best in Fully Observable, Predictable Environments: Reflex agents
perform well when the environment is fully observable, meaning the
agent has all the information it needs from the current percept to make
a decision.
Example:
In a simple vacuum cleaner agent:
Percept: Dirty floor.
Rule: "If dirty, then suck."
Action: The vacuum cleans the dirt whenever it detects it, but it won't
store information about whether it cleaned a particular spot in the past.
3) Model-Based Agent:
A Model-Based Agent is an intelligent agent that maintains an internal state
to track aspects of the environment that are not directly observable at any
given moment. This internal state is updated based on a model of how the
world evolves and how the agent's actions affect the environment. The
agent uses this model to handle partially observable environments more
effectively than reflex agents.
Key Components of a Model-Based Agent:
1. Internal State:
o The agent keeps an internal record of the state of the environment
that it cannot directly observe. This helps the agent maintain
useful information about the world over time, allowing it to make
decisions even with incomplete data.
2. Transition Model:
o This is a model of how the environment changes over time,
particularly as a result of the agent’s actions. It defines the
relationship between the current state of the environment and the
likely next state.
o Example: The agent knows that if it drives a certain speed for a
given amount of time, it will cover a specific distance. This helps it
predict future states based on current and past information.
3. Sensor Model:
o The sensor model defines how the agent’s perceptions (percepts)
relate to the state of the world. It provides an understanding of
how the environment reflects into the agent's sensors.
o Example: If it perceives darkness and headlights are on, the agent
infers that the sun has set, even if it hasn't directly observed the
sunset.
Example:
Consider a self-driving car (model-based agent):
Internal State: The car keeps track of road conditions, its location, and
nearby traffic.
Transition Model: If the car accelerates, it knows how its speed will
change the distance covered over time.
Sensor Model: Based on sensory inputs like lighting and traffic signals,
the car can infer environmental conditions such as night or day, even if it
can't directly see the sun.
4) Goal-Based Agent:
•Need for Goals: When multiple actions seem rational but lead to
different outcomes, a goal helps guide the agent's decision-making.
•Methods:
oEpisodic tasks: Simple, isolated tasks with no long-term effects.
Example: A robot assigned a new task adjusts its actions to achieve the
new goal.
5) Utility-Based Agents :
Utility-Based Agents:
6) Learning Agents:
Concept: Learning agents are designed to improve their performance over
time by learning from their experiences rather than relying solely on pre-
programmed instructions. This approach, inspired by Turing's vision in 1950,
is particularly useful in environments that are complex or unknown.
Components:
Performance Element: Initially, this is the core component that selects
actions based on the current state of the environment. It represents the
agent's behavior and decision-making process.
Learning Element: This component enhances the agent's ability by
incorporating feedback from a critic. It uses this feedback to refine and
improve the performance element, enabling the agent to adapt and
perform better over time.
Critic: Provides feedback on the agent’s actions and overall performance.
The critic evaluates how well the performance element is achieving its
goals and identifies areas for improvement.
Problem Generator: Proposes new actions or strategies for exploration.
While some of these suggestions may be sub-optimal in the short term,
they help the agent explore new possibilities and discover more effective
strategies in the long run.
Short-Term Sub-Optimality: The problem generator might suggest actions
that are not immediately optimal but are intended to explore and learn
about the environment.
Agent Representations:
1. Atomic:
o Description: Represents the state as an indivisible entity or "black
box" with no internal structure.
o Characteristics: Simple and minimal, but lacks detailed internal
representation.
o Example: A basic sensor that outputs a single value, like whether a
room is occupied or not.
2. Factored:
o Description: Represents the state as a vector of values, where
each value corresponds to a particular attribute or feature of the
state.
o Characteristics: More detailed than atomic representation; allows
for the decomposition of the state into multiple factors or
dimensions.
o Example: A robot's state might be represented by a vector
containing its position coordinates, battery level, and orientation.
3. Structured:
o Description: Represents the state with objects that can interact
with one another. It captures complex relationships and
interactions within the state.
o Characteristics: Most complex and expressive, allowing detailed
representation of states and their components.
o Example: A smart home system where the state includes various
objects like lights, thermostats, and doors, and their interactions
(e.g., lights turning on when the door is opened).
CHAPTER 3 SEARCH
Problem-Solving Agents:
A problem-solving agent aims to find a sequence of actions that lead to a
desired goal state. Here’s how it operates:
1. Goal Formulation:
o Description: Defines what the agent is trying to achieve, which
helps narrow down the available actions. It sets clear objectives
for the agent to work towards.
o Effect: Limits the action choices by focusing only on those that
contribute to achieving the goal.
2. Problem Formulation:
o Description: Provides a detailed description of the states, actions,
and transitions needed to reach the goal. It outlines the problem
space and how to navigate it.
o Effect: Sets up a structured approach for the agent to understand
and address the problem.
3. Search:
o Description: The agent explores various sequences of actions in its
model to find a viable path to the goal. It simulates different
scenarios and evaluates their outcomes.
o Effect: Produces a solution by identifying a sequence of actions
that successfully lead to the goal.
4. Execution:
o Open Loop System: In fully observable, deterministic, and known
environments, the agent can execute actions based on its plan
without needing to continuously monitor the environment. The
execution follows a predetermined sequence.
Example: A robotic arm performing a repetitive
manufacturing task.
o Closed Loop System: In partially observable or nondeterministic
environments, the agent must continuously monitor its percepts
and adjust its actions accordingly. This allows the agent to handle
unexpected changes and uncertainties in the environment.
Example: A self-driving car adjusting its route in response to
real-time traffic conditions.
1. State Space:
o Description: A collection of all possible states that the
environment can be in. This defines the entire set of conditions or
configurations the agent may encounter.
o Example: For a navigation problem, the state space includes all
cities and possible locations.
2. Initial State:
o Description: The starting point from which the agent begins its
journey. This is the specific state where the agent starts its search.
o Example: The initial state might be "Arad" in a travel problem.
3. Goal States:
o Description: One or more states that represent the objectives or
desired end conditions the agent aims to achieve.
o Example: In a travel problem, the goal states could be "Bucharest"
or any other destination.
4. Actions:
o Description: The set of possible actions available to the agent
from a given state. For each state sss, the function ACTIONS(s)
returns a set of actions that can be executed.
o Example: For the state "Arad", the actions might be {ToSibiu,
ToTimisoara, ToZerind}.
5. Transition Model:
o Description: Defines what happens when an action is executed in
a given state. It describes how the state changes in response to an
action.
o Function: RESULT(s, a) returns the new state that results from
performing action a in state s.
o Example: If the agent is in "Arad" and takes the action "ToSibiu",
the transition model might result in moving to the state "Sibiu".
State-Space Graph :
The state space can be represented as a graph in which the vertices are states
and the directed edges between them are actions.
Search Algorithms :
• A search algorithm takes a search problem as input and returns a solution or
indicates failure
Search Tree:
Concept: A search tree is a conceptual structure used to represent the
paths an agent explores while attempting to solve a problem. Each node
in the tree represents a state, and the branches represent actions
leading to new states.
• Node corresponds to a state in the state space
• Edge corresponds to an action
• The root of the tree corresponds to the initial state
• Search tree describes paths between states leading to the goal
• A state may appear multiple times in the search tree
Frontier:
Definition: The frontier (green in diagrams) is the boundary separating
the explored (interior) part of the search tree from the unexplored
(exterior) part. It consists of nodes that have been generated but not yet
expanded.
Expansion: A search algorithm chooses which frontier node to expand
next, meaning it explores the possible actions that can be taken from
that state.
Search Algorithm:
Task: The algorithm determines the order in which frontier nodes are
expanded. Different algorithms prioritize nodes differently (e.g., depth-
first, breadth-first).
Goal: The search continues until a goal state is reached, expanding
frontier nodes step by step.
Search Tree vs. State-Space Graph:
Search Tree: Represents various paths from the initial state toward the
goal, as the agent explores possible actions.
State-Space Graph: The underlying structure showing all possible states
and actions in the environment.
Relationship: The search tree is superimposed over the state-space
graph, tracing out paths from the initial state in an attempt to find the
goal.
Best-First Search :
Evaluation Function f(n):
Definition: Each node n is assigned an evaluation function f(n), which
determines the desirability of expanding that node.
Function: The value of f(n) can change over time depending on the
specific search algorithm being used.
Different Algorithms:
The choice of f(n) results in different search algorithms. For example:
o Greedy Best-First Search: f(n)=h(n) ,where h(n) is the heuristic
estimating the cost to reach the goal.
o A Search*: f(n)=g(n)+h(n), where g(n) is the cost to reach node n,
and h(n) is the estimated cost to the goal.
Selecting Nodes:
Out of all the nodes in the frontier, the algorithm selects the node with
the smallest f(n), as it is considered the most promising for reaching the
goal.
Multiple Frontier Additions:
A node may be added to the frontier multiple times if the agent
discovers a lower-cost path to that node. This ensures that the agent
always follows the most efficient route.
2. Frontier:
o Definition: The frontier is a queue that stores nodes yet to be
explored.
Operations on Frontier:
o IS-EMPTY(frontier): Returns true if the frontier has no nodes.
o POP(frontier): Removes and returns the top node from the
frontier.
o TOP(frontier): Returns the top node without removing it.
o ADD(node, frontier): Inserts the node in its proper place in the
frontier (depending on the search algorithm).
3. Types of Queues:
o Priority Queue: Pops the node with the minimum cost.
Usage: Best-First Search and A* Search.
o FIFO (First-In-First-Out) Queue: Pops the node that was added to
the queue the earliest.
Usage: Breadth-First Search (BFS).
o LIFO (Last-In-First-Out) Queue or Stack: Pops the most recently
added node.
Usage: Depth-First Search (DFS).
Performance Metrics for Search Algorithms:
1. Completeness:
o Definition: Determines if the algorithm always finds a solution
when one exists. It also evaluates whether the algorithm correctly
reports failure when no solution exists.
2. Cost Optimality:
o Definition: Measures whether the algorithm finds the solution
with the lowest possible cost (optimal solution).
3. Time Complexity:
o Definition: Refers to the time required by the algorithm to find a
solution, which is typically based on the number of states and
actions considered during the search process.
4. Space Complexity:
o Definition: Refers to the amount of memory or storage the
algorithm needs to perform the search, particularly for managing
nodes, frontiers, and paths.
Key Components:
1. Heuristic Function h(n):
o A heuristic function estimates the cost from the current state to
the goal.
o h(n)h(n)h(n) provides an estimate of how "close" the current node
is to the goal.
o The quality of the heuristic significantly impacts the performance
of the search.
o Example: In a route-finding problem, h(n) might represent the
straight-line distance to the goal.
2. Evaluation Function:
o The evaluation function f(n) determines which node to expand
next.
o It combines the heuristic estimate with the actual cost of reaching
the node (for optimal algorithms).
o Two common evaluation functions are:
f(n)=h(n) (used in greedy best-first search).
f(n)=g(n)+h(n) , where g(n) is the cost to reach node n from
the start (used in A* search).
Consistent Heuristic
Consistency is a property of heuristic functions used in search algorithms like
A*. It ensures that the heuristic does not underestimate the true cost, and it
maintains certain properties related to cost estimation.
Definition:
Consistent Heuristic: A heuristic h is consistent if, for every node n, every
action a leading to a successor n′, and the cost c(n,a,n′) of taking action a
from n to n′, the following condition holds: h(n)≤c(n,a,n′)+h(n′)
Key Points:
1. Triangle Inequality: The heuristic value h(n) should not be more than the
cost of reaching n′ plus the heuristic value h(n′). This ensures that taking
a path through n′ does not reduce the estimated cost to reach the goal.
2. Properties:
o Every Consistent Heuristic is Admissible: If a heuristic is
consistent, it is also admissible, meaning it never overestimates
the true cost to reach the goal.
o Not All Admissible Heuristics are Consistent: A heuristic can be
admissible but not consistent, as consistency is a stricter
condition.
3. Implications for A* Search:
o Optimal Path: With a consistent heuristic, the first time A*
reaches a node n, it will be along an optimal path.
o Node Expansion: A* will not expand any node with f(n) greater
than the optimal cost C∗ This is because consistent heuristics
ensure that the cost function f(n)=g(n)+h(n) will be tightly bound
to the true path costs.
4. Example:
o In a grid-based pathfinding problem, a consistent heuristic could
be the Manhattan distance (for a grid where movements are
restricted to horizontal and vertical directions) since it always
underestimates or exactly matches the true cost to the goal.
Weighted A* - Satisficing Search :
Weighted A* Search is a variant of the A* algorithm that adjusts the balance
between the cost to reach a node and the heuristic estimate of the cost to
reach the goal. It allows for quicker solutions at the expense of optimality.
Key Concepts:
1. Satisficing:
o Definition: The approach of finding a solution that is "good
enough" rather than the absolute optimal. It is particularly useful
when A* expands too many nodes and finding the perfect solution
is computationally expensive.
2. Weighted A* Search:
o Formula: f(n)=g(n)+W⋅h(n), where W>1.
g(n): Cost to reach node n from the start.
h(n): Heuristic estimate of the cost from n to the goal.
W: Weight factor that amplifies the heuristic function.
3. Detour Index:
o Usage: Adjusts the heuristic to account for factors such as road
curvatures or obstacles. It is used to modify the straight-line
distance to more accurately reflect the true cost.
o Example: If the detour index is 1.5, it means the heuristic value
will be 1.5 times the straight-line distance, accounting for
additional road curvature.
4. Example:
In route planning with road curves, if the heuristic function is the straight-
line distance and the roads have significant detours, Weighted A* might use
a detour index to better approximate the actual travel cost. For instance,
using W=2W = 2W=2 would make the search more focused on the heuristic
but could lead to faster, though potentially suboptimal, route discovery.
Improvements to A* Search :
A* Search can be memory-intensive, storing all explored nodes. Several
improvements address this issue, making A* more efficient in terms of memory
usage.
1. Iterative Deepening A* (IDA*)
Overview: IDA* modifies A* by using a depth-first approach with
iterative deepening.
Key Concepts:
o Cutoff: Instead of limiting search depth, IDA* uses a cutoff based
on the f-cost (f(n)=g(n)+h(n)).
o Iteration: The search is performed with increasing f-cost limits.
Each iteration explores nodes within the current f-cost cutoff.
o Bounded Iterations: The number of iterations is bounded by the
optimal cost C∗ if the f-cost values are integers.
Advantages:
o Memory Efficiency: Uses less memory than traditional A* as it
only stores nodes along the current search path.
o Complete: Guarantees to find a solution if one exists, similar to A*.
2. Recursive Best-First Search (RBFS)
Overview: RBFS is a recursive approach that focuses on the best node
within a limited f-cost.
Key Concepts:
o f-Limit: The algorithm maintains a limit on the f-value,
representing the best alternative path found so far.
o Recursion: It performs depth-first search within the f-limit. If a
node's cost exceeds the f-limit, the recursion unwinds.
o Backtracking: If the recursion limit is exceeded, the search
backtracks to explore other paths.
Advantages:
o Memory Efficiency: Uses less memory than A* as it avoids storing
all nodes. Only the current path and best alternative are kept in
memory.
o Completeness: RBFS is complete and will find a solution if one
exists, given that the heuristic is admissible and consistent.
TREE CONSTRUCTION
Root Node: Represents the initial state of the world (e.g., the
vacuum's starting position and the initial state of the dirt).
Children of the Root Node: Each child represents a possible action
the vacuum can take (e.g., move up, move down, or clean the
current position).
SEARCH STRATEGY
Key Concepts
1. Game Tree: The possible moves are represented in a tree structure,
where each node represents a game state.
2. Min and Max Players: One player (Max) aims to maximize their score,
while the other player (Min) aims to minimize Max's score.
3. Leaf Nodes: The end states of the game (win, lose, draw) are evaluated
to assign a score.
Algorithm Steps
1. Generate the Game Tree: Start from the current game state and generate
all possible future states up to a certain depth or until the game ends.
2. Evaluate Terminal Nodes: For terminal states, assign scores:
o Win for Max: High positive value (e.g., +10)
o Win for Min: High negative value (e.g., -10)
o Draw: Neutral value (e.g., 0)
3. Backpropagation:
o For each non-terminal node:
If it's Max's turn, select the maximum score from its
children.
If it's Min's turn, select the minimum score from its children.
4. Choose the Best Move: The root node (current state) will then have a
score, and the best move is the one that leads to the child with the
optimal score.
Applications
Board games (chess, tic-tac-toe)
Strategy games
Decision-making in artificial intelligence
Pseudocode
CHAPTER 6 : CONSTRAINT SATISFACTION PROBLEM