Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

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) ∧

You might also like