Passing extra parameters back through an optimisation function

9 visualizzazioni (ultimi 30 giorni)
Hi there, i have a bit of a problem... i'll try to explain: i have to optimise z in this case (i use ... as placeholders for the not relevant parts). so i started with this:
[z_opt] = fmincon(@(z)my_function(z,a,b),...);
then the function would be:
function my_results = my_function(z,a,b)
r1 = really_time_consuming_function_1(z,a);
r2 = really_time_consuming_function_2(z,a);
my_result = some_other_function(r1,r2,b);
so now i get my optimised z_opt and since i need both r1 and r2 in the main program as well i have to calculate them again after the optimisation:
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
i know, i could define global variables to pass r1 and r2 back to the main program, but i think there has to be a better way (without the use of global variables). so is there a way to pass back more than one result, but still just optimise the first one in the vector? (something like this...)
my_result = [some_other_function(r1,r2,b),r1,r2];
as far as i have seen, the "passing extra parameters" help just is about passing them through the optimisation function in the to-be-optimised-function, but not the other way round. thanks for any help,
manuel

Risposta accettata

Matt Kindig
Matt Kindig il 3 Apr 2012
One way is to wrap the call to fmincon in an outer function, and have the really_time_consuming_function's as a sub-function of this outer function, so that they share the same workspace. This way you can output variables r1 and r2 back to the main program. Something like
function [z_opt, r1, r2]=mainfcn(z,a,b, ...)
r1 = []; r2 = []; z_opt = []; %initialize vars
z_opt = fmincon(@my_function,...)
function my_result = my_function(z)
%a and b are accessible from the main function workspace
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
end
end

Più risposte (2)

Oleg Komarov
Oleg Komarov il 3 Apr 2012
If I interpret it correctly, you obtain z_opt and within my_function(z,a,b) you want to skip teh following part:
r1 = really_time_consuming_function_1(z,a);
r2 = really_time_consuming_function_2(z,a);
and do directly
my_result = some_other_function(r1,r2,b);
where r1 and r2 are the 'optimal' ones. However, with this approach you skip the optimization so I guess your objective is something else.
Guessing, you simply want to return the optimized r1 and r2 in order to avoid calling
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
Then write the function to return multiple outputs
function [my_results,r1,r2] = my_function(z,a,b)
...

m_si
m_si il 3 Apr 2012
the problem is that both r1 and r2 are changing with the yet unknow z. so for every optimisation step a get a new r1 and r2 and the optimum is depending from r1 and r2. and i can't do the whole thing backwards (because r1 is actually the failure probability i get using a FORM analysis).
basically i would like to get the "last" r1 and r2 (from the last optimisation step) out to the main program, you could call them r1_opt and r2_opt. these have to be the same values i get when i add
r1_opt = really_time_consuming_function_1(z_opt,a);
r2_opt = really_time_consuming_function_2(z_opt,a);
and i want to avoid that (to make the program faster...)
so i don't know if there is a way just to minimize one return value of a function that returns a whole vector so something such as:
[z_opt,r1,r2] = fmincon(@([z,:,:])my_function(z,a,b),...);
  1 Commento
Oleg Komarov
Oleg Komarov il 3 Apr 2012
[z_opt,r1,r2] = fmincon(@(z)my_function(z,a,b),...);
Should do the trick with the suggested change in my asnwer.

Accedi per commentare.

Categorie

Scopri di più su Parallel Computing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by