0% found this document useful (0 votes)
32 views32 pages

AI All Practical

Uploaded by

chessaum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views32 pages

AI All Practical

Uploaded by

chessaum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Question : Dhruv Likes React. Dhruv Likes Data Science. Dhruv Likes Node JS.

Bhargav Likes
Node JS . Bhargav Likes Java.

Question : Bargar is a Food. Sandwich is Food. Food is Pizza. Sandwich is Lunch. Pizza is Lunch.

Condition : Anything is a meal if it is Food . Is Pizza is Lunch ? Is Sandwich is Dinner ?


Question : City , Pin code , State

Question : Dhruv Teach IS. Charli Studies Java. Aliva Studies Java. Jeck Studies AI. Kirtan
Studies DC.Kirtan Teach Java. Akshar Teach AI. Akshar Teach DC.

Condition: X is a Professor of Y. If X teaches C and Y studies C. What does Charli Studies? Who
are the Student of Kirtan ?
Maximum-Minimum Number:

Cut :
Fail:
Arithmetic Operators :

Grade :
Tower of Hanoi :
Arithmetic Operator
Practical:-4

- Write a program to implement Recursion in PROLOG.

Program 1: Program to implement factorial.

- Program to implement factorial using CUT and FAIL predicates.


Practical:-5

Write a program to implement Lists in PROLOG.

- Program to implement add an element to the list in PROLOG.

- Program to define the relation list(item, list) so that the item is the last element of thelist
using concatenate.
PRACTICAL-6

Write a program to implement string operations in PROLOG. Implement string


operations like substring, String position, palindrome etc.)

- Code for Prolog program to check whether a given word is a palindrome or not in
Artificial Intelligence.
- Code for Prolog program to check whether a given list is palindrome or not in Artificial
Intelligence.
- Code for Prolog program to compare characters, strings and also reverse string in
Artificial Intelligence.
PRACTICAL-7

Write a prolog program to maintain family tree.

predicates

male(symbol)

female(symbol)

father(symbol, symbol)

husband(symbol, symbol)

brother(symbol, symbol)

sister(symbol, symbol)

listbrothers(symbol)

listsisters(symbol)

mother(symbol, symbol)

grandfather(symbol)

grandmother(symbol)

uncle(symbol)

aunt(symbol)

cousin(symbol)

listgrandsons(symbol)

listgranddaughters(symbol)

printmenu

action(integer)

repeat

clauses

male(dashrath).
male(ram).

male(laxman).

male(bharat).

male(luv).

male(kush).

male(son_of_laxman).

female(kaushalya).

female(sita).

female(urmila).

female(daughter_of_dashrath).

father(dashrath, ram).

father(dashrath, laxman).

father(dashrath, bharat).

father(ram, luv).

father(ram, kush).

father(laxman, son_of_laxman).

father(dashrath, daughter_of_dashrath).

husband(dashrath, kaushalya).

husband(ram, sita).

husband(laxman, urmila).

mother(X, Y) :-

husband(Z, X),

father(Z, Y).
brother(X, Y) :-

father(Z, X),

father(Z, Y),

X \== Y,

male(X).

sister(X, Y) :-

father(Z, X),

father(Z, Y),

X \== Y,

female(X).

listbrothers(X) :-

brother(Z, X),

write(Z), nl,

fail.

listsisters(X) :-

sister(Z, X),

write(Z), nl,

fail.

grandfather(X) :-

father(Y, Z),

father(Z, X),

write(Y), write(" is the grandfather of "), write(X), nl,

fail.

grandmother(X) :-
husband(Z, X),

father(Z, V),

father(V, Y),

write(Y), write(" is the grandmother of "), write(X), nl,

fail.

listgrandsons(X) :-

father(X, Z),

father(Z, Y),

male(Y),

write(Y), nl,

fail.

listgrandsons(X) :-

husband(Y, X),

father(Y, V),

father(V, Z),

male(Z),

write(Z), nl,

fail.

listgranddaughters(X) :-

father(X, Z),

father(Z, Y),

female(Y),

write(Y), nl,

fail.
listgranddaughters(X) :-

husband(Y, X),

father(Y, V),

father(V, Z),

female(Z),

write(Z), nl,

fail.

uncle(X) :-

brother(Z, Y),

father(Z, X),

male(Y),

write(Y), nl,

fail.

aunt(X) :-

husband(Z, Y),

brother(Z, V),

father(V, X),

write(Y), nl,

fail.

cousin(X) :-

father(Z, X),

father(V, Y),

Z \== V,

brother(V, Z),
write(Y), nl.

