Rule Based Systems (Prolog)

30
CPSC 433 Artificial Intelligence Rule Based Systems (Prolog) M. Reza Zakerinasab [email protected] Please include [CPSC433] in the subject line of any emails regarding this course. Slides originally created by Andrew M Kuipers. Some slides adopted from 600.325/425 Declarative Methods - J. Eisner

description

Rule Based Systems (Prolog). M. Reza Zakerinasab [email protected] Please include [CPSC433] in the subject line of any emails regarding this course. Slides originally created by Andrew M Kuipers. Some slides adopted from 600.325/425 Declarative Methods - J. Eisner. - PowerPoint PPT Presentation

Transcript of Rule Based Systems (Prolog)

Page 1: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Rule Based Systems(Prolog)

M. Reza [email protected]

Please include [CPSC433] in the subject line of any emails regarding this course.Slides originally created by Andrew M Kuipers.Some slides adopted from 600.325/425 Declarative Methods - J. Eisner

Page 2: Rule Based Systems (Prolog)

2

Prolog as constraint programming

• An ordinary constraint between two variables: Person and Food• Prolog makes you name this constraint.

Here’s a program that defines it:– eats(sam, dal). eats(josie, samosas).– eats(sam, curry). eats(josie, curry).– eats(rajiv, dal). …

• Now it acts like a subroutine! At the Prolog prompt you can type – eats(Person1, Food1). % constraint over two variables– eats(Person2, Food2). % constraint over two other variables

(Person, Food)Person Food

sam dal

sam curry

josie samosas

josie curry

rajiv dal

CPSC 433 Artificial Intelligence

Page 3: Rule Based Systems (Prolog)

3

Using Prolog

– eats(sam, dal). eats(josie, samosas).– eats(sam, curry). eats(josie, curry).– eats(rajiv, burgers). eats(rajiv, dal). …

– eats(Person1, Food), eats(Person2, Food).

• Person1=sam, Person2=josie, Food=curry• Person1=josie, Person2=sam, Food=curry …

Your program file (compiled)Sometimes called the “database”

“Query” that you type interactively

Prolog’s answer

CPSC 433 Artificial Intelligence

Page 4: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Adding more rules…

eats(sam, dal). eats(josie, samosas).eats(sam, curry). eats(josie, curry).eats(rajiv, burgers). eats(rajiv, dal).

compatible(Person1, Person2) :- eats(Person1, Food), eats(Person2, Food).

compatible(Person1, Person2) :- watches(Person1, Movie),watches(Person2, Movie).

compatible(hal, Person2) :- female(Person2), rich(Person2).

Page 5: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Basic Prolog Process

Negate query, set as current goal.

while current goal is non-empty:choose the leftmost subgoalif a rule applies to the subgoal:

select the first applicable rule (top-to-bottom)perform resolution on subgoal and selected rule

else:backtrack if possible, otherwise fail

success

Page 6: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

Logically sound?!!

?- witch(X).

Goals

Page 7: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

Page 8: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

Page 9: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)

Page 10: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)

Page 11: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0) madeofwood(_G0)

Page 12: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0) madeofwood(_G0)

Page 13: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)

Page 14: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)

Page 15: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(duck)

Page 16: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(duck)

Page 17: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(duck) woman(duck)

Page 18: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(duck) woman(duck)

fail! backtrack...

Page 19: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)

Page 20: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)floats(_G1) sameweight(_G1, _G0)

Page 21: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)floats(_G1) sameweight(_G1, _G0)

Page 22: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)floats(duck) sameweight(duck, _G0)

Page 23: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)floats(duck) sameweight(duck, _G0)

Page 24: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(_G0)floats(duck) sameweight(duck, girl)

Page 25: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(_G0)

floats(girl)

Page 26: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(_G0) woman(_G0)madeofwood(girl)

Page 27: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(girl) woman(girl)

Page 28: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(_G0)

burns(girl) woman(girl)

works this time

Page 29: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

Goalswitch(girl)

Page 30: Rule Based Systems (Prolog)

CPSC 433 Artificial Intelligence

Sir Bedevere’s Infamous Deductionwitch(X) :- burns(X),woman(X).woman(girl).burns(X) :- madeofwood(X).madeofwood(X) :- floats(X).floats(duck).floats(Y) :- floats(X),sameweight(X,Y).sameweight(duck,girl).

?- witch(X).

X = girl