Optimizing pathfinding in Constraint Logic Programming with Prolog

前端 未结 3 923
悲哀的现实
悲哀的现实 2020-12-10 13:13

I am working on a small prolog application to solve the Skyscrapers and Fences puzzle.

An unsolved puzzle:

3条回答
  •  被撕碎了的回忆
    2020-12-10 13:42

    A quick glance over your program suggests that you use reification quite heavily. Unfortunately, such formulations imply weak consistency in current systems like SICStus.

    Often, however, things can be formulated more compactly leading to better consistency. Here is one example which you might adapt to your needs.

    Say, you want to express that (X1,Y1) and (X2,Y2) are horizontal or vertical neighbors. You could say ( X1+1 #= X2 #/\ Y1 #= Y2 ) #\ ... for each possiblity (and check if your health insurance covers RSI).

    Or you can say abs(X1-X2)+abs(Y1-Y2) #= 1. In the olden tymes SICStus Prolog used to have a symmetric difference (--)/2 for that, but I assume you are using version 4.

    Above formulation maintains interval consistency (at least I conclude this from the examples I tried):

    | ?- X1 in 1..9, abs(X1-X2)+abs(Y1-Y2) #= 1.
    X1 in 1..9,
    X2 in 0..10,...
    

    So the X2 is readily constrained!

    There might be situations (as you indicate in your response) where you need the reified form to maintain other constraints. In this case you might consider to post both.

    Leaf through the manual, there are several combinatorial constraints that might be interesting too. And as a quick fix, smt/1 might help (new in 4.2.0). Would be interested to hear about this...

    Another possibility might be to use another implementation: For example library(clpfd) of YAP or SWI.

提交回复
热议问题