Re: [eclipse-clp-users] How to define coordinates that solve a system of quadratic equations, then use in search?

From: Marco Gavanelli <marco.gavanelli_at_...17...>
Date: Thu, 13 Aug 2009 08:46:37 +0200
-dp- wrote:
> 2) I'm trying to define a problem, and haven't been able to find the right
> guidance in the tutorial, examples, nor ref manual to tell me if it's
> feasible in ECLiPSe or not. Here it is: There are a large number of colored
> dots at integer coordinates throughout a 2D space. I'm only interested in
> red dots that appear in an area defined by a set of linear or quadratic
> equations. I would like to declare a set variable that is constrained by the
> equations (using $>= and so on), then declare a coordinate variable
> constrained to be "in" the set, and then use that coordinate variable in a
> search (e.g. "(red CoordVar)").
> 
> Ideally, the set or coordinate variables would be setup under a specialized
> predicate (aka within a subroutine) so it could be reused easily.
> 
> I realize that I might have to map each 2D coordinate to a unique integer
> value in order to get things working. (This is why I used ic_sets in my
> earlier example.)
> 
> Tutorial 9.4, "Finding solutions of real constraints" provides an example of
> a space defined by hyperplanes which is temptingly close to my problem, but
> it's not clear whether the vars can be constrained in a subroutine nor how
> to activate the delayed goals. Here's a first pass:
> 
> :- lib(ic).
> 
> setupScenario :-
> assert( red([1,1]) ), /* within space of interest; should be one and only
> solution */
> assert( red([2,2]) ). /* not within space of interest */
> /* imagine thousands more colored dot definitions here */
> 
> setupSpaceOfInterest(X, Y) :-
> X #:: -4..4,
> Y #:: -4..4,
> 4 $>= X^2 + Y^2, 4 $>= (X-1)^2 + (Y-1)^2, Y $>= X.
> 
> findRedsInSpaceOfInterest :-
> setupScenario,
> setupSpaceOfInterest(X, Y),
> findall([X,Y],red([X,Y]),CoordinatesOfAllRedsInSpaceOfInterest).
> 
> ?- findRedsInSpaceOfInterest.
> There are 13 delayed goals.
> Yes (0.00s cpu)
> Thanks for any tips,
> David Pautler

Dear David,

I am not sure I understand the problem correctly, sorry if the answer is 
not at the level you are expecting.

Your solution sounds reasonable to me: what is exactly that you do not 
like in the ECLiPSe answer? Maybe the fact that you do not see the 
required coordinates in the answer? This is because you do not put the 
output it in a parameter of you predicate findRedsInSpaceOfInterest:

findRedsInSpaceOfInterest(CoordinatesOfAllRedsInSpaceOfInterest) :-
  setupScenario,
  setupSpaceOfInterest(X, Y),
  findall([X,Y],red([X,Y]),CoordinatesOfAllRedsInSpaceOfInterest).

Now ECLiPSe should give you the coordinates

	[eclipse 2]: findRedsInSpaceOfInterest(C).

	C = [[1, 1]]

	There are 13 delayed goals. Do you want to see them? (y/n)

Maybe you do not like those 13 delayed goals?
This is due to the fact that you first impose constraints 
(setupSpaceOfInterest), then you use findall. Findall assigns the 
values, but then backtracks, so the assignment is lost, and the 
constraints remain in the store. This could be ok, it depends on what 
you have to do: if you want the constraints to remain there because you 
need them later, it is perfectly ok. If you do not want them anymore, 
you could impose the constraints inside findall:

findRedsInSpaceOfInterest(CoordinatesOfAllRedsInSpaceOfInterest) :-
  setupScenario,
  findall([X,Y],
     (setupSpaceOfInterest(X, Y),
      red([X,Y])
     ),CoordinatesOfAllRedsInSpaceOfInterest).

Now:

	[eclipse 2]: findRedsInSpaceOfInterest(C).

	C = [[1, 1]]
	Yes (0.00s cpu)

I think you should not use assert. Usually, one sets up the scenario 
only once, and then uses it many times, but if you set up the scenario 
each time you solve the problem, each time you solve it you actually add 
new clauses to your program:

	[eclipse 2]: findRedsInSpaceOfInterest(C).

	C = [[1, 1]]
	Yes (0.01s cpu)
	[eclipse 3]: findRedsInSpaceOfInterest(C).

	C = [[1, 1], [1, 1]]
	Yes (0.00s cpu)
	[eclipse 4]: findRedsInSpaceOfInterest(C).

	C = [[1, 1], [1, 1], [1, 1]]
	Yes (0.00s cpu)

The best way is probably create a file scenario.pl that contains all 
your dots, and load it together with your main file:

	:- lib(ic).
	:- [scenario].

	(rest of the program)

Otherwise, if you like using assert, you can remove setupScenario from 
your main predicate:

findRedsInSpaceOfInterest(CoordinatesOfAllRedsInSpaceOfInterest) :-
  findall([X,Y],
      (setupSpaceOfInterest(X, Y),
       red([X,Y])
      ),CoordinatesOfAllRedsInSpaceOfInterest).

and then invoke setupScenario before findRedsInSpaceOfInterest:

	[eclipse 2]: setupScenario.

	Yes (0.00s cpu)
	[eclipse 3]: findRedsInSpaceOfInterest(C).

	C = [[1, 1]]
	Yes (0.00s cpu)
	[eclipse 4]: findRedsInSpaceOfInterest(C).

	C = [[1, 1]]
	Yes (0.00s cpu)

Cheers,
Marco

-- 
Marco Gavanelli, Ph.D. in Computer Science
Dept of Engineering
University of Ferrara
Via Saragat 1 - 44100 Ferrara (Italy)
Tel  +39-0532-97-4833
Fax  +39-0532-97-4870
http://www.ing.unife.it/docenti/MarcoGavanelli/
Received on Thu Aug 13 2009 - 07:01:49 CEST

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