tags:

views:

68

answers:

3

Given a set of constraints, I would like to efficiently generate the set of values.

Suppose I have a few constraints on my Thungus[1]:

goodThungus(X) :-
               X > 100,
               X < 1000.
               sin(X) = 0.

Now, I can check a Thungus by asking:

goodThungus(500).

I would like to generate all good Thungi. I'm not sure how to do that; I'm really not sure about how to do it efficiently.

Note: this of course has to be a computable generation.

[1] Arbitrary object selected for this example.

+1  A: 

What you are asking for can't be done in the full general case: imagine doing f(X) = 0 where f is a function for which the roots cannot be analytically determined, for example. Or suppose f(X) is the function "does the program X halt?". No computer is going to solve that for you.

Your options are basically to either:

  • Limit the set of constraints to things that you can reason about. e.g. inequalities are good because you can identify ranges, then do intersections and unions on ranges efficiently etc.

  • Limit the set of values to a small enough number that you can test them individually against each of the constraints

UPDATE: For the kind of constraints stated in the question (ranges of real values and real-valued functions that can be analytically solved and have a finite number of solutions within any range) I would suggest the following approach:

  • Write a generating function that can iteratively return solutions for you function within a given range... this will need to be done analytically e.g. exploiting the fact that sin(X)=0 implies X=n*pi where n is any integer.
  • Do interval arithmetic and bounding on your range constraints to work out the range(s) that need to be scanned (in the example you would want the range 100 < X < 1000)
  • Apply your generating function to each of the target ranges in order to create all of the possible solutions.
mikera
Let us be real and suppose that the questions are computable.
Paul Nathan
Computable is still too weak a constraint to make the problem tractable. For example "is X prime?" is trivially computable but still has infinite solutions - and it becomes tricky to reason about how this interacts with other constraints. Perhaps you could clarify the kind of problem you are trying to solve?
mikera
Finding sets of numbers (integers and/or rationals) in a multivariate situation that satisfy certain constraints, e.g., the set of all numbers in a range when some predicate on each element is true.
Paul Nathan
OK, thanks for the clarification. The problem is that you will need to analyse the predicate to work out if you can analytically generate solutions. If not, you will probably have to scan the entire range, which might be doable with integers but impossible with rationals (since there are infinitely many in any range....)
mikera
@mikera: Well, I have a decided advantage: every numerical representation in a computer, ultimately, is mappable to the integers. It's sufficient to have a float up to some precision - which indeed can be enumerated. Additionally simplifying, it's probable that the rationals are bounded to have a maximum precision between 1/100 and 1/10000. So, no, I'm not super concerned about the range generation. Solution generation might well be a challenge. I'm hoping there is a technique more "AI"ish than just a brute-force of the intervals.
Paul Nathan
Well I guess you could run the predicate functions through a theorem prover to see if that can generate the solutions for you - something like Matlab perhaps?
mikera
A: 

He is searching any X in [100..1000] for that sin(x) = 0. But this is a pure mathematical problem, and not meant for relational logical deduction / backtracking. simple Prolog is not suited for this?

Hape
I'm not sure if *simple* prolog is idea. Possibly complex Prolog is. :-) I looked at Prolog constraint solvers (ie the one in SWI), but they were interested in a single value (from the examples?).
Paul Nathan
+1  A: 

I'll preface my suggestion by stating that I'm no expert in using numerical constraint logic programming systems, but here goes...

On the surface, I'd think that solving this kind of problem in PROLOG would be best suited to a numerical constraint logic programming system, perhaps such as CLP(R) (for reals) in SWI-PROLOG; unfortunately, the specific problem you've asked for is seeking to solve for a set of constraints including a non-linear constraint, which seems to be not well or widely supported amongst PROLOG implementations; instead, they seem to deal mainly with linear constraints and often have limited support for non-linear constraints such as X = sin(Y), for example.

Take SWI-PROLOG's CLP(R) library, and the following example program:

:- use_module(library(clpr)).

report_xsq_zeros :-
    findall(X, {0 = (X * X) - 10}, Results),
    write_ln(Results).

report_sin_zeros :-
    findall(X, {0 = sin(X)}, Results),
    write_ln(Results).

Now, executing report_xsq_zeros gives us:

 ?- report_xsq_zeros.
[3.16228, -3.16228]
true.

Here, the system correctly computed the zeros of the quadratic x^2 - 10, which are indeed approximately 3.16228 and -3.16228, where the range of X was unbounded. However, when we execute report_sin_zeros, we get:

 ?- report_sin_zeros.
[0.0]
true.

We see that the system only computed a single zero of the function sin(X), even though the range of X was indeed also unbounded. Perhaps this is because it is recognized that there are an infinite number of solutions here (though I'm only guessing...). If we were to program what you've asked for:

report_sin_zeros :-
    findall(X, {X > 100, X < 1000, 0 = sin(X)}, Results),
    write_ln(Results).

We get no results, as the underlying system only computed a single zero for sin(X) as shown earlier (i.e., binding X to 0.0 which lies outside the stated range):

 ?- report_sin_zeros.
[]
true.

I conclude that I've either not demonstrated proper usage of SWI-PL CLP(R) (I suggest you look into it yourself), or it won't solve your specific (non-linear) problem. Other CLP(R) implementations may behave differently to SWI-PROLOG CLP(R), but I don't have them installed so I can't check, but you could try SICSTUS CLP(R) or others; the syntax looks similar.

sharky
Iiiinteresting. I don't know if `sin` (per se) is actually going to be used, but case logic is going to feature heavily and I expect that monotonicity and discontinuity will be very much present. I will have to use your examples and attempt to expand on them. I'll let you know how it goes.
Paul Nathan