How to stop fmincon after running an elapsed time of T sec? - matlab

I am running fmincon for a gird. For some points in the grid there is no solution, exit flag=-2, and it takes half an hour sometimes to return the flag and start the next point in the grid. However, for the other points the answer is revealed in less than a min.
So I thought one possible solution to reduce the unnecessary running time is to define a stop criteria based on elapsed time for each point in the grid. In other words, How to stop fmincon after running an elapsed time of T sec?

You can use an output function:
tic
fmincon(.....,optimset('OutputFcn',#outfun,'MaxFunEval',20000))
outfun.m :
function stop = outfun(x,optimValues,state)
stop = toc>T;

Related

Stopping simulation based on CPU time in Dymola/Modelica

In Dymola, I'm able to do something like:
when time > 100 then
assert(false,"Simulation taking too long");
end when;
to stop simulations based on the time variable itself.
However, what i'd like to do is stop the simulation based on elapsed CPU time. Dymola has a way to output the CPU time and it shows up in the results as CPUtime, but I don't know how to access the variable. In other words, this is what i'd like to do, but the CPUtime variable isn't in scope:
when CPUtime > 100 then
assert(false,"Simulation taking too long");
end when;
Any suggestions, either how to access CPUtime, or other workarounds to kill simulations based on cpu time?
As already noted:
You can set this in Dymola 2022 in the simulation setup, or alternatively by setting Advanced.Simulation.MaxRunTime.
It's wall-clock time, which means that if you have a parallel simulation it will stop after 10s has passed and not when the cores together have spent 10s, and if you for some weird reason have a long sleep-call in the model it will still end.
(This was already noted in the comment - thanks Priyanka. However, stackoverflow for some reason warns that answers in comments may be lost.)

Is there a way to do a millisecond level delay in matlab?

Help for 'PAUSE' says
PAUSE(n) pauses for n seconds before continuing, where n can also be a
fraction. The resolution of the clock is platform specific. Fractional
pauses of 0.01 seconds should be supported on most platforms.
But in my case pause(0.01) doesn't do anything at all (pause, pause(n) with whole number n works)
Is there any way to make a millisecond level delay (50 ms, 100 ~ 500 ms) delay in matlab?
Matlab version is
MATLAB Version 7.9.0.529 (R2009b)
64 bit on a Windows 10 64 bit Home edition
I see two options. Let's call them the looping option and the native option. The first is just using a while loop to check if your desired time to wait is already reached. You can do this with MATLAB's stopwatch timer tic and toc. This is the normal time (not the CPU-time). As you are writing a loop, which runs at maximum speed, you might encounter a high CPU-usage but this should be OK if it is only for a couple of milliseconds.
%% looping
% desired time to wait
dt_des = 0.001; % 1 ms
% initialize clock
t_st = tic;
% looping
while toc(t_st) < dt_des
end
% read clock
toc(t_st)
The native option is using pause (make sure that you have enabled it once with pause('on')). I assumed from your question that this does not always work -- however, it does on my system (R2019b, windows 10).
%% use pause()
tic
pause(0.001)
toc
The results of both are
Elapsed time is 0.001055 seconds.
Elapsed time is 0.001197 seconds.
It's not too accurate but you might get better results if you tune the numbers on your PC.
you can also use
java.lang.Thread.sleep(10);
if you are using an old matlab version, see discussion here.

Passive wait until time elapses in MATLAB?

I am creating a real-time MATLAB animation. This means that I want to update a figure with respect to the real world clock time. For example, let's say that I want to update the figure every 0.5 seconds. I know of tic and toc, which will allow me to measure real world clock time. However, I do not want to iterate uselessly in a loop until for example toc>=0.5 is satisfied - rather, I want to do the more efficient "passive wait". Here's pseudo-code of my idea:
while (some break condition)
x = tic;
passive wait until toc>=0.5
// update figure
// update break condition
end
Could you please help me on how to do this in MATLAB?

MATLAB execution runtime limitation

I want to limit execution runtime of a function I am using in my code. Is it possible?
I am using Windows 10 with MATLAB R2015a (unlike in this question asked before: Matlab time limit for function execution), so has there been any changes to make it possible or are there any tweaks to make it possible?
For example:
H = transpose(homograpyMatrix);
t = projective2d(homograpyMatrix);
result = imwarp(img, t); % If execution takes more than X seconds - stop running
If imwarp is taking too long to compute (more than predefined X seconds) I want MATLAB to stop the running process. Is it possible?
Please note that I don't want to use tic-toc within a loop as a stopping condition, because this is not the case.
You have two options:
Parallel Computing Toolbox
With the Parallel Computing Toolbox, you can define a timeout for idle calculations. It is important to remember that this timeout is reset whenever your process enters a parfor loop or uses parfeval.
Hard-coded Timeout
In this instance you would implement a hard-coded timeout variable and check it at a regular interval to assess if you have to stop the running process or not.

Isolate Seconds from Matlab Clock

I am trying to achieve two things:
Start a function if the current time is an exact 10 seconds multiple and
Run the function in a loop every 10 seconds after that
I have a cronjob on AWS pulling data every 10 seconds. I want my MATLAB function to run specifically once that has been updated (every 10 seconds).
The function takes less than 10 seconds to run. Currently, I am doing:
tic
function
toc
elapsedTime=toc;
pause(10-elapsedTime);
but this dosent seem to be working. On top of that, I want the entire function to start at 1:00:10 (seconds).
I think using Matlab's
format shortg
c = clock
fix(c)
and some how starting at sec = [0,10,20....,60] would work, but I am not sure how to isolate the seconds from the system clock.