Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance issues on prolog
    primarykey
    data
    text
    <p>I'm currently implementing an prolog program to calculate the shortest path between two points. The framework exists already in a Java project. As a requirement the path must be implemented in prolog. Therefore I use gnu.prolog (<a href="http://www.gnu.org/software/gnuprologjava/" rel="nofollow">http://www.gnu.org/software/gnuprologjava/</a>)</p> <p>Out of java I call <code>searchPath(1,5,Path)</code> which will return <code>Path=[5,4,3,2,1]</code></p> <p>Here is my prolog code:</p> <pre><code>:- dynamic(path/3). findPath( [Goal | Rest], Goal, Temp, Temp, [Goal | Rest]). findPath( [A | Rest], Goal, Cost, Temp, Path) :- path(A,B,C), \+member(B, [A | Rest]), NewCosts is (Temp + C), findPath([B, A | Rest], Goal, Cost, NewCosts, Path). searchPath(Start,Goal,Path_to_goal) :- findPath([Start], Goal, Cost1, 0, Path), findPath([Start], Goal, Cost2, 0, Path2), Cost1=&lt;Cost2, Path_to_goal = Path. </code></pre> <p>I have two issues with that:</p> <ol> <li><p>The <code>searchPath</code> method <strong>should return the shortest path</strong>. However it does <strong>NOT</strong>. This results in the fact that my ghost "decides" to switch direction at some point resulting in the ghost jittering from left to right.</p></li> <li><p>My prolog code takes up to <strong>6 seconds</strong> to return a result. I don't have to tell you that this is far too much time. However sometimes prolog only needs <strong>19ms</strong>. I wasn't able to figure out on which circumstances this depends on. For example a path list containing 99 elements takes 19ms to calculate but the 6 seconds were spent on a list containing only 38 elements.</p></li> </ol> <p>Can you suggest any improvements?</p> <p>Thanks for your help in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload