0% found this document useful (0 votes)
15 views7 pages

Prolog Code

Uploaded by

Kaushal
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)
15 views7 pages

Prolog Code

Uploaded by

Kaushal
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/ 7

Make a production system and write Prolog Code to solve these problems in details –

a. Water jug,
b. Chess,
c. Tic tac toe
d. 7 bridge problem and
e. 8/15 queen problem

PROLOG CODE:-
% State: jug1_amount, jug2_amount

% Operations: fill, empty, pour

% Initial state

Initial_state(0, 0).

% Goal state (for example, we want 2 liters in the first jug)

Goal_state(2, _).

% Transition rules (operations)

Fill1(0, Jug2, 4, Jug2). % Fill jug1 (max capacity 4)

Fill2(Jug1, 0, Jug1, 3). % Fill jug2 (max capacity 3)

Empty1(Jug1, Jug2, 0, Jug2). % Empty jug1

Empty2(Jug1, Jug2, Jug1, 0). % Empty jug2

Pour1to2(Jug1, Jug2, NewJug1, NewJug2) :-

Jug1 > 0, % There is water in jug1

Jug2 < 3, % jug2 is not full

Pour is min(Jug1, 3 – Jug2),

NewJug1 is Jug1 – Pour,

NewJug2 is Jug2 + Pour.

Pour2to1(Jug1, Jug2, NewJug1, NewJug2) :-

Jug2 > 0, % There is water in jug2

Jug1 < 4, % jug1 is not full

Pour is min(Jug2, 4 – Jug1),

NewJug1 is Jug1 + Pour,


NewJug2 is Jug2 – Pour.

% Solve the problem

Solve(Jug1, Jug2, Solution) :-

Goal_state(Jug1, Jug2),

Write(Solution).

Solve(Jug1, Jug2, Solution) :-

( fill1(Jug1, Jug2, NewJug1, NewJug2) ;

Fill2(Jug1, Jug2, NewJug1, NewJug2) ;

Empty1(Jug1, Jug2, NewJug1, NewJug2) ;

Empty2(Jug1, Jug2, NewJug1, NewJug2) ;

Pour1to2(Jug1, Jug2, NewJug1, NewJug2) ;

Pour2to1(Jug1, Jug2, NewJug1, NewJug2) ),

Solve(NewJug1, NewJug2, [Operation | Solution]),

Write([Operation | Solution]).

CHESS
% Position: (X, Y), Direction: ‘up’ or ‘down’

Move_pawn(X, Y, ‘up’, NewX, NewY) :- NewX is X, NewY is Y + 1.

Move_pawn(X, Y, ‘down’, NewX, NewY) :- NewX is X, NewY is Y – 1.

% Example for pawn movement

% To move a pawn from (2,1) ‘up’

Move_example(NewX, NewY) :- move_pawn(2, 1, ‘up’, NewX, NewY).

TIC TAC TOE


% Check if a player has won

Win(Board, Player) :-

( Board = [Player, Player, Player, _, _, _, _, _, _] ; % Row 1


Board = [_, _, _, Player, Player, Player, _, _, _] ; % Row 2

Board = [_, _, _, _, _, _, Player, Player, Player] ; % Row 3

Board = [Player, _, _, Player, _, _, Player, _, _] ; % Column 1

Board = [_, Player, _, _, Player, _, _, Player, _] ; % Column 2

Board = [_, _, Player, _, _, Player, _, _, Player] ; % Column 3

Board = [Player, _, _, _, Player, _, _, _, Player] ; % Diagonal 1

Board = [_, _, Player, _, Player, _, Player, _, _] ). % Diagonal 2

% Check for a draw (no more moves possible)

Draw(Board) :-

\+ member(_, Board).

% Example usage

Example_game(Board) :- win(Board, ‘X’).

7 BRIDGES PROBLEM
% Representing the graph: Bridge(a, b) means there's a bridge between a and b.

bridge(a, b).

bridge(b, c).

bridge(c, d).

bridge(d, a).

bridge(a, e).

bridge(e, c).

bridge(c, a).

% Eulerian path exists if each vertex has an even degree, or exactly two vertices have an
odd degree
degree(Vertex, Degree) :- findall(_, bridge(Vertex, _), L), length(L, Degree).

% Check if the number of odd-degree vertices is 0 or 2

eulerian_path :-

findall(Vertex, degree(Vertex, Degree), Vertices),

