Re: [eclipse-users] User defined constraints.

From: Joachim Schimpf <js10_at_...2...>
Date: Mon, 12 Mar 2007 17:26:52 +0000
Malcolm Ryan wrote:
> I'm trying to do reasoning about graphs using a CLP in Eclipse. I'm  
> using the ic_symbolic library. I've created a domain of vertices:
> 
> :- local domain(vertex(v0, v1, v2, ...)).
> 
> And have defined edges as facts of the form: edge(From, To).

Although you can do that with lib(ic_symbolic), I would recommend
that you use integers to represent the vertices, and use lib(ic)
instead of ic_symbolic.

This will be more efficient, and remove the need to declare a domain.
Also, it is more compatible with the representation used in ECLiPSe's
library for graph computations, lib(graph_algorithms).


> 
> I define a constraint adjacent(From, To) which states that the  
> vertices given by From and To are adjacent. At the moment it is  
> defined as follows:
> 
> adjacent(From, To) :-
>          ground(From), !,
>          findall(T, edge(From, T), ToList),
>          To &:: ToList.
> 
> adjacent(From, To) :-
>          ground(To), !,
>          findall(F, edge(F, To), FromList),
>          From &:: FromList.
> 
> adjacent(From, To) :-
>          var(From), var(To),
>          suspend(adjacent(From, To), 2, [From,To]->inst).
> 
> As you can see, it only propagates the constraint if one of From or  
> To is grounded. Otherwise it suspends. I'd like to modify it so that  
> it propagates constraints more effectively. If From is constrained to  
> only a subset of all vertices, then To should be constrained to the  
> set of neighbouring vertices. Can I do this in Eclipse? How?

An easy and quite efficient way to do this is to use the
"Generalised Propagation" library lib(propia).

Assume you have compiled this code:

:- lib(ic).
:- lib(propia).

edge(1,2).
edge(1,4).
edge(2,3).
edge(2,1).

adjacent(X,Y) :-
	edge(X,Y) infers ic.


Here are some example queries:

?- adjacent(1, Y).
Y = Y{[2, 4]}
Yes (0.00s cpu)

?- adjacent(X, 3).
X = 2
Yes (0.00s cpu)

?- adjacent(X, Y).
X = X{[1, 2]}
Y = Y{1 .. 4}
There is 1 delayed goal.
Yes (0.00s cpu)

?- adjacent(X, Y), X = 1.
X = 1
Y = Y{[2, 4]}
Yes (0.00s cpu)

?- adjacent(X, Y), Y :: [2, 4].
X = 1
Y = Y{[2, 4]}
Yes (0.00s cpu)

?- adjacent(X, Y), Y :: [2, 4], Y = 4.
X = 1
Y = 4
Yes (0.00s cpu)
Abort

?- Y :: [2, 4], adjacent(X, Y).
Y = Y{[2, 4]}
X = 1
Yes (0.00s cpu)



-- Joachim
Received on Mon Mar 12 2007 - 17:27:30 CET

This archive was generated by hypermail 2.2.0 : Mon Jul 09 2018 - 02:05:29 CEST