Effacer les filtres
Effacer les filtres

How to update inputs for fmincon

2 vues (au cours des 30 derniers jours)
Dat Tran
Dat Tran le 15 Fév 2016
Commenté : Walter Roberson le 3 Fév 2023
Dear all,
How can I update Aeq, beq and pr for every iteration in such a way that: 1st iteration, the fmincon will get pr=pr0,Aeq=Aeq0,beq=beq0 to solve for p, 2nd iteration the input will be pr=p,Aeq=Aeq1,beq=beq1 to solve for p1, 3rd iteration pr=p1,Aeq=Aeq3,beq=beq3....and so on.
I would appreciate any helps!
Thanks,
Dat
function [p,fval] = MC_NT(p0,opts,N,pr0)
if nargin < 7
opts = optimoptions('fmincon','Algorithm','interior-point');
end
M=length(p0);
p0=p0(:);
pr=0.25*ones(M,1);
p=nan(M,N);
fval=nan(1,N);
pr0=0.25*ones(1,N);
for i=1:N
fun=@(p) sum(p.*log(p./pr));
[p(:,i),fval(i)] = fmincon(fun,p0(:),[],[],Aeq,beq,[],[],[],opts);
pr=p(:,i);
end
  2 commentaires
Federico Chiodarelli
Federico Chiodarelli le 3 Fév 2023
Hello, I'm actually facing the same problem (I need to update the initial guess at each iteration using the results of the previous one). Can you describe your problem and maybe generalize the solution please? Because the solution Walter Roberson gave seemed very specific for your problem, and I cannot apply it to mine.
It would be very helpful, thank you in advance!
Walter Roberson
Walter Roberson le 3 Fév 2023
Looking back I am no longer sure that what I proposed in 2016 was what was being looked for.
The code I proposed back then was for the case where you wanted to do a series of independent fmincon, each one using a set of parameters known in advance. So for example if you wanted to minimize a system for a starting point of 100 metres altitude, and then minimize again for a starting point of 200 metres altitude, and so on.
In the code I posted back then, the function to be minimized was effectively being updated each iteration according to the previous result -- but within any one call to fmincon(), the function was consistent.
But sometimes when people talk about updating something each iteration, they mean that the first time that fmincon internally updates its record of best-found-so-far that they want to update some of the parameters or constraints, so that the problem being minimized is changing as fmincon proceeds. fmincon() cannot handle that situation. fmincon() is designed in such a way that a call to the objective function with a given guess x, must always return the same result, no matter which iteration it is being called on. fmincon() cannot be used for stocastic systems for example,

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 15 Fév 2016
Change the Aeq,beq to Aeq{i}, beq{i} and pass those cell arrays in to MC_NT as additional parameters.

Dat Tran
Dat Tran le 15 Fév 2016
Modifié(e) : Walter Roberson le 15 Fév 2016
Thank you so much for the help! I have tried it but the solution is NaN.
Please correct me if I am wrong. As you suggested the function and objective function should be changed as below?
function [p,fval] = MC_NT(p0,opts,N,pr0,Aeq{i},beq{i})
[p(:,i),fval(i)] = fmincon(fun,p0(:),[],[],Aeq{i},beq{i},[],[],[],opts);
Thank you!
Dat
  1 commentaire
Walter Roberson
Walter Roberson le 15 Fév 2016
function [p,fval] = MC_NT(p0, N, pr0, Aeq, beq, opts)
if nargin < 6
opts = optimoptions('fmincon','Algorithm','interior-point');
end
M = length(p0);
p0 = p0(:);
pr = 0.25*ones(M,1);
p = nan(M,N);
fval = nan(1,N);
pr0 = 0.25*ones(1,N);
for i = 1:N
fun=@(p) sum(p.*log(p./pr));
[p(:,i), fval(i)] = fmincon(fun, p0(:), [], [], Aeq{i}, beq{i}, [], [], [], opts);
pr = p(:,i);
end

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by