include(odd_degree, Vertices, OddDegreeVertices),

length(OddDegreeVertices, Count),

(Count = 0 ; Count = 2).

odd_degree(Vertex) :- degree(Vertex, Degree), Degree mod 2 =\= 0.

8 QUEENS PROBLEM
% The 8 queens problem

Place_queens(Queens) :-

Queens = [Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8],

Q1 > 0, Q1 =< 8,

Q2 > 0, Q2 =< 8, Q2 \= Q1,

Q3 > 0, Q3 =< 8, Q3 \= Q1, Q3 \= Q2,

Q4 > 0, Q4 =< 8, Q4 \= Q1, Q4 \= Q2, Q4 \= Q3,

Q5 > 0, Q5 =< 8, Q5 \= Q1, Q5 \= Q2, Q5 \= Q3, Q5 \= Q4,

Q6 > 0, Q6 =< 8, Q6 \= Q1, Q6 \= Q2, Q6 \= Q3, Q6 \= Q4, Q6 \= Q5,

Q7 > 0, Q7 =< 8, Q7 \= Q1, Q7 \= Q2, Q7 \= Q3, Q7 \= Q4, Q7 \= Q5, Q7 \= Q6,

Q8 > 0, Q8 =< 8, Q8 \= Q1, Q8 \= Q2, Q8 \= Q3, Q8 \= Q4, Q8 \= Q5, Q8 \= Q6, Q8 \=


Q7,

Safe_queens(Queens).

% Check if no two queens threaten each other

Safe_queens([Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8]) :-

Safe(Q1, Q2, 1), safe(Q1, Q3, 2), safe(Q1, Q4, 3),

Safe(Q1, Q5, 4), safe(Q1, Q6, 5), safe(Q1, Q7, 6), safe(Q1, Q8, 7),
% Add similar checks for other pairs...

Safe(Qx, Qy, D) :- abs(Qx – Qy) =\= D. % Queens cannot be in the same diagonal.

Explain the following about BFS and DFS:

a. Define
b. Algorithm
c. Example with diagram
d. Space and time complexity
e. Prolog code
f. Dfs(Start, Goal) :- dfs_helper([Start], Goal).
PROLOG CODE FOR DEPTH FIRST SEARCH

Dfs_helper([Goal|_], Goal).

Dfs_helper([Start|Rest], Goal) :-

Findall(Neighbor, edge(Start, Neighbor), Neighbors), Append(Neighbors, Rest,


NewStack),

Dfs_helper(NewStack, Goal).
% Example edges: edge(From, To).

Edge(a, b).

Edge(a, c).

Edge(b, d).
Edge(c, e).

% Query: dfs(a, d).

CODE FOR BREADTH FIRST SEARCH

Bfs([Start|_], Goal) :- Start = Goal.

Bfs([Start|Rest], Goal) :-

Start \= Goal,

Findall(Neighbor, edge(Start, Neighbor), Neighbors),

Append(Rest, Neighbors, NewQueue),

Bfs(NewQueue, Goal).

% Example edges: edge(From, To).


Edge(a, b).

Edge(a, c).

Edge(b, d).

Edge(c, e).

% Query: bfs([a], d).

SIMPLE REFLEX AGENT

PROLOG CODE :-

% Action rules

Action(temperature(too_cold), wear_coat).

Action(temperature(too_hot), remove_coat).

% React based on perception

React(Perception) :- action(Perception, Action), write(Action).

MODEL BASED AGENT

PROLOG CODE :-

% State transition rules

Action(state(freezing), wear_coat).

Action(state(warm), remove_coat).

% React based on the internal model

React(State, Perception) :- action(State, Action), write(Action).

GOAL BASED AGENT

PROLOG CODE :-

% Goal definition

Goal(need_to_warm).

% Action rules

Action(temperature(too_cold), wear_coat).

% React based on the goal


React(Perception) :- goal(_), action(Perception, Action), write(Action).

UTILITY BASED AGENT

PROLOG CODE :-

% Utility function for actions

Utility(temperature(too_cold), 5).

Utility(temperature(too_hot), 1).

% React based on maximum utility

React(Perception) :- utility(Perception, Utility), Utility > 3, write(wear_coat).

LEARNING AGENT

PROLOG CODE :-

% Learn and record actions

Learn(Perception) :- assert(learned_action(Perception, wear_coat)).

% React based on learned actions

React(Perception) :- learned_action(Perception, Action), write(Action).

You might also like