0% found this document useful (0 votes)
13 views20 pages

AI-Lab Manual

Uploaded by

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

AI-Lab Manual

Uploaded by

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

CO1

S.NO DATE NAME OF THE EXPERIMENT SIGNATURE


MARKS

WRITE A PROGRAM IN PROLOG TO


1.
IMPLEMENT SIMPLE FACTS AND QUERIES

WRITE A PROGRAM IN PROLOG TO


2.
IMPLEMENT SIMPLE ARITHMETIC

WRITE A PROGRAM IN PROLOG TO SOLVE


3.
MONKEY BANANA PROBLEM

WRITE A PROGRAM IN PROLOG TO SOLVE


4.
TOWER OF HANOI

WRITE A PROGRAM IN PROLOG TO SOLVE 8


5.
PUZZLE PROBLEMS

WRITE A PROGRAM IN PROLOG TO SOLVE 4-


6.
QUEENS PROBLEM

WRITE A PROGRAM IN PROLOG TO SOLVE


7.
TRAVELING SALESMAN PROBLEM

WRITE A PROGRAM IN PROLOG FOR WATER


8.
JUG PROBLEM

WRITE A PROGRAM TO IMPLEMENT A TIC-


9.
TAC-TOE GAME.

WRITE A PYTHON PROGRAM TO IMPLEMENT


10.
SIMPLE CHATBOT
PSG POLYTECHNIC COLLEGE
COIMBATORE - 641004

R21352 ARTIFICIAL INTELLIGENCE -I


LABORATORY OBSERVATION

Name:……………………………………………………………………
Roll No:…………………………Batch:………………………………..
Semester:……………………………Branch:…………………………

CERTIFIED BONAFIDE LAB WORK DONE BY

Mr./ Miss ………………………………………………………………

Date:………………… STAFF IN-CHARGE


Ex. No :1 WRITE A PROGRAM IN PROLOG TO IMPLEMENT SIMPLE FACT
Date : AND QUERIES

AIM:
To write a program in prolog to implement simple facts and Queries.

PROBLEM DESCRIPTION:

1. The program represents a group of friends and their favourite foods using Prolog facts.
2. It defines friendship relations between individuals.
3. It stores what food each person likes.
4. A rule is created to identify best friends — friends who share at least one common
favourite food.
5. The program allows querying:
o To find who is friends with whom.
o To find what food each person likes.
o To find pairs of friends who have common food preferences (best friends).
6. This helps understand how Prolog uses facts and rules to model relationships and
answer logical queries.

PROGRAM:

% friends.pl

% Simple Facts
friend(alice, bob).
friend(bob, charlie).
friend(charlie, diana).
friend(diana, alice).

likes(alice, chocolate).
likes(bob, chocolate).
likes(charlie, sushi).
likes(diana, ice_cream).

% Rules
best_friend(X, Y) :- friend(X, Y), likes(X, Food), likes(Y, Food).
OUTPUT:

S.No Sample Queries

1. Who is Alice friends with?

?- friend(alice, Who).
2. What does Bob like?

% ?- likes(bob, Food).
3. Who are best friends sharing the same likes?

?- best_friend(X, Y).

RESULT:
Thus a program to implement simple facts and Queries in prolog has been executed
successfully.
Ex. No :2 WRITE A PROGRAM IN PROLOG TO IMPLEMENT SIMPLE ARTHEMETIC
Date: OPERATIONS

AIM:
To perform basic arithmetic operations (addition, subtraction, multiplication, and division)
and demonstrate how Prolog can handle these operations.

PROBLEM DESCRIPTION:

1. The program implements basic arithmetic operations using Prolog predicates.


2. It defines four operations: addition, subtraction, multiplication, and division.
3. Each operation takes two numbers as input and computes the result.
4. The program demonstrates how Prolog can perform calculations using the is operator.
5. Division by zero is not handled in this simple example and should be avoided during
queries.
6. Users can query the program to perform arithmetic calculations and get results
dynamically.

PROGRAM:

% Addition
add(X, Y, Result) :-
Result is X + Y.

% Subtraction
substract(X, Y, Result) :-
Result is X - Y.

% Multiplication
multiply(X, Y, Result) :-
Result is X * Y.

