Set a time limitation on algorithms
60 views (last 30 days)
Image Analyst on 10 Sep 2014
I agree with Guillaume. Depending on what alexa means, it may not be possible. MATLAB doesn't have that capability. We're not talking about a timer or checking time in a loop. We're talking about bailing out if a function does not complete in a required time - it's a different situation. I didn't understand either until someone explained it to me, so let me try to explain it here. He uses National Instruments LabView to do high speed, but relatively simple, operations on the assembly line. He explained it like this. Let's say you're examining parts coming down the assembly line and you've got 1 millsecond to analyze this part before you need to analyze the next one. And you have to examine them all - you can't skip any . But what if one pathological part comes down and is taking too long and if you finish that part, you'll miss the next one. Let's say you're calling bwconncomp() or regionprops() or similar and it's been 5 ms - well you would have missed the next 4 parts and that is unacceptable. So they (LabView) have a way to specify how long the function should take, and if it's longer than that to just bail out regardless of how far along it is . It returns a "did not finish" flag and you can mark that part as defective an move on to the next part. It might be better to flag that part as bad rather than to miss analysis of the next dozen or hundred parts while the pathological one finishes analysis. Does that make sense? As far as I know, and this other engineer knows, there is no capability to interrupt a certain function, say bwconncomp() or regionprops() in the middle yet continue on with the rest of the script. This question has come up on this forum before. I hope this explains the situation better.
On the other hand 3600 seconds is quite a long time and I don't think there's any MATLAB function that would take that long, so perhaps alexaa has the opportunity to bail out in the middle of the code by checking the elapsed time in her code, in between calls to MATLAB functions . But once a MATLAB function has started , there is no way to stop it that I know of. Perhaps if you had a separate m-file running and then totally killed off the main script and then tried to restart it and pick up where it left off. I don't know but that would be pretty tricky to make sure you're starting again with all the right values of the variables so that you can continue on as if nothing happened.
Iain on 10 Sep 2014
I may have an answer:
Matlab can be used to manage "parallel" computing jobs. One of the methods allows Matlab to stipulate a maximum length of time that can be used by the parallel job to complete as a timeout.
The parallel computing toolbox has a help page on "timeout". I suggest that you look at it.
Matlab can also use timeouts when reading/writing data to devices. That functionality is somehow in-built into the relevant functions.
Guillaume on 10 Sep 2014
Edited: Guillaume on 10 Sep 2014
The way it's normally done with proper programming language is to launch the processing on a secondary worker thread and use a timer (usually also on another thread) to kill the worker thread if it takes too long. The problem is that matlab is single threaded so you can't do that.
However, I did remember this morning that it has a parallel processing toolbox which may be able to do it. You wouldn't be really doing any parallel processing, just launching one worker and killing it if it takes too long. That assumes that matlab does not run the one worker on the main thread and that it's possible to kill threads in matlab.
Otherwise, it may be possible to use a spawn a timer thread using .Net, mex or maybe java (I don't know java) that would send a CTRL+C to matlab after a while. None of these are particularly simple. If you're using Labview you could also use a Labview timer to send CTRL+C to matlab.
Joseph Cheng on 9 Sep 2014
Edited: Joseph Cheng on 9 Sep 2014
alexaa1989 if you look closely at the Salaheddin's example
while toc < 5
it only does the while loop for when it is less than 5 seconds. So instead why not use the operator return or break to leave your algorithm when a specific condition is met? (which i believe was what everyone was referring to by mentioning using tic and toc.
such as for a script:
or for a function:
function [sum time] = conditiontest(timelimit)
sum = sum+1;
time = toc;
Matt J on 10 Sep 2014
Edited: Matt J on 10 Sep 2014
thank u all but ( timer ) did not work and the genetic algorithm goes on and on so as the tic toc.
If you are using ga(), you can use the OutputFcns option
to force a stop. Set the output state.StopFlag to a nonempty string when the toc output goes above a certain threshold.