Return to previous step

19 views (last 30 days)
Matheus
Matheus on 17 Aug 2017
Commented: Walter Roberson on 18 Aug 2017
Hello,
I have a code that includes some steps. I would like to know how to make the process return to the first step if a condition is true. In the loop if k=length of x0, the process needs to back to step 1. Also I need to know how many interactions the code did in the end.
function [x_opt,f_opt,k] = gc_completo(fob,g_fob,x0,tol_grad);
tic
c0 = feval(g_fob,x0);
k = 0;
if norm(c0) < tol_grad
x_opt = x0;
f_opt = feval(fob,x_opt);
else
d= -c0; %step 1
alfa0 = equal_interval_line_search(x0,d,fob,0.5,1e-6);
x1= x0+ alfa0*d;
c1 = feval(g_fob,x1); %evaluate gradient at x1
while norm(c1) > tol_grad
beta = (norm(c1)/norm(c0))^2;
d= -c1+beta*d;
alfa1 = equal_interval_line_search(x1,d,fob,0.5,1e-6);
x2= x1+alfa1*d;
c0=c1;
c1= feval(g_fob,x2);
x1=x2;
k=k+1;
end
x_opt = x1; %optimum point
f_opt = feval(fob,x_opt); %cost funtion value
end
toc

Answers (1)

Walter Roberson
Walter Roberson on 17 Aug 2017
It is not clear what an "interaction" is that you want to count. I will guess it has to do with the number of searches.
search_count = 0;
redo_step1 = true;
while redo_step1
redo_step1 = false;
d= -c0; %step 1
alfa0 = equal_interval_line_search(x0,d,fob,0.5,1e-6);
search_count = search_count + 1;
x1 = x0+ alfa0*d;
c1 = feval(g_fob,x1); %evaluate gradient at x1
k = 0;
while norm(c1) > tol_grad
beta = (norm(c1)/norm(c0))^2;
d= -c1+beta*d;
alfa1 = equal_interval_line_search(x1,d,fob,0.5,1e-6);
search_count = search_count + 1;
x2 = x1 + alfa1*d;
c0=c1;
c1= feval(g_fob,x2);
x1=x2;
k = k+1;
if k == length(x0)
redo_step1 = true;
end
end
end
  3 Comments
Image Analyst
Image Analyst on 18 Aug 2017
You forgot to put in a failsafe. Never use a while loop without one or you could get an infinite loop, which is what might be happening. For example if it's your expectation that the loop should never execute more than a million times, do this:
maxIterations = 1000000;
loopCounter = 0;
while redo_step1 && loopCounter < maxIterations
% Do stuff
loopCounter = loopCounter + 1;
end
if loopCounter >= maxIterations
warningMessage = sprintf('Max iterations of %d reached and loop exited early', maxIterations);
uiwait(warndlg(warningMessage));
end
Walter Roberson
Walter Roberson on 18 Aug 2017
You said that you wanted to redo step 1 in some cases. The framework I presented shows a way of handling that
  1. set a flag to true indicating that step1 needs to be done
  2. enter into a loop that will not exit until that flag is false
  3. assume that re-doing is the exception that will be specifically detected, so set the flag to be false inside the loop. This way the "while" loop will exit next time around unless something specifically says that it needs to be redone
  4. do the calculation. Inside the calculation, if the conditions are right, set the flag indicating you need to redo.
I would suggest, though, that probably instead of
if k == length(x0)
redo_step1 = true;
end
you want
if k == length(x0)
redo_step1 = true;
break
end
otherwise your inner loop is not exited when k reaches the boundary.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!