Explain Code Prolog Work State Node State State Action Node Action Action Cost Node Cost Q43822527
can you explain this code in prolog how does it work ?
state(node(State,_,_),State).
action(node(_,Action,_),Action).
cost(node(_,_,Cost),Cost).
initial_node(node([firstT,firstT,firstT],start,0)).
solution([thirdT,thirdT,thirdT]).
tK(secondT,firstT,thirdT).
w(X,Y,Z):-tK(Y,Z,X).
w(X,Y,Z):-tK(Z,Y,X).
w(X,Y,Z):-tK(Z,X,Y).
w(X,Y,Z):-tK(X,Z,Y).
w(X,Y,Z):-tK(Y,X,Z).
next(node([X,Y,Z],_,Cost),node([X1,Y,Z],move1_WithCost,Newcost)):-w(X,_,X1),X1=X,Newcost is Cost+1.
next(node([X,Y,Z],_,Cost),node([X,Y1,Z],move2_WithCost,Newcost)):-w(X,_,Y1),Y1=X,Y1=Y,Y=X,Newcost is Cost+1.
next(node([X,Y,Z],_,Cost),node([X,Y,Z1],move3_WithCost,Newcost)):-w(X,_,Z1),Z1=X,Z1=Z,Z1=Y,Z=Y,Z=X,Newcostis 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).
% node:
%
node([Node|_],Node,Node).
% 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).
% B F S Algorithm
%
insert_all(NewStates,Fringe,NewFringe):-
append(NewStates,Fringe,NewFringe).
%D F S Algorithm%
insert_all(NewStates,Fringe,NewFringe):-
append(Fringe,NewStates,NewFringe).
Expert Answer
Answer to can you explain this code in prolog how does it work ? state(node(State,_,_),State). action(node(_,Action,_),Action). co…
OR