Professional Documents
Culture Documents
Ai Lab Manual
Ai Lab Manual
Ai Lab Manual
“Artificial Intelligence”
“LAB MANUAL”
LAB #1
Name: Nooria Ijaz
BS-COMPUTERS
8th (F)
1. Introduction
3. Concept Map
3.1 Introduction
3.1.1 Intelligence
Ability to handle fuzzy questions, different thoughts over same measurements and
same knowledge leads towards
AI is accomplished by studying how human brain thinks, and how humans learn,
decide, and work while trying to solve a problem, and then using the outcomes of
this study as a basis of developing intelligent software and systems.
A self-driving car. 2850 Km driven without driver. Let’s meet ALVINN ->
https://www.youtube.com/watch?v=ilP4aPDTBPE
Manufacturing robots
Smart assistants
Proactive healthcare management
Disease mapping
Automated financial investing
Virtual travel booking agent
Social media monitoring
Inter-team chat tool
Conversational marketing bot
Natural Language Processing (NLP) tools
4. WHAT IS Z LANGUAGE?
Z is a model oriented formal specification language based on Zermelo-Fränkel
axiomatic set theory and first order predicate logic. It is a mathematical
specification language, with the help of which natural language requirements
can be converted into mathematical form.
5. WHAT IS PROLOG?
Prolog was one of the first logic programming languages,[7] and remains the
most popular among such languages today, with several free and commercial
implementations available. The language has been used for theorem proving,
[8]
expert systems,[9] term rewriting,[10] type systems,[11] and automated planning,
[12]
as well as its original intended field of use, natural language processing.[13]
[14]
Modern Prolog environments support the creation of graphical user
interfaces, as well as administrative and networked applications.
Prolog is well-suited for specific tasks that benefit from rule-based logical
queries such as searching databases, voice control systems, and filling
templates.
6. WRITE TYPES AND VERSIONS OF PROLOG
SOFTWARE?
GNU-Prolog SWI-Prolog
This document lists the SWI-Prolog data types and relevant information such as
their limits. It is mostly intended for programs that want to examine and/or
exchange arbitrary SWI-Prolog data.
Classical Prolog types
Variable
Integer
Floats
Atoms
Compound terms
SWI-Prolog extensions
Blobs
Strings
List cons-cell
Dictionaries
“Artificial Intelligence”
“SWI-PROLOG”
LAB #2
Artificial Intelligence
“SWI- Prolog”
Introduction :
Prolog is a logic programming language. It has important role in
artificial intelligence. Unlike many other programming languages,
Prolog is intended primarily as a declarative programming language.
In prolog, logic is expressed as relations (called as Facts and Rules).
Core heart of prolog lies at the logic being applied. Formulation or
Computation is carried out by running a query over these relations.
Installation in Linux :
a. Select I understand
b. Click Download swipl-8.1.12-1.x64.exe
6. This will open a Save As dialog on Windows. Select a directory
and click Save
In the lower left of the Internet browser screen a dialog will appear
with the name of the file
14. Click Next
15. Click Next
16. Click Next
17. Click Install
17 a. If during the install you get a dialog noting Error opening file for
writing: ...
To start SWI-Prolog
click SWI-Prolog
This will start SWI-Prolog console
How to uninstall SWI-Prolog.
Note: SWI-Prolog does not require the previous version be
uninstalled. If a previous version is installed then during the install an
additional dialog will appear asking if you want to uninstall the
previous version. You can answer either Yes or No depending upon
your preference; the install should complete successfully either way.
1. Using Windows File Explorer navigate to C:\Program
Files\swipl
2. Click on Uninstall.exe
3. For User Account Control dialog click Yes.
4. Click Uninstall
5. Click Close
NB The directory should be either empty or have only a few
files left in it. During this example the only file left
was C:\Program Files\swipl\boot64.prc
Example :
friends(raju, mahesh).
singer(sonu).
odd_number(5).
Explanation :
These facts can be interpreted as :
raju and mahesh are friends.
sonu is a singer.
5 is an odd number.
Key Features :
1. Unification : The basic idea is, can the given terms be made to
represent the same structure.
2. Backtracking : When a task fails, prolog traces backwards and
tries to satisfy previous task.
3. Recursion : Recursion is the basis for any search in program.
Running queries :
A typical prolog query can be asked as :
Query 1 : ?- singer(sonu).
Output : Yes.
Query 2 : ?- odd_number(7).
Output : No.
Advantages :
1. Easy to build database. Doesn’t need a lot of programming effort.
2. Pattern matching is easy. Search is recursion based.
3. It has built in list handling. Makes it easier to play with any
algorithm involving lists.
Disadvantages :
1. LISP (another logic programming language) dominates over prolog
with respect to I/O features.
2. Sometimes input and output is not easy.
Applications :
Prolog is highly used in artificial intelligence(AI). Prolog is also used
for pattern matching over natural language parse trees.
“Artificial Intelligence”
“SWI-PROLOG”
LAB #3
Windows User Artificial Intelligence
QUESTION:
W3HOW TO WRITE MULTIPLE RULES IN PROLOG?
Week2 and week3
Example 1 : Below food table shows the facts, rules, goals and their
english meanings.
English
Facts
meanings
// burger is
food(burger).
a food
// sandwich
food(sandwich).
is a food
// pizza is a
food(pizza).
food
// sandwich
lunch(sandwich).
is a lunch
// pizza is a
dinner(pizza).
dinner
Rules
// Every
food is a
meal(X) :- food(X). meal OR
Anything is
a meal if it
is a food
Queries / Goals
// Is pizza a
?- food(pizza).
food?
// Which
?- meal(X), lunch(X). food is meal
and lunch?
// Is
sandwich a
dinner?
?- dinner(sandwich).
Example 2 : Below student-professor relation table shows the facts,
rules, goals and their english meanings.
Facts English meanings
studies(charlie, csc135). // charlie studies csc135
studies(olivia, csc135). // olivia studies csc135
studies(jack, csc131). // jack studies csc131
studies(arthur, csc134). // arthur studies csc134
teaches(kirke, csc135). // kirke teaches csc135
teaches(collins, csc131). // collins teaches csc131
teaches(collins, csc171). // collins teaches csc171
teaches(juniper, csc134). // juniper teaches csc134
Rules
professor(X, Y) :- // X is a professor of Y if
teaches(X, C), studies(Y, X teaches C and Y studies
C). C.
Queries / Goals
// charlie studies what?
?- studies(charlie, What). OR
What does charlie study?
?- professor(kirke, // Who are the students of
Students). professor kirke.
From Example 1 :
(1) ?- meal(X), dinner(X).
(2) ?- meal(What).
(3) ?- meal(X), dinner(Y).
From Example 2 :
(1) ?- studies(Who, csc135). ( hint : after getting first
solution type ' ; ' to find all the possible solutions)
Fun Bonus : Using Example-2 just copy paste below
query and see the result -
?- studies(charlie, Which), teaches(Who,Which),
write('charlie studies '), write(Which), write(' and
professor '), write(Who), write(' teaches '),
write(Which).
“Artificial Intelligence”
“SWI-PROLOG”
LAB #4
Week04 16-3-2021
Practical #2
A fact is a predicate expression that makes a
declarative statement about the problem
domain. Whenever a variable occurs in a
Prolog expression, it is assumed to
be universally quantified. Note that all Prolog
sentences must end with a period.
steps:
o open prolog
o write facts to make pl file “named medicine”
o pass two arguments
o consult pl file (from knowledge base)
o query all facts (query window)
o take screen shot
o make your word file
practical statement
create following facts in prolog file.
Facts English meaning
1. likes(ALI, bilal). /* ali likes bilal*/
http://www.cs.trincoll.edu/~ram/cpsc352/notes/prolog/factsrules.html
Rules
(A-STAR ALGO. )
CODE:
OUTPUT:
“Artificial Intelligence”
“SWI-PROLOG”
LAB #5
Windows User Artificial Intelligence
“Variables and Unification”
1. Introduction
? - eats(fred,what).
Lab 3: Unification and Backtracking
However, Prolog will say no. The reason for this is that what does not
match with mangoes. In order to match arguments in this way we
must use a Variable. The process of matching items with variables is
known as unification.
Thus returning to our first question we can find out what fred eats by
typing
? - eats(fred,What).
What=mangoes
yes
4.2 Unification Examples
Let's consider some examples using facts. First consider the following
database.
loves(john,mary). loves(fred,hobbies).
eats(fred,mangoes).
How do we ask what fred eats. We could type in something like
?- eats(fred,what).
However Prolog will say no. The reason for this is that what does not
match with mangoes. In order to match arguments in this way we
must use a Variable. The process of matching items with variables is
known as unification. Variables are distinguished by starting with a
capital letter. Here are some examples:
X /* a capital letter */
?- eats(fred,What).
What=mangoes
yes
As a result of this query, the variable What has matched (or unified)
with mangoes. We say that the variable What now has the binding
mangoes. When we pose a query, if the query is successful, Prolog
prints both the variable and the variable name, as we see above.
Variable Examples 1
Let's consider some examples using facts. First consider the following
database.
loves(john,mary).
loves(fred,hobbies).
Who = hobbies /* Note the to Prolog Who is just the name of a variable,
it */
“Artificial Intelligence”
“SWI-PROLOG”
“Clusteringwith
LAB #6WEKA
Explorer”
Windows User Artificial Intelligence
1. Open a terminal window from the left bar. Go to
directory /opt/weka-3-6-13, then type command : java
–jar weka.jar.
Examples Of Conjunction :
Conjunction in Rule :
likes(mary, day) :- sunny, warm.
// Read as : mary likes a day, if it is sunny and warm.
Conjunction in Goal :
?- place(sunny), place(windy).
// Is place sunny and windy? OR Is place sunny? and Is
place windy?
“Artificial Intelligence”
“SWI-PROLOG”
LAB #9
Windows User Artificial Intelligence
SWI- PROLOG PERFORM
ARITHEMATIC OPERATIONS:
Don’t perform
“Artificial Intelligence”
“SWI-PROLOG”
LAB #10
Windows User Artificial Intelligence
“ LISTS and use of LIST”
“head & tail in LIST” .
Introduction
In Prolog list is an ordered sequence of components, where components can be
variables, constants, compound terms, numbers or another list.
Syntax : [components] - write your components within square brackets.
Examples : different ways to write list
[1,2,3,4].
[nova, james, 2].
[X, Y, james].
[A, [p, n, c], 4].
[likes(X,Y), cat(fur)].
[] - an empty list
Lab objective
This lab will give overview of list used in prolog to make data order or
sequence.
The student should be able to: Understand the term LIST,
COMPONENETS, ORDER, ELEMENTS
Students also be able to create order element LIST.
The most common way of invoking a function is by evaluating a list. For example,
evaluating the list (concat "a" "b") calls the function concat with arguments "a" and "b".
See Evaluation, for a description of evaluation.
When you write a list as an expression in your program, you specify which function to
call, and how many arguments to give it, in the text of the program. Usually that's just
what you want. Occasionally you need to compute at run time which function to call. To
do that, use the function funcall. When you also need to determine at run time how many
arguments to pass, use apply.
— Function: funcall function &rest arguments
funcall calls function with arguments, and returns whatever function returns.
Since funcall is a function, all of its arguments, including function, are evaluated
before funcall is called. This means that you can use any expression to obtain the
function to be called. It also means that funcall does not see the expressions you write for
the arguments, only their values. These values are not evaluated a second time in the act
of calling function; the operation of funcall is like the normal procedure for calling a
function, once its arguments have already been evaluated.
The argument function must be either a Lisp function or a primitive function. Special
forms and macros are not allowed, because they make sense only when given the
unevaluated argument expressions. funcall cannot provide these because, as we saw
above, it never knows them in the first place.
If you need to use funcall to call a command and make it behave as if invoked
interactively, use funcall-interactively (see Interactive Call).
(setq f 'list)
⇒ list
⇒ (x y z)
⇒ (x y (z))
⇒ list
⇒ 10
⇒ 10
⇒ (a b c x y z)
Sometimes it is useful to fix some of the function's arguments at certain values, and
leave the rest of arguments for when the function is actually called. The act of fixing
some of the function's arguments is called partial application of the function1. The result
is a new function that accepts the rest of arguments and calls the original function with
all the arguments combined.
(1+ 10)
⇒ 11
It is common for Lisp functions to accept functions as arguments or find them in data
structures (especially in hook variables and property lists) and call them
using funcall or apply. Functions that accept function arguments are often
called functionals.
Sometimes, when you call a functional, it is useful to supply a no-op function as the
argument. Here are two different kinds of no-op function:
— Function: identity arg
— Function: ignore &rest args
This function ignores any arguments and returns nil.
Lisp Defining Function:
Program #1
Program #2
Lisp Rest Parameters:
Program #1
Lisp Keyword Parameters:
Program #1
Program #2
Program # 3
• The do construct is also used for performing iteration
using LISP. It provides a structured form of iteration.
• The syntax for do statement −
• The dotimes construct allows looping for
some fixed nuber of iterations.
• For example, Create a new source code file named main.lisp and
type the following code in it: