Re: [eclipse-clp-users] Dynamic List/ Array

From: Joachim Schimpf <jschimpf_at_coninfer.com>
Date: Wed, 03 Dec 2014 17:08:08 +0000
On 03/12/14 07:58, Edgaonkar, Shrirang wrote:
> Dear CLP users,
>
> I have the following array. I need to know if we are able to insert new
> variables in the array or have constraints on the existing variables.
>
> M = []([](2, 3, 5, _), [](1, 4, 7, _)),
>
> After inserting new variables, the output should be as follows for example.
>
> M = []([](2, 3, 5, _, 34), [](1, 4, 7, _, 87)),

The first line says "M is equal to a 2x4 array".  This equation is part of your 
program logic, and you cannot change it.  However, you could construct a new 
array M2 that shares some of its elements with M:

	M  = []([](2, 3, 5, X14),     [](1, 4, 7, X24)),
	M2 = []([](2, 3, 5, X14, 34), [](1, 4, 7, X24, 87)).

Here, not only are the corresponding numerical elements identical, the variable 
elements X14 and X24 are also shared, i.e. these variables occur simultaneously 
in both arrays.

A more generic way to do that would be:

?- M = []([](2,3,5,_), [](1,4,7 _)),     % example array
    dim(M2, [2, 5]),                      % make a larger array
    share_common_elements(M, M2).         % share the overlap

M = []([](2, 3, 5, _87), [](1, 4, 7, _102))
M2 = []([](2, 3, 5, _87, _574), [](1, 4, 7, _102, _580))
Yes (0.00s cpu)

where share_common_elements/2 is defined as

share_common_elements(A1, A2) :-
     dim(A1, [N1,M1]),
     dim(A2, [N2,M2]),
     ( multifor(Index,1,[min(N1,N2),min(M1,M2)]), param(A1,A2) do
         arg(Index, A1, X), arg(Index, A2, X)
     ).


> The following is the way I need to add the constraint for the array.
>
> M[1, 4] + M[2, 4] #= 10,
>
> %Search API to solve the above constraint..
>
> and provide solution as
>
> M = []([](2, 3, 5, 2, 34), [](1, 4, 7, 8, 87)),
>
> Please let me know if this is possible.

Yes, this works exactly as you say:

?- M = []([](2,3,5,_),[](1,4,7,_)), M[1,4]+M[2,4] #= 10, M#::1..7, labeling(M).
M = []([](2, 3, 5, 3), [](1, 4, 7, 7))
Yes (0.00s cpu, solution 1, maybe more)
M = []([](2, 3, 5, 4), [](1, 4, 7, 6))
Yes (0.02s cpu, solution 2, maybe more)
M = []([](2, 3, 5, 5), [](1, 4, 7, 5))
Yes (0.03s cpu, solution 3, maybe more)
M = []([](2, 3, 5, 6), [](1, 4, 7, 4))
Yes (0.05s cpu, solution 4, maybe more)
M = []([](2, 3, 5, 7), [](1, 4, 7, 3))
Yes (0.05s cpu, solution 5)


-- Joachim
Received on Wed Dec 03 2014 - 17:08:19 CET

This archive was generated by hypermail 2.2.0 : Thu Dec 04 2014 - 01:16:36 CET