0% found this document useful (0 votes)
8 views11 pages

AI (Exp 2)

Artificial intelligence experiment

Uploaded by

dev606033
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)
8 views11 pages

AI (Exp 2)

Artificial intelligence experiment

Uploaded by

dev606033
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/ 11

UG Program in Artificial Intelligence & Data Science

Experiment 2

Experiment No. - 2

Date of Performance: 22/07/24

Date of Submission: 29/07/24

Program Execution/ Viva

formation/correction/ Answer to

ethical practices Documentation Timely sample Experiment Sign with


Date
(07) (02) Submission questions Total (15)

(03) (03)

3.1 Aim:

To write simple programs using PROLOG as an AI programming language for various tasks such as
establishing relations in a family tree, factorial calculation, LCD and GCD, and solving the Missionaries
and Cannibals problem.

3.2 Course Outcome (CO):

CO 2: Apply the most suitable search strategy and represent a natural language description of statements
in logic and apply the inference rules to design problem-solving agents.

3.3 Problem Statement:

1. Write a PROLOG program to establish relationships between two persons in a family tree.
2. Write a PROLOG program to calculate the factorial of a number.
3. Write a PROLOG program to find the LCD (Least Common Divisor) and GCD (Greatest Common
Divisor) of two numbers.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-12


UG Program in Artificial Intelligence & Data Science

4. Write a PROLOG program to solve the Missionaries and Cannibals problem.

3.4 Related Theory:

PROLOG (Programming in Logic) is a high-level programming language rooted in formal logic. It is


widely used for solving problems that involve knowledge representation and reasoning, such as expert
systems, natural language processing, and AI applications. Unlike imperative languages like C or Java,
PROLOG is a declarative language where the program logic is expressed as relations, represented by
facts and rules.

In PROLOG:

 Facts represent basic assertions about objects or relationships. For example, parent(rahul, isha).
states that Rahul is a parent of Isha.
 Rules are conditional statements that define relationships between facts. For example, grandparent(X,
Y) :- parent(X, Z), parent(Z, Y). defines the grandparent relationship.
 Queries are questions posed to the system to determine if certain statements are true based on the facts
and rules provided.

Key Features of PROLOG:

1. Backtracking: PROLOG uses backtracking to explore different possibilities and find solutions to a
problem.
2. Unification: PROLOG automatically attempts to unify variables and constants to satisfy queries.
3. Recursion: Like many logical languages, PROLOG heavily relies on recursion to solve complex
problems.

PROLOG is particularly suited for solving AI problems because it allows expressing problems in terms
of relationships and logical inference. Below are some examples of simple PROLOG programs that
demonstrate how the language works.

3.5 Program Listing and Output:

1. Family Tree (Establishing Relations between Two Persons):

parent(rahul, isha).
parent(rahul, sahil).
parent(isha, kiran).
parent(sahil, joy).

% Define relationships
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

% Queries

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-13


UG Program in Artificial Intelligence & Data Science

% ?- grandparent(rahul, kiran). (Answer: Yes)


% ?- sibling(isha, sahil). (Answer: Yes)

2. Factorial Program:

factorial(0, 1). % Base case


factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.

% Query
% ?- factorial(5, F). (Answer: F = 120)

3. LCD and GCD Program:

gcd(0, X, X) :- X > 0.
gcd(X, 0, X) :- X > 0.
gcd(A, B, G) :-
A > 0, B > 0,
A1 is A mod B,
gcd(B, A1, G).

% LCD is calculated using the formula: LCM * GCD = Product of two numbers
lcm(A, B, L) :-
gcd(A, B, G),
L is (A * B) // G.

% Query
% ?- gcd(48, 18, G). (Answer: G = 6)
% ?- lcm(48, 18, L). (Answer: L = 144)

4. Missionaries and Cannibals Problem:

% Missionaries and Cannibals Problem


% state(MissionariesLeft, CannibalsLeft, BoatPosition).

move(state(ML, CL, left), state(MR, CR, right)) :-


% Move missionaries and cannibals from left to right
ML > 0, MR is ML - 1, CR is CL, CL = CR.

solve(state(3, 3, left), state(0, 0, right)) :-


% Solves the problem by defining the starting and goal state
% And implementing the necessary transitions.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-14


UG Program in Artificial Intelligence & Data Science

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-15


UG Program in Artificial Intelligence & Data Science

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-16


UG Program in Artificial Intelligence & Data Science

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-17


UG Program in Artificial Intelligence & Data Science

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-18


UG Program in Artificial Intelligence & Data Science

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-19


UG Program in Artificial Intelligence & Data Science

3.6 Procedure:

1. Set up the PROLOG Environment:


o Install a PROLOG interpreter (e.g., SWI-PROLOG).
2. Create PROLOG Programs:
o Write the programs for each task (family tree, factorial, GCD/LCD, and Missionaries and
Cannibals problem).
3. Run the Programs:
o Enter the PROLOG environment and execute each program by inputting the queries.
4. Test and Debug:
o Test the programs with various input values to ensure correctness.
5. Analyze the Output:
o Verify the output for each query and ensure it matches expected results.

3.7 Conclusion:
Hence, we have learned how o write simple programs using PROLOG as an AI programming language for
various tasks such as establishing relations in a family tree, factorial calculation, LCD and GCD, and
solving the Missionaries and Cannibals problem.

3.8 Questions:

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-20


UG Program in Artificial Intelligence & Data Science
1. What is PROLOG, and how does it differ from imperative programming languages?
2. Explain the role of facts, rules, and queries in PROLOG.
3. How does backtracking work in PROLOG? Provide an example.
4. Write a PROLOG program to calculate the sum of elements in a list.
5. How is recursion handled in PROLOG? Explain with an example.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-21


UG Program in Artificial Intelligence & Data Science

6. What is unification in PROLOG, and how does it assist in solving logical queries?
7. Describe how the Missionaries and Cannibals problem is solved using PROLOG.
8. Explain the process of finding the GCD and LCD in PROLOG.
9. How would you modify the family tree program to include grandparents and great-grandparents?
10. What are the advantages of using PROLOG for AI applications?

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-22

You might also like