How to make Prolog depth-first-search algorithm go deeper into the tree? (Applied to Sokoban) -
i'm trying solve sokoban puzzle in prolog using depth-first-search algorithm, cannot manage search solution tree in depth. i'm able explore first level.
all sources @ github (links revision when question asked) feel free explore , test them. divided rules several files:
- board.pl: contains rules related board: directions, neighbourhoods,...
- game.pl: file states rules movements, valid positions,...
- level1.pl: defines board, position of boxes , solution squares sample game.
- sokoban.pl: tries implement dfs :(
i know need go deeper when new state created instead of checking if final state , backtracking... need continue moving, impossible reach final state 1 movement.
any help/advice highly appreciated, i've been playing around without improvements.
thanks!
ps.- ¡ah! i'm working swi-prolog, in case makes difference
ps.- i'm newbie prolog, , maybe i'm facing obvious mistake, reason i'm asking here.
this easy fix: in sokoban.pl
, predicate solve_problem/2
, limiting solution lists of single element in goal:
solve_dfs(problem, initial, [initial], [solution])
instead, mean:
solve_dfs(problem, initial, [initial], solution)
because solution can consist of many moves.
in fact, better search strategy iterative deepening, with:
length(solution, _), solve_dfs(problem, initial, [initial], solution)
iterative deepening complete search strategy , optimal strategy under quite general assumptions.
other that, recommend cut down significant number of impure i/o calls in program. there many predicates write on screen.
instead, focus on clear declarative description, , cleanly separate output description of solution looks like. in fact, let toplevel printing you: describe solution looks (you doing this), , let toplevel display solution variable bindings. also, think declaratively, , use better names dfs_moves/4
, problem_solution/2
instead of solve_dfs/4
, solve_problem/2
etc.
dcgs may in places of code more conveniently describe lists.
+1 tackling nice , challenging search problem prolog!
Comments
Post a Comment