some doubts about a Prolog program that use a moves graph to move blocks on 3 stack -


i studying prolog using ivan bratko book: programming artificial intelligence , finding problems trying understand how work exercise use graphs decide how move blocks , arrange them in order.

this image related program have do:

enter image description here

as can see in previous image blocks a,b,c can moved using number of admissible moves are:

  • a block can moved if @ top of stack
  • a block can moved on ground (on void stack)
  • a block can moved on block (at top of stack contains others blocks)

so these admissible moves induce graph of possible transitions between 1 state , state in graph, this:

enter image description here

so, can se in previous graph can represent situation using list of 3 sublist.

each sublist represent stack can put blocks according previous constraints

so example situation of central node of previous graph can represented by:

[[a], [b], [c]] 

because each stack contain single block

the situation represented node @ top left stacked 1 below other blocks c,a,b can represented by:

[[c,a,b], [], []] 

because blocks in single stack (the first one)

now, have write predicate s(situation1, situation2) true if situation1 , situation1 2 representations of world of blocks (like previous) , there legal move allowed between situazion1 , situazion2.

so represent thing in following using series of facts (this thing shown on slides of teacher , not on bratko book:

s([[a|ra],b,c],[ra,[a|b],c]). s([[a|ra],b,c],[ra,b,[a|c]]). … 

i interpret in way: have 2 stacks:

situation1 = [a|ra], b, c] ra rest of first stack without block (in case void because situation in have 1 block in each stacks)

situation2 = [ra,[a|b],c] situation first stack void (ra), second stack old second stack block on top , third stack contains c block

so represents legal transition...so declare series of facts represents explicitly possibles transitions.

but not idea (i can have many transitions encode in facts) on bratko book implement programmatically s(situation1, situation2) predicate in way:

/* base case: if delete x list , x head of list, newlist               tail of list */ del(x, [x|tail], tail).  /* general case: if head of list not x program have delete                  x in tail of list */ del(x, [y|tail], [y|tail1]) :- del(x, tail, tail1).   s(stacks, [stack1, [top1|stack2] | otherstacks]) :-                                   del([top1|stack1], stacks, stacks1),                                  del(stack1, stacks1, otherstacks).   goal(situation) :- member([a,b,c], situation).  solve(n,[n]) :- goal(n). 

the del/3 predicate simple (simply delete x item list) , there not interesting.

i have problem understand how work s/2 predicate calculate legal moves.

on book say:

the successor relation can programmed according following rule: situation2 successor of situation1 if there 2 stacks: stack1 , stack2 in situation1 , top block of stack1 can moved on stack2

intuitively not difficult me because appear clear move legal if can take block top of 1 of 3 stack , can put on top of stack (also void stack, corresponds situation in put block on ground)

but finding many difficult understand previous code of s/2 predicate when write:

s(stacks, [stack1, [top1|stack2] | otherstacks]) :-                                   del([top1|stack1], stacks, stacks1),                                  del(stack1, stacks1, otherstacks). 

i have difficult understand how read , when use del/3 predicate , represent stacks, stack1, stack2, stacks1 variable

you ask, following mean:

s(stacks, [stack1, [top1|stack2] | otherstacks]) :-                              del([top1|stack1], stacks, stacks1),                              del(stack1, stacks1, otherstacks). 

we can read thus: s/2 of course "step" (or "move", or "successor") relationship. have stacks list of stacks, begin with. 2nd param our result, of moving 1 block somehow among them.

since del backtracking predicate, s. so, produce results 1 one. here declarative reading helpful. :)

we read body thus: there a = [top1|stack1] among stacks, turn stacks1 a removed them. if had 3 stacks, we'll have 2 stacks1, , stack a, of first - top1 - element obviously, top block on stack (and stack1 stump, rest of stack). iow, pick stack a among 3 stacks, , rest stacks1. top block on a top1.

on next line. :) if delete stump stack1 2 stacks1, we'd otherstacks. that's nonsense, know stump not among stacks1. that's error (a "typo").

how correct it? our goal place top1 on 1 of stacks in stacks1, let's that:

    del(a,stacks1,b), result = [ stack1 ,         % stump                                  [top1 | a] |     % move top block                                  b]. 

which our 2nd argument, renaming of variables: a = stack2, b = otherstacks. so, correct second line is

                         %   del(stack1, stacks1, otherstacks).   % error                              del(stack2, stacks1, otherstacks).   % correct 

just in capellic's answer.

capellic right, naming horrible, :) it's easy mis-type. a, b, c better.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -