Set a time limitation on algorithms

43 views (last 30 days)
alexaa1989
alexaa1989 on 9 Sep 2014
Answered: per isakson on 10 Sep 2014
Hi everyone I need to set a time limitation on an algorithm for example after 3600 seconds the algorithm should stop working.
any ideas?
thank in advance...

Answers (8)

Image Analyst
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.
  2 Comments
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 10 Sep 2014
Edited: Salaheddin Hosseinzadeh on 10 Sep 2014
Thanks Image Analyst.
MATLAB can be such a let down sometimes :(
So it's not possible to stop a fdesign command in the middle of it as you said? Sometimes passing, improper parameters to fdesign makes it last forever. I've to use CTRL+C to terminate it, but I always though I can use a timer to stop it in such cases.
I've another question though, I've been sending a huge file to a device over serial port, and everything was ok, I could see it on the other device, but since the timout reached, MATLAB terminated the transfration! How does that work? I'd been using fprintf I believe, and all I did to get around it was to increase the timeout, so it ran for a longer time and the file transfer got completed. So, something was terminated even though the file transfer was in progress.
Many Thanks.
Guillaume
Guillaume on 10 Sep 2014
Serial port timeout is part of the operating system serial port interface (on windows, don't know about other OSes), so most likely matlab is using that and it's the OS, not matlab, that stops the communication.

Sign in to comment.


Iain
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
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.
Edit: see this official answer from Mathworks for additional details on timer and see Yair's blog for some way to implement multithreading in matlab.

Sean de Wolski
Sean de Wolski on 9 Sep 2014
tic and toc is one way, probably the easiest.
  1 Comment
alexaa1989
alexaa1989 on 9 Sep 2014
thanks alot but that just measures the run time of the loop and i already used tic toc for that but i want it to stop after a determined period

Sign in to comment.


Joseph Cheng
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:
i=0;
tic,
while 1
i=i+1;
if toc>5
disp(['done' num2str(toc)])
break
end
end
or for a function:
function [sum time] = conditiontest(timelimit)
sum=0;
tic;
while 1
if toc<=timelimit
sum = sum+1;
else
time = toc;
return
end
end

alexaa1989
alexaa1989 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.

Matt J
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.

per isakson
per isakson on 10 Sep 2014

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!