repeat.

repeat :- repeat.

action(1) :-

write("\nEnter name of person whose father is to be found: "),

readln(X),

write("\nFather of "), write(X), write(" is:"),

father(Z, X),

write(Z), nl,

fail.

action(2) :-

write("\nEnter name of person whose mother is to be found: "),

readln(X),

write("\nMother of "), write(X), write(" is:"),

mother(Z, X), write(Z), nl, fail.

action(3) :-

write("\nEnter name of person whose brothers are to be found: "),

readln(X),

write("\nBrothers of "), write(X), write(" are:\n"),

listbrothers(X), nl, fail.

action(4) :-

write("\nEnter name of person whose sisters are to be found: "),

readln(X),

write("\nSisters of "), write(X), write(" are:\n"),


listsisters(X), nl, fail.

action(5) :-

write("\nEnter name of person whose grandsons are to be found: "),

readln(X),

write("\nGrandsons of "), write(X), write(" are:\n"),

listgrandsons(X), nl, fail.

action(6) :-

write("\nEnter name of person whose granddaughters are to be found: "),

readln(X),

write("\nGranddaughters of "), write(X), write(" are:\n"),

listgranddaughters(X), nl, fail.

action(7) :-

write("\nEnter name of person whose uncles are to be found: "),

readln(X),

write("\nUncles of "), write(X), write(" are:\n"),

uncle(X), nl, fail.

action(8) :-

write("\nEnter name of person whose aunties are to be found: "),

readln(X),

write("\nAunties of "), write(X), write(" are:\n"),

aunt(X), nl , fail.

action(9) :-

write("\nEnter name of person whose cousins are to be found: "),

readln(X),
write("\nCousins of "), write(X), write(" are:\n"),

cousin(X), nl, fail.

action(0).

printmenu :-

repeat,

write("\n1. Display Father of?\n"),

write("2. Display Mother of?\n"),

write("3. List all brothers of?\n"),

write("4. List all sisters of?\n"),

write("5. List all grandson of?\n"),

write("6. List all granddaughter of?\n"),

write("7. List all uncles of?\n"),

write("8. List all aunty of?\n"),

write("9. List all cousins of?\n"),

write("0. Exit\n"),

write("Enter your choice: "),

readInt(Choice),

action(Choice), nl, repeat.

goal

makewindow(1, 2, 3, "Family Tree", 0, 0, 25, 80),

printmenu.
Output –

-Family Tree

¦1. Display Father of? ¦

¦2. Display Mother of? ¦

¦3. List all brothers of? ¦

¦4. List all sisters of? ¦

¦5. List all grandson of? ¦

¦6. List all granddaughter of? ¦

¦7. List all uncles of? ¦

¦8. List all aunty of? ¦

¦9. list all cousins of? ¦

¦0. exit¦

¦Enter your choice : 1 ¦

¦¦

¦Enter name of person whose father is to be found : ram ¦

¦¦

¦Father of ram is:dashrath ¦

¦¦

¦1. Display Father of? ¦

¦2. Display Mother of? ¦

¦3. List all brothers of? ¦

¦4. List all sisters of? ¦

¦5. List all grandson of? ¦

¦6. List all granddaughter of? ¦


¦7. List all uncles of? ¦

¦8. List all aunty of? ¦

¦9. list all cousins of? ¦

¦0. exit¦

¦Enter your choice : 3 ¦

¦¦

¦Enter name of person whose brothers are to be found : ram ¦

¦¦

¦Brothers of ram are: ¦

¦laxman ¦

¦bharat ¦

¦¦

¦1. Display Father of? ¦

¦2. Display Mother of? ¦

¦3. List all brothers of? ¦

¦4. List all sisters of? ¦

¦5. List all grandson of? ¦

¦6. List all granddaughter of? ¦

¦7. List all uncles of? ¦

¦8. List all aunty of? ¦

¦9. list all cousins of? ¦

¦0. exit¦

¦Enter your choice : 5 ¦

¦¦
¦Enter name of person whose grandsons are to be found : dashrath ¦

¦¦

¦Grandsons of dashrath are: ¦

¦luv ¦

¦kush ¦

¦son_of_laxman ¦

¦¦

¦1. Display Father of? ¦

¦2. Display Mother of? ¦

¦3. List all brothers of? ¦

¦4. List all sisters of? ¦

¦5. List all grandson of? ¦

¦6. List all granddaughter of? ¦

¦7. List all uncles of? ¦

¦8. List all aunty of? ¦

¦9. list all cousins of? ¦

¦0. exit¦

