Prolog manual or custom labeling

后端 未结 5 533
孤独总比滥情好
孤独总比滥情好 2020-12-19 18:14

I am currently writing a solver for a floor planning problem in Prolog and have some issues with the labeling part.

The current problem is my constraints are posted

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 18:30

    To complement the other answers (one contrasting labeling and propagation, one showing a dedicated labeling method), I now tackle a further very important aspect of this question:

    Very often, when beginners complain about the speed of their code, it turns out that their code in fact doesn't even terminate! More efficiency would not help in that case.

    Hence, this answer points you towards first ensuring actual termination of your relation.

    The best way to ensure termination of CLP(FD) programs is to separate them into 2 parts:

    1. the first, called the core relation, simply posts all constraints.
    2. the second uses labeling/2 to perform the actual search.

    Have you done this in your program? If not, please do. When this is done, make sure that the core relation, say solution/2 (where the arguments are: a term denoting the task instance, and the list of variables to be labeled) terminates universally by querying:

    ?- solution(Instance, Vs), false.

    If this terminates, then the following also terminates:

    ?- solution(Instance, Vs), label(Vs), false.

    Of course, in larger tasks, you have no chance to actually witness the termination of the latter query, but a good chance to witness the termination of the first query, because setting up the constraints is often much faster than actually obtaining even a a single solution.

    Therefore, test whether your core relation terminates!

提交回复
热议问题