Return to previous step

조회 수: 20 (최근 30일)
Matheus
Matheus 2017년 8월 17일
댓글: Walter Roberson 2017년 8월 18일
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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 8월 17일
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
Image Analyst
Image Analyst 2017년 8월 18일
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 2017년 8월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by