AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters...

49
T H E U N I V E R S I T Y O F E D I N B U R G H AI Present and Future Alan Smaill University of Edinburgh, School of Informatics [email protected] 24/01/19 Alan Smaill AI Present and Future 24/01/19 1/38

Transcript of AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters...

Page 1: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

AI Present and Future

Alan Smaill

University of Edinburgh, School of Informatics

[email protected]

24/01/19

Alan Smaill AI Present and Future 24/01/19 1/38

Page 2: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Admin

Drop-in Lab sessions 9–11 on Mondays start next week.

Problem set emailed, and on course page.

You should work through the material yourself;Learn Prolog Now will help here.

The lab session next week gives you a chance to get feedbackand let you discuss any problems you may have.

Alan Smaill AI Present and Future 24/01/19 2/38

Page 3: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Today

See Learn Prolog Now, chapters 5,6,10.

Logic Programming: ctd

Dealing with negation

Computational features outside pure Prolog

Searching graphs

Search strategies:(depth-first, iterative deepending, breadth first)

Alan Smaill AI Present and Future 24/01/19 3/38

Page 4: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Note

Several predicates seen so far or today are built-in in SWIProlog, maybe with different names.

append/3

member/2

length/2

It is good to know how to define them from scratch, ifnecessary.

LPN “predicate index” lists all the built-ins you are likely toneed, and more . . .

Alan Smaill AI Present and Future 24/01/19 4/38

Page 5: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Nonlogical features

So far we have worked mostly in pure Prolog.This provides:

solid logical basis

elegant solution to symbolic problems

But various practical things become inconvenient:arithmetic and I/O.

And standard proof search is not always efficient.

Can we control proof search better?

Alan Smaill AI Present and Future 24/01/19 5/38

Page 6: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Nonlogical features

So far we have worked mostly in pure Prolog.This provides:

solid logical basis

elegant solution to symbolic problems

But various practical things become inconvenient:arithmetic and I/O.

And standard proof search is not always efficient.

Can we control proof search better?

Alan Smaill AI Present and Future 24/01/19 5/38

Page 7: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Expression evaluation

Prolog has built-in syntax for arithmetic expressions;

But it is uninterpreted – simply syntax.

?- 2 + 2 = 4.

no.

?- X = 2+2.

X=2+2

?- display(2+2).

+(2,2)

Alan Smaill AI Present and Future 24/01/19 6/38

Page 8: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

evaluation predicate “is”

?- X is 2+2.

X=4.

?- X is 6*7.

X=42.

?- X is 2+Y.

! Instantiation error in argument 2 of is/2

! goal: _107 is 2+_111

Alan Smaill AI Present and Future 24/01/19 7/38

Page 9: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Machine Arithmetic with “is”

Addition (+), subtraction(-)

X is 2+(3-1).

X=4

multiplication (*), division(/), mod:

?- X is 42 mod 5, Y is 42 / 5.

X = 2,

Y = 8.4

Alan Smaill AI Present and Future 24/01/19 8/38

Page 10: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Warning

WARNING

Unlike “=”, “is” is not symmetric.

needs the mode (?,+)

so requires RHS to be ground (no variables):

?- 2+(3-1) is X.

! Instantiation error i...

Further, the RHS must be an arithmetic expression:

?- X is foo(z).

! Domain error ...

Alan Smaill AI Present and Future 24/01/19 9/38

Page 11: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Warning

WARNING

Unlike “=”, “is” is not symmetric.

needs the mode (?,+)

so requires RHS to be ground (no variables):

?- 2+(3-1) is X.

! Instantiation error i...

Further, the RHS must be an arithmetic expression:

?- X is foo(z).

! Domain error ...

Alan Smaill AI Present and Future 24/01/19 9/38

Page 12: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Lists and arithmetic

length of a list:possible definition:

len([], 0).

len([_|L], N) :- len(L, M), N is M+1.

Only works in mode (+,?)

The built-in (in sicstus) length/2 works in both directions.

Alan Smaill AI Present and Future 24/01/19 10/38

Page 13: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Arithmetic comparisons

There are several binary relations built-in as goals, written infix:

less than (<), greater than (>)

