You are on page 1of 39

Introduction to

Artificial Intelligence
Chapter 3 - Searching and Planning

by Mintesinot Getachew
❑ Solving Problems by Searching and
planning
❑ Constraint Satisfaction Problem
❑ Problem Solving Agents
❑ Problem spaces and search

Outline
❑ Knowledge and rationality
❑ Heuristic search strategies
❑ Search and optimization (gradient
descent)
❑ Adversarial search
❑ Planning and scheduling
❑ Avoiding Repeated States
❑ Dynamic game theory

2
Solving Problems
By Searching and
Planning

3
Solving Problems by Searching and planning
Solving Problems by Searching
❑ Searching is a fundamental problem-solving technique in AI.
❑ It involves systematically exploring a space of possible solutions until a
goal state is reached.
❑ The space of possible solutions can be represented as a search tree or
graph.
❑ There are different search algorithms, such as depth-first search,
breadth-first search, and A* search, each with its own advantages and
limitations.

4
Solving Problems by Searching and planning
Planning in AI
❑ Planning is the process of finding a sequence of actions that transform an
initial state into a goal state.
❑ It involves reasoning about the effects of actions and their interactions.
❑ There are different types of planning, such as forward planning and
backward planning.
❑ Planning can be done using search algorithms or rule-based systems.
❑ Planning is used in many applications, such as robotics, scheduling, and
game playing.

5
Constraint satisfaction problem (CSP)
❑ CSP is a type of problem in AI that involves finding a solution that satisfies
a set of constraints or limitations.
❑ Representation: CSP can be represented as a set of variables, a set of
domains for each variable, and a set of constraints that specify the
allowable combinations of values for the variables.
❑ Examples: CSP can be applied to various real-world problems, such as
scheduling, planning, routing, configuration, and design.
❑ Algorithms: There are various algorithms that can be used to solve CSP,
such as backtracking search, forward checking, arc consistency, and
constraint propagation.

6
Constraint satisfaction problem (CSP) …Cont’d
❑ Heuristics: To improve the efficiency of CSP algorithms, various heuristics
can be used, such as variable and value ordering, constraint propagation,
and domain splitting.
❑ Complexity: The complexity of CSP depends on the number of variables, the
size of the domain, and the number and nature of the constraints.
❑ Applications: CSP has many practical applications in different fields, such as
manufacturing, logistics, finance, and healthcare.
❑ Challenges: Some of the challenges of CSP include dealing with incomplete
or uncertain information, handling large and complex problem domains, and
developing efficient algorithms and heuristics.

7
AI Agent Problem Formulation

▪ Problem formulation involves defining the problem, its objectives, and the constraints
that the agent must operate under.

Key Components of Search Problems Formulation


▪ Initial State: The starting point for the agent, which can be a particular configuration of the environment.
▪ Actions: The set of actions that the agent can perform to change the state of the environment.
▪ Transition Model: A function that defines how the environment make transitions from one state to
another in response to an action taken by the agent.
▪ Goal State: The desired end state that the agent must reach to solve the problem.
▪ Performance Measure: A metric that defines how well the agent is performing in achieving the goal
state. 8
Example Problem: Maze

❑ Consider the problem of a robot that needs to navigate a


maze to reach a goal.
❑ The initial state could be the robot's starting position in
the maze.
❑ The actions could be the robot's movement in any of the
available directions (up, down, left, right).
❑ The transition model would determine the robot's new
position in the maze based on the action taken.
❑ The goal state would be the location of the goal in the
maze.
❑ The performance measure could be the number of steps
taken by the robot to reach the goal. 9
Search Strategies

❑ In Artificial Intelligence, the majority of work is focused on finding the right


search strategy for a problem.

❑ Properties of Search Strategies (Algorithms):


▪ Completeness: is the strategy guaranteed to find a solution when there is one?
▪ Time complexity: how long does it take to find a solution?
▪ Space complexity: how much memory does it need to perform the search?
▪ Optimality: does the strategy find the highest-quality solution when there are
several different solutions?

10
Types of search algorithms

We will discuss six of the fundamental


search algorithms, divided into two
categories, as shown below.

11
Uninformed search Strategies

❑ Uninformed search is sometimes referred to as blind search.

❑ The uninformed search does not contain any domain specific knowledge such
as closeness, the location of the goal.

