
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% private_FORLOOP_ROUTINE1
%
% The workhorse routine. 
%
% Ported to Matlab by Jason Lott, University of Alabama at Birmingham
%
% CONTACT:hoffa@uab.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function a = private_FORLOOP_ROUTINE1(b)


global NV
global NVMAX
global X
global XMAX
global XMIN
global DELTX
global DELMIN
global ERR
global FOBJ
global VEC
global DLX
global XS
global FSTORE
global DX
global SALVO
global XOSC
global FOSC
global ARG
global STCUT
global ACK
global FACUP
global RZERO
global XPLUS
global FSAVE
global FBEST
global XSAVE
global ABSDX
global FPREV
global DENOM
global DEL
global DXZ
global DXU
global DFZ
global DFU
global ABSVEC
global SUMV
global CINDER
global COXCOM
global COSIN
global STEPS
global J
global JFLAT
global JFLMIN
global JOCK
global JUMP
global JVARY
global JX
global K
global KERFEL
global KL
global KW
global MASK
global MATRX
global MINSOC
global MAXOSC
global MAXSTP
global NACK
global NACTIV
global NAH
global NCIRC
global NEQUAL
global NF
global NFLAT
global NFMAX
global NFSAV
global NGATE
global NGIANT
global NONZER
global NOREP
global NOSC
global NOUT
global NRETRY
global NSSW
global NSTEPS
global NT
global NTRACE
global NV
global NEXTRA
global NZIP
global tag_380
global tag_390
global wtag
global tag
global continueflag
global T
global Y
global YSIG
global grandtag
global KFLAG
global DX
global XS
global DLX
global fortag
global KFLAGENCOUNTER


grandtag = 1;
KFLAGENCOUNTER = 0;

