You are on page 1of 51

Certified Artificial Intelligence

Professional (CAIP)
Lecture 02: AI By Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 1


Outline
 Search Strategies

 Breadth First Search

 Depth First Search

 Local (Beam) Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 2


Outline
 Search Strategies

 Breadth First Search

 Depth First Search

 Local (Beam) Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 3


Search Strategies
Search Algorithms

Uninformed Search Informed Search

Breadth-first search Greedy best-first search

Depth-first search A* search

Uniform-cost search

Bidirectional search

Depth-limited search

Iterative deepening search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 4


Uninformed Search
• Uninformed search (also called blind search) has no information about states
beyond that provided in the problem definition.
◦ Examples: Breadth-First Search (BFS), Depth-First Search (DFS), Uniform-Cost Search (UCS),
Iterative Deepening Depth-First Search (IDDFS), Bidirectional Search

• These strategies have no additional information about states beyond that provided
in the problem definition.

• They only generate successors and distinguish a goal state from a non-goal state.

• The algorithm examines each node of the tree until it achieves the goal node.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 5


Informed Search
• Informed Search (also called heuristic search ) uses problem-specific
knowledge or heuristics to guide the search towards the goal state.
◦ Example: A* Search, Best-First Search, Hill Climbing Search, Greedy Best-First Search

• Best-first search: an instance of the general tree-search or graph-search algorithm


in which a node is selected for expansion based on an evaluation function, f(n).

• The evaluation function is construed as a cost estimate, so the node with the
lowest evaluation is expanded first.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 6


Informed Search
• The choice of evaluation function f(n) determines the search strategy.

• Most best-first algorithms include as a component of f a heuristic function,


denoted h(n):
h(n) = estimated cost of the cheapest path from the state at node n to a goal state.

• Example: Travelling salesman problem which asks the following question: "Given a
list of cities and the distances between each pair of cities, what is the shortest
possible route that visits each city exactly once and returns to the origin city?"

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 7


Lab Activity
Q1. Starting from the green node at the top, use graph search to reach the yellow
node using the least number of nodes. Show the graph search at each step till it
reaches the goal. Assume that all links have the same cost.

B1 B2

C1 C2 C3

D1 D3
D2 D4

E1 E2 E3

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 8


Lab Activity

• Task: use a search strategy to reach the goal state. State the following:

◦ Type of search strategy (Uniformed or informed)

◦ Actions

◦ Transition model

◦ Path cost (assume each step costs 1)

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 9


Outline
 Search Strategies

 Breadth First Search

 Depth First Search

 Local (Beam) Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 10


Breadth First Search
• Breadth First Search (BFS) is an example of Uninformed Search.

• It is a brute force method for searching, has no additional information beyond the
problem statement.

• Each node is either a goal state or non-goal state. So every node is examined.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 11


Breadth First Search
• BFS has TREE or GRAPH data structure.

• It starts from the root or some arbitrary


node.

• From here it explores the neighboring node


at the depth.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 12


Breadth First Search
• Problems
◦ Explored node is again explored via other
paths

◦ Root node: 1.

◦ Successive shallowest node: 2 , 4

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 13


Breadth First Search
• Problems
◦ Explored node is again explored via other
paths
◦ Explored 2 , 4

◦ Successive shallowest node: 5 , 2 (again)


◦ Consider the Arrow

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 14


Breadth First Search
• Problems
◦ Explored node is again explored via other
paths

◦ Explored 5 , 2 (again)

• This is closed loop tree searching


will goes on forever

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 15


Breadth First Search
• Solution
◦ Two types of Ques are made
◦ Frontier Que

◦ Explored Que / Visited Que

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 16


Breadth First Search
• Solution
◦ Two types of Ques are made
◦ Frontier Que

◦ Explored Que / Visited Que

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 17


Breadth First Search
• Solution
◦ Two types of Ques are made
◦ Frontier Que

◦ Explored Que / Visited Que

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 18


Breadth First Search
• FIFO Que
◦ Frontier is First in First Out Que.

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 19


Breadth First Search
• FIFO Que
◦ Frontier is First in First Out Que.

• Goal Test
◦ Goal test applied on all frontier ques

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 20


Breadth First Search
• Frontier empty
◦ If goal test still fails  Search Fails

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 21


Breadth First Search – Pseudo Code

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 22


Outline
 Search Strategies

 Breadth First Search

 Depth First Search

 Local (Beam) Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 23


Depth First Search
• Deepest Node in the Current Frontier is expended First

• Unlike BFS, it first goes Deep as much as Possible

• It is the implementation of Recursion.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 24


Depth First Search
• LIFO Que
◦ Last In First Out Que is used (Stack Memory)

