how to use ``while'' in implementing an algorithm?
Afficher commentaires plus anciens
Hello guys,
I would like to implement the following algorithm in MATLAB. Could someone please kindly help me out?
I have an objective function (f) that I would like to maximize with respect to 4 variables (c, p, ptilda, v). At each step, I assume one of the variable is the optimization variable and three others are fixed and given.
When I find the c_opt, p_opt, ptilda_opt, and v_opt I calculate the f with these valuse and I continue till the difference between two consecutive f is less than a given epsilon.
I will start with the initial values, but at the second iteration I would like to use the c_opt, p_opt, ptilda_opt, and v_opt that I found in previous iteration as an initial values. Can someone kindly take a look at the following code and tell me what modifications are needed in order to get what I am looking for? Thanks in advance.
%%
c0 = zeros(IL, JL, K, M); %initial value for variable c
p0 = ones(JW , K); %initial value for variable p
ptilda0 = zeros(IL, M, JL, K);%initial value for variable ptilda
v0 = cell(IL, JL, JL, K, M); %initial value for variable v. Each cell contains a matrix of size Nt*Nr
iteration = 1;
epsilon = 1e-2;
while 1
%%%%%% First Step
c = sym('c', [IL, JL, K, M)];
objfun_c_symbExpression = f(c, p0, ptilda0, v0);
objfun_c_AnonymousFunction = matlabFunction(objfun_c_symbExpression, 'Vars', {c});
objfun_c = @(c) objfun_c_AnonymousFunction(c);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[c_opt,fval_c,flag_c] = fmincon(objfun_c,c0,A_c,b_c,[],[],lb_c,ub_c,[],opts); % I have defined all A_c, b_c, lb_c, ub_c
%%%% Second Step
p = sym('p', [JW , K]);
objfun_p_symbExpression = f(c_opt, p, ptilda0, v0);
objfun_p_AnonymousFunction = matlabFunction(objfun_p_symbExpression, 'Vars', {p});
objfun_p = @(p) objfun_p_AnonymousFunction(p);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[p_opt,fval_p,flag_p] = fmincon(objfun_p,p0,[],[],[],[],lb_p,ub_p,nonlcon_p,opts);
%%%% Third Step
ptilda = sym('ptilda', [IL, M, JL, K]);
objfun_ptilda_symbExpression = f(c_opt, p_opt, ptilda, v0);
objfun_ptilda_AnonymousFunction = matlabFunction(objfun_ptilda_symbExpression, 'Vars', {ptilda});
objfun_ptilda = @(ptilda) objfun_ptilda_AnonymousFunction(ptilda);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[ptilda_opt,fval_ptilda,flag_ptilda] = fmincon(objfun_ptilda,ptilda0,A_ptilda,b_ptilda,[],[],lb_ptilda,ub_ptilda,nonlcon_ptilda,opts);
%%%% Fourth Step (I have problem in implementing this part)
v = sym('v', ....);
objfun_v_symbExpression = f(c_opt, p_opt, ptilda_opt, v);
objfun_v_AnonymousFunction = matlabFunction(objfun_v_symbExpression, 'Vars', {v});
objfun_v = @(v) objfun_v_AnonymousFunction(v);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[v_opt,fval_v,flag_v] = fmincon(objfun_v,v0,A_v,b_v,[],[],lb_v,ub_v,nonlcon_v,opts);
%% Calculate the f with the computed varaibles
f_evolution = [f_evolution, f(c_opt, p_opt, ptilda_opt, v_opt)];
%% termination criterion
if size(f_evolution, 2)>2
err = abs(f_evolution(end) - f_evolution(end - 1) );
if err < epsilon
break,
end
end
iteration=iteration+1;
end
12 commentaires
dpb
le 30 Avr 2019
The minimum location found has moved away from the initial point towards some boundary such that even if the actual solution itself is still computable and feasible, trying to compute numerical differences from that point with the various variables ends up reaching a point that (most likely) is trying to divide by zero or an exponential function with large argument of similar issue.
You didn't show the functional itself so we've no way to guess what that might look like; you'll want to try to plot results at and around the solution points to visualize what the function does in those neighborhoods if it isn't easy to tell simply by inspection.
Susan
le 1 Mai 2019
dpb
le 1 Mai 2019
Dunno....don't know what the function returns...is it 1D,2D,3D... :)
Whatever is a reasonable plotting routine for the type of the functional you're evaluationg...
Susan
le 1 Mai 2019
Susan
le 2 Mai 2019
dpb
le 2 Mai 2019
I've done so.
There's WAY too much general code without any attempt to isolate only the pieces needed to evaluate and plot the actual functional itself...I simply don't have the time nor energy to try to dig through it all and figure out "who's who in the zoo", sorry...
That should be a starting point for any type of fitting or optimization process--having a way to visualize the problem.
Susan
le 2 Mai 2019
dpb
le 2 Mai 2019
If you can prepare a subset of code (with data) that can be used to evaluate the functional over ranges of interest and still have difficulties in understanding what is going on I'll be glad to try to help further...but I suspect when you have done so, the problem will have become patently obvious to you already! :)
Susan
le 2 Mai 2019
Susan
le 2 Mai 2019
Réponses (0)
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!