0

I am trying to optimise and find the minimum Cost of a function. The program below uses findall/3 to iterate over all possible options of values which are generated from using the clpfd library provided by SWI-Prolog.

There are several Cost values that are generated using this program below which are gathered into a list. I know that in order to get the minimum value I can simply use the min_list/2 predicate available. However, what I want is that once the program finds a certain value, which is currently the minimum, while computing other options, if the value turns out to be greater than the minimum value, its not added the list.

So essentially, I want to optimise the program so that it simply accounts for the minimum value generated by the program.

optimise(input, arguments, Cost):-
    findall(Cost, some_predicate(input, arguments, Cost), List).

some_predicate(input, arguments, Cost):-
    Option in input..arguments, label(Option),
    find_data(Option, Value),
    find_cost(Value, Cost).

The above code has been modified so that it is condensed and but fulfils the purpose of the question.

Namit
  • 1,264
  • 7
  • 18
  • 34

1 Answers1

0

I think that findall it's not the right tool: here is some code I wrote time ago, that could help you. For instance, given

member_(X,Y) :- member(Y,X).

we can get the lower element

?- integrate(min, member_([1,2,4,-3]), M).
M = -3

I applied the usual convention to postfix with underscore a library predicate that swaps arguments, to be able to meta call it.

Just take that code as an example, paying attention to nb_setarg usage.

To see if it could work 'out-of-the-box', try:

:- [lag].

optimise(Input, Arguments, Cost):-
    integrate(min, some_predicate(Input, Arguments), Cost).
CapelliC
  • 57,813
  • 4
  • 41
  • 80
  • 1
    Thanks for that information, however, i don't *just* want the lower element, i want to optimise it for to find the minimum, as it needs be calculated using a cost funciton – Namit Jul 22 '13 at 22:55
  • 2
    Maybe you're missing the point: some_predicate will be called until it fails, *exactly* as does findall/3. integrate/3 it's (better, will be) a *space saving* replacement. – CapelliC Jul 23 '13 at 05:50