0% found this document useful (0 votes)
17 views224 pages

AI Notes - Module 1 To Module 3 - 4

Uploaded by

honeybunnyyyy37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views224 pages

AI Notes - Module 1 To Module 3 - 4

Uploaded by

honeybunnyyyy37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 224

Module 1

• Introduction: What is AI?


• Foundations and History of AI Intelligent
• Agents: Agents and environment
• Concept of Rationality
• The nature of environment
• The structure of agents.
Introduction to AI
• Artificial Intelligence (AI) refers to the simulation of human
intelligence in machines that are programmed to think and
learn like humans.
• The goal of AI is to develop systems that can perform tasks
that normally require human intelligence, such as visual
perception, speech recognition, decision-making, and language
translation.
AI can be categorized into two types:
1. Narrow AI (Weak AI): This type of AI is designed and trained
for a particular task. It excels in performing specific tasks but
lacks the broad cognitive abilities of human intelligence.
Examples include virtual personal assistants like Siri and Alexa.
2. General AI (Strong AI): This type of AI possesses the ability
to understand, learn, and apply knowledge across different
domains, similar to human intelligence. General AI is still a
theoretical concept and has not been fully realized.
Foundations of AI:
The foundations of AI rest on several key concepts and techniques:
1. Machine Learning (ML): ML is a subset of AI that involves the development of
algorithms and statistical models that enable computers to perform a task without
explicit programming. It includes supervised learning, unsupervised learning, and
reinforcement learning.
2. Neural Networks: Inspired by the structure of the human brain, neural networks
are computational models used in machine learning. Deep learning, a subfield of
ML, employs deep neural networks with multiple layers (deep neural networks) to
learn and make decisions.
3. Natural Language Processing (NLP): NLP focuses on the interaction between
computers and human language. It enables machines to understand, interpret, and
generate human-like text. Applications include language translation, chatbots, and
sentiment analysis.
4. Computer Vision: This field involves teaching machines to interpret and make
decisions based on visual data. Computer vision is used in facial recognition,
image classification, and autonomous vehicles.
History of AI:
The history of AI can be traced back to ancient times with the development of
mechanical devices designed to imitate human behavior. However, modern AI has
its roots in the mid-20th century:
1. 1950s-1960s: The term "artificial intelligence" was coined, and early AI research
focused on symbolic reasoning and problem-solving. Alan Turing proposed the
Turing Test as a measure of machine intelligence.
2. 1970s-1980s: AI faced challenges and criticisms, leading to the "AI winter." Expert
systems, rule-based systems that emulated human expertise, gained popularity
during this period.
3. 1990s-2000s: Machine learning approaches, such as neural networks, gained
traction. The development of more powerful computers and the availability of large
datasets contributed to the resurgence of AI.
4. 2010s-present: Deep learning and neural networks revolutionized AI, achieving
breakthroughs in image and speech recognition. AI applications became
ubiquitous in areas like healthcare, finance, and autonomous systems.
What is AI???
• Bellman 1978- the automation of activities that we associate with
human thinking, activities such as decision making, problem solving,
learning.
• Charniak +McDErmott 1985- the study of mind through the use of
computational models.
• Rich + Knight 1991-the study of how to make computers do things at
which at the moment people are better.
• Luger Stubblefield 1993-the branch of computer science that is
concerned with the automation of intelligent behaviour.
AI falls into four categories
• Thinking Humanly
• Thinking Rationally
• Acting Humanly
• Acting Rationally
• When discussing AI, the terms "Thinking Humanly," "Thinking
Rationally," "Acting Humanly," and "Acting Rationally" are often
associated with different perspectives on the goals and
capabilities of artificial intelligence systems. These concepts are
linked to four broad categories of AI:
1. Thinking Humanly:
1. Objective: The goal is to create AI systems that mimic human
cognitive processes and reasoning.
2. Approach: This approach involves understanding human thought
processes, including perception, problem-solving, decision-making,
and learning, and then implementing those processes in AI systems.
3. Example: Cognitive architectures that model human cognition and
reasoning, attempting to replicate the way humans think.
2. Thinking Rationally:
1. Objective: The emphasis is on creating AI systems that exhibit
rational thinking and logical reasoning.
2. Approach: This approach involves formalizing human knowledge and
reasoning using logic and rules. It aims to create AI systems that make
decisions based on sound logical principles.
3. Example: Expert systems, which use rules and knowledge bases to
make decisions in specific domains.
1. Thinking Humanly:
1. Example: Cognitive Architectures
1. Description: Cognitive architectures aim to replicate human cognitive processes,
including perception, learning, and problem-solving. They model aspects of
human cognition to achieve a more human-like AI.
2. Application: Soar (Symbolic Operators in a Realistic Architecture) is an
example of a cognitive architecture that seeks to emulate the human mind's
problem-solving and decision-making processes.
2. Thinking Rationally:
1. Example: Expert Systems
1. Description: Expert systems use rules and knowledge bases to make decisions
in specific domains. They formalize human expertise and logic to arrive at
conclusions or solutions.
2. Application: MYCIN, developed in the 1970s, was an expert system designed to
diagnose bacterial infections and recommend antibiotic treatments based on
logical rules and medical knowledge.
3. Acting Humanly:
1. Objective: The goal is to create AI systems that can perform tasks in
a way that is indistinguishable from human behavior.
2. Approach: This approach focuses on enabling AI systems to mimic
human actions and interactions, including natural language
understanding, speech recognition, and computer vision.
3. Example: Chatbots and virtual assistants that engage in natural
language conversations with users, aiming to emulate human
communication.
4. Acting Rationally:
4. Objective: The emphasis is on creating AI systems that make
decisions and take actions that lead to optimal or rational outcomes,
even if the process doesn't mimic human thinking.
5. Approach: This approach involves designing AI systems that make
decisions based on well-defined goals and logical reasoning, aiming
for optimal solutions.
6. Example: Game-playing AI, such as AlphaGo, which employs
strategies and reasoning to make optimal moves in the game.
3. Acting Humanly:
1. Example: Chatbots and Virtual Assistants
1. Description: Chatbots and virtual assistants are designed to engage in natural
language conversations with users, attempting to mimic human communication
and interaction.
2. Application: Siri (Apple's virtual assistant) and Google Assistant are examples
of AI systems that act in a way that simulates human conversation, providing
information, answering questions, and performing tasks based on user input.
4. Acting Rationally:
2. Example: AlphaGo
1. Description: AlphaGo is a computer program developed by DeepMind to play
the board game Go. It uses a combination of deep neural networks and tree
search techniques to make optimal moves, aiming for rational decision-making
rather than mimicking human thought processes.
2. Application: AlphaGo made headlines by defeating world champion Go players,
demonstrating rational decision-making and strategic thinking.
Intelligent Agents: Agents and environment,
Concept of Rationality, The nature of
environment, The structure of agents
• Intelligent Agents:
In the context of artificial intelligence, an intelligent agent is a
system that perceives its environment, processes information
and takes actions to achieve specific goals.
The concept of intelligent agents is fundamental to
understanding how AI systems interact with their surroundings.
Let's delve into key components related
to intelligent agents:
Agents and Environment:
• Agent: An agent is anything that can be viewed as perceiving
its environment through sensors and acting upon that
environment through effectors. In the context of AI, agents are
typically computer programs or systems.
• Environment: The environment is the external context in
which the agent operates. It includes everything outside the
agent that can potentially affect or be affected by the agent's
actions.
Concept of Rationality:
• Rational Agent: An agent is considered rational if it selects
actions that maximize its expected performance measure,
given its perceptual history and built-in knowledge.
• Performance Measure: This is the criterion used to evaluate
the success of an agent's behavior. It is often defined based on
the goals or objectives the agent is designed to achieve.
The Nature of Environment:
• Fully Observable vs. Partially Observable: An environment
is fully observable if an agent's sensors give it access to the
complete state of the environment at all times. If some aspects
of the environment are not visible, it is partially observable.
• Deterministic vs. Stochastic: In a deterministic environment,
the next state of the environment is completely determined by
the current state and the agent's actions. In a stochastic
environment, there is uncertainty, and the next state is not
entirely predictable.
• Episodic vs. Sequential: In an episodic environment, each
episode is a self-contained sequence of actions and
perceptions. In a sequential environment, the current decision
can affect all future decisions.
The Structure of Agents:
• Simple Reflex Agents: These agents select actions based
only on the current percept, ignoring the rest of the percept
history. They are reactive and lack memory.
• Model-Based Reflex Agents: These agents maintain an
internal state that depends on the percept history. They use this
internal state to make more informed decisions.
• Goal-Based Agents: Goal-based agents are driven by goals
or objectives. They consider future states and select actions
that lead to desirable outcomes.
• Utility-Based Agents: These agents evaluate different
outcomes in terms of a utility function, which assigns a
numerical value to each state or action. The agent aims to
maximize expected utility.
Let's illustrate the concepts of intelligent agents, environments, and their
structures with some examples:
1. Simple Reflex Agent:
1. Example: A thermostat in a room is a simple reflex agent. It senses the
current temperature (percept) and takes an action (e.g., turning on the
heater or the air conditioner) based solely on the current temperature.
2. Model-Based Reflex Agent:
1. Example: An automatic vacuum cleaner is a model-based reflex agent. It
senses the current state of the room (percept history), maintains an internal
model of the room, and decides on actions (moving in a particular direction)
based on both the current state and the history of states.
1. Goal-Based Agent:
1. Example: An autonomous delivery drone is a goal-based agent. Its goal is
to deliver a package to a specified location. The drone considers its current
location, the destination, and potential obstacles in its decision-making
process to plan a route and reach the goal.
2. Utility-Based Agent:
1. Example: A ride-sharing app uses a utility-based agent. The app
considers various factors such as distance, time, traffic conditions, and
user preferences to determine the best route and driver to maximize user
satisfaction (utility).
3. Fully Observable Environment:
1. Example: Chess playing software operates in a fully observable
environment. The current state of the chessboard is entirely visible to the
program, allowing it to make informed decisions based on the complete
information.
1. Partially Observable and Stochastic Environment:
1. Example: A self-driving car in a city is in a partially observable and
stochastic environment. The car's sensors provide information about the
immediate surroundings, but unexpected events (e.g., pedestrians crossing
unpredictably) and changing traffic conditions introduce uncertainty.
2. Sequential Environment:
1. Example: A virtual assistant like Siri or Google Assistant operates in a
sequential environment. It processes a series of user commands and
maintains context over multiple interactions to provide coherent and relevant
responses.
Intelligent agent
Nature of Environment
Agent Program
Example
Simple
Reflex
Agents
Model-Based Agents
• An effective way to handle partial observability is for the agent to
keep track of the part of the world it can’t see now
• The agent should maintain some sort of internal state that depends
on the percept history and thereby reflects at least some of the
unobserved aspects of the current state.
• For the braking problem, the internal state is not too extensive— just
the previous frame from the camera, allowing the agent to detect
when two red lights at the edge of the vehicle go on or off
simultaneously
• For other driving tasks such as changing lanes, the agent needs to
keep track of where the other cars are if it can’t see them all at once.
And for any driving to be possible at all, the agent needs to keep
track of where its keys are.
• Updating this internal state information as time goes by requires two
kinds of knowledge to be encoded in the agent program.
• First, we need some information about how the world evolves
independently of the agent—for example, that an overtaking car
generally will be closer behind than it was a moment ago.
• Second, we need some information about how the agent’s own
actions affect the world—for example, that when the agent turns the
steering wheel clockwise, the car turns to the right, or that after
driving for five minutes northbound on the freeway, one is usually
about five miles north of where one was five minutes ago.
• This knowledge about “how the world works”—whether
implemented in simple Boolean circuits or in complete scientific
theories—is called a model of the world. An agent that uses such a
model is called a model-based agent
Goal Based Agents
• Knowing something about the current state of the environment is
not always enough to decide what to do.
• For example, at a road junction, the taxi can turn left, turn right, or
go straight on.
• The correct decision depends on where the taxi is trying to get to. In
other words, as well as GOAL as a current state description, the agent
needs some sort of goal information that describes desirable
situations—for example, being at the passenger’s destination.
Sometimes goal-based action selection is straightforward
for example when goal satisfaction results immediately from a single
action.
Sometimes it will be trickier
for example, when the agent has to consider long sequences of twists
and turns to find a way to achieve the goal.
Search and planning are the subfields of AI devoted to finding action
sequences that achieve the agent’s goals.
Reflex Agent Goal Based

For the reflex agent, on the other The goal-based agent appears less efficient,
hand, we would have to rewrite it is more flexible because the knowledge
many condition–action rules. that supports its decisions is represented
explicitly and can be modified. If it starts to
rain, the agent can update its knowledge of
how effectively its brakes will operate; this
Reflex agent will automatically cause all of the relevant
behaviors to be altered to suit the new
Vs Goal conditions
based agent The reflex agent’s rules for when to The goal-based agent’s behavior can easily
turn and when to go straight will be changed to go to a different destination,
work only for a single destination; simply by specifying that destination as
they must all be replaced to go the goal.
somewhere new.
Example: Example:
The reflex agent brakes when it A goal-based agent, in principle, could
sees brake lights reason that if the car in front has its brake
lights on, it will slow down.
Utility-based agents
• Goals alone are not enough to generate high-quality behavior in
most environments.
• For example, many action sequences will get the taxi to its
destination (thereby achieving the goal) but some are quicker, safer,
more reliable, or cheaper than others.
• Goals just provide a crude binary distinction between “happy” and
“unhappy” states.
• Because “happy” does not sound very scientific, economists and
computer scientists use the term utility instead
• A performance measure assigns a score to any given sequence of
environment states, so it can easily distinguish between more and
less desirable ways of UTILITY FUNCTION getting to the taxi’s
destination.
• An agent’s utility function is essentially an internalization of the
performance measure. If the internal utility function and the external
performance measure are in agreement, then an agent that chooses
actions to maximize its utility will be rational according to the
external performance measure
Learning agents
• Turing (1950) considers the idea of actually programming his
intelligent machines by hand.
• The method he proposes is to build learning machines and then to
teach them.
• In many areas of AI, this is now the preferred method for creating
state-of-the-art systems
• Learning allows the agent to operate in initially unknown
environments and to become more competent than its initial
knowledge alone might allow
• A learning agent can be divided into four conceptual components, as
shown in Fig.
• The most important distinction is between the learning element,
which is responsible for making improvements,
• the performance element, which is responsible for PERFORMANCE
ELEMENT selecting external actions.
• The performance element is what we have previously considered to
be the entire agent: it takes in percepts and decides on actions.
• The learning element uses CRITIC feedback from the critic on how
the agent is doing and determines how the performance element
should be modified to do better in the future
• The last component of the learning agent is the problem generator. It
is responsible for suggesting actions that will lead to new and
informative experiences.
How the components of agent programs
work
Atomic representation
• Each state of the world is indivisible, it has no internal structure
Example- Consider the problem of finding a driving route from one end
of a country to the other via some sequence of cities
The algorithms
search and game-playing
Hidden Markov models
Markov decision processes
Factored Representation
• Splits up each state into a fixed set of variables or attributes, each of
which can have a value
• Two different factored states can share some attributes (such as
being at some particular GPS location) and not others (such as having
lots of gas or having no gas);
• This makes it much easier to work out how to turn one state into
another.
• With factored representations, we can also represent
uncertainty—for example, ignorance about the amount of gas in the
tank can be represented by leaving that attribute blank
• Many important areas of AI are based on factored representations,
including
• Constraint satisfaction algorithms
• Propositional logic
• planning
• Bayesian networks
• machine learning algorithms
Structured Representations

• For many purposes, we need to understand the world as


having things in it that are related to each other, not just
variables with values.
• For example, we might notice that a large truck ahead of
us is reversing into the driveway of a dairy farm but a
cow has got loose and is blocking the truck’s path
• TruckAheadBackingIntoDairyFarmDrivewayBlockedByLoo
seCow with value true or false.
• Instead, we would need a structured representation, in
which objects such as cows and trucks and their various
and varying relationships can be described explicitly.
Structured
representations underlie
• Relational databases
and first-order logic
• First-order probability
models
• Knowledge-based
learning
• much of natural
language understanding
• An agent is something that perceives and acts in an
environment. The agent function for an agent specifies the
action taken by the agent in response to any percept
sequence.
• The performance measure evaluates the behavior of the
agent in an environment. A rational agent acts so as to
maximize the expected value of the performance measure,
given the percept sequence it has seen so far.
Summarize • A task environment specification includes the performance
measure, the external environment, the actuators, and the
sensors. In designing an agent, the first step must always be to
specify the task environment as fully as possible.
• Task environments vary along several significant dimensions.
They can be fully or partially observable, single-agent or
multiagent, deterministic or stochastic, episodic or sequential,
static or dynamic, discrete or continuous, and known or
unknown.
• The agent program implements the agent function.
There exists a variety of basic agent-program designs
reflecting the kind of information made explicit and
used in the decision process. The designs vary in
efficiency, compactness, and flexibility. The appropriate
design of the agent program depends on the nature of
the environment.
• Simple reflex agents respond directly to percepts,
whereas model-based reflex agents maintain internal
state to track aspects of the world that are not evident
in the current percept. Goal-based agents act to achieve
their goals, and utility-based agents try to maximize
their own expected “happiness.”
• All agents can improve their performance through
learning
Problem Solving Agents
Module 2
• Problem-solving agents are a fundamental concept in artificial
intelligence (AI). These agents are designed to analyze a given
problem, determine the sequence of actions that will lead to a
solution, and execute these actions to reach the desired goal.
Here are some key components and characteristics of problem-
solving agents in AI:
• Perception:
1. Agents need to perceive and interpret their environment. Perception
involves gathering information about the current state of the world or
problem. Sensors or input mechanisms are used to collect relevant
data.
• Problem Formulation:
1. This step involves defining the problem, identifying the initial state,
goal state, and possible actions. Problem formulation is crucial as it
determines how well an agent can navigate and explore possible
solutions.
Search and Exploration:
• Problem-solving often involves searching through a space of
possible solutions. Various algorithms, such as depth-first
search, breadth-first search, or heuristic search algorithms like
A* search, can be employed to explore the solution space
efficiently.
Representation of Knowledge:
• Agents need a way to represent knowledge about the problem
domain. This representation allows them to store, retrieve, and
manipulate information relevant to finding a solution. Common
representations include graphs, state spaces, rules, and logic.
Reasoning and Decision Making:
• Agents must be capable of reasoning to draw inferences from
the available information. Decision-making involves selecting
the best action or sequence of actions to move from the current
state to the goal state.
Learning:
• Problem-solving agents may incorporate learning mechanisms
to improve their performance over time. Learning can be based
on experience, feedback, or interactions with the environment.
Actuators and Execution:
• Agents need the ability to act upon the environment. Actuators
or output mechanisms enable agents to perform actions based
on their decisions. This step involves translating the agent's
plan into executable actions.
Feedback and Adaptation:
• Agents may receive feedback from the environment or users.
This feedback can be used to adapt the agent's strategies,
improve its decision-making, and enhance overall performance.
• Performance Metrics:
1. Agents are often evaluated based on their ability to reach goals or
solve problems efficiently. Performance metrics are used to measure
the success of the agent in achieving its objectives.
• Knowledge Base:
1. Agents may maintain a knowledge base or memory to store
information that can be used for future problem-solving. This allows
agents to leverage past experiences and knowledge.
Examples
• Pathfinding Agent:
• Problem: Finding the shortest path from a start point to a goal point
on a map.
• Components:
• Perception: The agent perceives the current state of the map and
obstacles.
• Problem Formulation: Defines the initial state (start point), goal state
(destination), and possible actions (move to neighboring locations).
• Search and Exploration: Utilizes search algorithms like A* to navigate
through the map efficiently.
• Representation of Knowledge: Represents the map as a graph with
nodes and edges.
• Actuators and Execution: Actuators move the agent from one location to
another.
• Chess-playing Agent:
• Problem: Winning a game of chess against an opponent.
• Components:
• Perception: The agent perceives the current state of the chessboard.
• Problem Formulation: Defines the initial state (chessboard
configuration), goal state (winning the game), and possible actions
(legal chess moves).
• Reasoning and Decision Making: Uses algorithms like minimax with
alpha-beta pruning to decide the best move.
• Representation of Knowledge: Represents the current chessboard
position and possible moves.
• Actuators and Execution: Actuators make the agent's moves on the
chessboard.
• Robot Navigation Agent:
• Problem: Navigating a robot through an environment to reach
a destination.
• Components:
• Perception: Sensors provide information about the robot's
surroundings.
• Problem Formulation: Defines the initial state (current robot location),
goal state (destination), and possible actions (move in various
directions).
• Learning: Learns from past experiences to avoid obstacles or
optimize paths.
• Actuators and Execution: Motors and actuators enable the robot to
move in the desired direction.
• Medical Diagnosis Agent:
• Problem: Diagnosing a medical condition based on patient
symptoms.
• Components:
• Perception: Collects information about the patient's symptoms and
medical history.
• Problem Formulation: Defines the initial state (patient information),
goal state (accurate diagnosis), and possible actions (order tests,
consult specialists).
• Reasoning and Decision Making: Utilizes expert systems or
machine learning algorithms to analyze symptoms and make a
diagnosis.
• Representation of Knowledge: Represents medical knowledge in
the form of rules or a knowledge base.
• Feedback and Adaptation: Learns from patient outcomes to improve
future diagnoses.
• Network Routing Agent:
• Problem: Efficiently routing data packets through a computer
network.
• Components:
• Perception: Monitors network traffic, topology, and link statuses.
• Problem Formulation: Defines the initial state (current network
configuration), goal state (delivering packets), and possible actions
(selecting optimal routes).
• Search and Exploration: Uses routing algorithms (e.g., Dijkstra's
algorithm) to find the best path for data packets.
• Actuators and Execution: Directs data packets along the chosen
routes.
• Natural Language Processing Agent:
• Problem: Understanding and generating human language.
• Components:
• Perception: Receives input in the form of text or speech.
• Problem Formulation: Defines the initial state (input text), goal state
(understanding or generating meaningful responses), and possible
actions (language analysis, generation).
• Learning: Learns from context and user interactions to improve
language understanding and response generation.
• Actuators and Execution: Generates text or speech as output.
• Supply Chain Management Agent:
• Problem: Optimizing the flow of goods and services in a
supply chain.
• Components:
• Perception: Monitors inventory levels, demand, and supply chain
logistics.
• Problem Formulation: Defines the initial state (current inventory and
demand), goal state (efficient supply chain operations), and possible
actions (ordering, distribution).
• Learning: Adapts to changing market conditions and demand
patterns.
• Actuators and Execution: Initiates orders, adjusts inventory levels,
and coordinates logistics.
• Game Playing Agent in a Real-Time Strategy (RTS) Game:
• Problem: Winning a real-time strategy game against
opponents.
• Components:
• Perception: Observes the game state, opponent actions, and map
information.
• Problem Formulation: Defines the initial state (game state), goal
state (winning the game), and possible actions (building units,
attacking, defending).
• Learning: Adapts strategies based on opponent behavior and game
dynamics.
• Actuators and Execution: Executes in-game actions to achieve
strategic objectives.
• Energy Management System Agent:
• Problem: Optimizing energy consumption in a smart grid.
• Components:
• Perception: Monitors energy usage, grid conditions, and renewable
energy sources.
• Problem Formulation: Defines the initial state (current energy
consumption), goal state (efficient energy usage), and possible
actions (load balancing, demand response).
• Learning: Adapts to changes in energy availability and demand
patterns.
• Actuators and Execution: Controls energy distribution and
consumption.
Well-defined problems and solutions
EXAMPLE PROBLEMS
• A toy problem is intended to illustrate or exercise various problem-
solving methods.
• It can be given a concise, exact description and hence is usable by
different researchers to compare the performance of algorithms.
• A real-world problem is one whose solutions people actually care
about.
• Such problems tend not to have a single agreed-upon description,
but we can give the general flavor of their formulations.
Toy problems
• The 8-puzzle, an instance of which is shown in Figure 3.4, consists of
a 3 x3 board with eight numbered tiles and a blank space.
• A tile adjacent to the blank space can slide into the space.
• The object is to reach a specified goal state, such as the one shown
on the tight of the figure. The standard formulation is as follows:
States: A state description specifies the location of each of the eight
Ides and the blank in one of the nine squares.
• Initial state: Any state can be designated as the initial state. Note that
any given goal can he reached Front exactly half of the possible
initial states
• Actions: The simplest formulation defines the actions as movements
of the blank space Left, Right, Up, or Down. Different subsets of these
are possible depending on where the blank is.
• Transition model: Given a state and action, this returns the resulting
state; for example, if we apply Left to start state in Figure 3.4, the
resulting state has the 5 and the blank switched.
• Goal test: This checks whether the state matches the goal
configuration shown in Figure 3.4. (Other goal configurations are
possible.)
• Path cost: Each step costs 1, so the path cost is the number of steps in
the path.
• The 8-puzzle belongs to the family of sliding-block puzzles, which are often
used as test problems for new search algorithms in AI.
• This family is known to be NP-complete, so one does not expect to find
methods significantly better in the worst case than the search algorithms
described in this chapter and the next.
• The 8-puzzle has 91/2 =181, 440 reachable states and is easily solved.
• The 15-puzzle (on a 4 x 4 board) has around 1.3 trillion states, and random
instances can be solved optimally in a few milliseconds by the best search
algorithms.
• The 24-puzzle (on a 5 x 5 board) has around 1025 states, and random
instances take several hours to solve optimally
8-queens problem
• The goal of the 8-queens problem is to place eight queens on a
chessboard such that no queen attacks any other. (A queen attacks
any piece in the same row, column or diago-nal.) Figure 3.5 shows an
attempted solution that fails; the queen in the rightmost column is
attacked by the queen at the top left
• Although efficient special-purpose algorithms exist for this problem and for the whole n-queens family, it
remains a useful test problem for search algorithms.
• There are two main kinds of formulation.
• An incremental formulation involves operators that augment the state description, starting with an empty state;
for the 8-queens problem, this means that each action adds a queen to the state.
• A complete-state formulation starts with all 8 queens on the board and moves them amend. In either case, the
path cost is of no interest because only the final state counts.
• The first incremental formulation one might try is the following:
• States: Any arrangement of 0 to 8 queens on the board is a state.
• Initial state: No queens on the board.
• Actions: Add a queen to any empty square.
• Transition model: Returns the board with a queen added to the specified square.
• Goal test: 8 queens are on the board, none attacked.
• In this formulation, we have 69 • 63 • • • 57 1.8 x 1014 possible sequences to investigate.
• A better formulation would prohibit placing a queen in any square that is already attacked:
• States: All possible arrangements of n queens (0 < rt < 8), one per column in the leftmost n. columns, with no
queen attacking another.
• Actions: Add a queen to any square in the leftmost empty column such that it is not attacked by any other queen.
Searching for solutions
• A solution is an action sequence, so search algorithms work by
considering various possible action sequences.
• The possible action sequences starting at the initial state form a
search tree with the initial state at the root; the branches are actions
and the nodes correspond to states in the state space of the problem.
• Figure 3.6 shows the first few steps in growing the search tree for
finding a route from Arad to Bucharest.
• The root node of the tree corresponds to the initial state, In(Arad).
The first step is to test whether this is a goal state.
• Then we need to consider taking various actions. We do this by
expanding the current state; that is, applying each legal action to the
current state, thereby generating a new set of states.
• In this case, we add three branches from the parent node In(Arad)
leading to three new child nodes: In(Sibiu), In(Timisoara), and
In(Zerind). Now we must choose which of these three possibilities to
consider further.
• This is the essence of search—following up one option now and putting
the others aside for later, in case the first choice does not lead to a
solution.
• Suppose we choose Sibiu first. We check to see whether it is a goal state
(it is not) and then expand it to get In(Arad), In(Fagaras), In(Oradea), and
In(RimnicuVilcea).
• We can then choose any of these four or go back and choose Timisoara
or Zerind. Each of these six nodes is a leaf node, that is, a node with no
children in the tree.
• The set of all leaf nodes available for expansion at any given point is
called the frontier. (Many authors call it the open list, which is both
geographically less evocative and less accurate because other data
structures are better suited than a list.)
• In Figure 3.6, the frontier of each tree consists of those nodes with bold
outlines.
• The process of expanding nodes on the frontier continues until
either a solution is found or there are no more states to expand.
• The general TREE SEARCH algorithm is shown informally in Figure
3.7. Search algorithms all share this basic structure; they vary
primarily according to how they choose which state to expand
next—the so-called search strategy.
• The eagle-eyed reader will notice one peculiar thing about the
search tree shown in Figure 3.6: it includes the path from Arad to
Sibiu and back to Arad again! We say that In(Arad) is a repeated
state in the search tree, generated in this case by a loopy path.
• Loopy paths are a special case of the more general concept of
redundant paths, which exist whenever there is more than one way
to get from one state to another.
• Consider the paths Arad–Sibiu (140 km long) and
Arad–Zerind–Oradea–Sibiu (297 km long).
• Obviously, the second path is redundant—it’s just a worse way to get
to the same state.
• If you are concerned about reaching the goal, there’s never any
reason to keep more than one path to any given state, because any
goal state that is reachable by extending one path is also reachable
by extending the other.
Infrastructure for search algorithms
• Search algorithms require a data structure to keep track of the search tree
that is being constructed.
• For each node n of the tree, we have a structure that contains four
components:
• n.STATE: the state in the state space to which the node corresponds;
• n.PARENT: the node in the search tree that generated this node;
• n.ACTION: the action that was applied to the parent to generate the
node;
• n.PATH-COST: the cost, traditionally denoted by g(n), of the path from
the initial state to the node, as indicated by the parent pointers.
Given the components for a parent node, it is easy to see how to compute the
necessary components for a child node. The function CHILD-NODE takes a parent
node and an action and returns the resulting child node:
• The frontier needs to be stored in such a way that the search algorithm can
easily choose the next node to expand according to its preferred strategy.
• The appropriate data structure for this is a queue. The operations on a
queue are as follows:
• EMPTY?(queue) returns true only if there are no more elements in
the queue.
• POP(queue) removes the first element of the queue and returns it.
• INSERT(element, queue) inserts an element and returns the
resulting queue.
• Queues are characterized by the order in which they store the
inserted nodes.
• Three common variants are
• the first-in, first-out or FIFO queue, which pops the oldest element of the
queue;
• the last-in, first-out or LIFO queue (also known as a stack), which pops the
newest element of the queue;
• and the priority queue, which pops the element of the queue with the
highest priority according to some ordering function.
Measuring problem-solving performance
• Before we get into the design of specific search algorithms, we need
to consider the criteria that might be used to choose among them.
We can evaluate an algorithm’s performance in four ways:
• Completeness: Is the algorithm guaranteed to find a solution
when there is one?
• Optimality: Does the strategy find the optimal solution
• Time complexity: How long does it take to find a solution?
• Space complexity: How much memory is needed to perform the
search
Uninformed Search Strategies
• Also called Blind Search
• The term means that the strategies have no additional information
about states beyond that provided in the problem definition.
• All they can do is generate successors and distinguish a goal state
from a non-goal state
• All search strategies are distinguished by the order in which nodes
are expanded.
• Strategies that know whether one non-goal state is “more
promising” than another are called informed search or heuristic
search strategies;
Breadth-first search
• Breadth-first search is a simple strategy in which the root node is expanded
first, then all the successors of the root node are expanded next, then their
successors, and so on.
• In general, all the nodes are expanded at a given depth in the search tree
before any nodes at the next level are expanded.
• Breadth-first search is an instance of the general graph-search algorithm
(Figure 3.7) in which the shallowest unexpanded node is chosen for
expansion.
• This is achieved very simply by using a FIFO queue for the frontier.
• Thus, new nodes (which are always deeper than their parents) go to the
back of the queue, and old nodes, which are shallower than the new nodes,
get expanded first.
• The news about breadth-first search has been good.
• The news about time and space is not so good.
• Imagine searching a uniform tree where every state has b successors. The root of the
search tree generates b nodes at the first level, each of which generates b more nodes,
for a total of b2 at the second level.
• Each of these generates b more nodes, yielding b3 nodes at the third level, and so on.
Now suppose that the solution is at depth d.
• In the worst case, it is the last node generated d
at that level. Then the total number of
nodes generated is b + b2 + b3 + ··· + bd = O(b ) .
• As for space complexity: for any kind of graph search, which stores every expanded node
in the explored set, the space complexity is always within a factor of b of the time
complexity.
• For breadth-first dgraph
−1
search in particular, every node
d
generated remains in memory.
There will be O(b ) nodes in the explored set and O(b ) nodes in the frontier
• Two lessons can be learned from Figure 3.13.
• First, the memory requirements are a bigger problem for breadth-first search
than is the execution time. One might wait 13 days for the solution to an
important problem with search depth 12, but no personal computer has the
petabyte of memory it would take. Fortunately, other strategies require less
memory.
• The second lesson is that time is still a major factor. If your problem has a
solution at depth 16, then (given our assumptions) it will take about 350
years for breadth-first search (or indeed any uninformed search) to find it. In
general, exponential-complexity search problems cannot be solved by
uninformed methods for any but the smallest instances.
Uniform Cost Search
• Uniform Cost Search (UCS) is a classic algorithm used for
searching in graphs or trees. It is an extension of Dijkstra's
algorithm and is specifically designed for graphs with non-
negative edge weights. UCS is used to find the shortest path
from a starting node to a goal node in a weighted graph, where
the cost of each edge represents the distance or cost of
moving from one node to another.

Here's a basic overview of how UCS works:


1. Initialization: Start with the initial node and set its cost to zero. The
cost of all other nodes is set to infinity.
2. Priority Queue: Maintain a priority queue to keep track of the nodes
to be explored. Nodes are prioritized based on their current cost.
3. Exploration: Repeat the following steps until the goal node is
reached or the priority queue is empty:
1. Pop the node with the lowest cost from the priority queue.
2. Expand the node by considering its neighbors.
3. Update the cost of each neighbor if a lower cost path is found.
4. Add the neighbors to the priority queue.
4. Termination: The algorithm terminates when the goal node is
reached or when the priority queue is empty.
• UCS guarantees that it finds the optimal solution (shortest
path) as long as the edge weights are non-negative.
• The key idea is to always explore the node with the lowest cost
first, ensuring that the algorithm considers the least costly
paths first.
• One important thing to note is that UCS can become inefficient
if the edge weights are not uniformly distributed or if there are
significant variations in the edge costs.
• In cases where the graph has a large number of nodes and
edges, other algorithms like A* search may be more suitable,
as they incorporate heuristic information to guide the search.
• Let's consider a simple example to illustrate the Uniform Cost
Search algorithm. Suppose we have the following weighted
graph: Each edge is labeled with its corresponding weight. We
A --(4)-- B --(2)-- C want to find the shortest path from node A to node E
\ | / using UCS.
\ | / 1. Initialization:
(5) (3) (2) 1. Start at node A with a cost of 0. Set the cost of all
\ | / other nodes to infinity.
D --(6)-- E 2. Priority Queue: {A(0)}
2. Iteration 1:
1. Explore node A. Expand to neighbors B and D.
2. Update the cost of B and D: B(4), D(5).
3. Priority Queue: {B(4), D(5)}.
3. Iteration 2:
1. Explore node B. Expand to neighbors A, C, and D.
2. Update the cost of D: D(5) remains unchanged,
A(4) and C(6).
3. Priority Queue: {D(5), A(4), C(6)}.
4. Iteration 3:
1. Explore node D. Expand to neighbors A, B, and E.
2. Update the cost of A: A(4) remains unchanged,
B(4), E(11).
3. Priority Queue: {B(4),
Iteration 4:
1. Explore node B. Expand to neighbors A, C, and D.
2. Update the cost of C: C(6) remains unchanged.
3. Priority Queue: {C(6), A(4), E(11)}.
Iteration 5:
4. Explore node C. Expand to neighbors B and E.
5. Update the cost of E: E(8).
6. Priority Queue: {A(4), E(8)}.
Iteration 6:
7. Explore node A. Expand to neighbors B and D.
8. The goal node E is reached with a cost of 8.
9. The algorithm terminates.
The shortest path from A to E is A -> B -> C -> E with a total cost
of 8.
• When all step costs are equal, breadth-first search is optimal because it always
Uniform- expands the shallowest unexpanded node. By a simple extension, we can find an
algorithm that is optimal with any step-cost function. Instead of expanding the
shallowest node, uniform-cost search expands the node n with the lowest path
cost search cost g(n). This is done by storing the frontier as a priority queue ordered by g. The
algorithm is shown in Figure 3.14.
• In addition to the ordering of the queue by path cost, there are two
other significant differences from breadth-first search.
• The first is that the goal test is applied to a node when it is selected for
expansion (as in the generic graph-search algorithm shown in Figure 3.7)
rather than when it is first generated. The reason is that the first goal node
that is generated may be on a suboptimal path.
• The second difference is that a test is added in case a better path is found to
a node currently on the frontier
Depth-first search
• Depth-first search always expands the deepest node in the current
frontier of the search tree.
• The progress of the search is illustrated in Figure 3.16.
• The search proceeds immediately to the deepest level of the search
tree, where the nodes have no successors.
• As those nodes are expanded, they are dropped from the frontier, so
then the search "backs up" to the next deepest node that still has
unexplored successors.
• Depth-First Search (DFS) is another fundamental graph traversal
algorithm used to explore and navigate through a graph or tree.
• DFS starts at a designated node (often called the "source" node) and
explores as far as possible along each branch before backtracking.
• It uses a stack (either explicitly or through recursive calls) to keep
track of the nodes to be explored.
Here's a basic overview of how DFS works:
1. Initialization: Start with the source node and mark it as visited.
2. Explore: Explore one of the unvisited neighbors of the current node.
If there are multiple neighbors, choose one.
3. Recursion/Stack: If using recursion, recursively apply the DFS
algorithm to the chosen neighbor. If using a stack, push the neighbor
onto the stack.
4. Backtrack: If a node has no unvisited neighbors, backtrack to the
previous node either by returning from the recursive call or popping
a node from the stack.
5. Termination: Repeat steps 2-4 until all nodes are visited or until a
specific condition is met.

DFS does not necessarily find the shortest path between two nodes; it
is more concerned with exploring as far as possible along a branch
before backtracking. It's often used in applications where the goal is to
explore and discover all nodes in a graph, such as topological sorting,
cycle detection, and maze solving.
Here's a simple example to illustrate
DFS:
A -- B -- E
| |
C–D
Starting at node A:
1. Visit A (mark as visited)
2. Explore neighbors: Choose B, mark as visited.
3. Explore neighbors: Choose D, mark as visited.
4. Backtrack: No more unvisited neighbors for D, backtrack to B.
5. Explore neighbors: Choose E, mark as visited.
6. Backtrack: No more unvisited neighbors for E, backtrack to B.
7. Backtrack: No more unvisited neighbors for B, backtrack to A.
8. Explore neighbors: Choose C, mark as visited.
9. Backtrack: No more unvisited neighbors for C.
10. Termination: All nodes are visited.
• The order in which nodes are visited depends on the specific implementation and the order in which
neighbors are chosen.
• Depth-first search seems to have no clear advantage over breadth-first search, so why do we
include it? The reason is the space complexity.

• For a graph search, there is no advantage, but a depth-first tree search needs to store only a
single path from the root to a leaf node, along with the remaining unexpanded sibling
nodes for each node on the path.

• Once a node has been expanded, it can be removed from memory as soon as all its
descendants have been fully explored. For a state space with branching factor b and
maximum depth m, depth-first search requires storage of only O(bm) nodes.
Module 3
Informed Search Strategies
Informed Search
• Informed search algorithm contains an array of knowledge such as
how far we are from the goal, path cost, how to reach to goal node,
etc.
• This knowledge help agents to explore less to the search space and
find more efficiently the goal node.
• The informed search algorithm is more useful for large search space.
• Informed search algorithm uses the idea of heuristic, so it is also
called Heuristic search.
• A heuristic technique, or a heuristic, is any approach to problem
solving or self-discovery that employs a practical method that is not
guaranteed to be optimal, perfect, or rational, but is nevertheless
sufficient for reaching an immediate, short-term goal or
approximation. ~Wikipedia
Heuristics Function
• Heuristic is a function which is used in Informed Search, and it finds
the most promising path
• It takes the current state of the agent as its input and produces the
estimation of how close agent is from the goal.
• The heuristic method, however, might not always give the best
solution, but it guaranteed to find a good solution in reasonable time.
• Heuristic function estimates how close a state is to the goal.
• Heuristic function is represented by h(n), and it calculates the cost of
an optimal path between the pair of states.
• The value of the heuristic function is always positive.
• Heuristic function is given as:
h(n)<=h*(n)
Here, h(n) is heuristic cost, and h*(n) is the estimated cost.
Hence, heuristic cost should be less than or equal to the estimated cost.x
BEST FIRST SEARCH
• Idea: use an evaluation function f(n) for each node
• f(n) provides an estimate for the total cost.
• Expand the node n with smallest f(n).

Implementation:
• Order the nodes in fringe increasing order of cost.
• Special cases:
greedy best-first search
A* search
Greedy Best First Search 1/2

• Greedy best-first search algorithm always selects the path which


appears best at that moment. It is the combination of DFS and BFS
algorithms.

• In BFS and DFS, when we are at a node, we can consider any of the
adjacent as next node.

• So both BFS and DFS blindly explore paths without considering any
cost function.

• The idea of Best First Search is to use an evaluation function to


decide which adjacent is most promising and then explore.
Greedy Best First Search 2/2

• Best-first search allows us to take the advantages of both algorithms.

• With the help of best-first search, at each step, we can choose the
most promising node.

• In the best first search algorithm, we expand the node which is


closest to the goal node and the closest cost is estimated by heuristic
function, i.e.

• Were, f(n)= estimated cost from node n to the goal.

• We use a priority queue to store costs of nodes. So the


implementation is a variation of BFS, we just need to change Queue
to PriorityQueue.
Example 1/6

• We start from source "S" and search for goal "I" using given costs.
Example 2/6

S
• PriorityQueue initially contains S
• Remove S from PriorityQueue and process unvisited neighbors of S to
PriorityQueue
• PriorityQueue now contains {A, C, B} (C is put before B because C has
lesser cost)

A C B
Example 3/6

• Remove A from PriorityQueue and process unvisited neighbors of A


to PriorityQueue.

• PriorityQueue now contains {C, B, E, D}

C B E D
Example 4/6

• Remove C from PriorityQueue and process unvisited neighbors of C


to PriorityQueue.

• PriorityQueue now contains {B, H, E, D}

B H E D
Example 5/6

• Remove B from PriorityQueue and process unvisited neighbors of B


to PriorityQueue.

• PriorityQueue now contains {H, E, D, F, G}

H E D F G
Example 6/6

• Remove H from PriorityQueue. Since our goal "I" is a neighbor of H,


we return.
Takeaways
• Advantages:
 Best first search can switch between BFS and DFS by gaining the advantages of
both the algorithms.

 This algorithm is more efficient than BFS and DFS algorithms.

• Disadvantages:
 It can behave as an unguided depth-first search in the worst case scenario.

 It can get stuck in a loop as DFS.

 This algorithm is not optimal.

• Note: Performance of the algorithm depends on how well the cost or evaluation
function is designed.
Best-first Search Algorithm (Greedy
Search):
• Greedy best-first search algorithm always selects the path
which appears best at that moment.
• It is the combination of depth-first search and breadth-first
search algorithms.
• It uses the heuristic function and search. Best-first search
allows us to take the advantages of both algorithms.
• With the help of best-first search, at each step, we can choose
the most promising node.
• In the best first search algorithm, we expand the node which is
closest to the goal node and the closest cost is estimated by
heuristic function, i.e. f(n)= g(n). Where, h(n)= estimated cost
from node n to the goal.
The greedy best first algorithm is implemented by the priority queue.
Best first search algorithm:
Step 1: Place the starting node into the OPEN list.
Step 2: If the OPEN list is empty, Stop and return failure.
Step 3: Remove the node n, from the OPEN list which has the lowest
value of h(n), and places it in the CLOSED list.
Step 4: Expand the node n, and generate the successors of node n.
Step 5: Check each successor of node n, and find whether any node
is a goal node or not. If any successor node is goal node, then return
success and terminate the search, else proceed to Step 6.
Step 6: For each successor node, algorithm checks for evaluation
function f(n), and then check if the node has been in either OPEN or
CLOSED list. If the node has not been in both list, then add it to the
OPEN list.
Step 7: Return to Step 2.
Advantages:
• Best first search can switch between BFS and DFS by gaining
the advantages of both the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
• It can behave as an unguided depth-first search in the worst
case scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.
• Example:
Consider the below
search problem, and we
will traverse it using
greedy best-first search.
At each iteration, each
node is expanded using
evaluation function
f(n)=h(n) , which is given
in the below table.
A* Search Algorithm:

• A* search is the most commonly known form of best-first


search.
• It uses heuristic function h(n), and cost to reach the node n from
the start state g(n).
• It has combined features of UCS and greedy best-first search,
by which it solve the problem efficiently.
• A* search algorithm finds the shortest path through the search
space using the heuristic function. This search algorithm
expands less search tree and provides optimal result faster. A*
algorithm is similar to UCS except that it uses g(n)+h(n) instead
of g(n).
In A* search algorithm, we use search heuristic as well as the cost to
reach the node. Hence we can combine both costs as following, and this
sum is called as a fitness number.
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty
then return failure and stops.
Step 3: Select the node from the OPEN list which has the
smallest value of evaluation function (g+h), if node n is goal node
then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and
put n into the closed list. For each successor n', check whether n'
is already in the OPEN or CLOSED list, if not then compute
evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it
should be attached to the back pointer which reflects the lowest
g(n') value.
Step 6: Return to Step 2.
Advantages:
• A* search algorithm is the best algorithm than other search
algorithms.
• A* search algorithm is optimal and complete.
• This algorithm can solve very complex problems.
Disadvantages:
• It does not always produce the shortest path as it mostly based
on heuristics and approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all
generated nodes in the memory, so it is not practical for various
large-scale problems.
Example:
• In this example, we will traverse the
given graph using the A* algorithm.
The heuristic value of all states is
given in the below table so we will
calculate the f(n) of each state using
the formula f(n)= g(n) + h(n), where
g(n) is the cost to reach any node from
start state.
• Here we will use OPEN and CLOSED
list.
Initialization: {(S, 5)}
Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11),
(S--> A-->B, 7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it
provides the optimal path with cos
• A* algorithm returns the path which occurred first, and it does not search
for all remaining paths.
• The efficiency of A* algorithm depends on the quality of heuristic.
• A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">
Complete: A* algorithm is complete as long as:
• Branching factor is finite.
• Cost at every action is fixed.
Optimal: A* search algorithm is optimal if it follows below two conditions:
• Admissible: the first condition requires for optimality is that h(n) should be
an admissible heuristic for A* tree search. An admissible heuristic is
optimistic in nature.
• Consistency: Second required condition is consistency for only A* graph-
search.
• If the heuristic function is admissible, then A* tree search will always find
the least cost path.
• Time Complexity: The time complexity of A* search algorithm depends on
heuristic function, and the number of nodes expanded is exponential to the
depth of solution d. So the time complexity is O(b^d), where b is the
branching factor.
• Space Complexity: The space complexity of A* search algorithm
Hill Climbing Algorithm
• Hill climbing algorithm is a local search algorithm which continuously
moves in the direction of increasing elevation/value to find the peak
of the mountain or best solution to the problem. It terminates when it
reaches a peak value where no neighbor has a higher value.
• Hill climbing algorithm is a technique which is used for optimizing the
mathematical problems. One of the widely discussed examples of
Hill climbing algorithm is Traveling-salesman Problem in which we
need to minimize the distance traveled by the salesman.
• It is also called greedy local search as it only looks to its good
immediate neighbor state and not beyond that.
• A node of hill climbing algorithm has two components which are state
and value.
• Hill Climbing is mostly used when a good heuristic is available.
• In this algorithm, we don't need to maintain and handle the search
tree or graph as it only keeps a single current state.
Features of Hill Climbing:
Following are some main features of Hill Climbing Algorithm:
• Generate and Test variant: Hill Climbing is the variant of
Generate and Test method. The Generate and Test method
produce feedback which helps to decide which direction to
move in the search space.
• Greedy approach: Hill-climbing algorithm search moves in the
direction which optimizes the cost.
• No backtracking: It does not backtrack the search space, as it
does not remember the previous states.
State-space Diagram for Hill Climbing:
• The state-space landscape is a
graphical representation of the hill-
climbing algorithm which is showing
a graph between various states of
algorithm and Objective
function/Cost.
• On Y-axis we have taken the function
which can be an objective function or
cost function, and state-space on the
x-axis. If the function on Y-axis is
cost then, the goal of search is to
find the global minimum and local
minimum. If the function of Y-axis is
Objective function, then the goal of
the search is to find the global
maximum and local maximum.
Different regions in the state space
landscape:
• Local Maximum: Local maximum is a state which is better than
its neighbor states, but there is also another state which is
higher than it.
• Global Maximum: Global maximum is the best possible state
of state space landscape. It has the highest value of objective
function.
• Current state: It is a state in a landscape diagram where an
agent is currently present.
• Flat local maximum: It is a flat space in the landscape where
all the neighbor states of current states have the same value.
• Shoulder: It is a plateau region which has an uphill edge.
Types of Hill Climbing Algorithm:
• Simple hill Climbing:
• Steepest-Ascent hill-climbing:
• Stochastic hill Climbing:
1. Simple Hill Climbing:

Simple hill climbing is the simplest way to implement a hill


climbing algorithm. It only evaluates the neighbor node state
at a time and selects the first one which optimizes current
cost and set it as a current state. It only checks it's one
successor state, and if it finds better than the current state, then
move else be in the same state. This algorithm has the following
features:
• Less time consuming
• Less optimal solution and the solution is not guaranteed
Algorithm for Simple Hill Climbing:
• Step 1: Evaluate the initial state, if it is goal state then return
success and Stop.
• Step 2: Loop Until a solution is found or there is no new
operator left to apply.
• Step 3: Select and apply an operator to the current state.
• Step 4: Check new state:
• If it is goal state, then return success and quit.
• Else if it is better than the current state then assign new state as a
current state.
• Else if not better than the current state, then return to step2.
• Step 5: Exit.
2. Steepest-Ascent hill climbing:

• The steepest-ascent algorithm is a variation of simple hill


climbing algorithm. This algorithm examines all the neighboring
nodes of the current state and selects one neighbor node that is
closest to the goal state. This algorithm consumes more time as
it searches for multiple neighbors
Algorithm for Steepest Ascent Hill climbing :
• Evaluate the initial state. If it is a goal state then stop and return
success. Otherwise, make the initial state as the current state.
• Repeat these steps until a solution is found or the current state does
not change
• Select a state that has not been yet applied to the current state.
• Initialize a new ‘best state’ equal to the current state and apply it to produce
a new state.
• Perform these to evaluate the new state
• If the current state is a goal state, then stop and return success.
• If it is better than the best state, then make it the best state else continue the loop
with another new state.
• Make the best state as the current state and go to Step 2 of the second point.
• Exit from the function.
3. Stochastic hill climbing:

• Stochastic hill climbing does not examine for all its neighbor
before moving. Rather, this search algorithm selects one
neighbor node at random and decides whether to choose it as a
current state or examine another state.
Algorithm
• Evaluate the initial state. If it is a goal state then stop and return
success. Otherwise, make the initial state the current state.
• Repeat these steps until a solution is found or the current state
does not change.
• Select a state that has not been yet applied to the current state.
• Apply the successor function to the current state and generate all the
neighbor states.
• Among the generated neighbor states which are better than the
current state choose a state randomly (or based on some probability
function).
• If the chosen state is the goal state, then return success, else make it
the current state and repeat step 2 of the second point.
• Exit from the function.
State Space diagram for Hill Climbing
• The state-space diagram is a graphical representation of the set
of states our search algorithm can reach vs the value of our
objective function(the function which we wish to maximize).
• X-axis: denotes the state space ie states or configuration our
algorithm may reach.
• Y-axis: denotes the values of objective function corresponding
to a particular state.
• The best solution will be a state space where the objective
function has a maximum value(global maximum).
• Let's consider a simple one-dimensional hill climbing problem.
Imagine you are standing on a mountainous terrain, and your
goal is to reach the highest point. You can only move
horizontally (along the x-axis), and your objective is to find the
peak.
• Here's a simple representation of the terrain as a function
• f(x)=−(x−3)2+9
• Let's say you start at f(x)=0,x=0. The algorithm would work as
follows:
1.Evaluate the current position by computing f(x).
2.Determine the neighboring points (small steps in the positive or
negative x-direction).
3.Move to the neighbor with the highest f(x) value.
4.Repeat steps 1-3 until you reach a peak or a point where all
neighboring points have lower f(x) values.
More Examples
• 2D Hill Climbing
• Travelling Salesman Problem-The Traveling Salesman Problem involves
finding the shortest possible route that visits a set of cities and
returns to the original city. The objective is to minimize the total
distance traveled. The hill climbing algorithm can be applied to
explore different routes and improve the solution iteratively.
• Knapsack Problem: The Knapsack Problem is a classic
optimization problem where you have a set of items, each with a
weight and a value, and you want to determine the combination of
items to include in a knapsack to maximize the total value without
exceeding a given weight limit. Hill climbing can be used to explore
different combinations of items and improve the solution.
Chess Game (Local Search): In chess, hill climbing can be
applied to improve the evaluation function used by a chess-
playing algorithm. The algorithm can make moves and evaluate
the resulting board positions, iteratively selecting moves that lead
to better positions.
Network Routing: In network routing, the goal is to find the
optimal path for data packets to travel from a source to a
destination. Hill climbing can be applied to explore different
routes and improve the efficiency of data transmission.
Logical Agents-Knowledge Based
• In the context of artificial intelligence, a knowledge-based agent
is an intelligent agent that uses knowledge to make decisions
and solve problems.
• These agents are designed to operate on a knowledge base,
which is a collection of information that the agent can use to
reason and make informed decisions.
• The knowledge base typically includes facts, rules, and other
pieces of information relevant to the agent's domain.
• Logical agents, a subset of knowledge-based agents, use
logical reasoning to draw conclusions and make decisions
based on the information available in their knowledge base.
Here are some key components and characteristics of logical
agents:
1.Knowledge Base:
1. The knowledge base is a central component of a logical agent. It
contains statements or propositions about the world, and these
statements can be facts, rules, or a combination of both.
2. Facts represent information that is assumed to be true in the agent's
domain. For example, "Socrates is a human" could be a fact in a
knowledge base.
3. Rules are statements that define relationships between different facts.
For instance, a rule might state, "If someone is human and mortal, then
that person is mortal."
Inference Engine:
• The inference engine is responsible for drawing conclusions
from the information in the knowledge base. It applies logical
reasoning rules and deduction to infer new information.
• Common inference methods include modus ponens (applying a
rule to draw a conclusion) and resolution (resolving conflicting
information).
Logical Representation:
• Logical agents often use formal languages to represent
knowledge in a structured and unambiguous way. Propositional
logic and first-order logic are commonly employed for this
purpose.
• Propositional logic deals with propositions (statements that are
either true or false) and logical operators (such as AND, OR,
and NOT).
• First-order logic extends propositional logic by introducing
variables, quantifiers (such as ∀ for "for all" and ∃ for "there
exists"), and predicates.
Knowledge Acquisition:
• Logical agents may have mechanisms for acquiring new
knowledge from the environment. This could involve learning
from observations, interacting with users, or incorporating
information from external sources.
Example: Expert Systems:
• Expert systems are a practical application of logical agents.
They are designed to mimic the decision-making capabilities of
a human expert in a specific domain.
• Expert systems use a knowledge base of facts and rules, and
their inference engine simulates the logical reasoning process
to provide advice or solutions in the given domain.
Let's illustrate the usage of logical agents with a simple example
involving a knowledge base, rules, and logical inference.
Consider a logical agent designed to make decisions about
whether to play tennis based on weather conditions. The
knowledge base includes facts and rules related to playing
tennis.
Knowledge Base:
• Fact: SunnyFact: Sunny
• Fact: WarmFact: Warm
• Rule: If Sunny and Warm, then PlayTennisRule: If Sunny and W
arm, then PlayTennis
Here, "Sunny" and "Warm" are considered as facts, and the rule
states that if it's sunny and warm, the logical agent should play
tennis.
Inference Engine: The inference engine uses the knowledge base to
draw conclusions. Let's say the agent observes that it is both sunny
and warm:
1.Observation: Fact: SunnyFact: Sunny, Fact: WarmFact: Warm
2.Inference:
Rule: If Sunny and Warm, then PlayTennisRule: If Sunny and Warm,
then PlayTennis (Apply modus ponens)
3.Conclusion: Action: PlayTennisAction: PlayTennis
In this case, the logical agent, based on the logical rule, infers that it
should play tennis because the observed conditions match the rule's
antecedents.
x
Let's consider another example:
• Knowledge Base:
• Fact: Rainy
• Rule: If Rainy, then NoPlayTennis
Observation: Fact: Rainy
1.Inference: Rule: If Rainy, then NoPlayTennis(Apply modus ponens)
2.Conclusion: Action: NoPlayTennisAction: NoPlayTennis
• In this case, the agent infers that it should not play tennis because
the observed condition (rainy) matches the rule, leading to the
conclusion of not playing tennis.
• knowledge bases consist of sentences.
• These sentences are expressed according to the syntax of the
representation language, which specifies all the sentences that are
well formed.
• The notion of syntax is clear enough in ordinary arithmetic: “x + y = 4”
is a well-formed sentence, whereas “x4y+ =” is not.
Logical agents
Introduction
• The central component of a knowledge-based
agent is its knowledge base, or KB.
• The knowledge base is a set of Sentences
• Each sentence is expressed in a language called
a knowledge representation language
• There must be a way to add new sentence to the
knowledge base and a query to know
• The standard names would be TELL and ASK
• Both operations involve INFERENCE- that is
deriving new sentence from an old one.
• The knowledge level, where we need specify only
what the agent knows and what its goals are, in
order to fix its behavior.
• For example, an automated taxi might have the
goal of taking a passenger from San Francisco
to Marin County and might know that the Golden
Gate Bridge is the only link between the two
locations.
• Then we can expect it to cross the Golden Gate
Bridge because it knows that that will achieve
its goal.
• Notice that this analysis is independent of how
the taxi works at the implementation level.
• It doesn’t matter whether its geographical
knowledge is implemented as linked lists or
pixel maps, or whether it reasons by
manipulating strings of symbols stored in
WUMPUS World
• The Wumpus World is a classic artificial intelligence problem
used to illustrate the concepts of knowledge representation and
reasoning. It was introduced by Alfred V. Aho, John E. Hopcroft,
and Jeffrey D. Ullman in 1974.
• In the Wumpus World, the agent (an AI or a player) navigates
through a grid-based environment in search of treasure while
avoiding dangers. The environment contains pits, a wumpus (a
mythical creature that can be dangerous), and gold. The agent
has a limited set of actions it can perform, such as moving to
adjacent rooms, shooting arrows to kill the wumpus, and
grabbing the gold.
Here are some key elements of the Wumpus World:
1. Rooms and Grid: The environment is represented as a grid of
rooms, and the agent can move between adjacent rooms.
2. Pits: Some rooms contain pits, and if the agent falls into a pit, it
dies.
3. Wumpus: The wumpus is present in one of the rooms. If the agent
enters the room with the wumpus without shooting it first, it gets
eaten and dies.
4. Gold: There is a room with gold. The goal of the agent is to find and
grab the gold.
5. Arrows: The agent has a limited number of arrows. It can shoot
arrows to kill the wumpus, but the arrows are finite.
6. Percept: The agent receives sensory information about its
surroundings. For example, it can sense a breeze if there is a pit
nearby or smell the wumpus if it is in an adjacent room.
7. Actions: The agent can perform actions like moving, shooting
arrows, grabbing gold, and climbing out of the cave.
Challenges
• The challenge in the Wumpus World is to design an intelligent
agent that can navigate through the environment, avoid
dangers, and successfully achieve the goal (grabbing the gold)
while maximizing its utility and minimizing risks.
• The precise definition of the task environment is
given, as by the PEAS description:
• Performance measure: +1000 for climbing out of the
cave with the gold, –1000 for falling into a pit
or being eaten by the wumpus, –1 for each action
taken and –10 for using up the arrow. The game
ends either when the agent dies or when the agent
climbs out of the cave.
• Environment: A 4 × 4 grid of rooms. The agent
always starts in the square labeled [1,1], facing
to the right. The locations of the gold and the
wumpus are chosen randomly, with a uniform
distribution, from the squares other than the
• Actuators: The agent can move Forward, TurnLeft by
90◦, or TurnRight by 90◦.
• The agent dies a miserable death if it enters a
square containing a pit or a live wumpus. (It is
safe, albeit smelly, to enter a square with a dead
wumpus.)
• If an agent tries to move forward and bumps into a
wall, then the agent does not move.
• The action Grab can be used to pick up the gold if
it is in the same square as the agent.
• The action Shoot can be used to fire an arrow in a
straight line in the direction the agent is
facing.
• The arrow continues until it either hits (and
hence kills) the wumpus or hits a wall.
• The agent has only one arrow, so only the first
Shoot action has any effect. Finally, the action
Climb can be used to climb out of the cave, but
only from square [1,1].
• Sensors: The agent has five sensors, each of
which gives a single bit of information: –
• In the square containing the wumpus and in the
directly (not diagonally) adjacent squares, the
agent will perceive a Stench.
• In the squares directly adjacent to a pit, the
agent will perceive a Breeze.
• In the square where the gold is, the agent will
perceive a Glitter. – When an agent walks into
a wall, it will perceive a Bump.
• When the wumpus is killed, it emits a woeful
Scream that can be perceived anywhere in the
cave.
The percepts will be given to the agent program
in the form of a list of five symbols; for
example, if there is a stench and a breeze, but
• We use an informal knowledge representation
language consisting of writing down symbols in a
grid (as in Figures 7.3 and 7.4).
• The agent’s initial knowledge base contains the
rules of the environment, as described previously;
in particular, it knows that it is in [1,1] and
that [1,1] is a safe square; we denote that with
an “A” and “OK,” respectively, in square [1,1].
• The first percept is [None, None, None, None,
None], from which the agent can conclude that its
neighboring squares, [1,2] and [2,1], are free of
dangers—they are OK. Figure 7.3(a) shows the
agent’s state of knowledge at this point.
• A cautious agent will move only into a square that
it knows to be OK. Let us suppose the agent
decides to move forward to [2,1]. The agent
perceives a breeze (denoted by “B”) in [2,1], so
there must be a pit in a neighboring square. The
pit cannot be in [1,1], by the rules of the game,
• The agent perceives a stench in [1,2], resulting in
the state of knowledge shown in Figure 7.4(a). The
stench in [1,2] means that there must be a wumpus
nearby.
• But the wumpus cannot be in [1,1], by the rules of
the game, and it cannot be in [2,2] (or the agent
would have detected a stench when it was in [2,1]).
Therefore, the agent can infer that the wumpus is in
[1,3]. The notation W! indicates this inference.
Moreover, the lack of a breeze in [1,2] implies that
there is no pit in [2,2]. Yet the agent has already
inferred that there must be a pit in either [2,2] or
[3,1], so this means it must be in [3,1]. This is a
fairly difficult inference, because it combines
knowledge gained at different times in different
places and relies on the lack of a percept to make
one crucial step
• The agent has now proved to itself that there is
neither a pit nor a wumpus in [2,2], so it is OK to
LOGIC
• knowledge bases consist of sentences.
• These sentences SYNTAX are expressed according
to the syntax of the representation language,
which specifies all the sentences that are well
formed.
• The notion of syntax is clear enough in
ordinary arithmetic: “x + y = 4” is a well-
formed sentence, whereas “x4y+ =” is not.
• A logic must also define the semantics or
meaning of sentences. The semantics defines the
truth of each sentence with respect to each
possible world.
• For example, the semantics for arithmetic
specifies that the sentence “x + y = 4” is true
in a world where x is 2 and y is 2, but false
in a world where x is 1 and y is 1.
• In standard logics, every sentence must be
either true or false in each possible world—
there is no “in between.”
• When we need to be precise, we use the term model
in place of “possible world.”
• Whereas possible worlds might be thought of as
(potentially) real environments that the agent
might or might not be in, models are mathematical
abstractions, each of which simply fixes the truth
or falsehood of every relevant sentence.
• Informally, we may think of a possible world as,
for example, having x men and y women sitting at a
table playing bridge, and the sentence x + y = 4
is true when there are four people in total.
• Formally, the possible models are just all
possible assignments of real numbers to the
variables x and y.
• Each such assignment fixes the truth of any
sentence of arithmetic whose variables are x and
• This involves the relation of logical entailment
between sentences—the idea that a sentence follows
logically from another sentence.
• In mathematical notation, we write α |= β to mean
that the sentence α entails the sentence β.
• The formal definition of entailment is this: α |=
β if and only if, in every model in which α is
true, β is also true.
• Using the notation just introduced, we can write α
|= β if and only if M(α) ⊆ M(β) .
• The relation of entailment is familiar from
arithmetic; we are happy with the idea that the
sentence x = 0 entails the sentence xy = 0.
Obviously, in any model where x is zero, it is the
case that xy is zero (regardless of the value of
y).
• We can apply the same kind of analysis to the
wumpus-world reasoning example given in the
preceding section.
• Consider the situation in Figure 7.3(b): the
agent has detected nothing in [1,1] and a
breeze in [2,1].
• These percepts, combined with the agent’s
knowledge of the rules of the wumpus world,
constitute the KB. The agent is interested
(among other things) in whether the adjacent
squares [1,2], [2,2], and [3,1] contain pits.
• Each of the three squares might or might not
contain a pit, so (for the purposes of this
• The KB can be thought of as a set of sentences or as a
single sentence that asserts all the individual sentences.
• The KB is false in models that contradict what the agent
knows— for example, the KB is false in any model in which
[1,2] contains a pit, because there is no breeze in [1,1].
• There are in fact just three models in which the KB is true,
and these areshown surrounded by a solid line in Figure 7.5.
• Now let us consider two possible conclusions:
α1 = “There is no pit in [1,2].”
α2 = “There is no pit in [2,2].”
We have surrounded the models of α1 and α2 with dotted lines
in Figures 7.5(a) and 7.5(b), respectively.
By inspection, we see the following: in every model in which
KB is true, α1 is also true. Hence, KB |= α1: there is no pit
in [1,2].
Propositional Logic
• Powerful logic called propositional logic
• The syntax of propositional logic defines the
allowable sentences.
• The atomic sentences consist of a single
proposition symbol.
• Each such symbol stands for a proposition that
can be true or false. We use symbols that start
with an uppercase letter and may contain other
letters or subscripts, for example: P, Q, R,
W1,3 and North.
• The names are arbitrary but are often chosen to
have some mnemonic value—we use W1,3 to stand
for the proposition that the wumpus is in
• There are two proposition symbols with fixed
meanings:
• True is the always-true proposition and False
is the always-false proposition.
• Complex sentences are constructed from simpler
sentences, using parentheses and logical
connectives. There are five connectives in
common use
A simple knowledge base
Propositional Theorem Proving

• In propositional theorem proving, the goal is to establish the validity


of a given propositional formula or to find a counterexample that
proves its invalidity. This process involves the application of various
rules of inference and logical equivalences to transform the formula
and derive new formulas until a conclusion is reached.
• There are several approaches to propositional theorem proving:
1.Truth Table Method: This method involves constructing a truth table
that enumerates all possible truth values of the propositional
variables in the formula. By systematically evaluating the formula for
all possible combinations of truth values, one can determine its
validity.
2.Resolution Method: The resolution method is a more sophisticated
approach that involves transforming a formula into a set of clauses
and then applying resolution rules to derive new clauses. If a
contradiction is derived, the original formula is considered invalid.
• Theorem proving—applying rules of inference
directly to the sentences in our knowledge base to
construct a proof of the desired sentence without
consulting models. If the number of models is
large but the length of the proof is short, then
theorem proving can be more efficient than model
checking.
• The first concept is logical equivalence: two
sentences α and β are logically equivalent if they
are true in the same set of models. We write this
as α ≡ β. For example, we can easily show (using
truth tables) that P ∧ Q and Q ∧ P are logically
equivalent;
• An alternative definition of equivalence is as
follows: any two sentences α and β are equivalent
• The second concept we will need is validity. A
sentence is valid if it is true in all models.
• For example, the sentence P ∨ ¬P is valid.
Valid sentences are also known as tautologies—
they are necessarily true. Because the sentence
True is true in all models, every valid
sentence is logically equivalent to True.
• What good are valid sentences?
• From our definition of entailment, we can derive the
deduction theorem, which was known to the ancient
Greeks: For any sentences α and β, α |= β if and
only if the sentence (α ⇒ β) is valid.
Inference and proofs
• Let us see how these inference rules and
equivalences can be used in the wumpus world.
We start with the knowledge base containing R1
through R5 and show how to prove ¬P1,2, that
is, there is no pit in [1,2]. First, we apply
biconditional elimination to R2 to obtain
We just need to define a proof problem as
follows:
• INITIAL STATE: the initial knowledge base.
• ACTIONS: the set of actions consists of all the
inference rules applied to all the sentences
that match the top half of the inference rule.
• RESULT: the result of an action is to add the
sentence in the bottom half of the inference
rule.
• GOAL: the goal is a state that contains the
sentence we are trying to prove
Proof by resolution
• Search algorithms such as iterative deepening
search are complete in the sense that they will
find any reachable goal, but if the available
inference rules are inadequate, then the goal
is not reachable—no proof exists that uses only
those inference rules
• For example, if we removed the biconditional
elimination rule, the proof in the preceding
section would not go through. The current
section introduces a single inference rule,
resolution, that yields a complete inference
algorithm when coupled with any complete search
Conjunctive normal form
• A sentence expressed as a conjunction of
clauses is said to be in conjunctive normal
form or CNF . We now describe a procedure for
converting to CNF. We illustrate the procedure
by converting the sentence B1,1 ⇔ (P1,2 ∨
P2,1) into CNF. The steps are as follows:
• Eliminate ⇔, replacing α ⇔ β with (α ⇒ β) ∧
(β ⇒ α). (B1,1 ⇒ (P1,2 ∨ P2,1)) ∧ ((P1,2 ∨
P2,1) ⇒ B1,1) .
• Eliminate ⇒, replacing α ⇒ β with ¬α ∨ β:
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1)
.
• CNF requires ¬ to appear only in literals, so
we “move ¬ inwards” by repeated application of
the following equivalences from Figure 7.11:
• ¬(¬α) ≡ α (double-negation elimination)
• ¬(α ∧ β) ≡ (¬α ∨ ¬β) (De Morgan)
• ¬(α ∨ β) ≡ (¬α ∧ ¬β) (De Morgan)
In the example, we require just one application of the
last rule:
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ ((¬P1,2 ∧ ¬P2,1) ∨ B1,1) .
Now we have a sentence containing nested ∧ and ∨
operators applied to literals. We apply the
distributivity law from Figure 7.11, distributing ∨
over ∧ wherever possible. (¬B1,1 ∨ P1,2 ∨ P2,1) ∧
First Order Logic
Module 4
First order Logic
• In the topic of Propositional logic, we have seen that how to represent
statements using propositional logic. But unfortunately, in propositional
logic, we can only represent the facts, which are either true or false. PL is
not sufficient to represent the complex sentences or natural language
statements. The propositional logic has very limited expressive power.
Consider the following sentence, which we cannot represent using PL
logic.
• "Some humans are intelligent", or
• "Sachin likes cricket."
• To represent the above statements, PL logic is not sufficient, so we
required some more powerful logic, such as first-order logic.
First-Order logic:
• First-order logic is another way of knowledge representation in artificial
intelligence. It is an extension to propositional logic.
• FOL is sufficiently expressive to represent the natural language statements
in a concise way.
• First-order logic is also known as Predicate logic or First-order
predicate logic. First-order logic is a powerful language that develops
information about the objects in a more easy way and can also express the
relationship between those objects.
• First-order logic (like natural language) does not only assume that the
world contains facts like propositional logic but also assumes the following
things in the world:
• Objects: A, B, people, numbers, colors, wars, theories, squares, pits, wumpus, ......
• Relations: It can be unary relation such as: red, round, is adjacent, or n-any
relation such as: the sister of, brother of, has color, comes between
• Function: Father of, best friend, third inning of, end of, ......
• As a natural language, first-order logic also has two main parts:
• Syntax
• Semantics
Syntax of First-Order logic:

• The syntax of FOL determines which collection of symbols is a


logical expression in first-order logic. The basic syntactic
elements of first-order logic are symbols. We write statements
in short-hand notation in FOL.
• Basic Elements of First-order logic:
Constant 1, 2, A, John, Mumbai, cat,....
Variables x, y, z, a, b,....
Predicates Brother, Father, >,....
Function sqrt, LeftLegOf, ....
Connectives ∧, ∨, ¬, ⇒, ⇔
Equality ==
Quantifier ∀, ∃
Atomic sentences:

• Atomic sentences are the most basic sentences of first-order


logic. These sentences are formed from a predicate symbol
followed by a parenthesis with a sequence of terms.
• We can represent atomic sentences as Predicate (term1,
term2, ......, term n).
• Example: Ravi and Ajay are brothers: => Brothers(Ravi,
Ajay).
Chinky is a cat: => cat (Chinky).
• Complex Sentences:
Complex sentences are made by combining atomic sentences
using connectives.
First-order logic statements can be divided into two parts:
• Subject: Subject is the main part of the statement.
• Predicate: A predicate can be defined as a relation, which
binds two atoms together in a statement.
Consider the statement: "x is an integer.", it consists of two
parts, the first part x is the subject of the statement and second
part "is an integer," is known as a predicate.
Quantifiers in First-order logic:

• A quantifier is a language element which generates


quantification, and quantification specifies the quantity of
specimen in the universe of discourse.
• These are the symbols that permit to determine or identify the
range and scope of the variable in the logical expression. There
are two types of quantifier:
• Universal Quantifier, (for all, everyone, everything)
• Existential quantifier, (for some, at least one).
Universal Quantifier:

• Universal quantifier is a symbol of logical representation, which


specifies that the statement within its range is true for
everything or every instance of a particular thing.
• The Universal quantifier is represented by a symbol ∀, which
resembles an inverted A.
• If x is a variable, then ∀x is read as:
• For all x
• For each x
• For every x.
• Example:
• All man drink coffee.
• Let a variable x which refers to a cat so all x can be represented
in UOD as below:

∀x man(x) → drink (x, coffee).


It will be read as: There are all x where x is a man
who drink coffee.
Existential Quantifier:

• Existential quantifiers are the type of quantifiers, which express that


the statement within its scope is true for at least one instance of
something.
• It is denoted by the logical operator ∃, which resembles as inverted
E. When it is used with a predicate variable then it is called as an
existential quantifier.
• If x is a variable, then existential quantifier will be ∃x or ∃(x). And it
will be read as:
• There exists a 'x.'
• For some 'x.'
• For at least one 'x.'
• Example:
• Some boys are intelligent.

∃x: boys(x) ∧ intelligent(x)


It will be read as: There are some x where x is a boy
who is intelligent.
Points to remember:
• The main connective for universal quantifier ∀ is implication →.
• The main connective for existential quantifier ∃ is and ∧.
Properties of Quantifiers:
• In universal quantifier, ∀x∀y is similar to ∀y∀x.
• In Existential quantifier, ∃x∃y is similar to ∃y∃x.
• ∃x∀y is not similar to ∀y∃x.
• Some Examples of FOL using quantifier:
1. All birds fly.
• In this question the predicate is "fly(bird)."
• And since there are all birds who fly so it will be
represented as follows.
• ∀x bird(x) →fly(x).
2. Every man respects his parent.
• In this question, the predicate is "respect(x, y),"
where x=man, and y= parent.
• Since there is every man so will use ∀, and it will be
represented as follows:
• ∀x man(x) → respects (x, parent).

3. Some boys play cricket.


• In this question, the predicate is "play(x, y)," where
x= boys, and y= game. Since there are some boys so we
will use ∃, and it will be represented as:
• ∃x boys(x) → play(x, cricket).
4. Not all students like both Mathematics and
Science.
• In this question, the predicate is "like(x, y),"
where x= student, and y= subject.
• Since there are not all students, so we will use ∀
with negation, so following representation for
this:
• ¬∀ (x) [ student(x) → like(x,
Mathematics) ∧ like(x, Science)].

5. Only one student failed in Mathematics.


• In this question, the predicate is "failed(x, y),"
where x= student, and y= subject.
• Since there is only one student who failed in
Mathematics, so we will use following
representation for this:
• ∃(x) [ student(x) → failed (x,
• Free and Bound Variables:
The quantifiers interact with variables which appear in a suitable
way. There are two types of variables in First-order logic which
are given below:
• Free Variable: A variable is said to be a free variable in a
formula if it occurs outside the scope of the quantifier.
• Example: ∀x ∃(y)[P (x, y, z)], where z is a free variable.
• Bound Variable: A variable is said to be a bound variable in a
formula if it occurs within the scope of the quantifier.
• Example: ∀x [A (x) B( y)], here x and y are the bound
variables.
Using first order logic
• Assertions and queries in first-order logic
Sentences are added to a knowledge base using TELL, exactly as in propositional logic. Such
sentences are called assertions. For example, we can assert that John is a king, Richard is a
person, and all kings are persons:
TELL(KB, King(John)) .
TELL(KB, Person(Richard)) .
TELL(KB, ∀ x King(x) ⇒ Person(x)) .
We can ask questions of the knowledge base using ASK. For example,
ASK(KB, King(John)) returns true. Questions asked with ASK are called queries or goals.
Generally speaking, any query that is logically entailed by the knowledge base should be answered
affirmatively. For
example, given the two preceding assertions, the query
ASK(KB, Person(John))
should also return true. We can ask quantified queries, such as
ASK(KB, ∃ x Person(x))
The kinship domain

You might also like