You are on page 1of 50

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

§ A start state and a goal test “E”, 1.0

§ We have to find a list of states from start to goal and


is transited by given function
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?
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 1 node
b
… b nodes
b2 nodes
§ Parameters:
§ b: chỉ số rẽ nhánh - branching factor m tiers
§ 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)

§ Completeness bm nodes
§ 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
9 2
cost 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
Properties
§ Complexity
§ Visit all node have cost < C*
b c£1
§ Let e be the min edges, maximum depth C*/e …
§ O(bC*/e) c£2
C*/e depth
c£3
§ Memory
§ O(bC*/e)

§ Completeness
§ Require > 0

§ Optimal
§ Yes
Properties
§ 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
A graph has 6 states, with Start and Goal
state, draw the search tree from Start to
Goal with the following search:
§ DFS
§ BFS
§ Uniform-cost search

In each step, when there is multi nodes in


with the same priority, choose the node
with lowest alphabetical order
Exercise 2
The agent is directional and at all times faces some direction d ∈
(N, S, E, W). With a single action, the agent can either move
forward at an adjustable velocity v or turn.
- The turning actions are left and right, which change the agent’s
direction by 90 degrees. Turning is only permitted when the v=0.
- The moving actions are fast and slow. Fast increments and slow
decrements the velocity (v) by 1; in both cases the agent then
moves a number of squares equal to its NEW adjusted velocity.
Conditions: The car is not allowed to go into the wall. velocity 0
<= v <= Vmax. The agent’s goal is to find a plan which parks it
(stationary) on the exit square using as few actions (time steps)
as possible.
a. Define a state
b. Find maximum branching factor e. If we used an inadmissible heuristic in A* tree search,
could it change the completeness of the search?
c. Is the Manhattan distance from the agent’s location to the
exit’s location admissible? Why or why not?
f) If we used an inadmissible heuristic in A* tree search,
d. State and justify a non-trivial admissible heuristic for this could it change the optimality of the search?
problem which is not the Manhattan distance to the exit.

You might also like