less or equal (=<), greater or equal (>=)

arithmetic equality (=:=), inequality (=/=)

All of these have mode (+,+):both arguments must be ground.

Alan Smaill AI Present and Future 24/01/19 11/38

Page 14: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Collecting declarative solutions

There are built-in procedures to do this:

findall/3 builds a list of solutions:

findall( ?Pattern, ?Query, -ListOfSolutions)

%% Given infomation about the Simpsons:

?- findall(Y, ancestor(Y, bart), L).

L = [homer,marge,abe,jacqueline]

?- findall((X,Y), ancestor(X,Y), L).

L = [(abe,homer), (homer,bart), (homer,lisa) | ...]

Alan Smaill AI Present and Future 24/01/19 12/38

Page 15: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Collecting declarative solutions

There are built-in procedures to do this:

findall/3 builds a list of solutions:

findall( ?Pattern, ?Query, -ListOfSolutions)

%% Given infomation about the Simpsons:

?- findall(Y, ancestor(Y, bart), L).

L = [homer,marge,abe,jacqueline]

?- findall((X,Y), ancestor(X,Y), L).

L = [(abe,homer), (homer,bart), (homer,lisa) | ...]

Alan Smaill AI Present and Future 24/01/19 12/38

Page 16: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Basic Input/Output

read(?X) reads in a term, by default from standard input;– the term must be followed by a “.”

write(+X) prints out its argument as a term;– if X is not ground, variable names are not preserved.

nl/0 prints a newline.

Expression calculator, taking input from terminal:note non-terminating loop!

calc :- read(X),

Y is X,

write(X = Y), nl,

calc.

Alan Smaill AI Present and Future 24/01/19 13/38

Page 17: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Basic Input/Output

read(?X) reads in a term, by default from standard input;– the term must be followed by a “.”

write(+X) prints out its argument as a term;– if X is not ground, variable names are not preserved.

nl/0 prints a newline.

Expression calculator, taking input from terminal:note non-terminating loop!

calc :- read(X),

Y is X,

write(X = Y), nl,

calc.

Alan Smaill AI Present and Future 24/01/19 13/38

Page 18: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Cut

Sometimes, we have reason to believe we have reached theright / only possible answer

so no back-tracking is needed

in Pure Prolog, we cannot take advantage of this

Introduce a special “cut” predicate to allow this to beexpressed.

Cut just written by exclamation mark: !

Alan Smaill AI Present and Future 24/01/19 14/38

Page 19: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Cut

Sometimes, we have reason to believe we have reached theright / only possible answer

so no back-tracking is needed

in Pure Prolog, we cannot take advantage of this

Introduce a special “cut” predicate to allow this to beexpressed.

Cut just written by exclamation mark: !

Alan Smaill AI Present and Future 24/01/19 14/38

Page 20: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example

The “member of a list” predicate:

member(X, [X|_]).

member(X, [_|L]) :- member(X, L).

If this is used in mode (+,+), and X is found in the input list,there is no point in backtracking and looking for other solutions.

Alan Smaill AI Present and Future 24/01/19 15/38

Page 21: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example

The “member of a list” predicate:

member(X, [X|_]).

member(X, [_|L]) :- member(X, L).

If this is used in mode (+,+), and X is found in the input list,there is no point in backtracking and looking for other solutions.

Alan Smaill AI Present and Future 24/01/19 15/38

Page 22: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example

So, insert a cut in the first clause:

member(X, [X|_]) :- !.

member(X, [_|L]) :- member(X, L).

When a goal that matches member(X,Y) is called, if the firstclause succeeds, the second will not be used on backtracking.

Alan Smaill AI Present and Future 24/01/19 16/38

Page 23: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Safe use of cut

Cut can make programs more efficient

by avoiding pointless backtracking

But cuts can change the meaning of the program (not justefficiency).

Use with care!

Alan Smaill AI Present and Future 24/01/19 17/38

Page 24: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Negation as Failure

We can use cut to define negation as failure

not(G) :- G, !, fail; true.

This tries to solve G:

if successful, fail;otherwise succeed.

Alan Smaill AI Present and Future 24/01/19 18/38

