prolog - Solving a specific loop -


near(a1,b1).    near(b1,c1).    near(c1,d1).    near(d1,e1).    near(e1,f1). near(f1,g1).    near(a1,a2).    near(a2,a3).    near(a3,a4).    near(a4,a5). near(a5,a6).    near(a6,a7).    near(a7,a8).    near(a8,a9).    near(b1,b2). near(b2,b3).    near(b3,b4).    near(b4,b5).    near(b5,b6).    near(b6,b7). near(b7,b8).    near(b8,b9).    near(c4,d4).    near(d1,e4).    near(e4,f4). near(f4,g4).    near(g4,h4).    near(g1,g2).    near(g2,g3).    near(g3,g4). near(f4,f5).    near(f5,f6).    near(f6,f7).    near(f7,f8).    near(f8,f9). near(a6,b6).    near(b6,c6).    near(a9,b9).    near(b9,c9).    near(c9,d9). near(d9,f9).    near(f9,g9).    near(g9,h9).    near(h9,i9).  path(x,x). path(x,y):-    near(x,z), path(z,y),    write(x),    write('->'),    write(z).  maze(x,y):-findall(x,path(x,y),path_list),write(path_list). 

this finding way maze.

i want print out this

 route1 [18] a1 -> b1 -> c1 -> d1 -> e1 -> f1 -> g1 -> g2 -> g3 -> g4 -> h4 -> h5 -> h6 -> h7 -> h8 -> h9 -> i9 

and showing other 4 routes.

i think using cut? or backtracking. can't touch code..

generally, writing out intermediate steps backtracking algorithm takes solve problem very useless technique. of output produced path/2 second clause 'noise'.

this motivation leads trace/0 primary debug tool prolog. traces carry additional information, nesting level, indispensable make sense of output.

then suggest rid of write(s) in path/2, add argument 'carrying back' path, , write service predicate print:

path(x,x, [x]). path(x,y, [x|cs]):-     near(x,z), path(z,y, cs).  maze(x,y):- forall(path(x,y,ps),writeln(ps)).  ?- maze(a1,i9). [a1,b1,c1,d1,e4,f4,f5,f6,f7,f8,f9,g9,h9,i9] [a1,b1,b2,b3,b4,b5,b6,b7,b8,b9,c9,d9,f9,g9,h9,i9] [a1,a2,a3,a4,a5,a6,a7,a8,a9,b9,c9,d9,f9,g9,h9,i9] [a1,a2,a3,a4,a5,a6,b6,b7,b8,b9,c9,d9,f9,g9,h9,i9] true. 

not attractive, surely more useful.

note path/3 (as path/2) will loop forever in presence of loops...

edit using pqgraphviz, snippet

maze_g(x,y) :-     graph_window(maze_g(x,y),[]).  maze_g(x,y,g) :-     forall(path(x,y,ns),         (maplist(make_node(g),ns,ps)         ,nodes_chain(ps,cs)         ,make_edges(g,cs))). 

followed ?- maze_g(a1,i9)., yields

a basic pqgraphviz rendering paths


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -