Ar#ficial
Intelligence
Dr.
Qaiser
Abbas
Department
of
Computer
Science
&
IT
University
of
Sargodha
[email protected]
Wednesday
20
March
19
1
3
SOLVING
PROBLEMS
BY
SEARCHING
• In
which
we
see
how
an
agent
can
find
a
sequence
of
ac3ons
that
achieves
its
goals
when
no
single
ac3on
will
do.
Wednesday
20
March
19
2
3.1
PROBLEM-‐SOLVING
AGENTS
• Imagine
an
agent
in
the
city
of
Arad,
Romania,
enjoying
a
touring
holiday.
– Performance
measure
contains
many
factors:
it
wants
to
improve
its
suntan,
improve
its
Romanian,
take
in
the
sights,
enjoy
the
nightlife,
avoid
hangovers,
and
so
on.
– Current
State:
Now,
suppose
the
agent
has
a
nonrefundable
Ocket
to
fly
out
from
Bucharest
the
following
day.
– Goal:
Reaching
at
Bucharest
on
tomorrow.
Wednesday
20
March
19
3
3.1
PROBLEM-‐SOLVING
AGENTS
• Goal
formula#on
is
the
first
step
in
problem
solving,
based
on
the
current
situaOon
and
the
agent’s
performance
measure.
– Goals
help
organize
behavior
by
limiOng
the
objecOves
that
the
agent
is
trying
to
achieve
(performance
measures)
and
hence
the
acOons
it
needs
to
consider.
• Problem
formula#on
is
the
process
of
deciding
what
acOons
and
states
to
consider,
given
a
goal.
Wednesday
20
March
19
4
3.1
PROBLEM-‐SOLVING
AGENTS
Wednesday
20
March
19
5
3.1
PROBLEM-‐SOLVING
AGENTS
• Discussion
at
Romanian
Example:
– Driving
from
one
town
to
another
are
the
acOons
and
towns
are
the
states.
– Three
roads
lead
out
of
Arad,
one
toward
Sibiu,
one
to
Timisoara,
and
one
to
Zerind.
None
of
these
achieves
the
goal
Bucharest.
– If
the
agent
has
no
addiOonal
informaOon—i.e.,
if
the
environment
is
unknown
—then
it
has
no
choice
but
to
try
one
of
the
acOons
at
random.
– If
the
agent
has
the
map
of
Romania
(addiOonal
info.)
then
it
can
examine
future
acOons
that
can
lead
to
Bucharest.
Wednesday
20
March
19
6
3.1
PROBLEM-‐SOLVING
AGENTS
– In
observable
environment,
agent
knows
the
current
state
like
each
city
on
the
map
has
a
sign.
– In
discrete
environment,
finite
acOons
are
available
at
a
given
state
like
each
city
is
connected
to
a
small
number
of
other
ciOes.
– In
known,
agent
knows
which
states
are
reached
by
each
acOon.
• Having
an
accurate
map
fulfills
this
condiOon.
– In
determinis#c,
each
acOon
has
exactly
one
outcome.
• Under
ideal
condiOons,
drive
from
Arad
to
Sibiu
ends
up
in
Sibiu.
Of
course,
condiOons
are
not
always
ideal
(See
Chapter
4)
Wednesday
20
March
19
7
3.1
PROBLEM-‐SOLVING
AGENTS
• SoluOon
to
any
problem
is
a
fixed
sequence
of
acOons.
– It
could
be
a
branching
strategy
which
leads
to
different
acOons
on
different
percepts
like
in
driving
from
Arad
to
Sibiu
and
then
to
Rimnicu
Vilcea,
it
can
arrive
in
Zerind
by
accident
instead
of
Sibiu.
• If
the
environment
is
known
and
determinisOc,
then
agent
knows
the
states
ader
the
first
acOon,
second
acOon,
and
so
on,
respecOvely.
Wednesday
20
March
19
8
3.1
PROBLEM-‐SOLVING
AGENTS
• Looking
for
a
sequence
of
acOons
(soluOon)
that
reaches
the
goal
is
called
search.
• Then
these
acOons
can
be
carried
out.
This
is
called
the
execuOon
phase.
• This
“formulate,
search,
execute”
concludes
the
design
for
the
agent,
as
shown
in
Figure
3.1.
• During
execuOon,
it
ignores
its
percepts
when
choosing
an
acOon
because
it
knows
in
advance
what
they
will
be.
• These
are
called
an
open-‐loop
system,
because
ignoring
the
percepts
breaks
the
loop
between
agent
and
environment.
Wednesday
20
March
19
9
3.1
PROBLEM-‐SOLVING
AGENTS
Wednesday
20
March
19
10
3.1.1
Well
Defined
Problems
• A
problem
is
defined
by
five
components:
– Ini#al
state:
e.g.,
In(Arad)
– Ac#ons:
AcOons(s)
=
set
of
acOons
for
state
s
e.g.,
AcOons(In(Arad))
=
{Go(Sibiu),
Go(Timisoara),
Go(Zerind)}
– Transi#ons
or
States:
Result(s,
a)
=
the
successor
state
e.g.,
Result(In(Arad),
Go(Zerind))
=
In(Zerind)
– Goal
test:,
can
be
an
explicit
set
of
states,
e.g.,
{In(Bucharest)}
or
an
implicit
property,
e.g.,
checkmate
in
chess
– Path
cost:
is
the
sum
of
the
step
costs
C(s,
a,
sʹ′)
e.g.,
sum
of
distances,
number
of
acOons
executed,
etc.
• IniOal
state,
acOons,
and
transiOon
model
implicitly
define
the
state
space
of
the
problem.
• AbstracOng
(removing
details)
the
states
(the
traveling
companions,
etc.)
and
acOons’
descripOon
(turning
on
the
radio,
etc.)
is
necessary.
Wednesday
20
March
19
11
3.2
EXAMPLE
PROBLEM
DEFINITION
Vacuum
Cleaner
World
• States??:
dirt
and
robot
locaOons
(ignore
dirt
amounts
etc.)
• Ini#al
state??:
any
state
• Ac#ons??:
Led,
Right,
Suck,
NoOp
• Goal
test??:
no
dirt
in
any
locaOon
• Path
cost??:
1
per
acOon
(0
for
NoOp)
Wednesday
20
March
19
12
3.2
EXAMPLE
PROBLEM
DEFINITION
8-‐Puzzle
• States??:
a
3×3
matrix
of
integers
• Ini#al
state??:
any
state
• Ac#ons??:
move
the
blank
space:
led,
right,
up,
and
down
• Goal
test??:
equal
to
given
goal
state
• Path
cost??:
1
per
move
Wednesday
20
March
19
13
3.2
EXAMPLE
PROBLEMS
8-‐Queen
• Complete-‐state
formula#on:
Starts
with
all
8
queens
on
the
board.
– States:
Any
arrangement
of
0
to
8
queens
on
the
board
is
a
state.
– Ini#al
state:
No
queens
on
the
board.
– Ac#ons:
Add
a
queen
to
any
empty
square.
– Goal
test:
8
queens
are
on
the
board,
none
aqacked.
– Path
Cost:
1
per
move.
ü We
have
64
·∙
63
·∙
·∙
·∙
57
≈
1.8
×
1014
possible
sequences
to
invesOgate.
• Incremental
formula#on:
StarOng
with
an
empty
state
and
each
acOon
adds
a
queen
to
the
state.
– States:
one
per
column
in
the
ledmost
n
columns,
with
no
queen
aqacking
another.
– Ac#ons:
add
a
queen
to
any
square
in
the
ledmost
empty
column,
making
sure
that
no
queen
is
aqacked
ü State
space
reduced
from
1.8
×
1014
to
just
2,057,
and
soluOons
are
easy
to
find.
Wednesday
20
March
19
14
3.2
EXAMPLE
PROBLEMS
8-‐Queen
Wednesday
20
March
19
15
Find
a
Solu#on
for
8-‐Queen
Problem
Wednesday
20
March
19
16
3.2
EXAMPLE
PROBLEMS
• 3.2.2
Real-‐world
problems
(Airline
Travel
Website)
– States:
A
locaOon
(e.g.,
an
airport)
and
the
current
Ome.
The
state
must
record
extra
informaOon
about
“historical”
aspects
like
fare
bases,
domesOc,
internaOonal,
etc..
– Ini#al
state:
Specified
by
the
user’s
query.
– Ac#ons:
Take
any
flight
from
the
current
locaOon,
in
any
seat
class,
leaving
ader
the
current
Ome,
leaving
enough
Ome
for
within-‐airport
transfer
if
needed.
– Goal
test:
Final
desOnaOon
specified
by
the
user?
– Path
cost:
This
depends
on
monetary
cost,
waiOng
Ome,
flight
Ome,
customs
and
immigraOon
procedures,
seat
quality,
Ome
of
day,
type
of
airplane,
frequent-‐flyer
mileage
awards,
and
so
on.
Wednesday
20
March
19
17
3.2
EXAMPLE
PROBLEMS
(Read
it
by
yourself)
• Traveling
salesperson
problem:
Each
city
must
be
visited
exactly
once.
The
aim
is
to
find
the
shortest
tour.
• VLSI
layout:
problem
requires
posiOoning
millions
of
components
and
connecOons
on
a
chip
to
minimize
area,
circuit
delays,
stray
capacitances,
and
maximize
manufacturing
yield.
• Robot
naviga#on:
is
a
generalizaOon
of
the
route-‐
finding
with
an
infinite
set
of
possible
acOons
and
states.
• Automa#c
assembly:
Sequencing
of
complex
objects
by
a
robot
and
so
on.
Wednesday
20
March
19
18
3.1
PROBLEM-‐SOLVING
AGENTS
Wednesday
20
March
19
19
3.3
SEARCHING
FOR
SOLUTIONS
Wednesday
20
March
19
20
3.3
SEARCHING
FOR
SOLUTIONS
• Root
node
corresponds
to
the
iniOal
state,
In(Arad).
• Test
whether
this
is
a
goal
state.
(Clearly
it
is
not,
but
it
is
important
to
check
so
that
we
can
solve
trick
problems
like
“starOng
in
Arad,
get
to
Arad.”)
• Expanding
the
current
state;
thereby
generaOng
a
new
set
of
states.
Add
three
branches
from
the
parent
node
In(Arad)
leading
to
three
new
child
nodes:
In(Sibiu),
In(Timisoara),
and
In(Zerind).
Which
of
these
three
possibiliOes
to
consider
further.
Wednesday
20
March
19
21
3.3
SEARCHING
FOR
SOLUTIONS
• This
is
the
essence
of
search—following
up
one
opOon
now
and
puvng
the
others
aside
for
later,
in
case
the
first
choice
does
not
lead
to
a
soluOon.
• Suppose
we
choose
Sibiu
first.
We
check
to
see
whether
it
is
a
goal
state
(it
is
not)
and
then
expand
it
to
get
In(Arad),
In(Fagaras),
In(Oradea),
and
In(RimnicuVilcea).
• We
can
then
choose
any
of
these
four
or
go
back
and
choose
Timisoara
or
Zerind.
• All
leaf
nodes
are
kept
in
fronOer.
Wednesday
20
March
19
22
3.3
SEARCHING
FOR
SOLUTIONS
• Repeated
states
can
happen
in
tree
search
through
loopy
path
like
from
Sibiu
to
Arad
again.
• Considering
loopy
paths
(redundant
paths)
means
the
search
tree
is
infinite
and
in
other
words
no
soluOon.
• Reaching
the
goal
is
possible
ader
removing
these
redundant
paths.
Wednesday
20
March
19
23
3.3
SEARCHING
FOR
SOLUTIONS
• Algorithms
that
forget
their
history
are
doomed
to
repeat
it.
• Explored
set
to
remember
every
expanded
node.
• Newly
generated
nodes
that
match
with
ones
in
the
explored
set
or
the
fronOer
can
be
discarded.
Wednesday
20
March
19
24
3.3
SEARCHING
FOR
SOLUTIONS
• Route-‐
finding
on
a
rectangular
grid
in
Figure
3.9
is
a
parOcularly
important
example
in
computer
games.
Wednesday
20
March
19
25
Search
strategies
• A
strategy
is
defined
by
picking
the
order
of
node
expansion
• Strategies
are
evaluated
along
the
following
dimensions:
– Completeness—does
it
always
find
a
soluOon
if
one
exists
– OpOmality—does
it
always
find
a
least-‐cost
soluOon?
– Time
complexity—number
of
nodes
generated/expanded
– Space
complexity—maximum
number
of
nodes
in
memory
• Time
and
space
complexity
are
measured
in
terms
of
• b—maximum
branching
factor
of
the
search
tree
• d—depth
of
the
least-‐cost
soluOon
• m—maximum
depth
of
the
state
space
(may
be
∞)
Wednesday
20
March
19
26
3.4
UNINFORMED
SEARCH
STRATEGIES
• Uninformed
search
strategies
use
only
the
informaOon
available
in
the
problem
definiOon
and
have
no
addiOonal
informaOon.
♦
Breadth-‐first
search
♦
Uniform-‐cost
search
♦
Depth-‐first
search
♦
Depth-‐limited
search
♦
IteraOve
deepening
search
Wednesday
20
March
19
27
3.4
UNINFORMED
SEARCH
STRATEGIES
3.4.1
Breadth-‐first
search
– Breadth-‐first
search
is
a
simple
strategy
in
which
the
root
node
is
expanded
first,
then
all
the
successors
of
the
root
node
are
expanded
next,
then
their
successors,
and
so
on.
– Achieved
simply
by
using
FIFO
queue
for
the
fronOer.
– Slight
tweak
on
the
general
graph-‐search
algorithm
(Figure
3.7)-‐-‐Goal
test
is
applied
to
each
node
when
it
is
generated.
Wednesday
20
March
19
28
3.4.1
Breadth-‐first
search
Wednesday
20
March
19
29
Proper#es
of
breadth-‐first
search
• Complete??
Yes
if
b
is
finite
• Time??
1+b+b2
+b3
+...+bd
=
O(bd),
i.e.,
exponenOal
in
d
• Op#mal??
Yes,
if
step
cost
=
idenOcal,
Not
opOmal
in
general
• Space??
There
will
be
O(bd−1)
nodes
in
the
explored
set
and
O(bd)
nodes
in
the
fronOer,
so
the
space
complexity
is
O(bd).
– Problem:
it
can
easily
generate
1M
nodes/second,
so
ader
24hrs
it
has
used
86,000GB
(and
then
it
has
only
reached
depth
9
in
the
search
tree)
Wednesday
20
March
19
30
3.4.2
Uniform-‐cost
search
(UCS)
• When
all
step
costs
are
equal,
breadth-‐first
search
is
opOmal
because
it
always
expands
the
shallowest
unexpanded
node.
• By
a
simple
extension,
we
can
find
an
algorithm
that
is
opOmal
with
any
step-‐cost
funcOon.
• Instead
of
expanding
the
shallowest
node,
uniform-‐cost
search
expands
the
node
n
with
the
lowest
path
cost
g(n).
This
is
done
by
storing
the
fronOer
as
a
priority
queue
ordered
by
g.
The
algorithm
is
shown
in
Figure
3.14.
/*with
the
cumulaOve
costs
as
priority*/
Output:
IteraOon
1:
ES=S,
[SàRV,
80],
[SàF,
99]
IteraOon
2:
ES=S+RV,
[SàRVàP,
177],
[SàF,
99]
IteraOon
3:
ES=S+RV+F,
[SàRVàP,
177],
[SàFàB,
310]
IteraOon
4:
ES=S+RV+F+P,
[SàRVàPàB,
278],
[SàFàB,
310]
Wednesday
20
March
19
31
3.4.2
Uniform-‐cost
search
(UCS)
• Goal
test
is
applied
when
it
is
selected
for
expansion
(as
in
the
graph-‐search
algorithm)
rather
than
when
it
is
first
generated.
The
reason
is
that
the
first
goal
node
that
is
generated
may
be
on
a
subopOmal
path.
• Equivalent
to
breadth-‐first
search,
if
all
step
costs
are
equal.
Wednesday
20
March
19
32
Proper#es
of
UCS
• Complete??
Yes,
if
step
cost
≥
ε
>
0
• Time??
#
of
nodes
with
path
cost
g(n)
≤
C∗,
– i.e.,
O(b1+⌊C∗/ε⌋)
where
C∗
is
the
cost
of
the
opOmal
soluOon
and
ε
is
the
least
cost
of
every
acOon(step
cost).
• Space??
Same
as
Ome
• Op#mal??
Yes—nodes
are
expanded
in
increasing
order
of
g(n)
• Uniform-‐cost
search
is
guided
by
path
costs
rather
than
depths,
so
its
complexity
is
not
easily
characterized
in
terms
of
b
and
d.
• Algorithm’s
worst-‐case
Ome
and
space
complexity
is
O(b1+⌊C∗/ε⌋),
which
can
be
much
greater
than
bd.
Wednesday
20
March
19
33
3.4.3
Depth-‐first
search
• Depth-‐first
search
always
expands
the
deepest
node
in
the
current
fronOer
of
the
search
tree.
• Complete??
No:
it
fails
in
infinite-‐depth
spaces.
It
also
fails
in
finite
spaces
with
loops
but
if
we
modify
the
search
to
avoid
repeated
states
– complete
in
finite
spaces
• Time??
O(bm):
terrible
if
m
is
much
larger
than
d
but
if
soluOons
are
dense,
it
may
be
much
faster
than
breadth-‐first.
• Space??
O(bm):
i.e.,
linear
space!
• Op#mal??
No
Wednesday
20
March
19
34
3.4.3
Depth-‐first
search
Wednesday
20
March
19
35
3.4.4
Depth-‐limited
search
• Failure
of
depth-‐first
search
in
infinite
state
spaces
can
be
alleviated
by
supplying
depth
limit
l.
This
approach
is
called
depth-‐limited
search.
Read
it
yourself
Wednesday
20
March
19
36
3.4.4
Depth-‐limited
search
depth
limit
=
max
depth
to
search
to;
agenda
=
iniOal
state;
if
iniOal
state
is
goal
state
then
Read
it
yourself
return
soluOon
else
while
agenda
not
empty
do
take
node
from
front
of
agenda;
if
depth(node)
<
depth
limit
then
{
new
nodes
=
apply
operaOons
to
node;
add
new
nodes
to
front
of
agenda;
if
goal
state
in
new
nodes
then
return
soluOon;
}
IteraOve
Version
of
DLS
Wednesday
20
March
19
37
3.4.5
Itera#ve
deepening
depth-‐first
search
• Itera#ve
deepening
search
or
iteraOve
deepening
depth-‐first
search
gradually
increases
the
limit—first
0,
then
1,
then
2,
and
so
on—unOl
a
goal
is
found.
• So
is
the
combinaOon
of
depth
first
search
and
depth
limited
search.
Wednesday
20
March
19
38
3.4.5
Itera#ve
deepening
depth-‐first
search
Wednesday
20
March
19
39
3.4.6
Bidirec#onal
search
• Run
two
simultaneous
searches—one
forward
from
the
iniOal
state
and
the
other
backward
from
the
goal—hoping
that
the
two
searches
meet
in
the
middle
is
known
as
Bidirec#onal
Search.
• The
moOvaOon
is
that
bd/2
+
bd/2
is
much
less
than
bd.
• (Read
it
yourself)
Wednesday
20
March
19
40
Comparing
Uninformed
Search
Strategies
Wednesday
20
March
19
41
Lab
Assignment
No.2
• Implement
the
Romanian
Example
using
the
Depth
First
Search.
• For
reference
look
at
the
following
URL:
hqp://
centurion2.com/AIHomework/AI200/
ai200.php
Wednesday
20
March
19
42