¦Enter your choice : 7 ¦

¦¦

¦Enter name of person whose uncles are to be found : kus ¦

¦¦

¦Uncles of kush are: ¦

¦laxman ¦

¦bharat ¦
¦¦

¦1. Display Father of? ¦

¦2. Display Mother of? ¦

¦3. List all brothers of? ¦

¦4. List all sisters of? ¦

¦5. List all grandson of? ¦

¦6. List all granddaughter of? ¦

¦7. List all uncles of? ¦

¦8. List all aunty of? ¦

¦9. list all cousins of? ¦

¦0. exit¦

¦Enter your choice : ¦

¦¦

¦¦

¦Press the SPACE bar ¦


PRACTICAL-8

Write a program to implement BFS (for 8 puzzle problem or Water Jug problem
or any AI search problem).

domains

Node, Path, ExtendedPath = symbol*

predicates

breadthfirst(Node, Path, ExtendedPath)

extend(Node, Path, ExtendedPath)

conc(ExtendedPath, Path, ExtendedPath)

member(Node, ExtendedPath)

goal(Node)

clauses

breadthfirst(Node, Path, ExtendedPath) :- breadthfirst([[Node]], Path, ExtendedPath).

breadthfirst([[Node | Path] | _], [Node | Path], _) :- goal(Node).

breadthfirst([Path | Paths], Solution, ExtendedPath) :-

extend(Path, NewPaths, ExtendedPath), conc(Paths, NewPaths, Path1),

breadthfirst(Path1, Solution, ExtendedPath).

extend([Node | Path], NewPaths, ExtendedPath) :-

bagof([NewNode, Node | Path], (s(Node, NewNode), \+ member(NewNode, [Node |


Path]), \+ member(NewNode, ExtendedPath)), NewPaths), !.

extend(_, [], _).

conc([], L, L).

conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).

member(X, [X | _]). member(X, [_ | T]) :- member(X, T).

goal(Node) :-
Output –
PRACTICAL-9

Write a program to implement DFS (for 8 puzzle problem or Water Jug problem
or any AI search problem).
PRACTICAL-10

Write a program to Implement A* Algorithm.

%%%

%%%

%%% Nodes have form S#D#F#A

%%%where S describes the state or configuration

%%%D is the depth of the node

%%%F is the evaluation function value

%%%A is the ancestor list for the node

:- op(400,yfx,'#'). /* Node builder notation

*/ solve(State,Soln) :- f_function(State,0,F), search([State#0#F#[]],S), reverse(S,Soln).

function(State,D,F) :- h_function(State,H), F is D + H.

search([State#_#_#Soln|_], Soln) :- goal(State). search([B|R],S) :- expand(B,Children),

insert_all(Children,R,Ope n), search(Open,S).

insert_all([F|R],Open1,Open3) :- insert (F,Open1,Open2),

insert_all(R,Open2,Open3).

insert_all([],Open,Open).

insert(B,Open,Open) :- repeat_node(B,Open), !.

insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .

insert(B,[B1|R],[B1|S]) :- insert(B,R,S),!.

insert(B,[],[B]).repeat_node(P#_#_#_, [P#_#_#_|_]).

cheaper( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.

expand(State#D#_#S,All_My_Childr en) :- bagof(Child#D1#F#[Move|S],

(D1 is D+1,move(State,Child,Mov e), f_function(Child,D1,F)), All_My_Children).


PRACTICAL-11

Write a program to solve travelling salesman problem using Prolog.


PRACTICAL-12

Program: Study of dynamic database in PROLOG.

Predicates

reading writing delete find(integer)

startup(integer)

Database

unsortedDatabase(string,integer)

sortedDatabase(string)

Clauses

startup(0).

startup(Num):- write("Enter String = "),

readln(Name),

str_len(Name,Len),

asserta(unsortedDatabase(Name,Len)),

TempNum = Num - 1,

startup(TempNum).

writing:-sortedDatabase(Name),

write(Name),nl,fail. writing.

find(Index):- unsortedDatabase(Name,Index),

assertz(sortedDatabase(Name)),

retract(unsortedDatabase(Name,Index)),

find(Index).

find(Index):- Index = 255.

find(Index):- TempIndex = Index + 1,


find(TempIndex).

reading:- NumRead = 10,

startup(NumRead).

delete :- retract(sortedDatabase(_)), fail.delete.

Goal

Clearwindow,

makewindow(1,2,3,"String Operations",0,0,25,80),

reading,!,find(1),

write("\nString In Increasing Order Of Their Length Are : \n"),

writing,delete.

Output –

You might also like