I'm new to prolog. I need yor kind support regarding the following problm.
Given a start state and the end state of the puzzle, the prolog program should be able to print all the moves using BFS(breadth first search). For example,
1 3 2 1 2 3 4 6 5 ->-> 4 5 6 7 x 8 7 8 x
x is the empty tile. The other tiles should be moved up, down, right and left using the empty tile(x),and the final state should be arrived.
As the output, all the moves should be printed.
Thank you very much
--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.
On 5/01/2019, at 21:01, Kuniaki Mukai <kuniak...@gmail.com> wrote:[...]
IMHO, the enumerating all “solutions" for N-puzzle in BFS sounds very hard.
[...]
On Jan 5, 2019, at 13:51, Tharushi Geethma <tharushi...@gmail.com> wrote:
I'm new to prolog. I need yor kind support regarding the following problm.
Given a start state and the end state of the puzzle, the prolog program should
be able to print all the moves using BFS (breadth first search). For example,
1 3 2 1 2 3 4 6 5 ->-> 4 5 6 7 x 8 7 8 x
x is the empty tile. The other tiles should be moved up, down, right and left using the empty tile(x),and the final state should be arrived.
As the output, all the moves should be printed.
Thank you very much
% Common search algorithm
search(Algorithm, Goal, [FringeHead|FringeTail], Solution ):-
is_solution( FringeHead, Goal, Solution).
search(Algorithm, Goal, [FringeHead|FringeTail], Solution):-
update_fringe( Algorithm, FringeHead, FringeTail, UpdatedFringe),
search(Algorithm, Goal, UpdatedFringe, Solution).
% Only expand_fringe differs from bfs and dfs ( And UCS and A*)
update_fringe( bfs, CurrentState, CurrentFringe, UpdatedFringe):-
findall( S, successor(CurrentState, S), Successors), % Optionally add anti-cycle logic
append( CurrentFringe, Successors, UpdatedFringe). % FIFO append
update_fringe(dfs, CurrentState, CurrentFringe, UpdatedFringe):-
findall( S, successor(CurrentState, S), Successors), % TODO: Add anti-cycle logic
append( Successors, CurrentFringe, UpdatedFringe). % LIFO append
% bfs(+Fringe, +Goal, -Solution).
bfs( Fringe, Goal, Solution ):-
member(SomeState, Fringe),
is_solution(SomeState, Goal, Solution). % Base case
bfs( Fringe, Goal, Solution ):-
expand_fringe(Fringe, NewFringe),
bfs(NewFringe, Goal, Solution ). % Recursive case ( I hope this does tail recursion )
% expand_fringe( +OldFringe, -NewFringe)
expand_fringe( OldFringe, NewFringe):-
findall( S, ( member(X, OldFringe), successor(X,S) ), NewFringe).