how to use ``while'' in implementing an algorithm?

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

Susan
Susan le 30 Avr 2019
Modifié(e) : dpb le 30 Avr 2019
I have got a weird results.
Here is the situation, I have started with an initial ptilda0 that satisfied all the constraints, fmincon() gives me the optimal ptilda. At the second round I used this ptilda as an initial value and call fmincon(), fmincon gives me the optimal value for the ptilda. I call this optimal value as an initial value and call fmincon() again. Now, fmincon() gives me error
Error using sqpInterface
Finite difference derivatives at initial point contain Inf or NaN values. Fmincon cannot continue.
Error in fmincon (line 823)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpInterface(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
Error in Catculate_optimal_ptilda (line 31)
[ptilda_opt,fval_ptilda,flag_ptilda] = fmincon(objfun_ptilda,ptilda0,A_ptilda,b_ptilda,[],[],lb_ptilda,ub_ptilda,nonlcon_ptilda,opts);
anyone knows what deoes it mean? How should I get rid of this error? Thanks
dpb
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
Susan le 1 Mai 2019
Thank you so much for your reply.
Could you please tell me how I can "plot results at and around the solution points to visualize what the function does in those neighborhoods"? Thanks in advance
dpb
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
Susan le 1 Mai 2019
thanks again for your reply.
I am still struggling with solving this error:
"Error using sqpInterface
Finite difference derivatives at initial point contain Inf or NaN values. Fmincon cannot continue."
I have attached my codes. Could you please kindly take a look and let me know how I can solve the issue? The main code that needs to be executed called "test_Lagrangian" and it calls several functions. I have attached all of them. Thanks again.
I DO appreciate your help and time.
Susan
Susan le 2 Mai 2019
Could you please give me some feedbacks? Thanks
dpb
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
Susan le 2 Mai 2019
Ok, thanks.
dpb
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! :)
Stephen23
Stephen23 le 2 Mai 2019
@Su san: do you have the optimization toolbox ?
Susan
Susan le 2 Mai 2019
@Stephen Cobeldick Yes, I do.
Susan
Susan le 2 Mai 2019
@dpb Thanks for offering the help.
Would you mind if I ask you what you mean by a subset of code? I didn't get you quit well. The code calls several functions to calculate a parameter of interest.
Thanks again

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Function Creation dans Centre d'aide et File Exchange

Question posée :

le 30 Avr 2019

Commenté :

le 2 Mai 2019

Community Treasure Hunt

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

Start Hunting!

Translated by