Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Introduction To Artificial Intelligence Course

sixth Lecture - logical Knowledge representation

Eng. Manal Farhoudah , Eng. Loujain AboKaff

Session Outlines
Introduction.
Variables and expressions.
Facts and Clauses.
Logical Functions.
Exercise.

Introduction
pyDatalog is a powerful logic language with very few syntactic elements, mostly coming from Python.

pyDatalog statements are declarative : they describe the result we want, leaving to the computer the task of
finding the appropriate solutions.

Let's Start with inatall pyDatalog :

In [1]:
!pip install pyDatalog

Requirement already satisfied: pyDatalog in c:\users\touhama\anaconda3\lib\site-packages


(0.17.4)

Now, we can import it:

In [2]:
from pyDatalog import pyDatalog as py

Variables and expressions


The next step is to declare the variables we'll use:

In [3]:
py.create_terms('X,Y')

Variables appear in logic queries, which return a printable result

In [4]:
# give me all the X and Y so that X is True and Y is False
print((X==True) & (Y==False))

X | Y
-----|------
True | False
True | False

Facts and Clauses


The Datalog engine store facts and clauses ((Rules)), The first step is to define facts and clauses names using
create_terms function

In [5]:
py.create_terms('parent,ancestor,brother, X,Y,Z')

In [6]:

+parent('bill','john adams')

The next step is to insrts facts: The following statement inserts the fact that John Adams is the parent of Bill :

parent(son,father)

In [7]:
print(parent('bill',X))

X
----------
john adams

Logical clauses - Rules


Logic clauses make the engine smarter.
Note that <= means "if" (not the arithmetic comparison), and that the "or" is implemented by 2 clauses.

The following rule explain when X is a brother of Y : if Z is the parent of X,and Z is the parent of a person Y, and X
is not Y.

In [8]:
brother(X,Y) <= (parent(X,Z)) & (parent(Y,Z)) & (X!=Y)
Out[8]:
brother(X,Y) <= parent(X,Z)&parent(Y,Z)&!=(X,Y)

In [9]:
print(brother('bill',Y))

[]

In [10]:
+ parent('Mark','john adams')

In [11]:
print(brother('bill',Y))

Y
----
Mark

The following predicates explain recursively when Y is an ancestor of X : either Y is the parent of X, or Y is the
parent of a person Z who is an ancestor of X.
In [12]:
ancestor(X,Y) <= parent(X,Y)
ancestor(X,Y) <= parent(Z,Y) & ancestor(X,Y)

Out[12]:
ancestor(X,Y) <= parent(Z,Y)&ancestor(X,Y)

In [13]:
print(ancestor('bill', X))

X
----------
john adams

In [14]:
+ parent('john adams','George')

In [15]:
print(ancestor('bill', X))

X
----------
john adams

Exercise

The following are the marks scored by 5 students:

ahmad 90
sami 45
khaled 60
carol 70
mohamad 80

Enter the above data using pydatalog and write a quaries for the following:

1. print student name and mark of all student.


2. who has scored 80 marks?
3. what mark has been scored by sami?
4. write a rule for finding grade letters for marks 'grades' and use the rule to find the grade letter of a given
mark.
5. use the previous rule to print all student who failed

Define Terms

In [16]:
py.create_terms('X,Y,marks,grades')

Insert Facts

In [17]:
+marks('ahmad',90)
+marks('sami',45)
+marks('khaled',85)
+marks('carol',70)
+marks('mohammad',80)
Queries

In [18]:
print(marks(X,Y))

X | Y
---------|---
mohammad | 80
carol | 70
khaled | 85
sami | 45
ahmad | 90

In [19]:
print(marks(X,80))

X
--------
mohammad

In [20]:
print(marks('sami',X))

X
--
45

Rules

In [21]:
grades(X,'A') <= marks(X,Y) & (Y>= 90) & (Y<= 100)
grades(X,'B') <= marks(X,Y) & (Y>= 80) & (Y< 90)
grades(X,'C') <= marks(X,Y) & (Y>= 70) & (Y< 80)
grades(X,'D') <= marks(X,Y) & (Y>= 60) & (Y< 70)
grades(X,'F') <= marks(X,Y) & (Y< 60)
Out[21]:
grades(X,'F') <= marks(X,Y)&<(Y,'60')

In [22]:
print(grades(X,Y))

X | Y
---------|--
sami | F
carol | C
khaled | B
mohammad | B
ahmad | A

In [23]:
print(grades(X,'F'))

X
----
sami

Logical Function
A function defines one value for a given argument. It is similar to a python dictionary.

A function can be queried.


A function can be queried.

In [25]:
py.create_terms('manager,count_of_direct_reports,X,Y,Z,FirstStd')

In [27]:
# the manager of bill is George ann. Bill has only one manager.
+ (manager['bill']=='George ann')

In [29]:
print(manager['bill']==X) # X is 'John Adams'

X
----------
George ann

The following example apply Factorial Function by datalog:

In [33]:
py.create_terms("N,Factorial")

Factorial[1]=1
Factorial[N]=N*Factorial[N-1]

In [34]:
from pyDatalog import pyDatalog as py
py.create_terms('Factorial,N')
Factorial[N]=N*Factorial[N-1]
print(Factorial[3]==N)

N
-
6

In [37]:
print(Factorial[5]==N)

N
---
120

Aggregate functions

In [38]:
+manager('tom','marry')
+manager('sam','marry')
+manager('marry','john')

In [39]:

(count_of_direct_reports[Y]==len_(X))<=manager(X,Y)
print(count_of_direct_reports['marry']==Z)

Z
-
2

Some aggregate functions:

len_ (Y) : is the count of values of Y

sum_ (Y, for_each=Z)) : sum of Y for each Z. (Z is used to distinguish possibly identical Y values)
sum_ (Y, for_each=Z)) : sum of Y for each Z. (Z is used to distinguish possibly identical Y values)

min_, max_ (Y, order_by=Z)) : the minimum (or maximum) of Y sorted by Z.

In [40]:
(FirstStd[''] == max_(X, order_by= Y))<= (marks(X,Y))
Out[40]:
FirstStd[1]==!2(*,X,Y) <= marks(X,Y)

In [41]:
print(FirstStd['']==Z )

Z
-----
ahmad

In [ ]:

You might also like