lecture 8 search techniques

Post on 21-Mar-2016

55 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Lecture 8 Search Techniques. Faculty of Information Technology Multimedia University. 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. - PowerPoint PPT Presentation

TRANSCRIPT

TCP1211-Logic Programming

Lecture 8Lecture 8Search Search

TechniquesTechniquesFaculty of Information Technology

Multimedia University

TCP1211-Logic Programming

OutlineOutline State representation of real-world State representation of real-world

problems.problems. Solving a problem by traveling from the Solving a problem by traveling from the

initial state to the final state. initial state to the final state. Search strategies:Search strategies:

Depth First SearchDepth First Search Breadth First SearchBreadth First Search

TCP1211-Logic Programming

What is the problem???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 Understanding the problemproblem

In order to solve the problem, first we need to represent theProblem 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 - State representation - ExampleExampleweatherThe world under study

humidity temperaturewind 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…)Example (cont…)

For some people, a desirable weather is the sunny one.

Humidity = LowTemperature = MediumWind = MildRain = No

TCP1211-Logic Programming

Another exampleAnother 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, CNorthF, W

FSouth, WSouth

GNorth, CNorth

F, G

FSouth, GSouth,

WNorth, CNorth

F

GSouth,

FNorth, WNorth, CNorth

F, C

FSouth, CSouth,

WNorth,GNorth

Ferry Goat Ferry Cabbage

F,W

FSouth, WSouth,GSouth,

CNorth...

...

Ferry Wolf

TCP1211-Logic Programming

State Transition State Transition A state transition of the world under study is best depictedby a directed graph

Root (Initial State)TransitionChange of State

Node 2(state 2)

Node 3(state 3)

Node 1 (state 1)

Node 4 (state 4) ...

.

.

.

Final State (Desirable State)

Node X

TCP1211-Logic Programming

Where is the solution?Where is the solution?Root (Initial State)

.

.

.

Final State (Desirable State)

Node X

Answer Pathfrom Initial Stateto Desirable State

.

.

.

Node Y

Just a Path

TCP1211-Logic Programming

Search StrategiesSearch StrategiesTwo 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 SearchDepth First SearchI

A

D

B C

E G

K

M F

H

F

F

F

Level 0

Level 1

Level 2

Level 3

Level 4

Nodes Visited : I A D B E K M FAnswer Path : I B E K F

TCP1211-Logic Programming

Characteristics of DFSCharacteristics 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 Solving a problem means…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 Finding Answer Path using DFSusing 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-stateAnswer-Path = path and return this path

else if path can be extended to a New-state

save Current-statecontinue 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 algorithmExtend a path to a New-stateFind the Next-state of the Previous-state in the path that is not already in the paththe state found is the New-State

TCP1211-Logic Programming

I

A

D

B C

E G

K

M F

H

F

F

F

Path Effect

I Initial stateIA extend

If path has reached Final-stateAnswer-Path = path and return this path

else if path can be extended to a New-state

save Current-statecontinue to extend the path from the New-state

else backtrack and try to extend the path from the Previous-state

in a different direction

Find the Next-state of the Previous-state in the path that is not already in the paththe state found is the New-State

IAD extend

IAbacktrackIbacktrackIB extend

IBE extend

IBEK extend

IBEKM extend

IBEKbacktrackIBEKF extend

TCP1211-Logic Programming

Requirements for DFSRequirements 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

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

intial_state(i).final_state(f).

I

A

D

B C

E G

K

M F

H

F

F

F

TCP1211-Logic Programming

[n,n,n,n]

[s,s,n,n]

fail

[s,n,s,n]

[n,n,s,n]

[s,s,s,n]

[n,s,n,n][n,s,s,n]

fail[s,s,n,s]

[n,s,n,s][s,s,s,s]

...

...fail

TCP1211-Logic Programming

Initial StateFarmer: northWolf: northGoat: northCabbage: north

Final StateFarmer: southWolf: southGoat: southCabbage: south

(Transitions)next states

initial_state([n,n,n,n])final_state([s,s,s,s])

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 SearchBreadth First SearchI

A

D

B C

E G

K

M F

H

F

F

F

Level 0

Level 1

Level 2

Level 3

Level 4

Node Visited: I A B C D E G F

Answer Path: I C F

TCP1211-Logic Programming

However…However…Breadth-first search strategy is:

• Complicated (difficult to implement)

• Always give the shortest path

TCP1211-Logic Programming

SummarySummary You should know how to build the state You should know how to build the state

representation of a search problem. representation of a search problem. Two search strategies – DFS, BFS.Two search strategies – DFS, BFS. You should also know how to implement DFS You should also know how to implement DFS

in Prolog. in Prolog.

TCP1211-Logic Programming

Prolog Programming – Tip No. Prolog Programming – Tip No. 22

Arguments of a clause are not fixed for Arguments of a clause are not fixed for input or input.input or input.

Example: The predicate Example: The predicate conc(L1,L2,L3)conc(L1,L2,L3)..

conc([],L2,L2).conc([],L2,L2).conc([X|L1Tail],L2,[X|L3Tail]):-conc([X|L1Tail],L2,[X|L3Tail]):-

conc(L1Tail,L2,L3Tail).conc(L1Tail,L2,L3Tail).

Input/output

TCP1211-Logic Programming

Prolog Tip No. 2 (cont.)Prolog Tip No. 2 (cont.)

All of the following queries will work:All of the following queries will work:

?-conc([a,b],[c,d],L).?-conc([a,b],[c,d],L). ?-conc(L,[c,d],[a,b,c,d]).?-conc(L,[c,d],[a,b,c,d]). ?-conc(L1,L2,[a,b,c,d]).?-conc(L1,L2,[a,b,c,d]).

TCP1211-Logic Programming

Prolog Tip No. 2 (cont)Prolog Tip No. 2 (cont)

Consider this familiar example:Consider this familiar example:

factorial(0,1).factorial(0,1).factorial(M,N):- M1 is M – 1,factorial(M,N):- M1 is M – 1, factorial(M1,N1),factorial(M1,N1), N is M * N1.N is M * N1. %clue%clue

Will the following query work? Will the following query work? ?-factorial(5,N).?-factorial(5,N).

TCP1211-Logic Programming

Prolog Tip No. 2 (cont)Prolog Tip No. 2 (cont)

Will the following query work? Will the following query work? ?-factorial(M,120).?-factorial(M,120).

The clue is The clue is N is M * N1.N is M * N1.

top related