Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)):-

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).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

1. Organize and support your main points

Answered: 1 week ago

Question

3. Move smoothly from point to point

Answered: 1 week ago

Question

5. Develop a strong introduction, a crucial part of all speeches

Answered: 1 week ago