Explain Code Prolog Work Thank Node Node Node Node State Node State State Action Node A Q43822395
Can you explain this code in Prolog how does it work?
Thank You.
node([Node|_],Node,Node).
state(node(State,_,_),State).
action(node(_,Action,_),Action).
cost(node(_,_,Cost),Cost).
initial_node(node([0,0,0,0],start,0)).
solution([3,_,8,_]).
solution([3,_,_,8]).
solution([_,3,8,_]).
solution([_,3,_,8]).
solution([_,_,3,8]).
solution([_,_,8,3]).
next(node([X,Y,Z,I],_,Cost),node([4,Y,Z,I],fill(verySmall),Newcost)):-X<4,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,7,Z,I],fill(small),Newcost)):-Y<7,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,12,I],fill(big),Newcost)):-Z<12,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,Z,20],fill(veryBig),Newcost)):-I<20,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([0,Y,Z,I],empty(verySmall),Newcost)):-X>0,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,0,Z,I],empty(small),Newcost)):-Y>0,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,0,I],empty(big),Newcost)):-Z>0,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,Z,0],empty(veryBig),Newcost)):-I>0,Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X1,7,Z,I],pour(verySmall,small),Newcost)):-Y<7, X>=7-Y, X1 is X-(7-Y), Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([0,Y1,Z,I],pour(verySmall,small),Newcost)):-Y<7, X<7-Y, Y1 is X+Y, Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([4,Y1,Z,I],pour(small,verySmall),Newcost)):-X<4, Y>=4-X, Y1 is Y-(4-X), Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X1,0,Z,I],pour(small,verySmall),Newcost)):-X<4, Y<4-X, X1 is X+Y, Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y1,12,I],pour(small,big),Newcost)):-Z<12, Y>=12-Z, Y1 is Y-(12-Z), Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,0,Z1,I],pour(small,big),Newcost)):-Z<12, Y<12-Z, Z1 is Y+Z, Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([4,Y,Z1,I],pour(verySmall,big),Newcost)):-X<4, Z>=4-X, Z1 is Z-(4-X), Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X1,Y,0,I],pour(verySmall,big),Newcost)):-X<4, Z<4-X, X1 is X+Z , Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,12,I1],pour(veryBig,big),Newcost)):-Z<12, I>=12-Z, I1 is I-(12-Z), Newcost is Cost+1.
next(node([X,Y,Z,I],_,Cost),node([X,Y,Z1,0],pour(veryBig,big),Newcost)):-Z<12, I<12-Z, Z1 is I+Z , Newcost is Cost+1.
%solve:
solve([Path|_],_,Path):-node(Path,Node,_),state(Node,State),solution(State).
solve([Path|Open],Closed,Solution):-node(Path,Node,_),state(Node,State),+ member(State,Closed),!, expand(Path,NewStates),insert_all(NewStates,Open,NewOpen),solve(NewOpen,[State|Closed],Solution).
solve([_|Open],Closed,Solution):-solve(Open,Closed,Solution).
%expand :
expand(Path,NewPaths):- node(Path,Node,_),findall([NewNode|Path],next(Node,NewNode),NewPaths).
% search:
search:-initial_node(Node),solve([[Node]],[],Sol),reverse(Sol,Solution),show(Solution).
% the show function:
show([]):- nl. show([Node|Reset]):- state(Node,S), action(Node,A),cost(Node,F), write(S),write(‘ ‘), write(A),write(‘ ‘),write(F),write(‘ ‘), nl, show(Reset).
% Breadth-First Search Algorithm
insert_all(NewStates,Fringe,NewFringe):-append(Fringe,NewStates,NewFringe).
% Depth-First Search Algorithm
%insert_all(NewStates,Fringe,NewFringe):-append(NewStates,Fringe,NewFringe).
Expert Answer
Answer to Can you explain this code in Prolog how does it work? Thank You. node([Node|_],Node,Node). state(node(State,_,_),State)….
OR