Re: [eclipse-clp-users] Constraints on list

From: Kish Shen <kisshen_at_...5...>
Date: Wed, 28 Jan 2015 03:06:36 +0000
Hi Shirang,

On 24/01/2015 01:06, Edgaonkar, Shrirang wrote:
> Dear Eclipse-clp users,
>
>
>
>    Following is my problem. I have searched through the forum archives for this solution but in vain. I have encountered dynamic structures but not list. I need to add constraints on List through indexed elements as below. The resultant List should be evaluated based on the constraints. In the eclipse samples diaz_pool.pl at_most_one is what i mean to create. Instead of at_most_one it will be at_most_N in my case. It is just a thought though. I have mentioned the desired output too.
>

I am not sure if I correctly understand what you are asking about -- you 
seem to be asking how you can access a list element via its 'index', 
i.e. its position in the list. This is a not a question about 
constraints, as your title suggest, but a question about how to program 
and use lists in Prolog.

The short answer is that ECLiPSe does not provide any concise syntax to 
allow an indexed access to the list elements. This is because accessing 
an element of a list requires traversing the list from the head until 
you reach the element.

If you need to access elements with an index, then using a list is 
normally a bad idea. It sounds like you are unfamiliar with Prolog, as 
you say you have not encountered lists before. If this is the case, I 
strongly suggest that you learn Prolog first, so that you know how to 
use the data structures Prolog/ECLiPSe provide.

What is the reason you want to use a list? The example you show, in 
which the length of the list is known at run-time, you can use an array.

Lists are normally used when you don't know at run time how large the 
data would be. However, in the case of constraints, by the time you post 
the constraint, the number of elements in the list is known. If you need 
to access the individual elements via an index, you should convert the 
list to an array (e.g. using =../2), and you can access the array 
element using the syntax you show (A[1] etc.).

About constraints on lists -- it looks like you are talking about finite 
domain constraints here. Both ic and gfd provide finite domain 
constraints that works on collections (lists or arrays/matricies). You 
have not describe what you want your at_most_N constraint to do, but if 
you want to constrain the number of occurrences of values in the 
collection, both ic and gfd provide several constraints to do this -- 
e.g. atmost/3, occurrences/3, gcc/2 -- do these do what you want?

Cheers,

Kish


I

>
> :- lib(ic).
>
> solve(A,B) :-
>
>   B :: 1 .. 10,
>
>   length(A, B),
>
>   A[4] #= 1,
>
>    A[6] #= 2,
>
>   search([B],0,input_order,indomain_split,complete,[]).
>
>
>
>
>
>
>
> Output
>
> ?- solve(A, B).
> A = [_,_,_,_,1,_2]
> B = 6
> Yes (0.00s cpu, solution 1, maybe more)
>
>
>
Received on Wed Jan 28 2015 - 03:06:59 CET

This archive was generated by hypermail 2.3.0 : Tue Apr 16 2024 - 09:13:20 CEST