0% found this document useful (0 votes)
30 views31 pages

Artificial Intelligence: Guru Tegh Bahadur Institute of Technology

perfect

Uploaded by

pappu
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)
30 views31 pages

Artificial Intelligence: Guru Tegh Bahadur Institute of Technology

perfect

Uploaded by

pappu
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/ 31

Guru Tegh Bahadur Institute of Technology, Delhi

(Affiliated to Guru Gobind Singh Indraprastha University, Dwarka, New Delhi)

Department of Computer Science & Engineering

PRACTICAL FILE

Artificial Intelligence

(Paper code: CIE-374T)

Submitted to: Submitted by:


Ms. Esha Saxena Name: Om Verma
Assistant Professor Roll No: 01276807222

1
Index

S.No. Name of Experiment Date T.Sign


1

10

11

12

13

2
Experiment-1
Aim:
Study of PROLOG
Theory:
Prolog is a logic programming language that finds its origins in the
realms of artificial intelligence, automated theorem proving, and
computational linguistics. Developed in the 1970s, Prolog stands out
from many other programming languages due to its primary focus on
declarative programming. In Prolog, a program is essentially a
collection of facts and rules that define relations. Computation in
Prolog is initiated by querying these relations, allowing for a unique
approach to problem-solving.
At the core of Prolog's design is its foundation in first-order logic, a
formal logic system. Unlike traditional imperative programming
languages, which focus on how to achieve a result, Prolog
emphasizes what relationships exist within the problem domain. This
declarative nature makes Prolog well-suited for tasks involving
database operations, symbolic mathematics, and natural language
processing, among others.
One of Prolog's distinguishing features is its use of a single data type
called a "term." Terms in Prolog can be atoms, numbers, variables, or
compound terms. Atoms are symbols or names, while numbers can
be floats or integers. Variables in Prolog are placeholders for terms
and are denoted by strings starting with an uppercase letter or
underscore. Compound terms consist of a functor (an atom) and a
number of arguments (terms), allowing for the creation of complex
structures.

3
Prolog programs are built using rules and facts. Facts are statements
that are always true, while rules define how new facts can be inferred
from existing ones. For example, a fact `human(socrates)` states that
Socrates is human, while a rule `mortal(X) :- human(X)` defines that X
is mortal if X is human. These rules and facts can be queried to derive
new information.
Execution in Prolog involves proving queries against the defined facts
and rules. Prolog uses a resolution method called SLD resolution
(Selective Linear Definite clause resolution) to find solutions to
queries. The Prolog engine attempts to find a resolution refutation of
the negated query. If successful, the query is considered true, and
any variable bindings are reported as solutions.
Prolog's logical nature allows for elegant solutions to certain types of
problems, particularly those involving symbolic reasoning and
knowledge representation. However, it can be challenging for
programmers coming from imperative programming backgrounds to
grasp the declarative paradigm and the concept of backtracking,
which is used extensively in Prolog to explore alternative solutions.
Despite its challenges, Prolog remains a powerful tool for various
applications, including theorem proving, expert systems, term
rewriting, type systems, automated planning, and natural language
processing. Its unique approach to programming continues to attract
researchers and developers interested in logic-based problem-
solving.

4
Experiment-2
Aim:
Write simple fact for the statements using PROLOG:
 Ram likes mango.
 Seema is a girl.
 Bill likes Cindy.
 Rose is red.
 John owns gold.
Code:
likes(ram, mango).
girl(seema).
likes(bill, cindy).
color(rose, red).
owns(john, gold).

5
Output:

6
Experiment-3
Aim:
Write statement to perform basic Arithmetic operations:
 Addition
 Subtraction
 Multiplication
 Division
Code:
X is 2+2.
X is 5-2.
X is 3*(4+2).
X is (8/4).
X is (8/4)/2.
X is 4<3.
X is 4>3.
X is 2+2, x>3.
X is 2+2> 3*2.

7
Output:

8
Experiment-4
Aim:
Write a program to implement input, output and predicates in Prolog
to Find the cube of a number.
Code:
cube :-
write('Write a Number: '),
read(Number),
process(Number).

process(Stop) :- !.
process(Number) :-
C is Number * Number * Number,
write('Cube of '), write(Number), write(': '), write(C), nl,
cube.

9
Output:

10
Experiment-5
Aim:
Write predicates, one converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below freezing using
PROLOG.
Code:
% Predicate to convert centigrade temperatures to Fahrenheit
c_to_f(C, F) :- F is (C * 9/5) + 32.

% Predicate to check if a temperature is below freezing


below_freezing(C) :- c_to_f(C, F), F < 32.

11
Output:

12
Experiment-6
Aim:
Write a program to implement Factorial of a given number using
PROLOG.
Code:
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, Result1),
Result is N * Result1.

13
Output:

14
Experiment-7
Aim:
Write a program to implement Depth First Search Traversal.
Code:
connected(1,7,1).
connected(1,8,1).
connected(1,3,1).
connected(7,4,1).
connected(7,20,1).
connected(7,17,1).
connected(8,6,1).
connected(3,9,1).
connected(3,12,1).
connected(9,19,1).
connected(4,42,1).
connected(20,28,1).
connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).
connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Visited) :-

15
connected2(Current, Next, _),
not(member(Next, Visited)).

depth_first(Goal, Goal, _, [Goal]).


depth_first(Start, Goal, Visited, [Start|Path]) :-
next_node(Start, NextNode, Visited),
depth_first(NextNode, Goal, [NextNode|Visited], Path).

16
Output:

