You are on page 1of 51

Artificial Intelligence

Search

Instructors: Ta Viet Cuong


Rational Agents
 Definitions:
 An agent which acts in a way that is expected to maximize to its
performance measure, given the evidence provided by what it
perceived and whatever built-in knowledge it has.
 Key terms:
 Performance measure: how you measure your score?
 Perceived: what kind of information we perceived?
 Built-in knowledge
 Rational: Come from various aspect/perspective
Rational Agents
 We need definitions of:
 Environment
 Input/Perceptions
 Reward or score function
 Action
 Some examples:
 Game-based
Example: Automatic Driving Car
 A setup includes:
 Environment: straight line, curve, obstacles, cars
 Perception: Camera, GPS, Sound
 Score function: safety, speed, fuel
 Actions: turn left, turn right, speed/slow, stop …
Today
 Planning

 Searching

 Uninformed search methods


 Depth-First Search
 Breadth-First Search
 Uniform-Cost Search
Reflex Agents

 Reflex Agent:
 Choose action base on current percept (and
maybe memory)
 May have memory or a model of the world’s
current state
 Do not consider the future consequences of
their actions

[Demo: reflex optimal (L2D1)]


[Demo: reflex optimal (L2D2)]
Video of Demo Reflex Optimal
 Pacman environment
 Đi sang ô cạnh
 Di chuyển: -1
 Ăn: 10
 Reflect agent:
 Cứ tăng điểm thì đi
Video of Demo Reflex Odd
Planning Agents

 Planning Agent:
 Look into future: What if ?
 Decisions plan with considerations a
consequence
 Think by having a model of the world/actions
 Have a goal

 Optimal vs complete planning

 Planning vs. replanning


Video of Demo Replanning
Video of Demo Mastermind
Search Problems
Search Problems
 A Search Problem includes:

 State space

“N”, 1.0

 Transition Function
“E”, 1.0

 A start state and a goal test


How to model the world
Example: Traveling in Romania

 Space:
 Cities
 Transition function:
 Roads: Go to adjacent city with cost =
distance
 Start:
 Arad
 End:
 Is state == Bucharest?

 Solution?
What’s in a State Space?
Fully Observable

"Ghost" can kill


"Big Dot" can "scare"
ghost for N steps

Search space can be modeled by tasks

 Path finding:  Finding path cover all dots


 States: (x,y) location  States: {(x,y), dot booleans}
 Actions: NSEW  Actions: NSEW
 Successor: update location  Successor: update location
only and possibly a dot boolean
 Goal test: is (x,y)=END  Goal test: dots all false
How big is the search space
 Given:
 Agent positions: 120
 Food count: 30
 Ghost positions: 12
 Agent facing: NSEW

 How many
 Full space:
120x(230)x(122)x4
 Path finding:
120
 Eating all dots:
120x(230)
Safety path planning

 Problem: Ăn hết các chấm + luôn để các con ghost sợ hãi (N = 10)
 How we define a state?
 (agent position, dot booleans, big dot booleans, remaining scared time)
Graph Search Space vs Tree Search Space
State Space Graphs

 The search problem can be converted into a


graph

 Usually, it’s too BIG


Search Tree
This is now / start
“N”, 1.0 “E”, 1.0

Possible futures

 In general, we allow revisted the node in search tree


Search space vs search tree

Search space Search tree


S

a G d e p
b c
b c e h r q
e
d f a a h r p q f
S h
p q f q c G
p q r
q c G a

a
Đồ thị không gian trạng thái vs. Cây tìm kiếm

Cho đồ thị Độ lớn cây tìm kiếm từ S

S G

Important: Lots of repeated structure in the search tree!


Search Tree
Search Example: Romania
Searching by Tree

 Main approach:
 Go from start
 Expand the nodes which are not expanded
 Avoid repeating nodes
Pseudocode

 Main question: Which nodes to expand


 Search strategy
 Technical question:
 How much space we need to stores
 How to know a node has been visited
Example: Tree Search
a G
b c
e
d f
S h
p q r
Depth-First Search
Depth-First Search
a G
b c
e
d f
S h
p q r

d e p

b c e h r q

a a h r p q f

p q f q c G

q c G a

a
DFS is good or bad?
Search Properties
 Completeness: Always find a solution if it exists
 Optimal: Always find the best solution
 Run time
 Memory b
1 node
… b nodes
b2 nodes
 Parameters:
m tiers
 b: chỉ số rẽ nhánh - branching factor
 m: độ sâu is the maximum depth
 Lời giải có thể nằm ở các độ sâu khác nhau
bm nodes

 How many nodes?


 1 + b + b2 + …. bm = O(bm)
DFS properties
 Time complexity
 Move from left to right 1 node
b
 Looking for the entire tree … b nodes
 If m is finite, takes time O(bm) b2 nodes
