0

I've solved a problem about the assignment of the articles in a conference, using ECLiPSe. The goal is: similar articles should be in the same sessions. This is the solution that works in ECLiPSe:

:- lib(fd).

    paper(1, 'An Empirical Study on Using Stereotypes to Improve Understanding of UML Models').
    paper(2, 'Tool-Supported Customization of UML Class Diagrams for Learning Complex System Models').
    paper(3, 'Understanding Class Evolution in Object-Oriented Software').

    paper(4, 'A Simple Static Model for Understanding the Dynamic Behavior of Programs').
    paper(5, 'Reuse in Reverse Engineering').
    paper(6, 'Working in Pairs as a Means for Design Knowledge Building: An Empirical Study').

like(X, Y) :-
    element(I, [1, 1, 2, 4, 4, 5], X),
    element(I, [2, 3, 3, 5, 6, 6], Y).

conference([T1, T2, T3], [T4, T5, T6]) :-
    % --- domains
    L = [1, 2, 3, 4, 5, 6],
    X1::L, X2::L, X3::L, X4::L, X5::L, X6::L,
    % --- constraints
    alldifferent([X1, X2, X3, X4, X5, X6]),
    like(X1, X2), like(X1, X3), like(X2, X3), like(X4, X5), like(X4, X6), like(X5, X6), 
    % --- searching
    labeling([X1, X2, X3, X4, X5, X6]),
    % --- converting ids to titles
    paper(X1, T1), paper(X2, T2), paper(X3, T3), paper(X4, T4), paper(X5, T5), paper(X6, T6).

In prolog, the problem is the definition of constraints and the labeling. I know that ECLiPSe uses propagation alghorithms but in prolog I have to use backtracking strategies.

How can I do the porting of this code in prolog?

jschimpf
  • 4,904
  • 11
  • 24
user840718
  • 1,563
  • 6
  • 29
  • 54

1 Answers1

3

SWI-Prolog has an implementation of CLP(FD) that supports most of the functionality in ECLiPSe's version, but with a somewhat different syntax. Here's conference:

:- use_module(library(clpfd)).

conference([T1, T2, T3], [T4, T5, T6]) :-
    Domain = 1..6,
    [X1, X2, X3, X4, X5, X6] ins Domain,

    all_different([X1, X2, X3, X4, X5, X6]),
    like(X1, X2), like(X1, X3), like(X2, X3),
    like(X4, X5), like(X4, X6), like(X5, X6), 

    label([X1, X2, X3, X4, X5, X6]),

    paper(X1, T1), paper(X2, T2),
    paper(X3, T3), paper(X4, T4),
    paper(X5, T5), paper(X6, T6).

The other predicates work as-is. Instead of label/1, you can also use labeling/2, which offers additional control over the constraint search algorithm.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Well, in my case is not allowed to use external library, because I have to write the constraint solver by myself in pure prolog. – user840718 Jun 11 '13 at 19:13
  • @user840718: then what is the question? Just replace the constraints by `member/2` or `nth1/3` calls and checks. – Fred Foo Jun 11 '13 at 19:16
  • You're right, but the code is too declarative. I need to implement a strategy of resolution of constraints, generate and test or standard-backtracking. I don't know about the implementation of the algorithm, so I cannot use external lib and implement it. – user840718 Jun 11 '13 at 20:26
  • @user840718: True. Come to think of it, `all_different` and the domain constraints should probably be replaced by `permutation/2`. The rest of the code will have to reshuffled until it works, then optimized; but that's doable by some guided trial and error. – Fred Foo Jun 12 '13 at 09:11