Cancel objective function evaluation in Optimization

1 view (last 30 days)
Dear Matlab Users,
Background:
I want to fit a model to my experimental data by using optimizers from Optimization Toolbox (so far I tried fminunc and fmincon). Thus, in the objective function I use a complex Matlab Model (including ODE) to get simulation results for all my experimental data points and then calculate the mean error between experimental and simulation data. The design variables are 5 input parameters to the simulation model. Due to the influence of the "real world" in my experimental data the objective function is non-smooth and not differentiable.
Problem:
The optimization algorithms sooner or later suggest a configuration of the design variables which leads to extremely long calculations within the ODE or a divergence in the simulation model. Even when setting physically reasonable upper and lower bounds for each design variable, some combinations of design variables will always lead to a physically impossible simulation (divergence or endless calculation time).
Is there a way to catch these cases and tell the optimization algorithm to try a new configuration of design variables and re-evaluate the objective function within the iteration? I can't find anything regarding this in the MATLAB help or forum, although this should be a common issue in optimization?
So far I can only use fminsearch optimization, as no big changes in the design variables are being done here.
Love to hear from some experts.
Best regards
Malte
  4 Comments
Walter Roberson
Walter Roberson on 2 Dec 2021
When the model cannot be solved, does the model get stuck in the ode ("extremely long calculations within the ODE" you said), or does the ODE produce nan or inf, or does the ODE finish normally but the optimizer is having trouble locating feasible points, or does the ODE finish normally but the optimizer is having trouble finding improvements over a configuration you would say is poor but techically within the constraints ?
The strategies are different for the different cases.

Sign in to comment.

Accepted Answer

Malte Semmel
Malte Semmel on 3 Dec 2021
The build-in event location functionalitiy of the ode can be used to force the ode to quit after a certain time. See answer of Jared Barber in this thread:

More Answers (1)

Alan Weiss
Alan Weiss on 2 Dec 2021
Edited: Alan Weiss on 2 Dec 2021
I think that you can write an output function for the ODE solver that will halt the solver if it takes too long or reaches too large a value. Something like the following (this is untested code, take it with a large grain of salt):
function status = myOutputFcn(t,y,flag)
persistent tm
status = 0;
switch flag
case 'init'
tm = tic;
case 'done'
% Nothing to do here
else
elapsed = toc(tm);
if elapsed > 5 % or any other time that is too large
status = 1; % stop the ODE solver
end
if norm(y) > 1e3 % or any other size that is too large
status = 1
end
end
Call the ODE solver with options:
options = odeset('OutputFcn',@myOutputFcn)
like this:
[t,y] = ode45(odefun,tspan,y0,options)
Then if you find that your ODE solver bailed through the output function you can set the objective function to NaN. Or maybe create a nonlinear constraint function that takes a positive value, such as the time or size of the ODE solution when it gets halted.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Malte Semmel
Malte Semmel on 3 Dec 2021
I think I found a similar solution to what you meant, Alan:
The build-in event location functionalitiy of the ode can be used to force the ode to quit after a certain time. See answer of Jared Barber in this thread:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!