17
Experiment-8
Aim:
Write a program to implement Breadth First Search Traversal.
Code:
% connected(Start, Goal, Weight)
connected(1,7,1).
connected(1,8,1).
connected(1,3,1).
connected(7,4,1).
connected(7,20,1).
connected(7,17,1).
connected(8,6,1).
connected(3,9,1).
connected(3,12,1).
connected(9,19,1).
connected(4,42,1).
connected(20,28,1).
connected(17,10,1).

% Define a predicate to check if two nodes are connected


bidirectionally.
connected2(X, Y, D) :- connected(X, Y, D).
connected2(X, Y, D) :- connected(Y, X, D).
18
next_node(Current, Next, Path) :-
connected2(Current, Next, _),
not(member(Next, Path)).

breadth_first(Goal, Goal, _, [Goal]).


breadth_first(Start, Goal, Visited, [Start|Path]) :-
findall(NextNode,
(next_node(Start, NextNode, Visited), not(member(NextNode,
Visited))),
NextNodes),
member(Next, NextNodes),
append(Visited, [Next], Visited2),
breadth_first(Next, Goal, Visited2, Path).

19
Output:

20
Experiment-9
Aim:
Write a program to solve 4-Queen problem using Prolog.
Code:
queens(N, Queens) :- length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).

board([], [], N, N, _, _).

board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-


Col is Col0+1,
functor(Vars, f, N),
constraints(N, Vars, VR, VC),
board(Queens, Board, Col, N, VR, [_|VC]).

constraints(0, _, _, _) :- !.

constraints(N, Row, [R|Rs], [C|Cs]) :-


arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).

21
queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).

22
Output:

23
Experiment-10
Aim:
Write a program to implement Water Jug Problem.
Code:
solve_jugs(Start, Capacities, End, Solution) :-
solve_jugs([start(Start)], Capacities, [], End, Solution).

solve_jugs([Node|_], _, _, End, [Node|Visited]) :-


node_state(Node, End).
solve_jugs([Node|Nodes], Capacities, Visited, End, Solution) :-
findall(Successor,
(successor(Node, Capacities, Visited, Successor),
\+ member(Successor, Visited)),
Successors),
append(Nodes, Successors, NewNodes),
solve_jugs(NewNodes, Capacities, [Node|Visited], End, Solution).

successor(Node, Capacities, Visited, Successor) :-


node_state(Node, State),
jug_transition(State, Capacities, Action, State1),
Successor = node(Action, State1, Node),
\+ member(State1, Visited).

24
jug_transition(State0, Capacities, empty_into(Source, Target), State1)
:-
volume(Source, State0, Content0),
Content0 > 0,
jug_permutation(Source, Target, Unused),
volume(Target, State0, Content1),
volume(Target, Capacities, Capacity),
Content0 + Content1 =< Capacity,
volume(Source, State1, 0),
volume(Target, State1, Content2),
Content2 is Content0 + Content1,
volume(Unused, State0, Unchanged),
volume(Unused, State1, Unchanged).

jug_transition(State0, Capacities, fill_from(Source, Target), State1) :-


volume(Source, State0, Content0),
Content0 > 0,
jug_permutation(Source, Target, Unused),
volume(Target, State0, Content1),
volume(Target, Capacities, Capacity),
Content1 < Capacity,
Content0 + Content1 > Capacity,
25
volume(Source, State1, Content2),
volume(Target, State1, Capacity),
Content2 is Content0 + Content1 - Capacity,
volume(Unused, State0, Unchanged),
volume(Unused, State1, Unchanged).

volume(small, jugs(Small, _Large, _Reservoir), Small).


volume(large, jugs(_Small, Large, _Reservoir), Large).
volume(reservoir, jugs(_Small, _Large, Reservoir), Reservoir).

jug_permutation(Source, Target, Unused) :-


select(Source, [small, large, reservoir], Residue),
select(Target, Residue, [Unused]).

node_state(start(State), State).
node_state(node(_Transition, State, _Predecessor), State).

narrative(start(Start), Capacities, End) -->


"Given three jugs with capacities of:", newline,
literal_volumes(Capacities),
"To obtain the result:", newline,
literal_volumes(End),
"Starting with:", newline,
26
literal_volumes(Start),
"Do the following:", newline.

narrative(node(Transition, Result, Predecessor), Capacities, End) -->


narrative(Predecessor, Capacities, End),
literal_action(Transition, Result).

literal_volumes(Volumes) -->
indent, literal(Volumes), ";", newline.

literal_action(Transition, Result) -->


indent, "- ", literal(Transition), " giving:", newline,
indent, indent, literal(Result), newline.

literal_volumes(jugs(Small, Large, Reservoir)) -->


literal_number(Small), " gallons in the small jug, ",
literal_number(Large), " gallons in the large jug, and ",
literal_number(Reservoir), " gallons in the reservoir".

literal_number(Number) --> { number_chars(Number, Chars) }, Chars.

literal(small) --> "small jug".


literal(large) --> "large jug".
27
literal(reservoir) --> "reservoir".
indent --> " ".

newline --> "\n".

28
Output:

29
Experiment-11
Aim:
Write a program to solve the Monkey Banana problem using
PROLOG.
Code:
on(floor, monkey).
on(floor, chair).
in(room, monkey).
in(room, chair).
in(room, banana).
at(ceiling, banana).
strong(monkey).
grasp(monkey).
climb(monkey, chair).

push(monkey, chair) :- strong(monkey).


under(banana, chair) :- push(monkey, chair).

canreach(banana, monkey) :- (at(floor, banana); at(ceiling, banana)),


under(banana, chair), climb(monkey, chair).
canget(banana, monkey) :- canreach(banana, monkey),
grasp(monkey).

30
Output:

31

You might also like