You are on page 1of 75

Artificial Intelligence

CU6051NI

Lecture 2 - Search Agents


BSc (Hons) Computing
Week 2

Instructor: Sarun Dahal


Sarun.dahal@islingtoncollege.edu.np
Learning objectives
➢ Familiar with different agent-programs
➢ Get acquainted with various types of searching problems
➢ Describe strategies (framework) for formulating searching problems
➢ Be able to describe uninformed search algorithms (BFS and DFS)

CU6051NI Artificial Intelligence January 17, 2024 2


Agent programs
Goal-based agents

 Knowing something about the current state of environment is not always sufficient
o Example, what action a taxi should take (turn left, right, go straight) at junction?

 With current state description, agents need a goal information describing desirability.

 Agents consider the impact of actions on future states and work towards a goal.

CU6051NI Artificial Intelligence January 17, 2024 3


Agent programs
Goal-based agents

 Agents tries to get answers of the following questions during decision making
1. What will happen if I do such-and-such? And
2. Will that make me happy? (help to achieve my goals)

 Searching and planning methods help to find sequence of actions to achieve goals

 We will be learning about only searching methods in the module.

CU6051NI Artificial Intelligence January 17, 2024 4


Example of Searching Problems
Examples

CU6051NI Artificial Intelligence January 17, 2024 6


Examples

CU6051NI Artificial Intelligence January 17, 2024 7


Examples

CU6051NI Artificial Intelligence January 17, 2024 8


Examples

Explore
CU6051NI Artificial Intelligence January 17, 2024 9
Examples

The 8-queen problem: on a chess board, place 8 queens so that no queen is attacking any
other horizontally, vertically or diagonally.

CU6051NI Artificial Intelligence January 17, 2024 10


Formulation of Search Problems
Problem Solving Agent
 Intelligent agents try to maximize their performance measure

 One way to maximize the performance measure is if an agent can adopt a goal and
aim at satisfying it.

 How do you define a problem?


o Goal formulation
o Problem formulation

CU6051NI Artificial Intelligence January 17, 2024 12


Formulation of Search Problem
 Goal formulation
o Goals help to organize agent’s behavior to achieve its goal.
o Agent’s task is to find possible actions (now & in future) to achieve the goal.
o Is first step and considers the current situation and performance measure.
o A goal is a set of world state where the goal is satisfied.

Example: if the task is to travel from one town to another to reach a destination,
then arriving at each town along the way is an example of a state.
 Problem formulation
o Is a process to decide what actions and state to consider, given a goal.

CU6051NI Artificial Intelligence January 17, 2024 13


Formulation of Search Problem
 Agent: entity that perceives its environment and acts upon that environment.
 State: a configuration of an agent and its environment

CU6051NI Artificial Intelligence January 17, 2024 14


Formulation of Search Problem
 A search problem can be formulated in five steps
Initial state - the state in which an agent starts

CU6051NI Artificial Intelligence January 17, 2024 15


Formulation of Search Problem
Actions – choices that can be made in a state
o ACTIONS(s) returns set of actions that can be executed in state s

CU6051NI Artificial Intelligence January 17, 2024 16


Formulation of Search Problem
Transition model – a description of what state results from performing any applicable
action in any state
o RESULT(s, a) returns the state resulting from performing action a in state s

CU6051NI Artificial Intelligence January 17, 2024 17


Formulation of Search Problem
State space - the set of all states reachable from the initial state by any sequence of
actions

CU6051NI Artificial Intelligence January 17, 2024 18


Formulation of Search Problem
Goal test – A way to determine whether a given state is a goal state.
Path cost – Numerical cost associated with a given path
Agent chooses a cost function that reflects its own performance measure.

CU6051NI Artificial Intelligence January 17, 2024 19


Formulation of Search Problem
 Search Problem
o Initial state
o Action
o Transition model
o Goal test
o Path cost function
 Solution
o A sequence of actions that lead from the initial state to a goal state.
 Optimal solution
o A solution that gives lowest path cost among all solutions.

CU6051NI Artificial Intelligence January 17, 2024 20


Examples
 States: all arrangements of 0 to 8 queens on the board.
 Initial state: No queen on the board
 Actions: Add a queen to any empty square
 Transition model: updated board
 Goal test: 8 queens on the board with none attacked

CU6051NI Artificial Intelligence January 17, 2024 21


Examples

start state goal state

CU6051NI Artificial Intelligence January 17, 2024 22


Examples
 States: Location of each of the 8 tiles in
the 3x3 grid
 Initial state: Any state
 Actions: Move Left, Right, Up or Down
 Transition model: Given a state and an
action, returns resulting state
 Goal test: state matches the goal state?
 Path cost: total moves, each move costs 1.

CU6051NI Artificial Intelligence January 17, 2024 23


Examples

Find the shortest route


between 2 cities

CU6051NI Artificial Intelligence January 17, 2024 24


Examples
 States: {Los Angeles, New York,
Denver,…}
 Initial state: In Boston
 Actions: Go New York, etc.
 Transition model:
Results(In(Boston),Go(New
York)) = In(New York)
 Goal test: In(Denver)
 Path cost: path length in kms

CU6051NI Artificial Intelligence January 17, 2024 25


Real-world examples
• Route finding problem: going from one location to another, think google maps

CU6051NI Artificial Intelligence January 17, 2024 26


Real-world examples
• Travelling salesperson problem: Find the shortest tour to visit each city exactly once

CU6051NI Artificial Intelligence January 17, 2024 27


State space vs. search space
 State space: a physical configuration
 Search space: an abstract configuration represented by a search tree or graph of
possible solutions.
 Search tree: models the sequence of actions
