Re: [eclipse-clp-users] Propagation question for IC

From: Panagiotis Stamatopoulos <takis_at_...90...>
Date: Wed, 1 Apr 2020 12:34:10 +0300
Thank you very much for your reply, Joachim!

Actually, the origin of my question is a puzzle I was trying to solve:
https://alexis.lindaikejisblog.com/photos/shares/5d56b650ce177.jpg

I wanted to stick to IC, as I intended to use it for a course I teach.
In addition, I wanted to get the answer just by propagation, without
any labeling, as the puzzle can be solved just by logical inferences.
Following your suggestion to use the reified form of #::, I ended with
the program that follows. Do you believe that there could be a better
solution, as far as readability is concerned (and advertisement of the
advantages of CLP)? Anyway, this program works fine.
Thank you very much, again.

      Takis

-------------------------------------------------------------------------

:- lib(ic).

code(X1, X2, X3) :-
    [X1, X2, X3] #:: 0..9,
    corcor(X1, X2, X3, 6, 8, 2, 1),
    corwro(X1, X2, X3, 6, 1, 4, 1),
    corwro(X1, X2, X3, 2, 0, 6, 2),
    corcor(X1, X2, X3, 7, 3, 8, 0),
    corwro(X1, X2, X3, 7, 3, 8, 0),
    corwro(X1, X2, X3, 7, 8, 0, 1).

/* corcor/7: N is the number of correct and well placed
  * digits of code X1/X2/X3 compared to N1/N2/N3 */

corcor(X1, X2, X3, N1, N2, N3, N) :-
    #::(X1, N1, B1),
    #::(X2, N2, B2),
    #::(X3, N3, B3),
    N #= B1 + B2 + B3.

/* corwro/7: N is the number of correct but wrongly placed
  * digits of code X1/X2/X3 compared to N1/N2/N3 */

corwro(X1, X2, X3, N1, N2, N3, N) :-
    #::(X1, [N2, N3], B1),
    #::(X2, [N1, N3], B2),
    #::(X3, [N1, N2], B3),
    N #= B1 + B2 + B3.

-------------------------------------------------------------------------

On 01-Apr-20 11:20 AM, Joachim Schimpf wrote:
> On 31/03/2020 19:22, Panagiotis Stamatopoulos wrote:
>> Hello All,
>>
>> I hope you are healthy and safe. May I ask a (maybe trivial) question
>> in the context of the IC library? Why the query
>>
>> ?- X #:: [1, 3], B #= (X #= 2).
>> X = X{[1, 3]}
>> B = B{[0, 1]}
>> There is 1 delayed goal.
>>
>> does not enforce Β to be equal to 0?
> 
> 
> IC's arithmetic equality and inequality constraints generally
> enforce bounds-consistency, that's why the hole in the domain
> of X is not taken into account.
> 
> On could argue that for simple cases like X #= Y+Const stronger
> domain-consistency should be enforced, this is the reason for
> having http://eclipseclp.org/doc/bips/lib/ic/ac_eq-3.html .
> Unfortunately, a reified version of ac_eq/3 is missing!
> 
> If, as in your example, the equation contains only one variable,
> you could use the reified form of #::, i.e.
> 
> ?- X #:: [1, 3], #::(X, 2, B).
> X = X{[1, 3]}
> B = 0
> 
> 
> By the way, lib(gfd) also does what you expect:
> 
> ?- lib(gfd).
> 
> ?- X #:: [1, 3], B #= (X #= 2).
> X = X{[1, 3]}
> B = 0
> 
> 
> Cheers,
> Joachim
> 
> 
> _______________________________________________
> ECLiPSe-CLP-Users mailing list
> ECLiPSe-CLP-Users_at_lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/eclipse-clp-users
Received on Wed Apr 01 2020 - 10:46:31 CEST

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:21 CEST