Page 25: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Negation as Failure

We can use cut to define negation as failure

not(G) :- G, !, fail; true.

This tries to solve G:

if successful, fail;otherwise succeed.

Alan Smaill AI Present and Future 24/01/19 18/38

Page 26: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

How it works

not(member(5,[2,1]))

member(5,[2,1]),!,fail

member(5,[1]),!,fail

member(5,[]),!,fail

true

Alan Smaill AI Present and Future 24/01/19 19/38

Page 27: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

How it works

not(member(1,[2,1]))

member(1,[2,1]),!,fail

member(1,[1]),!,fail

!,fail

fail

true

Alan Smaill AI Present and Future 24/01/19 20/38

Page 28: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Negation as Failure

Built-in syntax: \+ G

Example: people who are not teachers:

q(X) :- person(X),

\+ teach(X,Y).

Warning:

We can read \+ G as the logical “not G” only if G is groundwhen we start solving it.

HEURISTIC: delay negation after other goals to allow negatedgoals to become ground.

Alan Smaill AI Present and Future 24/01/19 21/38

Page 29: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Negation as Failure

Built-in syntax: \+ G

Example: people who are not teachers:

q(X) :- person(X),

\+ teach(X,Y).

Warning:

We can read \+ G as the logical “not G” only if G is groundwhen we start solving it.

HEURISTIC: delay negation after other goals to allow negatedgoals to become ground.

Alan Smaill AI Present and Future 24/01/19 21/38

Page 30: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Searching graphs

find(X,X).

find(X,Z) :- edge(X, Y),

find(Y, Z)

find(X,Y) attempts to determine whether node Y can be reachedfrom node X following edges in the graph. This will backtrack overdifferent edges.

Problem: this loops easily if the graph is cyclic.

edge(a,b).

edge(b,c).

edge(c,a).

Alan Smaill AI Present and Future 24/01/19 22/38

Page 31: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Searching graphs

find(X,X).

find(X,Z) :- edge(X, Y),

find(Y, Z)

find(X,Y) attempts to determine whether node Y can be reachedfrom node X following edges in the graph. This will backtrack overdifferent edges.

Problem: this loops easily if the graph is cyclic.

edge(a,b).

edge(b,c).

edge(c,a).

Alan Smaill AI Present and Future 24/01/19 22/38

Page 32: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Searching graphs ctd

To avoid looping:

Remember where we have been.

Stop if we try to visit a node we have already seen.

find2(X,X,_).

find2(X,Z,P) :- \+ member(X,P),

edge(X,Y),

find2(Y,Z,[X|P]).

NB, this requires mode (+,?,+).Call initially with empty list:

?- find2(p,q,[]).

Alan Smaill AI Present and Future 24/01/19 23/38

Page 33: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Search Spaces

Given by:

Set of states s1, s2, . . .

Goal predicate goal(X )

Step predicate s(X ,Y ) that says we can go from state X tostate Y

A start state (or states)

A solution is a path leading a start state to a state Gsatisfying goal(G ).

Alan Smaill AI Present and Future 24/01/19 24/38

Page 34: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Search Spaces

Given by:

Set of states s1, s2, . . .

Goal predicate goal(X )

Step predicate s(X ,Y ) that says we can go from state X tostate Y

A start state (or states)

A solution is a path leading a start state to a state Gsatisfying goal(G ).

Alan Smaill AI Present and Future 24/01/19 24/38

Page 35: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example: Blocks world

Take configuration of blocks as a list of three towers, each towerbeing a list of blocks in a tower from top to bottom.

A

B

C

[[c,b,a],[],[]]

Alan Smaill AI Present and Future 24/01/19 25/38

Page 36: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example: Blocks world

Move a block from top of a tower to top of another tower:

A

B

C

[[b,a],[],[c]]

Alan Smaill AI Present and Future 24/01/19 26/38

Page 37: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example: Blocks world

Next move:

A B C

[[a],[b],[c]]

Alan Smaill AI Present and Future 24/01/19 27/38

Page 38: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Example: Blocks world

Then —

A

B C

[[],[a,b],[c]]

Alan Smaill AI Present and Future 24/01/19 28/38

