AI
AI
AI_Module3
Topics:
1. Informed Search Strategies:
a. Greedy best-first search,
b. A*search,
c. Heuristic functions.
2. Logical Agents:
a. Knowledge–based agents,
b. The Wumpus world,
c. Logic,
d. Propositional logic, Reasoning patterns in
Propositional Logic
1|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
2|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Following Figure shows the progress of a greedy best-first search using hSLD to
find a path from Arad to Bucharest.
The first node to be expanded from Arad will be Sibiu because it is closer to
Bucharest than either Zerind or Timisoara. The next node to be expanded will
be Fagaras because it is closest. Fagaras in turn generates Bucharest, which is
the goal.
3|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Figure: Stages in a greedy best-first tree search for Bucharest with the straight-
line distance heuristic hSLD . Nodes are labelled with their h-values.
4|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
In this search example, we are using two lists which are OPEN and CLOSED Lists.
Following are the iteration for traversing the above example.
5|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
6|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
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.
A* search Algorithm:
7|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Advantages:
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.
8|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Figure below illustrates the Stages in an A∗ search for Bucharest. Nodes are
labelled with f = g + h. The h values are the straight-line distances to Bucharest
9|Page
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Example 2: 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.
10 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Solution:
To apply the A* Search algorithm to the given graph, we will use the following steps:
Step3 : Iteration1
Select the node from the Open List with the lowest f(n) (i.e., S).
Expand S. Its neighbors are A and G.
o g(A)=1, h(A)=3 → f(A)=1+3=4
o g(G)=10,h(G)=0 → f(G)=10+0=10
Add A and G to the Open List and move S to the Closed List.
Open List: [A(4),G(10)]
Closed List: [ S ]
Step3 : Iteration2
Step3 : Iteration3
11 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Step3 : Iteration4
Select node with the lowest f(n): G (smallest f(n)=6).
G is the goal node, so the algorithm terminates here.
Final Solution
The path found is S→A→C→G.
Total Cost: The total cost is the path S→A→C→G which is 1+1+4=6.
1.
2.
12 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
3.
13 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Admissible:
The first condition requires for optimality is that h(n) should be an admissible
heuristic for A* tree search. An admissible heuristic is one that never
overestimates the cost to reach the goal. Because g(n) is the actual cost to reach
n along the current path, and f(n) = g(n) + h(n), we have as an immediate
consequence that f(n) never overestimates the true cost of a solution along the
current path through n. If the heuristic function is admissible, then A* tree
search will always find the least cost path.
14 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Figure illustrates the A typical instance of the 8-puzzle. The solution is 26 steps
long.
The average solution cost for a randomly generated 8-puzzle instance is about
22 steps. The branching factor is about 3. (When the empty tile is in the middle,
four moves are possible; when it is in a corner, two; and when it is along an edge,
three.) This means that an exhaustive tree search to depth 22 would look at
about 322 ≈ 3.1 × 1010 states.
h1 = the number of misplaced tiles. For Figure, all of the eight tiles are out of
position, so the start state would have h1 = 8. h1 is an admissible heuristic
because it is clear that any tile that is out of place must be moved at least once
h2 = the sum of the distances of the tiles from their goal positions. Because tiles
cannot move along diagonals, the distance we will count is the sum of the
horizontal and vertical distances. This is sometimes called the city block distance
or Manhattan distance. h2 is also admissible because all any move can do is
move one tile one step closer to the goal. Tiles 1 to 8 in the start state give a
Manhattan distance of h2 = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18 .
As expected, neither of these overestimates the true solution cost, which is 26.
The performance of heuristic search algorithms depends on the quality of the
heuristic function. One can sometimes construct good heuristics by relaxing the
problem definition, by storing precomputed solution costs for subproblems in a
pattern database, or by learning from experience with the problem class.
15 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Experimentally it is determined that h2 is better than h1. That is for any node n,
h2(n) ≥ h1(n). This implies that h2 dominate h1. Domination translates directly
into efficiency. A∗ using h2 will never expand more nodes than A∗ using h1.
A problem with fewer restrictions on the actions is called a relaxed problem. The
state-space graph of the relaxed problem is a super graph of the original state
space because the removal of restrictions creates added edges in the graph.
Because the relaxed problem adds edges to the state space, any optimal solution
in the original problem is, by definition, also a solution in the relaxed problem;
but the relaxed problem may have better solutions if the added edges provide
short cuts. Hence, the cost of an optimal solution to a relaxed problem is an
admissible heuristic for the original problem.
For example, if the 8-puzzle actions are described as
A tile can move from square A to square B if
A is horizontally or vertically adjacent to B and B is blank,
we can generate three relaxed problems by removing one or both of the
conditions:
a) A tile can move from square A to square B if A is adjacent to B.
b) A tile can move from square A to square B if B is blank.
c) A tile can move from square A to square B.
Admissible heuristics can also be derived from the solution cost of a subproblem
of a given problem. For example, Figure below shows a subproblem of the 8-
puzzle instance.
16 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Fig: A subproblem of the 8-puzzle instance. The task is to get tiles 1, 2, 3, and 4
into their correct positions, without worrying about what happens to the other
tiles.
17 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Inductive learning methods are most effective when provided with relevant
features of a state for predicting its value, rather than relying solely on the raw
state description.
For instance, a feature like "number of misplaced tiles" (x1(n)) can be useful in
predicting the distance of a state from the goal in an 8-puzzle. By gathering
statistics from randomly generated 8-puzzle configurations and their actual
solution costs, one can use these features to predict h(n).
The constants (c1 and c2) are adjusted to achieve the best fit with the actual
data on solution costs. It is expected that both c1 and c2 are positive, as
misplaced tiles and incorrect adjacent pairs make the problem more challenging.
While this heuristic satisfies the condition h(n) = 0 for goal states, it may not
necessarily be both admissible and consistent.
18 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
19 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Example: Syllogism
A syllogism is a form of deductive reasoning where a conclusion is drawn from
two given or assumed propositions (premises).
• Premise 1: All humans are mortal.
• Premise 2: Socrates is a human.
• Conclusion: Therefore, Socrates is mortal.
20 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
3. Axioms: Some sentences in the knowledge base may be dignified with the
name "axiom," especially when they are taken as given without being derived
from other sentences. Axioms are fundamental statements that serve as
foundational knowledge for the agent.
5. ASK Operation: The agent needs a way to query the knowledge base to
retrieve information. The standard operation for querying is referred to as
ASK. It allows the agent to ask questions about what is known.
6. Inference : Both TELL and ASK operations may involve inference, which is the
process of deriving new sentences from existing ones. Inference must adhere
to the requirement that answers derived from the knowledge base follow
logically from the information previously TELLed to the knowledge base.
22 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
23 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
24 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Fig 7.3
Fig 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.
25 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
• 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,
so there must be a pit in [2,2] or [3,1] or both. The notation “P?” in Figure
7.3(b) indicates a possible pit in those squares. At this point, there is only
one known square that is OK and that has not yet been visited. So the
prudent agent will turn around, go back to [1,1], and then proceed to
[1,2].
• 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 move there. We do not show the agent’s state of
knowledge at [2,2]; we just assume that the agent turns and moves to
[2,3], giving us Figure 7.4(b). In [2,3], the agent detects a glitter, so it
should grab the gold and then return home.
26 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
3.2.c Logic:
In AI Logic is a fundamental component of logical representation and
reasoning. It enables machines to understand and represent data and
knowledge in a reasoning way. Logical reasoning is a process of inferring a
conclusion based on observations or data. It is concerned with the principles
of reasoning and how conclusions can be drawn from given premises. Logic
provides the theoretical foundation for reasoning.
Types of Logic:
Syntax : Syntax refers to the structure and rules governing the formation of
sentences or expressions in a language or representation system.Syntax is
the set of rules that dictate which sentences are well-formed in the
representation language. For example, "x + y = 4" adheres to the syntax,
while "x4y+ =" does not.
The semantics defines the truth of each sentence with respect to each
possible world. 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.” If a sentence α
is true in model m, we say that m satisfies α or sometimes m is a model of α.
We use the notation M(α) to mean the set of all models of α.
28 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Example: The relation of entailment is familiar from arithmetic; 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)
Wumpus World Example: Consider the situation in Figure below: the agent
has detected nothing in [1,1] and a breeze in [2,1].
These precepts, 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 example) there are 2^ 3 =8 possible models. These eight
models are shown in Figure 7.5 below:
29 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Fig7.5 Possible models for the presence of pits in squares [1,2], [2,2], and [3,1]. The KB
corresponding to the observations of nothing in [1,1] and a breeze in [2,1] is shown by the solid
line. (a) Dotted line shows models of α1 (no pit in [1,2]). (b) Dotted line shows models of α2 (no
pit in [2,2]).
:
the agent cannot conclude that there is no pit in [2,2]. (Nor can it conclude
that there is a pit in [2,2].)
30 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Model Checking:
Model checking is an inference algorithm that checks if a conclusion holds in
all models where the premises are true. In the Wumpus-world example,
model checking is applied to determine if certain conclusions (e.g., "There is
no pit in [1,2]") hold in all possible models consistent with the agent's
knowledge (KB). The inference algorithm illustrated in Figure 7.5 is called
model checking, because it enumerates all possible models to check that α is
true in all models in which KB is true, that is, that M(KB) ⊆ M(α).
Completeness:
31 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
32 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Grounding:
33 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
34 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Biconditional ⇔ (if and only if): The sentence W1,3 ⇔ ¬W2,2 is a biconditional.
Some texts represent this as ≡.
35 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
36 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Semantics
• The semantics defines the rules for determining the truth of a sentence
with respect to a particular model.
• In propositional logic, a model simply fixes the truth value—true or
false—for every proposition symbol.
• For example, if the sentences in the knowledge base make use of the
proposition symbols P1,2, P2,2, and P3,1, then one possible model is
• m1 = {P1,2 = false, P2,2 = false, P3,1 = true}
For complex sentences, we have five rules, which hold for any subsentences
P and Q in any model m (here “iff” means “if and only if”):
37 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
Now that we have defined the semantics for propositional logic, we can
construct a knowledge base for the Wumpus world as follows :
Sentences
• There is no pit in [1,1]:
R1 : ¬P1,1 .
• A square is breezy if and only if there is a pit in a neighboring square. This
has to be stated for each square; for now, we include just the relevant
squares:
R2 : B1,1 ⇔ (P1,2 ∨ P2,1) .
R3 : B2,1 ⇔ (P1,1 ∨ P2,2 ∨ P3,1) .
• The preceding sentences are true in all Wumpus worlds:
R4 : ¬B1,1
R5 : B2,1 .
38 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
In those three models, ¬P1,2 is true, hence there is no pit in [1,2]. On the other
hand, P2,2 is true in two of the three models and false in one, so we cannot
yet tell whether there is a pit in [2,2]. Figure 7.9 reproduces in a more precise
form the reasoning illustrated in Figure 7.5.
A general algorithm for deciding entailment in propositional logic is shown in
Figure 7.10. The algorithm is sound because it implements directly the
definition of entailment, and complete because it works for any KB and α and
always terminates—there are only finitely many models to examine. If KB
and α contain n symbols in all, then there are 2n models. Thus, the time
complexity of the algorithm is O(2n). (The space complexity is only O(n)
because the enumeration is depth-first). Every known inference algorithm for
39 | P a g e
Source Book: Artificial Intelligence by Stuart Russel and Peter Norvig. Notes Compiled by: Dr. Thyagaraju G S, Professor, HOD-CSE, SDMIT.
40 | P a g e