MATLAB Answers

Objective and constraints having a common function - Problem based in Matlab 2016b

6 views (last 30 days)
Hi,
In my current optimization problem, both the objective and cosntraint functions are calculated following a time-consuming simulink run. In order to avoid having to run the simulation twice at each iteration (one to compute the objective, one to compute the constraint), I would like to apply the same method as described in https://fr.mathworks.com/help/optim/ug/objective-and-constraints-using-common-function.html.
However, my work only gives access to Matlab 2016b, and the functions used are not available yet : optimvar, fcn2optimexpr.
Is there a way to convert my function "compute_all" (which computes both the objective and the constraint) to an optimization expression, but in Matlab 2016b ?
Thank you for your help,
Thomas

  0 Comments

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 31 Jul 2020
Perhaps what you are looking for is in Objective and Nonlinear Constraints in the Same Function.
Alan Weiss
MATLAB mathematical toolbox documentation

  5 Comments

Show 2 older comments
Thomas Gilormini
Thomas Gilormini on 31 Jul 2020
Maybe I expressed myself badly.
My first option was indeed to use only fmincon with the method described in https://fr.mathworks.com/help/optim/ug/objective-and-nonlinear-constraints-in-the-same-function.html. This worked fine, I had my functions computeall and runobjconstr. However, I now think that Multistart is more adapted to my problem. From what I have seen in the documentation, this is how I should write it :
prob = createOptimProblem('fmincon', 'objective', @obj_fun, 'nonlcon', @cons_fun, 'x0',[5 6.25], 'lb',[0.1 0.1], 'ub', [30 30]);
ms = MultiStart('UseParallel',false,'Display','iter');
[bestx,fval,exitflag,output,solutions] = run(ms, prob, 100) ;
Isn't this the problem-based approach ? In this case, my issue is that I do not know how to split my computeall function into the objective and constraint functions when creating the problem. Is there another way to use Multistart ?
Thanks for your help,
Thomas
Alan Weiss
Alan Weiss on 31 Jul 2020
No, this is not the problem-based approach. However, I finally see what you are getting at. MultiStart runs the objective and constraint functions inside its call, and does not necessarily enable you to avoid the double evaluation. I think that you could write your objective and constraint functions using persistent variables to get the same functionality as the nested function trick. But that would take some work and testing.
I suggest that you simply write a loop calling fmincon repeatedly with different initial points, using the nested function trick to avoid double evaluation. Really, all MultiStart does is run such a loop. Well, it also gathers the results in a convenient form. You can duplicate that little nicety using an idea I described in Loren's blog.
The bottom line is, you might as well just use the nested function trick and fmincon because it is not so straightforward to get that functionality in MultiStart. Or write the equivalent computeall function using persistent variables, and then show us how you made it work.
Alan Weiss
MATLAB mathematical toolbox documentation
Thomas Gilormini
Thomas Gilormini on 4 Aug 2020
Okay, thanks a lot for your help ! I think I will follow your advice and simply run a loop using fmincon with different starting points, this seems more adapted to me.
Thank your for all the answers,
Thomas

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!