❑ Blind search algorithms are often used in situations where the state space is
small or the problem is relatively simple.
❑ Class of general-purpose search algorithms which operates in brute force-way.

12
Common Uninformed search Algorithms
❑ Breadth-first search (BFS): explores all neighbor nodes before proceeding to the next level.
❑ Depth-first search (DFS): explores as far as possible along each branch before backtracking.
❑ Iterative deepening search (IDS): performs a series of depth-limited searches with increasing
depth limits.
❑ Uniform-cost search (UCS): expands the node with the lowest path cost.
❑ Bidirectional search: performs two simultaneous searches, one from the initial state and one
from the goal state, and stops when the two meet in the middle.
❑ Depth-limited search (DLS): limits the depth of the search to a specified level.

13
Depth First Search

❑ Depth-first search is a recursive algorithm for traversing a tree or graph


data structure.
❑ It is called the depth-first search because it starts from the root node
and follows each path to its greatest depth node before moving to the
next path.
❑ DFS uses a stack data structure for its implementation.
❑ The process of the DFS algorithm is similar to the BFS algorithm.

14
Example: Search using Depth first algorithm

Agents Initial state: S Solution


Goal state: G
S
S 1
5 Visited list: S, C, D, E
9 6
6
3 1 2 C D F
C D E F

9 2 5 7 2
2
D G
G G H J
8
7 E
G
G H

Path S→C→D→E→G 15
◉ Completeness: DFS search algorithm is complete within finite state space
as it will expand every node within a limited search tree.

◉ Time Complexity: Time complexity of DFS will be equivalent to the node


traversed by the algorithm. It is given by:

T(n)= 1+ n2+ n3 +.........+ nd = O(nd)


Where, d = the depth of the search tree and n = number of nodes in level.

◉ Space Complexity: DFS algorithm needs to store only single path from the
root node, hence space complexity of DFS is equivalent to the size of the
fringe set, which is O(bm).
◉ DFS search algorithm is non-optimal, as it may generate a large number of
steps or high cost to reach to the goal node.
16
Breadth First Search

❑ Breadth-first search is the most common search strategy for traversing


a tree or graph. This algorithm searches breadthwise in a tree or graph,
so it is called breadth-first search.
❑ BFS algorithm starts searching from the root node of the tree and
expands all successor node at the current level before moving to nodes
of next level.
❑ The breadth-first search algorithm is an example of a general-graph
search algorithm.
❑ Breadth-first search implemented using FIFO queue data structure.

17
Example: Search using Breadth first algorithm

Agents Initial state: S Solution


Goal state: G

S 1
5 S Visited list: S, C, D, F
9 6
6
3 1 2
C D E F
C D F
9 2 5 7 2
2
G G H J
D G E E J
8
7
G

Path S→C→G
18
◉ Time Complexity: Time Complexity of BFS algorithm can be obtained by
the number of nodes traversed in BFS until the shallowest Node.
○ Where the d= depth of shallowest solution and n = number of nodes
at every state.

T (b) = 1+n2+n3+.......+ nd = O(nd)

◉ Space Complexity: S(n) = O(nd) (Space complexity of BFS algorithm is


given by the Memory size of frontier).
◉ Completeness: BFS is complete, meaning for a given search tree, BFS will
come up with a solution if it exists.
◉ Optimality: BFS is optimal as long as the costs of all edges are equal.

19
Iterative deepening search (IDS)

❑ A combination of DFS and BFS algorithms used for uninformed search


when the goal node's depth is unknown
❑ Process: Performs DFS up to a certain "depth limit" and increases it after
each iteration until the goal node is found
❑ Benefits: Combines fast search of BFS with memory efficiency of DFS
❑ Use cases: Useful for large search spaces and when the depth of the
goal node is unknown.

20
Completeness: This algorithm is complete is if the branching factor is finite.

Time Complexity: Let's suppose b is the branching factor and depth is d then the worst-case
time complexity is O(bd).

Space Complexity: The space complexity of IDS will be O(bd).

Optimal: IDS algorithm is optimal if path cost is a non- decreasing function of the depth of the
node.

21
Uniform-cost Search Algorithm

❑ Used for traversing weighted tree/graph with different edge costs


❑ The goal is to find a path where the cumulative sum of costs is the least.
❑ Expands nodes based on path costs from root node
❑ Implemented using priority queue with lowest cumulative cost having
highest priority
❑ Equivalent to BFS if all edge costs are same.
cost(node) = cumulative cost of all nodes from root

22
Example: Search using Uniform-Cost algorithm

Agents Initial state: S Solution


Goal state: G

S 1
5 S Visited list: S, C, F, D, E, J
9 6 5
6 9 6
3 1 2
C D E F
C D F 6
2 5 9
9 2 5 7 2 2
2 3 9
G G H J
D G E J
8 8 14 8 8
7 1 5 7 7
G D G H G
9 13 15 15

Path S→F→E→G 23
Depth-Limited Search Algorithm

❑ Similar to DFS but with predetermined depth limit


❑ Solves infinite path drawback of DFS
❑ Node at depth limit treated as having no successors
❑ Terminates with standard failure value or cutoff failure value
❑ Standard failure value happens when the problem has no solution
❑ Cutoff failure value happens when no solution is found within given
depth limit.

24
Completeness: DLS search algorithm is complete if the solution is above the depth-limit.

Time Complexity: Time complexity of DLS algorithm is O(bℓ).

Space Complexity: Space complexity of DLS algorithm is O(b×ℓ).

Optimal: Depth-limited search can be viewed as a special case of DFS, and it is also not
optimal even if ℓ > d.

25
Bidirectional Search Algorithm

❑ Runs two simultaneous searches: forward from initial state and


backward from goal state
❑ Replaces single search graph with two subgraphs
❑ Stops when two graphs intersect
❑ Can use BFS, DFS, DLS, etc. for search technique in both directions.

26
Bidirectional Search Algorithm
…Cont’d

❑ Advantages:
❑ Bidirectional search is fast.
❑ Bidirectional search requires less memory
❑ Disadvantages:
❑ Implementation of the bidirectional search tree is difficult.
❑ In bidirectional search, one should know the goal state in advance.

27
Informed (Heuristic) Search strategies

❑ Heuristic search algorithms use domain knowledge. An example of


❑ Problem information is available which can guide the informed search
algorithm
search.
❑ Heuristic search strategies can find a solution more 1. Greedy Search
efficiently than an uninformed search strategy. 2. A* Search

❑ Heuristic search can solve much complex problem which


could not be solved in another way.

28
Traveling salesman problem

Mizan Agaro

50 KM
105 KM
Bonga Jimma

Hawassa

What are algorithms that can be used to solve TSP, prepare at least 4 page report. (Assignment) 29
Best-first Search Algorithm (Greedy Search)

❑ This algorithm always selects the path which appears best at that moment.
❑ We expand the node closest to the goal node.
❑ The “closeness” is estimated by a heuristic h(x).
h(x) = Estimate of distance of node x from the goal node.
Lower the value of h(x), closer is the node from the goal.
❑ It is the combination of depth-first search and breadth-first search
algorithms.
❑ Best-first search allows us to take the advantages of both algorithms.

30
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 VISITED 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 VISITED list. If the node has not been in both list, then add it to the OPEN list.
Step 7: Return to Step 2.

31
Example
Question. 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.

32
Solution
In this search example, we are using two lists which are OPEN and VISITED Lists. Following are the iteration for
traversing the above example.

Initialization: Open [A, B], Visited [S]

Iteration 1: Open [A], Visited [S, B]

Iteration 2: Open [E, F, A], Visited [S, B]


: Open [E, A], Visited [S, B, F]

Iteration 3: Open [I, G, E, A], Visited [S, B, F] Path S→B→F→G Cost 28 33


: Open [I, E, A], Visited [S, B, F, G]
A* Tree Search

❑ Combines the strengths of uniform-cost search and greedy search.


❑ It combines features of UCS and greedy best-first search to efficiently solve
problems
❑ The algorithm finds the shortest path through the search space using the
heuristic function
❑ Strategy: Choose the node with the lowest f(x) value.

34
Algorithm of A* search

Step 1: 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.

35
Example
Question. 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 VISITED list.

36
Solution
In this search example, we are using two lists which are OPEN and VISITED Lists. Following are the iteration for
traversing the above example.

Initialization: {(S, 5)}

Iteration 1: {(S--> A, 4), (S-->G, 10)}


Path S→A→C→G Cost 6
Iteration 2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}

Iteration 3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11),


(S--> A-->B, 7), (S-->G, 10)}

Iteration 4: will give the final result, 37


Points to remember

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

38
Introduction to AI
"Artificial intelligence is the future and the future
is here.” Dave Waters

39

You might also like