How to implement a solve predicate for this “moving blocks” Prolog exercise?

后端 未结 1 1866
闹比i
闹比i 2021-01-23 11:02

I am studying Prolog using Ivan Bratko book: Programming for Artificial Intelligence and I am finding some difficulties to implement the final part of an exercise proposed

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 11:21

    There will be loops in space search graph, then you can switch to some form of bound search. The easier I know it's bounded depth search:

    ?- length(Situation,_), solve([[c,a,b],[],[]], Situation).
    Situation = [[[c, a, b], [], []], [[a, b], [c], []], [[b], [a], [c]], [[], [b, c], [a]], [[], [a, b|...], []]] .
    

    length/2 enumerates unbound lists of growing length. So we get a result.

    Note that this will still loop if there are no solutions from initial state to goal/1. If this is bad, I think solve/2 will need a service solve2/2 predicate, that will get the path, to enable the usual \+ memberchk(NewState, Visited) after the nondeterministic s/2 call. Then it will be (untested)

    solve(N, SearchPath) :- solve2([N], SearchPath).
    
    solve2([N|Visited], [N|Visited]) :- goal(N).
    solve2([N|Visited], Path) :-
       s(N,N1),
       \+ memberchk(N1, Visited),
       solve2([N1,N|Visited], Path).
    

    0 讨论(0)
提交回复
热议问题