o Root: initial state
o Branches: actions
o Nodes: results from actions. A node has: parent, children, depth, path cost,
associated state in the state space.
 Expand: A function that given a node, creates all children nodes

CU6051NI Artificial Intelligence January 17, 2024 28


Search Space Regions
 The search space is divided into three regions:
1. Explored (a.k.a. Closed List, Visited Set)
2. Frontier (a.k.a. Open List, the Fringe)
3. Unexplored.
 The essence of search is moving nodes from regions (3) to (2) to (1), and the essence
of search strategy is deciding the order of such moves.
 In the following we adopt the following color coding:
o orange nodes are explored,
o grey nodes are the frontier,
o white nodes are unexplored, and
o black nodes are failures.
CU6051NI Artificial Intelligence January 17, 2024 29
Searching Algorithms
Examples of search agents
Let's show the first steps in growing the search tree to find a route from San Francisco to
another city

CU6051NI Artificial Intelligence January 17, 2024 31


Examples of search agents

o orange nodes are explored,


o grey nodes are the frontier,
o white nodes are unexplored,

o black nodes are failures.

CU6051NI Artificial Intelligence January 17, 2024 32


Examples of search agents

o orange nodes are explored,


o grey nodes are the frontier,
o white nodes are unexplored,

o black nodes are failures.

CU6051NI Artificial Intelligence January 17, 2024 33


Examples of search agents
o orange nodes are explored,
o grey nodes are the frontier,
o white nodes are unexplored,

o black nodes are failures.

CU6051NI Artificial Intelligence January 17, 2024 34


Search Strategies
 A strategy is defined by picking the order of node expansion
 Strategies are evaluated along the following dimensions:
o Completeness - Does it always find a solution if one exists?
o Time complexity - Number of nodes generated/expanded
o Space complexity - Maximum number of nodes in memory
o Optimality - Does it always find a least-cost solution?
 Searching strategies
o Uninformed search – Given no information about the problem but definition
o Informed search – Given some knowledge or guidance where to look for solution

CU6051NI Artificial Intelligence January 17, 2024 35


Uninformed search
 Use no domain knowledge!
 Strategies
o Breadth-first search (BFS): Expand shallowest node
o Depth-first search (DFS): Expand deepest node

CU6051NI Artificial Intelligence January 17, 2024 36


Breadth-first search (BFS)
Breadth-first search (BFS)
BFS: Expand shallowest node first

CU6051NI Artificial Intelligence January 17, 2024 38


Breadth-first search (BFS)
BFS: Expand shallowest node first

Frontier: C, D, E
Frontier: A (root ) Frontier: B, C

CU6051NI Artificial Intelligence January 17, 2024 39


Breadth-first search (BFS)
BFS: Expand shallowest node first

Frontier: D, E, F, G Frontier: E, F, G, H, I
Frontier: C, D, E

CU6051NI Artificial Intelligence January 17, 2024 40


Breadth-first search (BFS)
BFS: Expand shallowest node first

Frontier: E, F, G, H, I Frontier: F, G, H, I, J, K Frontier: G, H, I, J, K, L, M

CU6051NI Artificial Intelligence January 17, 2024 41


Breadth-first search (BFS)
BFS: Expand shallowest node first
implementation:
FIFO (Queue)

Frontier: G, H, I, J, K, L, M Frontier: H, I, J, K, L, M, N, O Frontier: I, J, K, L, M, N, O

CU6051NI Artificial Intelligence January 17, 2024 42


Depth-first Search (DFS)
Depth-first Search (DFS)
DFS: Expand deepest node first

CU6051NI Artificial Intelligence January 17, 2024 44


Depth-first Search (DFS)
DFS: Expand deepest node first

Frontier: A (root) Frontier: B, C Frontier: D, E, C

CU6051NI Artificial Intelligence January 17, 2024 45


Depth-first Search (DFS)
DFS: Expand deepest node first

Frontier: D, E, C Frontier: H, I, E, C Frontier: I, E, C

CU6051NI Artificial Intelligence January 17, 2024 46


Depth-first Search (DFS)
DFS: Expand deepest node first

Frontier: I, E, C Frontier: E, C Frontier: J, K, C

CU6051NI Artificial Intelligence January 17, 2024 47


Depth-first Search (DFS)
implementation:
DFS: Expand deepest node first LIFO (Stack)

Frontier: J, K, C Frontier: K, C Frontier: C

CU6051NI Artificial Intelligence January 17, 2024 48


Depth-first Search (DFS)
DFS: Expand deepest node first

CU6051NI Artificial Intelligence January 17, 2024 49


Exercises
Exercise
Question: What is the order of visits of the nodes and the path returned by BFS and DFS?

S – start
G – goal
ignore the numbers in
the path for now

CU6051NI Artificial Intelligence January 17, 2024 51


Exercise: BFS
Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 53


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 54


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 55


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 56


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 57


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 58


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 59


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 60


Exercise: BFS

CU6051NI Artificial Intelligence January 17, 2024 61


Exercise: DFS
Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 63


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 64


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 65


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 66


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 67


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 68


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 69


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 70


Exercise: DFS

CU6051NI Artificial Intelligence January 17, 2024 71


Summary
Summary
• Discussed the structures of agent.
• Acquainted with different searching problems.
• Learned how to formulate search problems systematically.
• Understood the DFS and BFS search algorithms, uninformed searching strategies.

CU6051NI Artificial Intelligence January 17, 2024 73


End of Lecture 2

CU6051NI Artificial Intelligence January 17, 2024 74


Thank you !
Any questions ?

CU6051NI Artificial Intelligence January 17, 2024 75

You might also like