Professional Documents
Culture Documents
Kotrikalan village,
Sehore Dt.Bhopal-466144
CSA2001-FOUNDAMENTALS IN AI and
ML LABORATORY
FALL SEMSTER
Reg No-21BCE10601
INDEX
15. Write a Program to Implement Travelling Salesman Problem using Python. 36-38
LAB PROGRAM CASE STUDY
1. Study of facts, objects, predicates and variables in PROLOG.
FACTS: -
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.
likes(john, susie). /* John likes Susie */
likes(X, susie). /* Everyone likes Susie */
likes(john, Y). /* John likes everybody */
likes(john, Y), likes(Y, john). /* John likes everybody and everybody
likes John */
likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary
*/
not(likes(john,pizza)). /* John does not like pizza */
likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.
OBJECTS: -
PREDICATES: -
There was a simple program that has five clauses. Head is a compound term for each first
three clauses with functor parent. It has two arguments with 2 arity.
1. parent(emma, robert).
2. parent(A, B) :- father(A, B).
3. parent(A, B) :- mother(A, B).
4. father(kevin, mary).
5. mother(anne, mary).
The following program includes the clauses for which functor parent is contained by the
head, but a different arity.
For example:
1. parent(kevin).
2. parent(A) :- son(A, B).
3. /* A is parent if A has a son B */
VARIABLES: -
Suppose we want to ask, "What course does Jeff teach"? This could be written as:
Is there a subject, X, which Jeff teaches?
Here, X, is a variable which stands for an object which the questioner does not know
about yet.
To answer the question, Prolog to find out the value of X, if it exists. As long as we do
not know the value of a variable it is said to be unbound or uninstantiated. When a
value is found, the variable is said to instantiated or bound to the value. The name of a
variable must begin with a capital letter or an underscore character, "_".
To ask Prolog to find the course which Jeff teaches, the following query is entered:
lectures(jeff, X)?
X = 611 output from Prolog
When there is more than one possible answer, Prolog will try to find all of them.
2. Study of Rules and Unification in PROLOG.
RULES
The previous question can be restated as a general rule:
One person, Teacher, teaches another person, Student if
X lectures in a subject, Subject and
Student studies Subject.
This is also called a clause. Facts are unit clauses and rules are non-unit clauses. ":-"
means "if" or "is implied by". This symbol is often called the neck symbol. The left
hand side of the neck is called the head. The right hand side of the neck is called
the body. The comma, ",", separating the goals is stands for and.
Unification in Prolog
We will give a goal to evaluate and Prolog will work through the clauses in the database.
In this, Prolog attempts to match the goal with each clause. The matching process works
from left to right. The goal will fail if no match is found. If a match is found, the action will
take.
Prolog uses the unification technique, and it is a very general form of matching technique.
In unification, one or more variables being given value to make the two call terms
identical. This process is called binding the variables to values. For example, Prolog can
unify the terms cat(A), and cat(mary) by binding variable A to atom mary that means we
are giving the value mary to variable A. Prolog can unify person(Kevin, dane) and person(L,
S) by binding L and S to atom kevin and dane, respectively.
In starting, all variables have no value. In unification, once a variable bound to the value,
it can be made unbound again and then perhaps be bound to a new value using the
backtracking.
b(1).
b(4).
c(1).
c(3).
d(4).
The query a(X). produces
1 ?- a(X).
false.
2 ?-
but with this code
a(X) :- b(X),!,c(X).
a(X) :- d(X).
b(1).
b(4).
c(1).
c(3).
d(4).
The query a(X). results in :
1 ?- a(X).
X = 1.
Input/Output: -
• A Prolog program can read data from input streams, and write data to output streams.
• Data coming from the user's terminal is treated as just another input stream. Data output to the
terminal is treated as another output stream. Both of these "pseudo-files" are referred to by the
name user.
• The names of other files can be chosen by the programmer according to the rules of the OS.
COMPOUND GOALS: -
DYNAMIC DATABASE
Dynamic database can change dynamically at execution time and are of two types. Type1:
created at each execution. It grows, shrinks and is deleted at the end of program.
● This type of database is no longer available after program finishes its execution and is
called working memory.
Type2: Other type of dynamic databases are those which are stored in files and called
database files.
– These types of databases are not part of any particular program and are available for
use in future by different programs using system defined predicates called save and
consult.
– While executing a Prolog program one can load database file(s) using 'consult'
predicate.
– These files can be updated dynamically at run time and saved using 'save' predicate.
● Clauses can be added to a database at run time using following predicates. asserta(X)
& assertz(X) - succeed by adding fact X in the beginning & at the end of database of
facts respectively.
● For example, asserta(father(mike, john)) adds fact father(mike, john) in the beginning
of current database.
● Similarly obsolete clauses can be deleted by using system defined predicate called
retract from dynamic database at the run time.
● For example, retract(father(mike, X)) deletes the first fact father(mike, _) from working
memory.
● retractall(X) deletes all the clauses from a database whose head match with X.
● Example, retractall(father(X, Y)) deletes all the facts father( _ , _ ) and retractall( _ )
deletes all the clauses from the working memory.
Problem Statement:
Perform following String operations with and without pointers to arrays (without using the
library functions):
a. substring
b. palindrome
c. compare
d. copy
e. reverse.
Explanation:
For above statement "String operations with and without pointers to arrays without using
library function" we need to should know how exactly above operation works on string.
So for that we here implemented various operations on strings like substring, palindrome,
string comparisons, string copy, reverse the given string etc.
In second operation we check a sstring characters in such a manner that- first character
compared with last one, second with second last n so on.. you will understand when you
see code.
In third we just check two strings. we compare string character by character from left side.
In fourth, we just copy string characters of first string into second string which is easy to
understand when you see code for string copy.
In last, for string reverse used swapping method that swaps first and last character,
second n second last character and so on.
Program:
#include <stdio.h>
Output:
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit
A palindrome
Press a Character
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit
1st>2nd
Press a Character
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit
Enter a String:sasha
Result=sasha
Press a Character
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit
Enter a String:race
Result=ecar
Press a Character
LAB program 3
Name – Tushar Kumar
Reg. no. – 21BCE11274
1. Write a prolog program to maintain family tree.
Input:-
Output:-
2. Write a prolog program to implement all set operations (Union, intersection, complement etc.)
Input:-
Output:-
// Driver Code
int main()
{
// Create a instance
struct library lib[100];
i = input = count = 0;
printf("\n\n********######"
"WELCOME TO E-LIBRARY "
"#####********\n");
printf("\n\n1. Add book infor"
"mation\n2. Display "
"book information\n");
printf("3. List all books of "
"given author\n");
printf(
"4. List the count of book"
"s in the library\n");
printf("5. Exit");
// Add book
case 1:
break;
if (strcmp(ar_nm,
lib[i].author)
== 0)
printf("%s %s %d %f",
lib[i].book_name,
lib[i].author,
lib[i].pages,
lib[i].price);
}
break;
Output:-
# Constructor
def init (self):
while queue:
# Constructor
def init (self):
# Driver code
Lab Programs
3. Write a Program to Implement Tic-Tac-Toe game using
Python.
# Tic-Tac-Toe Program using
# random number in Python
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
winner = player
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
# Driver Code
print("Winner is: " + str(play_game()))
4. Write a Program to Implement Travelling
Salesman Problem using Python.
# Python3 program to implement traveling salesman
# problem using naive approach.
from sys import maxsize
from itertools import permutations
V=4
# update minimum
min_path = min(min_path, current_pathweight)
return min_path
# Driver Code
if name == " main ":