سؤال

I'm currently trying to use prolog out of java using gnu.prolog (http://www.gnu.org/software/gnuprologjava/).

Thanks to the great help of CapelliC I now have a prolog program which works perfect for my purpose. The problem is that gnu.prolog does not support reverse/2 nor does it support nb_setarg/3. Java will throw an error:

Exception in thread "Game" java.lang.IllegalArgumentException: The goal is not currently active

It isn't a big issue to implement reverse/2 on my own but I have no idea how to replace nb_setarg/3 (setarg/3 also doesn't work)

Here is my prolog code:

findPath(_Limit, [Goal | Rest], Goal, Temp, Temp, [Goal | Rest]) :- !.

findPath(Limit, [A | Rest], Goal, Cost, Temp, Path) :-
    path(A,B,C),
    \+member(B, Rest),
    NewCosts is (Temp + C),
    NewCosts < Limit,
    findPath(Limit, [B, A | Rest], Goal, Cost, NewCosts, Path).

searchPath(Start, Goal, Path_to_goal) :-
    S = path_len([], 50),
    repeat,
    arg(2, S, Limit),
    (   findPath(Limit, [Start], Goal, Cost, 0, Path)
    ->  (   Cost < Limit
        ->  nb_setarg(1, S, Path),
        nb_setarg(2, S, Cost),
        fail
        )
    ;   true
    ),
    arg(1, S, Rev),
    reverse(Rev, Path_to_goal).

I tried to use JPL from SWI Prolog but I wasn't able to run it because of severel exceptions pointing out, that Eclipse wasn't able to find the library correctly. I always get one of the following Exceptions:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpl in java.library.path


UnsatisfiedLinkError: D:\Program Files\Prolog\bin\jpl.dll: Can't find dependent libraries

SWI-Prolog: [FATAL ERROR:
    Could not find system resources]

Even after following this and this guide I wasn't able to resolve my problems. Neither on Windows (32bit) nor on Ubuntu (64bit).

Do you have an solutions for me how I can either get JPL running or how to be able to use nb_setarg/3? Up to now I spent one and a half days without any results. Quite frustrating...

هل كانت مفيدة؟

المحلول

I'm sorry, but my suggestion to use GProlog setarg as replacement for SWI-Prolog nb_setarg was wrong. Now I reworked the code in a simpler and (I hope) more effective way, working under any ISO Prolog.

% this data is from original Prolog Dijkstra' algorithm implementation
:- initialization( consult(salesman) ).

:- dynamic(best_so_far/2).

path(X,Y,Z) :- dist(X, Y, Z).
path(X,Y,Z) :- dist(Y, X, Z).

findPath([Goal | Rest], Goal, Temp, Temp, [Goal | Rest]) :-
    !.
findPath([A | Rest], Goal, Cost, Temp, Path) :-
    path(A, B, C),
    \+ member(B, Rest),
    NewCost is Temp + C,
    best_so_far(Limit, _),
    NewCost < Limit,
    findPath([B, A | Rest], Goal, Cost, NewCost, Path).

% ?- searchPath(aberdeen, glasgow, L, P).
%
searchPath(Start, Goal, BestLen, BestPath) :-
    retractall(best_so_far(_, _)),
    asserta(best_so_far(1000000, [])),
    findPath([Start], Goal, Cost, 0, Path),
    % if we get here, it's because a lower Cost exists
    retractall(best_so_far(_, _)),
    asserta(best_so_far(Cost, Path)),
    fail
    ;
    best_so_far(BestLen, BestPath).

If you want to fasten a bit, there is a very simple heuristic that should be applicable: namely make findPath greedy, selecting first lower cost branchs. That can be done with setof+member...

نصائح أخرى

I'm going insane with this...

As I already mentioned above, there are some differences between Prolog in Java and Prolog via SWI.

I'm currently using this code:

% this data is from original Prolog Dijkstra' algorithm implementation
:- dynamic(best_so_far/2).


findPath([Goal | Rest], Goal, Temp, Temp, [Goal | Rest]) :-
    !.
