Re: [eclipse-clp-users] List to arrays

From: Annick Fron <list_at_afceurope.com>
Date: Thu, 30 Jun 2016 16:01:19 +0200
Hi,

	Your mail helped me a lot.
	I rewrote the program , but got some problems to define that all columns must be different. I wrote something, but which gives me sometimes the same columns.
What is wrong with it ?

	Best regards

	Annick
:-lib(ic).
:-lib(ic_global).
:-lib(listut).
:-lib(lists).
:-lib(arrays).
%:- lib(matrix_util).
:- import sequence/4 from ic_global_gac.
:- import alldifferent_matrix/1 from ic_global_gac.


essai(N,Span,Matrix) :-

   Nb is (N*(N-1)//2),
   dim(Matrix,[Nb,N]),
   Matrix#::0..1,


  ( for(J,1,N), param(Matrix,Nb,Span) do
      sequence(0,1,Span,Matrix[1..Nb,J])
  ),

  ( for(I,1,Nb), param(Matrix,N) do
      sum(Matrix[I,1..N]) #= 2
  ),
   differentcols(Matrix,Nb,N),
  array_flat(1, Matrix, Vars),
  labeling(Vars).


differentcols(M,Nb,N):-
   (for(I,1,Nb),param(M,N,Nb) do
       Col1 is M[I],
       Iplus1 is I + 1,
       (for(J,Iplus1,Nb),param(M,Col1,N) do
           Col2 is M[J],
           not(equalcol(Col1,Col2)))).

equalcol(X,Y,N):-dim(X,[N]),dim(Y,[N]),X#::0..1, Y#::0..1,
( for(J,1,N),param(X,Y) do A is X[J], B is Y[J], A#=B).

Le 29 juin 2016 à 03:54, Joachim Schimpf <jschimpf_at_coninfer.com> a écrit :

> On 28/06/16 15:04, Annick Fron wrote:
>> Hi,
>> 
>> 	I have made progress in my program.
>> I have almost a solution, but I would need to use :
>> 
>> alldifferent_matrix on the columns, but I can’t find what type is expected.
> 
> alldifferent_matrix/1 takes a matrix (2-dimensional array)
> and places alldifferent/1 constraints on its rows AND columns.
> 
> So "alldifferent_matrix on the columns" does not make sense.
> 
> I would anyway recommend that you use individual alldifferent/1
> constraints instead of alldifferent_matrix/1 (it is a prototype that
> currently works only for special cases like square matrices).
> 
> 
> Some further recommendations:
> 
> Do not use lib(matrix_util), it is an old library from the times
> before ECLiPSe supported arrays properly.  It represents matrices
> as lists of lists, which is hardly ever convenient.  Use dim/2
> to create matrices (arrays of arrays).
> 
> It is seldom necessary to convert between lists and arrays.
> Most constraints, and predicates like labeling/1, accept both.
> This would be a more compact version of your code:
> 
> essai(N,Span,M) :-
> 
>    Nb is N*(N-1)//2,
> 
>    dim(M, [Nb,N]),
>    M #:: 0..1,
> 
>    ( for(J,1,N), param(M,Nb,Span) do
>        sequence(0,1,Span,M[1..Nb,J])
>    ),
> 
>    ( for(I,1,Nb), param(M,N) do
>        sum(M[I,1..N]) #= 2
>    ),
> 
>    array_flat(1, M, Vars),
>    labeling(Vars).
> 
> 
> Cheers,
> Joachim
> 
> 
>> :-lib(ic).
>> :-lib(ic_global).
>> :-lib(listut).
>> :-lib(lists).
>> :-lib(arrays).
>> :- lib(matrix_util).
>> :- import sequence/4 from ic_global_gac.
>> :- import alldifferent_matrix/1 from ic_global_gac.
>> 
>> 
>> essai(N,Span,Cols,Rows) :-
>> 
>>    Nb is (N*(N-1)//2),
>>    matrix(Nb,N,Rows,Cols),
>> 
>>    (foreach(XList,Cols),param(Span) do
>>        array_list(X,XList),
>>        dim(X,[Nb]),
>>        X #::0..1,
>>        sequence(0,1,Span,X)
>>    ),
>> 
>>    (foreach(RowList,Rows),param(N) do
>>        array_list(Row,RowList),
>>            dim(Row,[N]),
>>            Row #:: 0..1,
>>            sum(Row[1..N]) #= 2
>> 
>>    ),
>>    (foreach(RowList,Rows),param(N) do
>>    array_list(Row,RowList),
>>    labeling(Row))
>> .
> 
> 
> ------------------------------------------------------------------------------
> Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
> Francisco, CA to explore cutting-edge tech and listen to tech luminaries
> present their vision of the future. This family event has something for
> everyone, including kids. Get more information and register today.
> http://sdm.link/attshape
> _______________________________________________
> ECLiPSe-CLP-Users mailing list
> ECLiPSe-CLP-Users_at_lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/eclipse-clp-users
Received on Thu Jun 30 2016 - 14:17:29 CEST

This archive was generated by hypermail 2.2.0 : Fri Jul 01 2016 - 15:13:12 CEST