Page 39: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Prolog representation

State is a list of stacks of blocks:

[[a,b,c],[],[]]

Transitions move a block from the top of one stack to the topof another:

s([[A|As],Bs,Cs], [As,[A|Bs],Cs]).

s([[A|As],Bs,Cs], [As,Bs,[A|Cs]]).

...

Can specify particular goal position:

goal([[],[],[a,b,c]]).

Alan Smaill AI Present and Future 24/01/19 29/38

Page 40: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

An abstract problem space

s(a,b).

s(b,c).

s(c,a).

s(c,f(d)).

s(f(N),f(g(N))).

s(f(g(X)),X).

goal(d).

Think of the graph generated by thesedeclarations.

In this case:

the graph is infinite

there is a loop near the top of the graph

Alan Smaill AI Present and Future 24/01/19 30/38

Page 41: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

abstract space ctd

a

b

c

f(d)

f(g(d))

f(g(g(d)))

... g(d)

d

Alan Smaill AI Present and Future 24/01/19 31/38

Page 42: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

problem 1: cycles

We can already see in the blocks world example and in the abstractsearch space that it is easy to follow actions around in cycles, andnot find the goal, even if there is a path to the goal.

There are two main approaches to deal with this:

remember where you’ve been (as seen before) OR . . .

work with depth bound

Alan Smaill AI Present and Future 24/01/19 32/38

Page 43: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

problem 1: cycles

We can already see in the blocks world example and in the abstractsearch space that it is easy to follow actions around in cycles, andnot find the goal, even if there is a path to the goal.

There are two main approaches to deal with this:

remember where you’ve been (as seen before) OR . . .

work with depth bound

Alan Smaill AI Present and Future 24/01/19 32/38

Page 44: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Problem 2: Infinite State Space

Compare the graph from the abstract search space.Depth First Search has similar problems to Prolog proof search:

We may miss solutions because state space is infinite;

Even if state space is finite, may wind up finding “easy”solution only after a long exploration of pointless part ofsearch space

Alan Smaill AI Present and Future 24/01/19 33/38

Page 45: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Solution 2: depth bounding

Keep track of depth, stop if bound exceeded

Note: does not avoid loops (can do this too)

dfs_bound(_,Node,[Node]) :-

goal(Node).

dfs_bound(N,Node,[Node|Path]) :-

N > 0,

s(Node,Node1),

M is N-1,

dfs_bound(M,Node1,Path)

Alan Smaill AI Present and Future 24/01/19 34/38

Page 46: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Problem 3: what is a good bound?

In general, we just don’t know in advance:

Too low? –Might miss solutionsToo high? – Might spend a long time searching pointlessly

Alan Smaill AI Present and Future 24/01/19 35/38

Page 47: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Solution 3: iterative deepening

Use the following with some small start value for N

dfs_id(N,Node,Path) :-

dfs_bound(N,Node,Path)

;

M is N+1,

dfs_id(M,Node,Path).

NB: if there is no solution, this will not terminate.

Alan Smaill AI Present and Future 24/01/19 36/38

Page 48: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

Breadth first search

Keep track of all possible solutions, try shortest ones first;do this by maintaining a “queue” of solutions

bfs([[Node|Path]|_], [Node|Path]) :-

goal(Node).

bfs([Path|Paths], S) :-

extend(Path,NewPaths),

append(Paths,NewPaths,Paths1),

bfs(Paths1,S).

bfs_start(N,P) :- bfs([[N]],P).

Alan Smaill AI Present and Future 24/01/19 37/38

Page 49: AI Present and Futurehomepages.inf.ed.ac.uk/smaill/prolog2.pdf · See Learn Prolog Now, chapters 5,6,10. Logic Programming: ctd Dealing with negation Computational features outside

TH

E

U N I V E RS

IT

Y

OF

ED I N B U

RG

H

extending paths

extend([Node|Path],NewPaths) :-

findall([NewNode,Node|Path],

s(Node,NewNode),

NewPaths),

!.

%% if there are no next steps,

%% bagof will fail and we’ll fall through.

extend(_Path,[]).

Alan Smaill AI Present and Future 24/01/19 38/38