% Division
% Note: Division by zero is not handled in this basic example.
divide(X, Y, Result) :-
Result is X / Y.

OUTPUT:

S.No Sample Queries

1. ?- add(5, 3, Sum).

2.

?- subtract(10, 4, Diff).

3.

?- multiply(6, 7, Prod).

4. ?- divide(15, 3, Quotient).

5. ?- subtract(5, 2, Result).

6. ?- multiply(4, 6, Result).

7. ?- divide(10, 2, Result).
RESULT:
Thus a program to implement simple arithmetic using prolog have been executed
successfully.

Ex.No: 3 WRITE THE PROGRAM IN PROLOG TO SOLVE


Date: MONKEY BANANA PROBLEM

AIM:
To write the program using prolog to find a sequence of actions for the monkey to reach the banana .

PROBLEM DESCRIPTION:

1. The Monkey Banana Problem is a classic example used in Artificial Intelligence and
logic programming to illustrate problem-solving using rules and facts.
2. In this problem, a monkey wants to get a banana hanging from the ceiling but cannot
reach it directly.
3. The monkey can:
o Move around on the floor,
o Push a box under the banana,
o Climb the box,
o And grasp the banana once it reaches it.
4. Prolog models this problem using facts that describe the initial state (locations and
abilities) and rules that describe the possible actions and conditions to succeed.
5. Facts specify where the monkey, box, and banana are, and what the monkey can do.
6. Rules describe:
o When the monkey can push the box,
o When it can climb the box,
o When it can reach the banana,
o And finally, when it can get the banana.
7. Using these facts and rules, Prolog can answer queries like “Can the monkey get the
banana?” by logically deducing a sequence of actions.

PROGRAM:
% Facts
at(monkey, floor).
at(box, floor).
at(banana, ceiling).

strong(monkey).
can_climb(monkey).
can_grasp(monkey).

% Rules
push_box :- strong(monkey), at(monkey, floor), at(box, floor).

climb_box :- can_climb(monkey), at(monkey, floor), at(box, floor).

reach_banana :- push_box, climb_box, at(banana, ceiling).

get_banana :- reach_banana, can_grasp(monkey).

OUTPUT:

S.No Sample Queries

1. Where is the monkey?

?- at(monkey, Location).

2. Where is the box?

?- at(box, Location).

3. Where is the banana?

?- at(banana, Location).

4. Is the monkey strong?

?- strong(monkey).

5. Can the monkey climb?


?- can_climb(monkey).

6. Can the monkey grasp?

?- can_grasp(monkey).

7. Can the monkey push the box?

?- push_box.

8. Can the monkey climb the box?

?- climb_box.

9. Can the monkey reach the banana?

?- reach_banana.

10. Can the monkey get the banana?

?- get_banana.

RESULT:
Thus a program using prolog to find a sequence of actions for the monkey to reach the
banana is successfully executed .

Ex.No: 4
Write the program in prolog to solve tower of Hanoi
Date:
AIM:
To write a program in Prolog to solve the Tower of Hanoi problem using recursion.

PROBLEM DESCRIPTION:

1. The program simulates the Tower of Hanoi puzzle.


2. The problem involves moving a stack of disks from one rod to another, using a helper
rod.
3. The following rules must be followed:
o Only one disk can be moved at a time.
o Only the top disk of any rod can be moved.
o No disk may be placed on top of a smaller disk.
4. The program uses recursive logic to:
o Move N-1 disks to the helper rod.
o Move the largest disk to the target rod.
o Move the N-1 disks from the helper rod to the target rod.
5. It prints and tracks each move required to solve the puzzle.
6. This helps understand how Prolog uses recursion and list manipulation to solve
problems.

PROGRAM:
% hanoi.pl

% Base Case: Move 1 disk


hanoi(1, From, To, _, [move(From, To)]) :-
write('Move top disk from '), write(From), write(' to '), write(To), nl.

% Recursive Case: Move N disks


hanoi(N, From, To, Via, Moves) :-
N > 1,
N1 is N - 1,
hanoi(N1, From, Via, To, Moves1),
hanoi(1, From, To, _, [move(From, To)]),
hanoi(N1, Via, To, From, Moves2),
append(Moves1, [move(From, To)|Moves2], Moves).
OUTPUT:

