How to update inputs for fmincon

2 次查看(过去 30 天)
Dat Tran
Dat Tran 2016-2-15
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 个评论
Federico Chiodarelli
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 2023-2-3
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,

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2016-2-15
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 2016-2-15
编辑:Walter Roberson 2016-2-15
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 个评论
Walter Roberson
Walter Roberson 2016-2-15
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by