Prolog manual or custom labeling

后端 未结 5 534
孤独总比滥情好
孤独总比滥情好 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:34

    First, always try built-in heuristics. ff is often a good strategy.

    For custom labeling strategies, it is often easiest to first convert the domain to a list, then reorder the list, and then simply use member/2 to assign the values of the domain using the new order.

    A good building black is dom_integers/2, relating a finite CLP(FD) domain to a list of integers:

    :- use_module(library(clpfd)).
    
    dom_integers(D, Is) :- phrase(dom_integers_(D), Is).
    
    dom_integers_(I)      --> { integer(I) }, [I].
    dom_integers_(L..U)   --> { numlist(L, U, Is) }, Is.
    dom_integers_(D1\/D2) --> dom_integers_(D1), dom_integers_(D2).
    

    Your specific strategy is easily expressed on a list of such ordered integers, relating these integers to a second list where the values occur in the order you describe:

    outside_in([]) --> [].
    outside_in([I]) --> [I].
    outside_in([First|Rest0]) --> [First,Last],
            { append(Rest, [Last], Rest0) },
            outside_in(Rest).
    

    Sample query and result:

    ?- phrase(outside_in([1,2,3,4]), Is).
    Is = [1, 4, 2, 3] ;
    false.
    

    Combining this with fd_dom/2 and dom_integers/2, we get (bindings for variables other than X omitted):

    ?- X in 10..20,
       fd_dom(X, Dom),
       dom_integers(Dom, Is0),
       phrase(outside_in(Is0), Is),
       member(X, Is).
    X = 10 ;
    X = 20 ;
    X = 11 ;
    X = 19 ;
    X = 12 ;
    X = 18 ;
    etc.
    

    Nondeterminism is preserved by member/2.

提交回复
热议问题