0% found this document useful (0 votes)
7 views5 pages

9589 AI Exp6

AI Exp6

Uploaded by

Mudabbir Bhat
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)
7 views5 pages

9589 AI Exp6

AI Exp6

Uploaded by

Mudabbir Bhat
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/ 5

Fr.

Conceicao Rodrigues College of Engineering, Mumbai

Artificial Intelligence

Academic Term: 2023-24


Practical No: 6

Title: Implementation of 8-puzzle problem using


heuristic algorithm
Date of Performance: 5/3/24

Date of Submission: 12/3/24

Roll No: 9589

Name of the Student: Mudabbir Bhat

Rubrics for Evaluation:


Performance Indicator Excellent Good Tota
Sr.
Below l
N
Average Scor
o
e
1 On time NA 00 (Not
Completion & 02 (On on
Submission (02) Time ) Time)
2 Logic/Theory 03(Correc NA 01
understanding(0 t) (Tried)
2)
3 Coding Standards (05): 05(All 03 01
Comments/indention/Na used) (Partia (rarely
min l) followe
g conventions d)
Debugging/
Output/Test Cases

Total
Signature of the Teacher
AI EXPERIMENT 6
CODE:

target( [[ 1 , 2 , 3],
[ 4 , 5 , 6],
[ 7 , 8 , _]] ).

from( [[1 , 2 , 3],


[ _ , 4 , 6],
[7 , 5 , 8]]).

% A *backtrackable* predicate which proposes a new position


(RowNew,ColNew)
% for the hole at position (Row,Col). The hole is moved in direction
% MoveDirection
% This is not as nice as pattern matching over a pair of
% states because you can't make it run "backwards" to determine a %
move and input matrix from an output matrix.

% new_hole_position(Row,Col,RowNew,ColNew,MoveDirection)

new_hole_position(Row,Col,RowNew,Col,down) :- Row < 2, RowNew is


Row+1.
new_hole_position(Row,Col,RowNew,Col,up) :- Row > 0, RowNew is
Row-1.
new_hole_position(Row,Col,Row,ColNew,right) :- Col < 2, ColNew is
Col+1.
new_hole_position(Row,Col,Row,ColNew,left) :- Col > 0, ColNew is Col-1.

% Pick the value at (Row,Col) from MatrixIn into ValOld and


% Put ValNew at (Row,Col), giving MatrixOut. This is used to %
generate a new state from an existing state and a "hole move".

pick_and_put_matrix(Row,Col,MatrixIn,ValOld,ValNew,MatrixOut) :-
pick_and_put(Row,MatrixIn,RowlistOld,RowlistNew,MatrixOut),
pick_and_put(Col,RowlistOld,ValOld,ValNew,RowlistNew).

pick_and_put(Index,ListIn,ValOld,ValNew,ListOut) :-
length(Prefix,Index), append([Prefix,[ValOld],Suffix],ListIn),
append([Prefix,[ValNew],Suffix],ListOut),
!.

% Moving the hole from (Row,Col) to (RowNew,ColNew)


move_hole(Row,Col,RowNew,ColNew,MatrixIn,MatrixOut) :-
pick_and_put_matrix(Row,Col,MatrixIn,[],Val,MatrixMid),
pick_and_put_matrix(RowNew,ColNew,MatrixMid,Val,[],MatrixOut).

% Find out where the hole is in MatrixIn as we don't %


keep track of that information.

cur_hole_position(Row,Col,MatrixIn) :-
nth0(Row,MatrixIn,RowList),
cur_hole_position_in_row(Col,RowList),!.

cur_hole_position_in_row(Col,RowList) :-
nth0(Col,RowList,[]).

% For showing off, the number of states visited is counted in % a


thread-local variable that is non-backtrackably incremented.

nb_inc_counter :-
nb_getval(counter,X), XX
is X+1,
nb_setval(counter,XX).

% The search proper. Perform a single move from one state (matrix)
% to the next state (matrix)
%
% move(+CurrentState,+GoalState,
% -SolutionAsGrowingOpenListToWhichOneAppends
% +StatesOnPathSoAsToNotVisitAStateTwiceToWhichOnePrepends,
% +DepthCountdownForIterativeDeepening)

move(Matrix,Matrix,[],_,_) :- !.
move(MatrixIn,MatrixTarget,[MatrixMid|Moves],MatrixesOnPath,Depth)
:-
Depth > 1,
nb_inc_counter,
cur_hole_position(Row,Col,MatrixIn),
new_hole_position(Row,Col,RowNew,ColNew,_MoveDirection),
move_hole(Row,Col,RowNew,ColNew,MatrixIn,MatrixMid),
\+ member(MatrixMid,MatrixesOnPath),
SmallerDepth is Depth-1,

move(MatrixMid,MatrixTarget,Moves,[MatrixMid|MatrixesOnPath],Small
erDepth).
% Printout curclicues

print_and_reset_counter :-
nb_getval(counter,C), (C>0
-> format("Examined ~d positions~n",[C])
; true),
nb_setval(counter,0).

format_moves([Matrix],_) :-
format_matrix(Matrix).
format_moves([Matrix,Matrix2|Moves],Index) :-
format_matrix(Matrix), format("Move
~d~n",[Index]), Index2 is Index+1,
format_moves([Matrix2|Moves],Index2).

format_matrix([[A,B,C],[D,E,F],[G,H,I]]) :-
enlarge(A,AE), enlarge(B,BE),
enlarge(C,CE), enlarge(D,DE),
enlarge(E,EE), enlarge(F,FE),
enlarge(G,GE), enlarge(H,HE),
enlarge(I,IE), format("+--------+~n",[]),
format("|~s,~s,~s|~n",[AE,BE,CE]),
format("|~s,~s,~s|~n",[DE,EE,FE]),
format("|~s,~s,~s|~n",[GE,HE,IE]),
format("+--------+~n",[]).

enlarge(X,XE) :-
format(string(S)," ~q",[X]),
sub_string(S,_,2,0,XE).

% "Main" predicate.

run(Moves) :-
from(MatrixFrom),
target(MatrixTarget),
nb_setval(counter,0),
between(1,30,MaxDepth), % backtrackable; iterative deepening
print_and_reset_counter,
format("Maximum depth is ~d~n",[MaxDepth]),
move(MatrixFrom,MatrixTarget,Moves,[MatrixFrom],MaxDepth),
announce_success([MatrixFrom|Moves]).
announce_success(Moves) :-
length(Moves,Length), AdjustedLength is Length-
1, nb_getval(counter,C),
format("Found a solution of ~d moves by examination of ~d
positions.~n",[AdjustedLength,C]),
format_moves(Moves,1)

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy