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

Experiment 5

The document outlines an experiment to solve the Water Jug Problem using SWI-PROLOG. It describes the theory behind the problem, which involves measuring a specific amount of water using two jugs with different capacities, and provides a Prolog code implementation that utilizes a Breadth-First Search (BFS) algorithm to find the solution. The code includes predicates for validating input, performing BFS, and defining possible state transitions.

Uploaded by

jhachandergagan
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)
8 views2 pages

Experiment 5

The document outlines an experiment to solve the Water Jug Problem using SWI-PROLOG. It describes the theory behind the problem, which involves measuring a specific amount of water using two jugs with different capacities, and provides a Prolog code implementation that utilizes a Breadth-First Search (BFS) algorithm to find the solution. The code includes predicates for validating input, performing BFS, and defining possible state transitions.

Uploaded by

jhachandergagan
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/ 2

Experiment – 5

Aim :- write a program to solve water jug problem

Apparatus used :- SWI-PROLOG.

Theory :- The Water Jug Problem involves measuring a specific amount of water
using two jugs of different capacities. The allowed operations include filling a jug
to its full capacity, completely emptying it, or transferring water from one jug to
the other until either the first jug is empty or the second is full. The goal is to
reach a state where at least one jug contains the desired volume of water. This
problem is commonly solved using systematic search techniques, such
as Breadth-First Search (BFS), to explore all possible states and find the
optimal sequence of actions. It is a classic puzzle often associated with the Die
Hard movie scenario, where precise measurements must be achieved with
limited resources.
Code :-
% Main predicate to solve the water jug problem
% Usage: water_jug(4, 3, 2, Solution).
% Returns Solution as a list of states from initial to goal
water_jug(Jug1Cap, Jug2Cap, Target, Solution) :-
% Basic input validation
Jug1Cap > 0,
Jug2Cap > 0,
Target >= 0,
Target =< max(Jug1Cap, Jug2Cap),

% Initialize BFS with starting state


bfs([[(0,0)]], Jug1Cap, Jug2Cap, Target, [], Solution).

% BFS implementation
bfs([[State|Path]|_], _, _, Target, _, [State|Path]) :-
% Check if current state meets target
(State = (Target, _); State = (_, Target)).

bfs([[State|Path]|Rest], Jug1Cap, Jug2Cap, Target, Visited, Solution) :-


% Find all valid next states
findall([NewState,State|Path],
(next_state(State, Jug1Cap, Jug2Cap, NewState),
\+ member(NewState, Visited)),
NewPaths),
% Add new paths to queue
append(Rest, NewPaths, UpdatedQueue),
% Mark current state as visited
bfs(UpdatedQueue, Jug1Cap, Jug2Cap, Target, [State|Visited], Solution).

% All possible state transitions


next_state((J1, J2), Jug1Cap, _, (Jug1Cap, J2)). % Fill Jug1
next_state((J1, J2), _, Jug2Cap, (J1, Jug2Cap)). % Fill Jug2
next_state((_, J2), _, _, (0, J2)). % Empty Jug1
next_state((J1, _), _, _, (J1, 0)). % Empty Jug2

% Transfer from Jug1 to Jug2


next_state((J1, J2), _, Jug2Cap, (NewJ1, NewJ2)) :-
Transfer is min(J1, Jug2Cap - J2),
OUTPUT
EXPERIMENT 5 :-

You might also like