• Space Complexity
◦ It is less space complex then BFS.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 25


Depth First Search – Example
• Concept Remains same as of BFS.
◦ Two type of ques are made.

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 26


Depth First Search – Example
• 0th Node is visited First

• 1 , 2 , 3 Nodes are in the Frontier

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 27


Depth First Search – Example
• 1st Node is Expanded

• 2 , 3 Nodes are in the Frontier (same as previous)


◦ Shows less space complexity against BFS

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 28


Depth First Search – Example
• 2nd Node is Expanded

• 4 , 3 Nodes are in the Frontier

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 29


Depth First Search – Example
• 4th Node is Expanded

• 3 Node is in the Frontier

• Remember! Goal Test is Applied to all the Nodes before exporting to


Explored Que

Explored Que

Frontier Que

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 30


Depth First Search – Variants
• Depth Limited Search
◦ Search with the Limited Depth
◦ If graph is deep, DFS can exhaust system resources. So, instead of DFS, DLS is
implemented.

• Iterative Deepening DFS


◦ DFS may expand node more than necessary.
◦ To avoid this problem, shorter paths are used.
◦ All paths are given weights.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 31


Depth First Search – Pseudo Code

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 32


Outline
 Search Strategies

 Breadth First Search

 Depth First Search

 Local (Beam) Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 33


Local Search
• Heuristic Technique
◦ Extends graph by the most promising node.

◦ Path Cost is also in consideration.

• Non – Systematic Search


◦ Systematics Searches when one another node is
explored in sequence.

◦ Local Search is based on heuristic values.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 34


Local Search – Advantages
◦ Memory consumption is very little.

◦ Constant amount of space is occupied, as they operate in only a single path.

◦ Reasonable solutions are found in large state spaces.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 35


Local Search – Examples
• Factory Floor Layout
◦ What is the optimal way for equipment to be placed inside the factory for less cost?

• High Level Reasoning for Robots


◦ Is the robot performing tasks optimally or at the right time?
◦ Example: Vacuum Cleaner Robot working inside a clothing shop.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 36


Local Search – Variants
◦ Hill Climbing Search

◦ Simulated Annealing

◦ Genetic Algorithms

◦ Local Beam Search

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 37


Local Search – Hill Climbing
• Climb the hill and reach the highest peak.

• Neighbor states are compared with the current


state.
◦ If any of the neighbor is better current node
position is changed from current node to the
neighboring node

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 38


Hill Climbing – Pseudo Code

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 39


Hill Climbing – Example
• 8 Queen Problem
◦ Place 8 Queens in such a way that no queen is attacking each
other

◦ Heuristic = No. of Pair Queen Attacking each other

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 40


Hill Climbing – Example
• 8 Queen Problem
◦ Place 8 Queens in such a way that no queen is attacking each
other

◦ Heuristic = No. of Pair Queen Attacking each other

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 41


Hill Climbing – Example
• 4 Queen Problem

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 42


Hill Climbing – Example
• Hospital Problem
◦ Place two hospitals inside a state space with four houses. What would be the optimal cost?

◦ Initially the cost is 17.

◦ Heuristic:
◦ Changing direction = 1 ,

◦ Move along straight line = 2

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 43


Hill Climbing – Example
• Hospital Problem
◦ Place two hospitals inside a state space with four houses. What would be the optimal cost?

◦ After iteration: Cost = 11

◦ Less than the previous one but still not optimal.

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 44


Hill Climbing – Example
• Hospital Problem
◦ Place two hospitals inside a state space with four houses. What would be the optimal cost?

◦ After iteration: Cost = 11

◦ Less then the previous one but not optimal

◦ Move this hospital to red block

◦ Cost = 9

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 45


Hill Climbing – Example
◦ Why algo fails in optimality?
◦ Stuck inside the Local Maxima

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 46


Hill Climbing
• Types

◦ Stochastic Hill Climbing


◦ Choose randomly from available uphill moves

◦ Random Restart Hill Climbing


◦ Try and Try Strategy.

◦ If tried once but do not succeed try again and again with random initialization

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 47


Lab Assignment
• Go from Karlsruhe to Kassel using
Breadth First Search and Depth First Search
◦ Keep the distance that is travelled

◦ Keep the record of all the explored Nodes

◦ Compare the results

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 48


Lab Assignment
• 8 Puzzle Problem
◦ Use local search to go from Initial State to
the Final State
◦ Horizontal and Vertical Movement is allowed
only.

◦ Find cost?
◦ Hint: Check how many moves is required for
one piece from initial position to goal position.
Add all!

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 49


Any Questions?

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 50


That’s it for today.
See you next week!

13-Apr-2023 National Center of Artificial Intelligence (NCAI) 51

You might also like