Hello,
My question has two parts. In the first part, I would like to inquire about best logic-programming practices related to coding a specific portion of my program. In the second part, I have more direct programming-related questions.
Part I: My program evaluates goals with different strategies, and has the following blueprint.
% TimeOut is set to some value
program(Goal):-
attempt_strategy_1(Goal);
attempt_strategy_2(Goal);
...
attempt_strategy_n(Goal);
attempt_strategy_k(Goal):-
time_out(strategy_k(Goal), TimeOut, Result),
Result \== time_out.
From problem-domain knowledge, I know that there is almost always a strategy that will solve the problem in less than TimeOut milliseconds ; it’s just not possible to know apriori which one is going to be.
For this reason, I envelop the evaluation of strategies in a time_out/3 predicate as a way of saying “If strategy i
is not able to solve the problem in Timeout milliseconds, then move to strategy i+1
, and keep doing this because eventually one strategy will solve the problem within TimeOut milliseconds”.
This saves me a great amount of evaluation time as I do not have to exhaustively search all the space of a given strategy if I know that it should have found a solution by now – if it was the right strategy to pursue.
My question: Is there a better way of dealing with this other than using timeouts?
Part II: I overindulged in using timeouts to the point where I started using timeouts within timeouts. I noticed that for the following case:
p1(G):- time_out(p2(G), TimeOut1, Result1).
p2(G):- time_out(G, TimeOut2, Result2).
If TimeOut2 > TimeOut1
, say TimeOut1=10000
and TimeOut2=50000
, the evaluation of p2(G)
will continue past TimeOut1=10000
and up to TimeOut2=50000
if a solution has not been found by then.
My Question: Is this expected, or am I doing something very wrong and convoluted?
Before posting this cry for help, I searched the topics in this forum looking for similar issues; and found that there is a more SWI-like predicate for timeouts namely call_with_time_limit/2.
My Question: Should I be using this latter instead for my timeouts?
Many thanks,
Amine.