Change the value of a variable between each iteration of fminsearch

Ivan Cantarin

Ivan Cantarin (view profile)

さんによって質問されました 2019 年 3 月 13 日

Jeff Miller (view profile)

さんによって コメントされました 2019 年 3 月 15 日
Jeff Miller

Jeff Miller (view profile)

さんの 回答が採用されました
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 ?

Alan Weiss

Alan Weiss (view profile)

2019 年 3 月 13 日
I do not understand what you hope to accomplish. You are free to ask fminsearch to take just one iteration and then stop, but I don't think that is what you want.
If you would explain what you hope to get out of this, what you are really trying to obtain as the result of some calculation, then maybe we would have some ideas.
Alan Weiss
MATLAB mathematical toolbox documentation
John D'Errico

John D'Errico (view profile)

2019 年 3 月 13 日
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.
Stephen Cobeldick

Stephen Cobeldick (view profile)

2019 年 3 月 14 日
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" ?

サインイン to comment.

2 件の回答

Jeff Miller (view profile)

2019 年 3 月 14 日
採用された回答

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.

Ivan Cantarin

Ivan Cantarin (view profile)

2019 年 3 月 15 日
I don't know, how can I try this ?
Jeff Miller

Jeff Miller (view profile)

2019 年 3 月 15 日
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.

サインイン to comment.

Alan Weiss (view profile)

2019 年 3 月 14 日

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 件のコメント

Stephen Cobeldick

Stephen Cobeldick (view profile)

2019 年 3 月 14 日
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 ! サインイン to comment.