Lecture 8 Search Techniques

Download Report

Transcript Lecture 8 Search Techniques

Lecture 8
Search Techniques
Faculty of Information Technology
Multimedia University
TCP1211-Logic Programming
Outline



State representation of real-world problems.
Solving a problem by traveling from the
initial state to the final state.
Search strategies:
Depth First Search
 Breadth First Search

TCP1211-Logic Programming
What is the problem???
Problem is a part of a world in a “bad state” (or unwanted state)
Solving a Problem is taking that glimpse of the world and “transit”
it to a “better state” (or desirable state)
Solving a problem is:
•
Represent (all possible) states of the world under study
•
Find a procedure (a way) to change the states
TCP1211-Logic Programming
Understanding the problem
In order to solve the problem, first we need to represent the
Problem  State representation.
A world under study (the problem) consists of some relevant entities
Each entity has attribute(s)
The values of the attributes of all entities denote the current state
of the world under study.
TCP1211-Logic Programming
State representation - Example
The world under study
humidity
weather
temperature
wind
rain
entities
The state of the weather is the states of the following entities:
•
•
Humidity (High/Medium/Low)
Temperature (High/Medium/Low)
•
Wind (Strong/Medium/Mild)
•
Rain (No/Drizzling/Heavy)
TCP1211-Logic Programming
Example (cont…)
For some people, a desirable weather is the sunny one.
Humidity = Low
Temperature = Medium
Wind = Mild
Rain = No
TCP1211-Logic Programming
Another example
A farmer must ferry a wolf, a goat and a
cabbage from the north bank of a river to the
south bank, using a boat that is too small to
take more than one of the three across at
once. If he leaves the wolf and the goat
together, the wolf will eat the goat, and if he
leaves the goat with the cabbage, the goat
will eat the cabbage. How can he get all
three across the river safely
TCP1211-Logic Programming
FNorth, WNorth, GNorth, CNorth
F, W
Ferry Wolf
F, C
F, G
Ferry Goat
FSouth, WSouth
Ferry Cabbage
FSouth, GSouth,
WNorth, CNorth
GNorth, CNorth
F
WNorth,GNorth
GSouth,
F,W
FNorth, WNorth, CNorth
FSouth, WSouth,GSouth,
...
FSouth, CSouth,
...
CNorth
TCP1211-Logic Programming
State Transition
A state transition of the world under study is best depicted
by a directed graph
Transition
Change of State
Node 2
(state 2)
Node 1
(state 1)
Root (Initial State)
Node 4
(state 4)
...
Node 3
(state 3)
.
.
.
Node X
Final State
(Desirable State)
TCP1211-Logic Programming
Where is the solution?
Root (Initial State)
Just
a Path
.
.
.
.
.
.
Answer Path
from Initial State
to Desirable State
Node X
Node Y
Final State
(Desirable State)
TCP1211-Logic Programming
Search Strategies
Two Basic Search Strategies:
•
Depth-first search
•
Breadth-first search
Depth-first search goes to the left as far as possible before branching
to the next right branch
Breadth-first search visits all the nodes of one level before moving
to the next level
TCP1211-Logic Programming
Depth First Search
Level 0
I
Level 1
Level 2
A
D
Level 3
Level 4
B
E
K
M
C
G
F
F
F
Nodes Visited : I A D B E K M F
Answer Path : I B E K F
TCP1211-Logic Programming
H
F
Characteristics of DFS
•
simple to implement
•
answer path might be longer than necessary
•
may get stuck in an infinite branch while the answer is just
in the next branch
TCP1211-Logic Programming
Solving a problem means…
Given a system that represents a real world,
the purpose of the search is to find a sequence of state transitions
that brings the system from the initial state to the desired (final) state
Generally:
•
The initial state is well known and UNIQUE
•
The final (desired) state is unknown
(but recognizable once it occurs)
•
May have many final (desired) states
TCP1211-Logic Programming
Finding Answer Path using DFS
1.
Start with a singleton initial path consisting of the initial state
2.
Extend this path, step by step, until it reaches a state
which can be recognized as a final state.
TCP1211-Logic Programming
If path has reached Final-state
Answer-Path = path and return this path
else
if path can be extended to a New-state
save Current-state
continue to extend the path from the New-state
else
backtrack and try to extend the path from the Previous-state
in a different direction
Extend algorithm
Extend a path to a New-state
Find the Next-state of the Previous-state in the path
that is not already in the path
the state found is the New-State
TCP1211-Logic Programming
Path
Effect
I
Initial state
IA
extend
IAD
extend
IA
backtrack
I
backtrack
IB
extend
IBE
extend
I
A
D
E
K
M
IBEK
B
C
G
F
F
H
F
F
extend
If path has reached Final-state
Answer-Path = path and return this path
else
IBEK
backtrack
if path can be extended to a New-state
save Current-state
IBEKF
extend
continue to extend the path from the New-state
else
Find the Next-state of the Previous-state in the path
backtrack and try to extend the path from the Previous-state
that is not already in the path
in a different direction
the state found is the New-State
IBEKM
extend
TCP1211-Logic Programming
Requirements for DFS
To solve problems using the DFS strategy, we must first
•
Define the initial state
•
Define the final state (or how it is recognized)
•
Define the next state (transitions)
TCP1211-Logic Programming
depth_first_search(AnsPath) :initial_state(Init),
depth_first([Init],AnsPath).
depth_first([S|Path],[S]) :final_state(S),!.
depth_first([S|Path],[S|AnsPath]) :extend([S|Path],S1),
depth_first([S1,S|Path],AnsPath).
extend([S|Path],S1) :next_state(S,S1),
not(member_state(S1,[S|Path])).
member_state(X,[X|_]).
member_state(X,[_|T]) :- member_state(X,T).
TCP1211-Logic Programming
intial_state(i).
final_state(f).
I
next_state(i,a).
next_state(i,b).
next_state(i,c).
next_state(a,d).
next_state(b,e).
next_state(b,g).
next_state(e,k).
next_state(k,m).
next_state(k,f).
...
A
D
B
E
K
M
C
G
F
F
F
TCP1211-Logic Programming
H
F
[n,n,n,n]
[s,s,n,n]
[s,n,s,n]
...
fail
[n,n,s,n]
fail
[s,s,s,n]
[n,s,s,n]
fail
...
[n,s,n,n]
[s,s,n,s]
[n,s,n,s]
[s,s,s,s]
TCP1211-Logic Programming
Final State
Farmer: south
Wolf: south
Goat: south
Cabbage: south
Initial State
Farmer: north
Wolf: north
Goat: north
Cabbage: north
initial_state([n,n,n,n])
final_state([s,s,s,s])
(Transitions)
next states
next_state(S, S1) :- move(S, S1), safe(S1).
safe([F,W,G,C]) :- F=G,!.
%Farmer with Goat
%OR
safe([F,W,G,C]) :- F=W, F=C. %Farmer with Wolf and with Cabbage
TCP1211-Logic Programming
move([F,W,G,C],[F1,W,G,C]) :- cross(F, F1).
move([F,W,G,C],[F1,F1,G,C]) :- cross(F, F1).
move([F,W,G,C],[F1,W,F1,C]) :- cross(F, F1).
move([F,W,G,C],[F1,W,G,F1]) :- cross(F, F1)..
cross(n, s).
cross(s,n).
TCP1211-Logic Programming
Breadth First Search
Level 0
I
Level 1
Level 2
A
D
Level 3
Level 4
B
E
C
G
K
F
F
M
F
Node Visited: I A
B C
D
E
Answer Path: I C F
TCP1211-Logic Programming
H
F
G F
However…
Breadth-first search strategy is:
•
Complicated (difficult to implement)
•
Always give the shortest path
TCP1211-Logic Programming
Summary



You should know how to build the state
representation of a search problem.
Two search strategies – DFS, BFS.
You should also know how to implement DFS
in Prolog.
TCP1211-Logic Programming
Prolog Programming – Tip No. 2


Arguments of a clause are not fixed for input or input.
Example: The predicate conc(L1,L2,L3).
Input/output
conc([],L2,L2).
conc([X|L1Tail],L2,[X|L3Tail]):conc(L1Tail,L2,L3Tail).
TCP1211-Logic Programming
Prolog Tip No. 2 (cont.)

All of the following queries will work:



?-conc([a,b],[c,d],L).
?-conc(L,[c,d],[a,b,c,d]).
?-conc(L1,L2,[a,b,c,d]).
TCP1211-Logic Programming
Prolog Tip No. 2 (cont)

Consider this familiar example:
factorial(0,1).
factorial(M,N):- M1 is M – 1,
factorial(M1,N1),
N is M * N1.
%clue

Will the following query work?
?-factorial(5,N).
TCP1211-Logic Programming
Prolog Tip No. 2 (cont)

Will the following query work?
?-factorial(M,120).

The clue is  N is M * N1.
TCP1211-Logic Programming