while grandtag == 1;
   
   fortag = 1;
   %disp('Im starting the 640 protocol!')
   %pause
   
   for JX=1:NV
      
     
      if fortag == 1;
         
         NOUT = 0;												%NOUT= Number of trial points out of bounds; used in setting JFLAT(JX)
         NEQUAL = 0;											%NEQUAL = Number of trial points with FOBJ==FBEST; used in setting JFLAG(JX)
         JFLAT(JX)=0;											%JFLAT(JX) will be nonzero if changing X(JX) did not change FOBJ
         VEC(JX) = RZERO;									%VEC(J) = current vector number of steps in X(J)
         DLX(JX) = RZERO	;									%DLX(JX) = change in X(JX)
         
         if MASK(JX) == 0
            
            NACK = NACK + 1;
            ABSDX = DX(JX);
         
            if ABSDX<RZERO
               
               ABSDX = -ABSDX;
               
            end
            
            XSAVE = X(JX);
            XPLUS = XSAVE + DX(JX);
         
            if XPLUS == XSAVE
               
               JFLAT(JX) = 2;
               X(JX) = XSAVE;
               NCIRC = NCIRC + 1;
               
               if NCIRC>=NACTIV
                  
                  FSTORE(JX) = FBEST;
                  tag = 1;
               
                  if NTRACE>=1 & tag ==1
                     
                     disp('PRINTING REMAINING TRACES')
                     disp('FBEST= '), disp(FBEST)
                     disp('NF= '), disp(NF)
                     disp('VALUES OF X(J)= ')
                  
                     for J=1:NV
                        
                        disp(X(J))
                        
                     end
                     
                  elseif tag == 1
                     
                     NGATE = 1;
                     
                     for J=1:NV
                        
                        if MASK(J)~=0
                           
                           DX(J) = DX(J)/STCUT;
                           
                        else
                           
                           ABSDX = DX(J);
                           
                           if ABSDX<RZERO
                              
                              ABSDX = -ABSDX;
                              
                           end
                           
                           if ABSDX>DELMIN(J)
                              
                              NGATE = 0;
                              
                           end
                           
                           DX(J) = DX(J)/STCUT;
                           
                        end
                        
                     end
                     
                     if NGATE == 1
                        
                        KFLAG = 1
                        %disp('ENTERING KFLAG EXIT from STEPIT')
                        %pause
                        private_KFLAG_EXIT(KFLAG);
                     
                     elseif NFLAT<=0
                        
                        if NTRACE>=1
                           
                           disp('STEP SIZES REDUCED TO: ')
                           
                           for J=1:NV
                              
                              disp(DX(J))
                              
                           end
                           
                        end
                        
                        if NF>NFMAX
                           
                           KFLAG = -2;
                           %disp('ENTERING KFLAG EXIT from STEPIT')
                           %pause
                           private_KFLAG_EXIT(KFLAG);
                        
                        else
                           
                           NCIRC = 0;
                           NZIP = 0;
                           NACK = 0;
                           grandtag = 1;
                           fortag = 0;						%640 Routine-starts me back over in the for-loop, reseting NCIRC
                           
                        end
                        
                     else
                        
                        JFLMIN = 5;
                        
                        for J=1:NV
                           
                           if MASK(J) == 0 & JFLAT(J)<JFLMIN
                              
                              JFLMIN = JFLAT(J)
                              
                           end
                           
                        end
                        
                        if JFLMIN>=1
                           
                           KFLAG=2;
                           %disp('ENTERING KFLAG EXIT from STEPIT')
                           %pause
                           private_KFLAG_EXIT(KFLAG);
                        
                        else
                           
                           if NTRACE>=1
                              
                              disp('STEP SIZES REDUCED TO:')
                              
                              for J=1:NV
                                 
                                 disp(DX(J))
                                 
                              end
                              
                           end
                           
                           if NF>NFMAX
                              
                              KFLAG = -2;
                              %disp('ENTERING KFLAG EXIT from STEPIT')
                              %pause
                              private_KFLAG_EXIT(KFLAG);
                           
                           else
                              
                              NCIRC = 0;  %CALL TO 640 ROUTINE
                              NZIP = 0;
                              NACK = 0;
                              fortag = 0;
                              grandtag = 1;
                              
                           end
                           
                        end
                        
                     end
                     
                  end
                  
               else
                  
                  if (NZIP<=0 & NACK<=1) | (VEC(JX) == RZERO)
                     
                     FSTORE(JX) = FBEST;
                     
                     if NF>NFMAX
                        
                        KFLAG = -2;
                        %disp('ENTERING KFLAG EXIT from STEPIT')
                        %pause
                        private_KFLAG_EXIT(KFLAG);
                                                                       
                     else
                        
                        %CONTINUE IN BIG FOR LOOP
                        
                     end
                     
                  else
                     
                     ABSVEC = VEC(JX);
                     
                     if ABSVEC<RZERO
                        
                        ABSVEC = -ABSVEC;
                        
                     end
                     
                     if ABSVEC<FACUP
                        
                        %disp('ENTERING 250 ROUTINE from STEPIT 1')
                        %pause
                        private_250_ROUTINE(1)
                        
                     else
                        
                        DX(JX) = DX(JX)*ACK;
                        VEC(JX) = VEC(JX)/ACK;
                        tag = 1;
                        
                        if NOSC>0
                           
                           for J=1:NOSC
                              
                              ERR(JX,J) = ERR(JX,J)/ACK;
                              
                           end
                           
                        end
                        
                        if tag == 1 & NTRACE>=1
                           
                           disp('STEP SIZE '),disp(JX),disp(' INCREASED TO '),disp(DX(JX))
                           
                        end
                        
                        %disp('ENTERING_250_ROUTINE fromt STEPIT 2')
                        %pause
                        private_250_ROUTINE(1)
                        
                     end
                     
                  end
                  
               end
               
            else
               
               if XPLUS~=XSAVE
                  
                  X(JX) = XSAVE +DX(JX);
                  JVARY = 0;
               
                  if JOCK>0
                     
                     JOCK = 0;
                     JVARY = JX;
                  
                  end
                  
                  if (X(JX)>=XMIN(JX))&(X(JX)<=XMAX(JX))
                     
                     private_FUNK(1);
                     NF = NF+1;
                     JVARY = JX;
                     FPREV = FOBJ;
                     
                     if FOBJ<FBEST
                        
                        NCIRC = 0;
                        NSTEPS = 0;
                        DEL = DX(JX);
                        wtag = 1;
                        
                        while wtag == 1
                           
                           FPREV = FBEST;
                           FBEST = FOBJ;
                           VEC(JX) = VEC(JX) + DEL/ABSDX;
                           DLX(JX) = DLX(JX) + DEL;
                           NSTEPS = NSTEPS + 1;
                           
                           if NSTEPS>=MAXSTP
                              
                              JOCK = 1;
                              %disp('ENTERING 210 ROUTINE from STEPIT 1')
                              %pause
                              private_210_ROUTINE(1);
                              %disp('Im back')
                              wtag = 0;												%Kicks me out of this damn while loop
                              continueflag = 0;
                              
                           else
                              
                              DEL = ACK*DEL;
                              XPLUS = XSAVE;
                              XSAVE = X(JX);
                              X(JX) = XSAVE + DEL;
                           
                              if (X(JX)<XMIN(JX))|(X(JX)>XMAX(JX))
                                 
                                 X(JX) = XSAVE;
                                 %disp('ENTERING 210 ROUTINE from STEPIT 2')
                                % pause
                                 private_210_ROUTINE(1);
                             
                                 wtag = 0;		
                                 %Kicks me out of this damn while loop
                                 continueflag = 0;
                                 
                              else
                                 
                                 private_FUNK(1);
                                 NF = NF +1;
                                 
                                 if FOBJ<FBEST
                                    
                                    wtag = 1;	
                                    %Continue in while loop
                                       
                                 else
                                    
                                    wtag = 0;										%Kicks me out of this damn while loop, to continue with next statements
                                    continueflag =1;
                                    
                                 end
                                 
                              end
                              
                           end
                           
                        end
                        
                        if continueflag == 1
                           
                           DXZ = XSAVE-XPLUS;
                           DXU = X(JX)-XSAVE;
                           DFZ = FBEST-FPREV;
                           DFU = FOBJ-FBEST;
                           DENOM = DFZ*DXU-DFU*DXZ;
                           
                           if DENOM == RZERO
                              
                              X(JX) = XSAVE;
                              %disp('ENTERING 210 ROUTINE from STEPIT 3')
                              %pause
                              private_210_ROUTINE(1);
                           
                           else
                              
                              DEL = (DFZ*DXU^2+DFU*DXZ^2)/(DENOM+DENOM);
                              X(JX)=XSAVE+DEL;
                              
                              if X(JX) == XSAVE
                                 
                                 %disp('ENTERING 210 ROUTINE from STEPIT 4')
                                 %pause
                                 private_210_ROUTINE(1);
                                 
                              else
                                 
                                 private_FUNK(1);
                                 NF = NF +1;
                                 
                                 if FOBJ>=FBEST
                                    
                                    X(JX) = XSAVE;
                                    %disp('ENTERING 210 ROUTINE from STEPIT 5')
                                    %pause
                                    private_210_ROUTINE(1);
                                    
                                 else
                                    
                                    FBEST = FOBJ;
                                    DLX(JX) = DLX(JX) + DEL;
                                    VEC(JX) = VEC(JX) + DEL/ABSDX;
                                    JOCK = 1;
                                    %disp('ENTERING 210 ROUTINE from STEPIT 6')
                                    %pause
                                    private_210_ROUTINE(1);
                                    
                                 end
                                 
                              end
                              
                           end
                           
                        end
                        
                     else
                        
                        if FOBJ==FBEST
                           
                           NEQUAL = 1;
                           
                        end
                        
                        %disp('ENTERING 90 ROUTINE from STEPIT 1')
                        %pause
                        private_90_ROUTINE(1);
                        
                     end
                     
                  else
                     
                     NOUT = 1;
                     %disp('ENTERING 90 ROUTINE from STEPIT 2')
                     %pause
                     private_90_ROUTINE(1);
                     
                  end
                  
               else
                  
                  JFLAT(JX) = 2;
                  %disp('ENTERING 140 ROUTINE from STEPIT 3')
                  %pause
                  private_140_ROUTINE(1);
               
               end
               
            end
            
         else
            
            JFLAT(JX) = 1;
            %disp('ENTERING 530 from STEPIT 4')
            %private_530_ROUTINE(1);
            
         end
         
         if (JX == NV)&(grandtag == 1) &(fortag == 1)
            
            grandtag = 0;						%okay, so i made it through the for loop, now kick me out so I can move on!
            fortag = 0;
            %disp('ive been kicked out completely!')
            %disp(JX)
            %pause
               
         elseif (fortag == 0) & (grandtag == 1)
            
           % disp('Im initiating the 640 protocal!')
            fortag = 0;
            disp(JX)
           % pause
            
            
         elseif (fortag == 1) & (grandtag == 1)
            
            %disp('Im being incremented!')
            %disp(JX)
           
         end
         
      end
      
   end
   
end


