Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Depth limited search in prolog (vanilla meta-interpreter)

I need to modify the vanilla meta-interpreter in order to make a search with limited depth. I'm using the following code for testing my sollution:

value(wire1,1).
connected(wire2, wire1).
connected(wire3, wire2).
connected(wire4, wire3).
connected(wire5, wire4).
connected(wire6, wire5).
connected(wire7, wire6).
connected(wire8, wire7).
connected(wire9, wire8).
value(W,X):-connected(W,V), value(V,X).

And the target is that something like:

solve(value(w9,X), 3).     /*depth =3, it should return false*/
solve(value(w9,X), 20).    /*depth=20 is enought for returning X=1*/

By the way my code is

solve(true,_):-!.
solve((A,B),D) :-!, solve(A,D), solve(B,D).
solve(A,D) :- clause(A, B),solve(B,D2),D=D2+1,D>0).

But it don't work property. Can you help me? Thanks a lot in advance

like image 427
Caroline Avatar asked Oct 23 '11 19:10

Caroline


1 Answers

An interesting page on metaprogramming came from a good developer: Markus Triska. Here (A Couple of Meta-interpreters in Prolog) you find both theory and practice. For instance:

... Another group of extensions aims to improve the incomplete default computation strategy. We start from an MI that limits the depth of the search tree:

        mi_limit(Goal, Max) :-
                mi_limit(Goal, Max, _).

        mi_limit(true, N, N).
        mi_limit((A,B), N0, N) :-
                mi_limit(A, N0, N1),
                mi_limit(B, N1, N).
        mi_limit(g(G), N0, N) :-
                N0 > 0,
                mi_clause(G, Body),
                N1 is N0 - 1,
                mi_limit(Body, N1, N).
like image 97
CapelliC Avatar answered Oct 23 '22 05:10

CapelliC