findPath([A | Rest], Goal, Cost, Temp, Path) :-
    path(A, B, C),
    \+ member(B, Rest),
    NewCost is Temp + C,
    best_so_far(Limit, _),
    NewCost < Limit,
    findPath([B, A | Rest], Goal, Cost, NewCost, Path).

% ?- searchPath(aberdeen, glasgow, L, P).
%
searchPath(Start, Goal, BestLen, BestPath) :-
    retract_all(best_so_far(_, _)),
    asserta(best_so_far(50, [])),
    findPath([Start], Goal, Cost, 0, Path),
    % if we get here, it's because a lower Cost exists
    retract_all(best_so_far(_,_)),
    asserta(best_so_far(Cost, Path)),
    fail
    ;
    best_so_far(BestLen, BestPath).


retract_all(Term):-
    retract(Term),fail.
retract_all(_).

Asking for a result in SWI Prolog I'll get an answer in 0.016 seconds. Java needs 15 seconds for the same result!

Furthermore and even worse: at some point gnu prolog delivers a completely different result.

Here is some outline of Java:

From 190 to 221
pathList: [221, 191, 190]
distance: 2

From 191 to 221
pathList: [221, 251, 252, 253, 223, 193, 194, 195, 196, 197, 198, 199, 169, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 151, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191]
distance: 43

From 190 to 221
pathList: [221, 191, 190]
distance: 2

You can clearly see, that there is a path from 191 to 221. But instead of returning this result (pathList: [221,191]) I get a competely different path leading backwards from where my ghost came. Running the Query searchPath(191,221, Distance, Path) in SWI Prolog (instantly) returns

7 ?- searchPath(191,221, Cost, Path).
Cost = 1,
Path = [221, 191].

Once again: I'm using the exact same code. I Copy&Pasted it to make sure. And I'm passing the correct arguments (that's why I print them out).

I really don't know how to thank you (especially CapelliC). I'm sure you already spent far too much time for me. But I'm definitely on my wits' end.

Edit: Thought it might be useful to see my java code:

private int decideHunterMovement() {
        // term which contains the result of the prolog method
        VariableTerm pathTerm = new VariableTerm("Path");
        VariableTerm distanceTerm = new VariableTerm("Distance");
        Integer movement;
        List<IntegerTerm> pathList = new LinkedList<IntegerTerm>();

        // Create the arguments to the compound term which is the question
        IntegerTerm hunterPosition = new IntegerTerm(hunter.getPosition());
        IntegerTerm targetPosition = new IntegerTerm(pacman.getPosition()); // target for hunter is the pacman position

        long time= System.nanoTime ();
        Term[] arguments = { hunterPosition, targetPosition, distanceTerm, pathTerm};
        // Construct the question
        CompoundTerm goalTerm = new CompoundTerm(AtomTerm.get("searchPath"), arguments);
        // Execute the goal and return the return code.
        int rc;
        System.out.println("From " + hunterPosition + " to " + targetPosition);
        try{
            // Create the answer
            rc = interpreter.runOnce(goalTerm);
            time = (System.nanoTime () - time) / 1000 / 1000;
            System.out.println("Result in:" + time+ "ms");
            // If it succeeded.
            if (rc == PrologCode.SUCCESS || rc == PrologCode.SUCCESS_LAST){
                // Get hold of the actual Terms which the variable terms point to
                Term path = pathTerm.dereference();
                Term distance = distanceTerm.dereference();
                // Check it is valid
                if (path != null){
                    if (path instanceof CompoundTerm){
                        // convert CompoundTerm to a Java LinkedList
                        convertToList((CompoundTerm) path, pathList);
                        if(VERBOSE_MODE){
                            System.out.println("pathList: " + pathList);
                            System.out.println("distance: " + (IntegerTerm) distance + "\n");
                        }
                    }else{
                            throw new NoAnswerException("PROLOG ERROR: Answer is not a CompundTerm: (" + path + ")");
                    }
                }else{
                    throw new NoAnswerException("PROLOG ERROR: Answer null when it should not be null");
                }
            }else{
                throw new NoAnswerException("PROLOG ERROR: Goal failed");
            }
        } catch (NoAnswerException e) {
            e.printStackTrace();
        } catch (PrologException e1) {
            e1.printStackTrace();
        }

        movement = decideMovement(pathList);
        return movement;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top