S.
Sample Queries Description
No
?- hanoi(2, left, right, Solves Tower of Hanoi for 2 disks and
1.
center, Moves). displays the steps
Solves Tower of Hanoi for 3 disks with
2. ?- hanoi(3, a, c, b, M).
rods a, b, c

RESULT:
Thus, a Prolog program to solve the Tower of Hanoi puzzle using recursion has been successfully
executed and verified.
Ex.No: 5
Write the program in prolog to solve 8 puzzle problems
Date:

AIM:
To solve the 8-puzzle using the A* search algorithm in Prolog, combining cost-so-far (g)
and heuristic (h) for optimal moves.

PROBLEM DESCRIPTION
The 8-Puzzle Problem is a classic sliding puzzle that consists of a 3×3 grid containing 8
numbered tiles and one blank space (represented as 0). The objective is to move the tiles
into a specified goal configuration by sliding the blank space up, down, left, or right to
swap with an adjacent tile.
The puzzle starts in an initial state and the player must reach the goal state using the
minimum number of moves.
To solve this puzzle efficiently, we use the A* (A-star) search algorithm. It combines:
● g(n): the cost (number of moves) from the start to the current state
● h(n): the heuristic estimate of the cost from the current state to the goal (in this case, the
Manhattan distance)
● f(n) = g(n) + h(n): the total estimated cost of a solution path going through the current node
The algorithm maintains:
● An Open List (priority queue) of nodes to explore, ordered by the lowest f(n)
● A Closed Set of already-explored states to avoid repeated work
The A* search continues expanding the most promising paths until it finds the goal
configuration.
Example:
● Initial State: [1, 2, 3, 4, 0, 5, 6, 7, 8]
● Goal State: [1, 2, 3, 4, 5, 6, 7, 8, 0]

PROGRAM:

% Goal state
goal([1,2,3,4,5,6,7,8,0]).

% Move: Swap blank (0) with adjacent tile


move([A,B,0,D,E,F,G,H,I], [A,0,B,D,E,F,G,H,I]). % left
move([A,0,C,D,E,F,G,H,I], [0,A,C,D,E,F,G,H,I]). % left
move([A,B,C,D,E,0,G,H,I], [A,B,C,D,0,E,G,H,I]). % left
move([A,B,C,D,0,F,G,H,I], [A,B,C,0,D,F,G,H,I]). % left
move([A,B,C,D,E,F,0,H,I], [A,B,C,D,E,F,H,0,I]). % right
move([A,B,C,D,E,F,G,0,I], [A,B,C,D,E,F,0,G,I]). % right
move([A,B,C,0,E,F,G,H,I], [0,B,C,A,E,F,G,H,I]). % up
move([A,B,C,D,0,F,G,H,I], [A,B,C,0,D,F,G,H,I]). % up
move([A,B,C,D,E,F,0,H,I], [A,B,C,D,E,F,G,0,I]). % down

% Solve using simple recursive search (not full A*, simplified)


solve(State) :-
goal(State),
write('Already at goal state!'), nl.

solve(State) :-
move(State, Next),
goal(Next),
write('Move: '), write(State), nl,
write('→ '), write(Next), nl,
write('Goal reached!'), nl.

solve(State) :-
move(State, Next),
write('Trying move: '), write(State), nl,
solve(Next).

OUTPUT:

S.No Sample Queries

1. solve([1,2,3,4,5,6,7,8,0]).

2. goal([1,2,3,4,5,6,7,8,0]).

3. move([1,2,3,4,0,5,6,7,8], Next).

RESULT:
Thus, the program to solve the 8-Puzzle problem using the A* search algorithm in Prolog has
been successfully executed.
Ex.No: 6
Write a program in prolog to solve 4 queens problem
Date:

AIM:
To write a program in Prolog to solve the 4-Queens Problem using backtracking.

PROBLEM DESCRIPTION:
1. The 4-Queens problem involves placing 4 queens on a 4×4 chessboard such that:
o No two queens are in the same row.
o No two queens are in the same column.
o No two queens are on the same diagonal.
2. The solution is represented as a list of 4 numbers, where the index is the row and the number
is the column position of the queen in that row.

PROGRAM:

% Entry point
four_queens(Solution) :-
Solution = [Q1, Q2, Q3, Q4],
permutation([1, 2, 3, 4], Solution),
safe(Solution).

% Check all queens are placed safely


safe([]).
safe([Q|Others]) :-
safe(Others),
no_attack(Q, Others, 1).

% Ensure no queen attacks another diagonally


no_attack(_, [], _).
no_attack(Q, [Q1|Rest], D) :-
Q =\= Q1 + D,
Q =\= Q1 - D,
D1 is D + 1,
no_attack(Q, Rest, D1).
OUTPUT:

S.No Sample Queries

1. ?- four_queens(S).

2. four_queens(Solution).

3. safe([2,4,1,3]).

RESULT:
Thus, the Prolog program to solve the 4-Queens Problem using backtracking and constraint
satisfaction has been successfully executed and tested.

Ex.No: 7
Write a program in Prolog to solve travelling salesmen problem
Date:

AIM:
To write a program in Prolog to solve the Travelling Salesman Problem (TSP) using brute-force
path generation and distance calculation.

PROBLEM DESCRIPTION:

The Travelling Salesman Problem (TSP) is a classic optimization problem in which a salesman
needs to visit a list of cities exactly once and return to the starting city. The goal is to find the
shortest possible route that visits each city once and returns to the origin.

In this Prolog program:

1. We represent cities and the distances between them using facts.

2. The program generates all possible permutations of city routes using backtracking.

3. For each possible path, it calculates the total distance.

4. The program returns all valid paths along with their total travel cost.

5. Optionally, we can find the shortest route by comparing all costs.

This problem helps demonstrate the use of brute-force search, recursion, list processing, and
backtracking in Prolog.

PROGRAM:

% Distance between cities

distance(a, b, 10).

distance(a, c, 15).

distance(a, d, 20).

distance(b, c, 35).

distance(b, d, 25).

distance(c, d, 30).

% To handle both directions

distance(X, Y, D) :- distance(Y, X, D), X \= Y.

% Calculate total distance of a path

path_cost([_], 0).
path_cost([City1, City2 | Rest], Cost) :-

distance(City1, City2, D),

path_cost([City2 | Rest], RestCost),

Cost is D + RestCost.

% TSP main predicate

tsp(Path, Cost) :-

Cities = [b, c, d],

permutation(Cities, Perm),

Path = [a | Perm],

append(Path, [a], FullPath),

path_cost(FullPath, Cost).

OUTPUT:

S.No Sample Queries

1. tsp(Path, Cost).

2. setof(Cost-Path, tsp(Path, Cost), Results).


RESULT:

Thus, the Prolog program to solve the Travelling Salesman Problem has been executed
successfully and the shortest path with total cost was obtained.

Ex.No: 8
Write a program in Prolog for water jug problem
Date:
AIM:

To write a Prolog program to solve the Water Jug Problem using state transitions and backtracking.

PROBLEM DESCRIPTION:

Given two jugs with different capacities (say, 4 liters and 3 liters) and no measuring scale, the task is
to measure exactly 2 liters of water. The solution is found by applying a sequence of
operations: fill, empty, or pour from one jug to the other.

This Prolog program explores all possible states and applies operations to reach the goal.

PROGRAM:

% Goal: Get 2 liters in 4-liter jug

solve :-

move(0, 0).

% Goal state

move(2, _) :-

write('Reached goal: (2, _)'), nl.

% Possible moves

move(A, B) :-

write('Current state: ('), write(A), write(', '), write(B), write(')'), nl,

A < 4 -> move(4, B); % Fill 4L jug

B < 3 -> move(A, 3); % Fill 3L jug

A > 0 -> move(0, B); % Empty 4L

B > 0 -> move(A, 0); % Empty 3L

A > 0, B < 3 -> T is min(A, 3-B), A1 is A - T, B1 is B + T, move(A1, B1); % Pour 4->3


B > 0, A < 4 -> T is min(B, 4-A), A1 is A + T, B1 is B - T, move(A1, B1)

).

OUTPUT:

S.No Sample Queries

1. ?- solve.

2. ?- move(0, 0).

RESULT :

Thus, the Prolog program to solve the Water Jug Problem has been executed successfully and
the goal state (2, _) has been reached.

You might also like