Artificial Intelligence - Prolog Programming - Ryo Hatano JAIST Oct 16, 2012.
George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem...
-
Upload
arturo-westmoreland -
Category
Documents
-
view
242 -
download
2
Transcript of George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem...
George F Luger
ARTIFICIAL INTELLIGENCE 5th editionStructures and Strategies for Complex Problem Solving
Programming in Logic (PROLOG)
Luger: Artificial Intelligence, 5th edition. © Pearson Education Limited, 2005
1
PROLOG
• What is True
• What needs to be Proved
Not
• How to do it like in other Programming languages
2
EXAMPLE 1Set of facts:
parent(ali,ahmad).
parent(ahmad,salem).
parent(ali,fatema).
parent(fatema,osama).
male(ali).
male(ahmad).
male(salem).
male(osama).
female(fatema).
5
EXAMPLE 1? parent(ali, ahmad).
By unifying this statement with the axioms (assertions) in order.
The above query (goal) also ends with a period
Ans:
yes
?parent(ali,X).
The above statement asks about all ali’s child
Semicolon asks for more answers
Ans:
X=ahmad; Note: by unifying X with ahmad
X=fatema;
no 7
EXAMPLE 1?parent(X,ahmad), male(X).
Comma “,” means and
Who is ahmad’s father
It first unifies the first part, i.e parent(X,ahmad).
It finds that X could take the value of ali.
It then attempts to prove male(ali)
Ans:
X=ali;
No
Backtracking on two levels to find other answers9
EXAMPLE 1Add the following rule (considered rule not fact):
father(X,Y):- parent(X,Y) , male(X).
father(X,Y) called head
parent(X,Y) , male(X) called body
?father(X,ahmad).
Ans:
X=ali;
No
?father(fatema,Y).
no
Because fatema is unified with X and there is no way to prove that male(fatema)
10
EXAMPLE 1sibling(X,Y):- parent(Z,X) , parent(Z,Y).
?sibling(X,Y)
X=ahmad,
Y=ahmad;
X=ahmad,
Y=fatema;
X=salem,
Y=salem;
X=fatema,
Y=ahmad;
X=fatema,
Y=fatema;
X=osama,
Y=osama;
no11
EXAMPLE 1sibling(X,Y):- parent(Z,X) , parent(Z,Y), X\
==Y.
?sibling(X,Y)
X=ahmad,
Y=fatema;
X=fatema,
Y=ahmad;
no12
EXAMPLE 1Define uncle relation, uncle(X,Y).
uncle(X,Y):-
parent(Z,Y),
parent(G,Z),
parent(G,X),
X\==Z. 13
HW: Define the following rules:• mother
• son
• daughter
• sister
• sibling
• grandfather(X,Y):-parent(X,Z),parent(Z,Y), male(X).
• grandmother
• cousin(X,Y)14
Built-in mathematical predicates• X=:=Y true when X equals Y
• X=\=Y true when X does not equal to Y
• X<Y true when X is less than Y
• X>Y true when X is greater than Y
• X=<Y true when X is less than or equal to Y
• X>=Y true when X is greater than or equal to Y
15
Built-in Mathematical predicates?6 = : = 4 +2.
yes
?8 = : = 4*2.
yes
?7= : = 4+6.
no
?6 =:= 6.
yes 16
Built-in logical predicates• X= =Y true when X can be unified with
Y
• X\= =Y true when X is not the same as Y
• X=Y attempts to unify X with Y
17
Built-in logical predicates?5 \= 4.
yes
?ahmad @<basem.
yes
? 'Ali' @< 'Basem'.
yes
? X=1+2.
X=1+2 20
Built-in logical predicates? X is 1+2.
X=3
? X is 4*2.
X=8
?X is 7//3.
X=2
? X is 5, X is 3+3.
no 21
Mathematical operations+ Addition
- Subtraction
* Multiplication
/ Real division
// Integer division
mod modulus
^ Exponent
22
Negation as failurehome(X):-not out(X).
out(ali).
?home(ali).
no
?home(X).
no
?home(zaki).
yes
?out(zaki).
no23
Recursion
A program that calls itself
Assume the following relations and attempt to find predecessor
parent(ali,ahmad).
parent(ahmad,fatema).
parent(fatema,osama).
25
Recursion• A non recursion version
predecessor(X,Y):- parent (X,Y).
predecessor(X,Y):- parent (X,Z), parent(Z,Y).
predecessor(X,Y):- parent (X,Z1), parent (Z1,Z2), parent(Z2,Y).
• The above program is applicable to a limited number of generations.
26
Recursion• Recursion version
predecessor(X,Y):- parent (X,Y).
predecessor(X,Y):- parent (X,Z), predecessor(Z,Y).
27
Recursion? predecessor(ali,ahmad).
yes
? predecessor(ali,fatema).
yes
? predecessor(ali,osama).
yes.
? predecessor(ali,X).
X=ahmad 28
RecursionFactorial
• fact(0,1).
• fact(N,X):-
N>0, N1 is N-1,
fact(N1,X1),
X is N*X1.
?fact(0,X).
X=1.
?fact(1,X).
X=130
List Processing• Empty lists are denoted as []
• [H | T] list of :– head H representing the first element.
– tail T represents the rest of the elements.
– [5, 2, 7, 10] H is 5, T is [2, 7, 10]
32
List Processing - membership• Test for a membership in a list
member(X,[X | T]).
member(X,[ _ | T]):-
member(X,T).
?member(5,[5, 7]).
yes
?member(5,[1, 5]).
yes 35
List Processing - membershipTo get all members of a list you can use:
?member(X,[a,b,c,d]).
X=a;
X=b;
X=c;
X=d;
no
36
List Processing - count• To count how many members in a list
count([],0).
count([X | T],C):-
count(T,C1),
C is C1+1.
?count([2,4,7],C).
C=3
37
List Processing - sum• To calculate the sum of the elements in a list
sum([],0).
sum([X | T],S):-
sum(T, S1),
S is S1+X.
?sum([2,4,7],S).
S=13
38
List Processing - append• To append two lists and produce a list containing the
elements of the first list then the elements of the second list
append([], L, L).
append([H | T1], L, [H | T]):
append(T1, L, T).
39
List Processing - append?append([r,t],[a,b,c],L).
L=[r,t,a,b,c].
?append(L1,L2,[a,b,c]).
L1=[]
L2=[a,b,c];
L1=[a]
L2=[b,c];
L1=[a,b]
L2=[c];
L1=[a,b,c]
L2=[];
no 40
List Processing - split• To divide a list into two parts positive and negative
positive([],[],[]).
positive([X|Y],[X|L1], L2) :-
X >= 0,
positive(Y,L1, L2) .
positive([X|Y],L1, [X|L2]) :-
positive(Y,L1, L2).
41
List Processing – write elements• To print the elements of a list
write_a_list([]).
write_a_list([H|T]):-
write(H), nl, write_a_list(T).
? write_a_list([5, 6, 7]).
5
6
7 42
Cut operator• To find a maximum of two numbers, you might
write:
max(X,Y,X):-X>=Y.
max(X,Y,Y).
?max(5,3,M).
M=5;
M=3;
no43
Cut operator• To find a maximum of two numbers, you might
write:
max(X,Y,X):-X>=Y,!.
max(X,Y,Y).
?max(5,3,M).
M=5;
no
44
Cut operator• Add an element to a list only if that element does
not exist already in the list
add(X, L, L):-
member(X,L), !.
add(X, L, [X|L]).
45
Self test list exercises1. Calculate the max/min value in a list.
? min_in_list([5,7,3,7,9], A).
A=3
? max_in_list([8,4,9,4],X).
X=9
46
Self test list exercises2. Calculate how many times a specific
number appears in a list.
3. Take a list as input and return its reverse in another list.
4. Take two lists as input and return their union, intersection, and difference in another list.
5. Delete one number from a list.
47
Homework1. Write a program to calculate the absolute
value of X, absval(X,Y) where X is the input and Y is the output.
2. Write a program to define the even(X) which is true only if X is even.
3. Write a program write_reverse to print the elements of a list in a reverse order
4. Write a program that simulates repeat assuming repeat does not exist.
48
PROLOG Self test examples
?concatenate([2, 3, 5], [7, 9, 5], B).
B=[2, 3, 5, 7, 9, 5]
?order([2, 8, 3, 5], B).
B=[2, 3, 5, 8]
49
The fail predicatecountry(jordan).
country(egypt).
country(england).
print_countries:-
country(X),
write(X),
nl,
fail.
print_countries.50
The repeat command
do:-
repeat, read(X), square(X).
do.
square(stop):-!.
square(X):-
Y is X*X, write(Y), nl, fail.
52
The repeat commanddo:-
read(X), square(X).
square(stop).
square(X):-
X\==stop,
Y is X*X,
write(Y), nl,
do.
54
Dynamic programs
• Programs that are able to modify themselves.
• Modification means the ability to add/ remove facts at runtime.
• Such procedures include:– assert(C)– retract(C)– abolish(C)
58
Dynamic programs - assert
• assert(C)
?parent(zaid, faris).
no
?assert(parent(zaid, faris)).
yes
?parent(zaid, faris).
yes59
Dynamic programs - assert
• asserta(C) adds the new statement C to the beginning of the program.
• assertz(C) adds the new statement C to the end of the program.
60
Dynamic programs - retract
• retract removes a statement from the program
?retract(parent(zaid, faris)).
yes
?parent(zaid, faris).
no
61
Dynamic programs - abolish
?abolish(parent,2).
yes
removes all statements defined as parent with arity 2.
62
Structuresemployee(Name,House_no,Street_name,City_name, Day,Month,Year).
employee(ali,5,salt_street,amman,12,10,1980).
?employee(ali,No,S,C,D,M,Y).
No=5
S=salt_street
C=amman
D=12
M=10
Y=1980
63
Structuresemployee(ali,address(5,salt_street,amman),date(12,10,1980)).
employee(hasan,address(12,university_street,zarqa),date(7,3,1985)).
employee(samer,address(9,madina_street,amman),date(2,9,1987)).
?employee(hasan,A,D).
A=address(12,university_street,zarqa)
D=date(7,3,1985)
?employee(Name,address(No,St,amman),_).
Name=ali
No=5
St=salt_street;
Name=samer
No=9
St=madina_street;
false
no
64
Structures?employee(Name,A,date(_,_,1985)).
Name=hasan
A=address(12,university_street,zarqa);
no
?employee(ali,A,date(Day, Month, Year)), 2011- Year>25.
A=address(5,salt_street,amman)
Day=12
Month=10
Year=1980;
no 65
Structures – Binary Trees
• Binary trees when empty are called nil.
• It may contain 3 components:– Left subtree
– Root
– Right subtree
• It can be represented using the following structure:bin_tree(Left_subtree, Root, Right_subtree).
67
Structures – Binary Trees
bin_tree(bin_tree(nil,2,nil),5, bin_tree(bin_tree(nil,7,nil),10, bin_tree(nil,9, bin_tree(nil,3,nil)))). 70
Example 3
2
5
10
7 9
3
Structures – Binary Trees - Count
count(nil,0).
count(bin_tree(Left,Root,Right),C):-
count(Left,C1),
count(Right,C2),
C is C1+C2+1.
71
Structures – Binary Trees – Count V2
count(nil,0).
count(bin_tree(Left,_,Right),C):-
count(Left,C1),
count(Right,C2),
C is C1+C2+1.
72
Structures – Binary Trees - Sum
sum(nil,0).
sum(bin_tree(Left,Root,Right),S):-
sum(Left,S1),
sum(Right,S2),
S is S1+S2+Root.
73
Structures – Binary Trees - depth
depth(nil,0).
depth(bin_tree(Left,Root,Right),D):-
depth(Left,D1),
depth(Right,D2),
D1>=D2,
D is D1+1.
depth(bin_tree(Left,Root,Right),D):-
depth(Left,D1),
depth(Right,D2),
D2>D1,
D is D2+1. 74
Structures – Binary Trees – depth V2
depth(nil,0).
depth(bin_tree(Left,Root,Right),D):-
depth(Left,D1),
depth(Right,D2),
max(D1,D2,M),
D is M+1.
max(X,Y,X):- X>=Y,!.
max(X,Y,Y):- X<Y.
75
Binary Tree - Hw• Write a PROLOG program max_tree to find
the maximum value in a binary tree (bin_tree) assuming all values are numeric.
• Write a PROLOG program to add a new value to an ordered binary tree.
• Write a PROLOG program to display the values stored in an ordered binary tree in ascending order.
76