Change the value of a variable between each iteration of fminsearch

9 vues (au cours des 30 derniers jours)
Ivan Cantarin
Ivan Cantarin le 13 Mar 2019
Commenté : Jeff Miller le 15 Mar 2019
Hello, I need help to find a way for switching the value of a variable between each iteration of an optimisation by "fminsearch". Is it even possible ?
Actually, it's a ponderation factor of a cost function which I made in Simulink.
My code :
x0=[39,102]; %Initialisationd de Kc, Ti et des vecteurs
[x,fval,history] = my_problem_2(x0); %On ne fait tourner la routine d'optimisation
Kp_v = history(:,1);
Ti_v = history(:,2);
[p,~] = size(Kp_v);
Jc_v_opt = ones(p,1); %On initialise les différents vecteurs
gamma_v = ones(p,1);
gamma= 5000;
for i = 1:p
Kp_inc = Kp_v(i,1); %On parcourt les vecteurs de l'historique des points, et pour chaque duo de points (Kp et Ti), on calcule le coût associé
Ti_inc = Ti_v(i,1);
Kps=num2str(Kp_inc); %On transforme une valeur en un string pour pouvoir l'envoyer dans le bloc
Tis=num2str(Ti_inc);
set_param('test_PI_vis/PI incrémental pour centrale de vis','Kp_inc',Kps,'Ti_inc',Tis);
[~,~,Y1,Y2,Y3] =sim('test_PI_vis',600000,[]); %On simule avec les paramètres de cette simulation pendant 100000 sec
Jc_v_opt(i,1)=Y1(end); %En sortie de la simu, on a les coût qu'on envoie dans des vecteurs
if Y2(end) == 0 %On calcule le gamma pour chaque set de paramètres parcouru par la routine d'optimisation
gamma = 1;
else
gamma = Y3(end)/Y2(end);
end
if gamma == 0
gamma_v(i,1) = 1;
else
gamma_v(i,1) = gamma;
end
end
The function my_problem_2 is the function doing the optimisation with fminsearch.
Actually, I want to calculate gamma and change its value for each iteration. I'm calculating it for each iteration in the loop but at the moment, it's useless, because I don't know how to change it when fminsearch is working.
Here is the code of my_problem_2 if it can help :
function [x,fval,history] = my_problem_2(x0)
history = [];
options = optimset('OutputFcn', @myoutput);
[x,fval] = fminsearch('m_optipi_vis', x0,options);
function stop = myoutput(x,~,state)
stop = false;
if isequal(state,'iter')
history = [history; x];
end
end
end
And the code of my function m_optipi_vis :
function [Jc]=m_optipi_vis(x) %Permet d'utiliser la fonction fminsearch dans le but de trouver le meilleur Kp et Ti
% X est donné dans la fonction fmnsearch
Kp_inc=x(1); %On donne une valeur initiale pour Kp
Ti_inc=x(2); %On donne une valeur initiale pour Ti
Kps=num2str(Kp_inc); %On transforme une matrice en un string pour pouvoir l'envoyer dans le bloc
Tis=num2str(Ti_inc);
set_param('test_PI_vis/PI incrémental pour centrale de vis','Kp_inc',Kps,'Ti_inc',Tis);
[~,~,Y1,~,~]=sim('test_PI_vis',600000,[]); %600000 est le temps de simulation
Jc=Y1(end);
end
I know I'm asking a lot but I'm really blocked on this.
Can someone help me plz ?
  3 commentaires
John D'Errico
John D'Errico le 13 Mar 2019
Alan is as puzzled as I am, I think for good reason. What you are doing would seem to be creating an objective function that is signifcantly non-smooth, and probably discontinuous. As such, it will make fminsearch potentially fail (I'd say you will make your results unstable), even if you managed to figure out how to do this.
Your goal may possibly be accomplishable using a tool like GA, which is more tolerant of such shenanigans. But even there, it is not at all clear what you are trying to accomplish.
Stephen23
Stephen23 le 14 Mar 2019
Ivan Cantarin's "Answer" moved here:
Actually, I have a cost function with two parts. And I want to make the cost of both parts always equal, so fminsearch minimizes them equally at each iteration.
For that, I need to calculate at each iteration gamma which is the ponderation factor, and change it to make the cost of both parts equal !
But maybe, like you said, it's impossible to do this with "fminsearch" ?

Connectez-vous pour commenter.

Réponse acceptée

Jeff Miller
Jeff Miller le 14 Mar 2019
If gamma has to be changed at each iteration, can't you let the error function change it? The error function is called at each iteration.
  2 commentaires
Ivan Cantarin
Ivan Cantarin le 15 Mar 2019
I don't know, how can I try this ?
Jeff Miller
Jeff Miller le 15 Mar 2019
I don't understand the problem well enough to give any detailed suggestions, but my general idea was that the error function could simply call the function to recompute gamma, passing as parameters the current level and flow costs and whatever other values are needed.

Connectez-vous pour commenter.

Plus de réponses (1)

Alan Weiss
Alan Weiss le 14 Mar 2019
I still think that I do not understand what you are really trying to do. But if you wnat to keep two functions equal, then in fmincon you can represent this requirement as a nonlinear equality constraint. Such a constraint will not necessarily be satisfied at each iteration, but should be satisfied well enough by the end.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 commentaire
Stephen23
Stephen23 le 14 Mar 2019
Ivan Cantarin's "Answer" moved here:
I will show you with a screen, it will be easier.
You see, there are the level cost, and the flow cost. But my fminsearch is minimizing the global cost. I want both costs to be the same order of magnitude at each iteration, so fminsearch optimizes them equally and doesn't focus on the most important cost. And like I said, for that, gamma needs to be calculate at each iteration and changed in the Workspace !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Event Functions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by