m tiers
 Memory complexity
 O(bm)

bm nodes
 Completeness
 Need m < infinity

 Optimal
 No
Breadth-First Search
Breadth-First Search
Use: queue a G
b c
Instead stack
e
d f
S h
p q r

d e p
Increase
b c e h r q
level of
searching
a a h r p q f

p q f q c G

q c G a

a
BFS properties
 Time complexity:
 O(bs) 1 node
b
 If s < m, then we have advantage over DFS … b nodes
s
 Memory complexity? b2 nodes
 O(bs)
bs nodes
 Store the last row
 Completeness:
 Yes bm nodes

 Optimal
 Find the least edges path
Quiz: DFS vs BFS
DFS vs BFS

 When will BFS outperform DFS?

 When will DFS outperform BFS?

[Demo: dfs/bfs maze water (L2D6)]


Video of Demo Maze Water DFS/BFS (part 1)
Video of Demo Maze Water DFS/BFS (part 2)
Search by depth
 Compare DFS and BFS
 Run DFS with depth = 1, if not b
 Run DFS with depth = 2, if not …

 Run DFS with depth = 3, if not

 Trade-off: Revisited a lot of states


 No win-win solution
 The cost of uninformed search
Uniform Cost Search
a GOAL
2 2
b c
3
2
1 8
2 e
3 d
f
9 8 2
START h
1 4 2

p 4 r
15
q
Uniform Cost Search
Uniform cost search - UCS
UCS, a.k.a Dijkstra
2 a G
Uniform cost search - UCS b c
1 8 2
Go from nodes with least 2 e
3 d f
cost 9 2
S h 8
1
1 p q r
15

S 0

d 3 e 9 p 1

b 4 c e 5 h 17 r 11 q 16
11
Boundary a 6 a h 13 r 7 p q f
Of search
p q f 8 q c G

q 11 c G 10 a

a
Tính chất
 Complexity
 Visit all node have cost < C*
b c1
 Let  be the min edges, maximum depth C*/ …
 O(bC*/) c2
C*/ depth
c3
 Memory
 O(bC*/)

 Completeness
 Require > 0

 Optimal
 Yes
Tính chất
 Càng đi nhiều bước thì "đường viền" tìm c1

kiếm càng rộng c2
c3
 The bad:
 Phải tìm kiếm về mọi hướng
 Không có thông tin về điểm đến
 Ví dụ: bài toán giao thông tìm đường đi ngắn
nhất (đi về hướng nào đó)

Start Goal
Cấu trúc dữ liệu cần thiết
 Về cài đặt, các thuật toán
search khác nhau ở chỗ "thứ tự
tìm kiếm"
 "Hàng đợi"
 BFS/DFS không cần phải tìm trong logn
 UCS cần sử dụng priority-queue
Vấn đề về mô hình hóa

 Trên thực tế, agent cần:


 Mô hình hóa môi trường/thế
giới - world model
 Tìm kiếm trong mô hình đó
 Độ tốt của lời giải từ bài toán
tìm kiếm sẽ chỉ tốt ngang đc
cách agent mô hình
Exercise 1
Cho đồ thị như hình, vẽ cây tìm kiếm và tìm
đường đi từ Start-Goal với:
 DFS
 BFS
 Uniform-cost search
Ở mỗi step, thì node có thứ tự nhỏ nhất sẽ
được chọn mở rộng đầu tiên. Mỗi node
được mở rộng 1 lần
Exercise 2
Cho ma trận với các vật cản với agent là xe oto(hình tam giác).
Agent di chuyển trong ma trận với vận tốc v, nguyên không âm
và không quá Vmax. Agent có thể quay 1 trong 4 hướng N, S, E,
W.
 Ở mỗi step, agent có thể có 1 trong các hành động sau:
 1. Tăng tốc nếu v < Vmax
 2. Đi chậm lại nếu v >= 0
 3. Giữ nguyên, agent đi tiếp theo hướng đang có nếu v > 0,
 4. Quay trái hoặc quay phải, chỉ thực hiện được nếu v = 0.
Cau hoi:
a. Tính chỉ số rẽ nhánh (branching factor) lớn nhất, nhỏ nhất
b. Vẽ cây BFS với độ sâu 2
Exercise 2
Code Assignment:
Initializing car environment and implement methods for calculate the
cost in each step, with input are :
• N: size of maze,
• xs, ys: coordinate of the start position,
• xg, yg: coordinate of the goal,
• [(xwi, ywi, xwj, ywj), ...]: coordinates of the walls
• Vmax: the max of velocity
According the maze above, we have: N = 5, (xs, ys) = (0, 4), (xg, yg) =
(0, 0),
walls: [(0, 3, 1, 3), (0, 4, 0, 1)], assume: Vmax = 3

You might also like