Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
infinite while loop

Subject: infinite while loop

From: Vithoban Vithoban

Date: 5 Jul, 2013 06:15:09

Message: 1 of 4

hi ! i am getting an infinite while loop.

can anyone tell me why?

while length(visited_list)~=total_num_nodes
    
    [new_node,new_route]=objective_func_nnb(wait_list,tabu_major,tabu_minor,prev_node,load);
    
    cap_para=capacity1(load,new_node);
    
    if cap_para==1
        
        [time_para,edprev]=time1(new_node,new_route,prev_node);
        
    else
        time_para=0;
    end
    
    
    if time_para==1
        
        x=length(visited_list);
        visited_list(x+1)=new_node;
        
        for c=1:length(wait_list)
            if wait_list(c)==new_node
                wait_list(c)=0;
            end % end of if statement
        end % end of for loop
        
        g=length(tour);
        tour(g+1)=new_node;
        route(g)=new_route;
        load=load+demand(new_node);
        tabu_minor=zeros(total_num_nodes,3);
        tabu_major=0;
        
        prev_node=new_node; % assigns the newly accepted node as the prev_node ot be used in the next iteration
        
        disp('updation of node has been done')
        
        
        % end of if
        
        % if either time/capacity is not satisfied
    else
        
        tabu_minor(new_node+1,new_route)=1;
        
        p=0;
        
        for d=1:3
            if tabu_minor(new_node+1,d)==1
                p=p+1;
            end % end of if statement
            
        end % end of for loop
        if p==3 % if statement to assign next node as depot if condition satisfied
            y=length(tabu_major);
            tabu_major(y+1)=new_node;
            
            length_wait=0;
            for c=1:length(wait_list)
                if wait_list(c)~=0
                    length_wait=length_wait+1;
                end
            end
            
            if length(tabu_major)==length_wait % to check if the vehicle has to move to the depot next
                %this check is performed only after the tabu_major has been updated. without updation,there is no need to check since more nodes will be available to be searched
                x=length(tour);
                tour(x+1)=0;
                
                min=10;% assigns value to min
                
                carried=capacity-load;
                for e=1:3
                    if sum(b(prev_node+1,1,e),a(prev_node+1,1,e)*carried)<=min
                        min=b(prev_node+1,1,e)+a(prev_node+1,1,e)*carried;
                        fact=e;
                    end %end of if
                end % end of for loop
                route(x)=fact;
                prev_node=0;
                load=0;
                tabu_major=0;
                tabu_minor=zeros(total_num_nodes,3);
                
            end % end of if statement
            
        end % end of if statement
        
    end
    
end % end of while

Subject: infinite while loop

From: Marc

Date: 6 Jul, 2013 15:18:10

Message: 2 of 4

"Vithoban Vithoban" wrote in message <kr5o9d$jih$1@newscl01ah.mathworks.com>...
> hi ! i am getting an infinite while loop.
>
> can anyone tell me why?
>
> while length(visited_list)~=total_num_nodes
>
> [new_node,new_route]=objective_func_nnb(wait_list,tabu_major,tabu_minor,prev_node,load);
>
> cap_para=capacity1(load,new_node);
>
> if cap_para==1
>
> [time_para,edprev]=time1(new_node,new_route,prev_node);
>
> else
> time_para=0;
> end
>
>
> if time_para==1
>
> x=length(visited_list);
> visited_list(x+1)=new_node;
>
> for c=1:length(wait_list)
> if wait_list(c)==new_node
> wait_list(c)=0;
> end % end of if statement
> end % end of for loop
>
> g=length(tour);
> tour(g+1)=new_node;
> route(g)=new_route;
> load=load+demand(new_node);
> tabu_minor=zeros(total_num_nodes,3);
> tabu_major=0;
>
> prev_node=new_node; % assigns the newly accepted node as the prev_node ot be used in the next iteration
>
> disp('updation of node has been done')
>
>
> % end of if
>
> % if either time/capacity is not satisfied
> else
>
> tabu_minor(new_node+1,new_route)=1;
>
> p=0;
>
> for d=1:3
> if tabu_minor(new_node+1,d)==1
> p=p+1;
> end % end of if statement
>
> end % end of for loop
> if p==3 % if statement to assign next node as depot if condition satisfied
> y=length(tabu_major);
> tabu_major(y+1)=new_node;
>
> length_wait=0;
> for c=1:length(wait_list)
> if wait_list(c)~=0
> length_wait=length_wait+1;
> end
> end
>
> if length(tabu_major)==length_wait % to check if the vehicle has to move to the depot next
> %this check is performed only after the tabu_major has been updated. without updation,there is no need to check since more nodes will be available to be searched
> x=length(tour);
> tour(x+1)=0;
>
> min=10;% assigns value to min
>
> carried=capacity-load;
> for e=1:3
> if sum(b(prev_node+1,1,e),a(prev_node+1,1,e)*carried)<=min
> min=b(prev_node+1,1,e)+a(prev_node+1,1,e)*carried;
> fact=e;
> end %end of if
> end % end of for loop
> route(x)=fact;
> prev_node=0;
> load=0;
> tabu_major=0;
> tabu_minor=zeros(total_num_nodes,3);
>
> end % end of if statement
>
> end % end of if statement
>
> end
>
> end % end of while

It is very hard to trouble shoot this without some of the values for length and total_num_nodes.

That said, I don't see where either of these two arrays(?) are being updated in this loop, so why would you expect the condition up front to change if your not changing those values?

I may have missed something but if you can add some values or a snippet of the code before hand, so that we can run on our machines, maybe I can follow it a bit better.

Subject: infinite while loop

From: Guru

Date: 6 Jul, 2013 16:04:10

Message: 3 of 4

"Vithoban Vithoban" wrote in message <kr5o9d$jih$1@newscl01ah.mathworks.com>...
> hi ! i am getting an infinite while loop.
>
> can anyone tell me why?
>
> while length(visited_list)~=total_num_nodes
>
> [new_node,new_route]=objective_func_nnb(wait_list,tabu_major,tabu_minor,prev_node,load);
>
> cap_para=capacity1(load,new_node);
>
> if cap_para==1
>
> [time_para,edprev]=time1(new_node,new_route,prev_node);
>
> else
> time_para=0;
> end
>
>
> if time_para==1
>
> x=length(visited_list);
> visited_list(x+1)=new_node;

Ok, so the only way you have to not have an infinite loop relies on time_para being set to 1 from the time1 function when cap_para is 1. What I would suggest is that you either add some debugging breakpoints and/or change your code (remove semicolons) so that you can trace the values of cap_para, time_para within the while loop. Additionally your assignment of x is completely superfluous here. You should simply be using end instead of x in the visited_list assignment on the last line here.

Subject: infinite while loop

From: Marc

Date: 6 Jul, 2013 18:25:13

Message: 4 of 4

"Marc" wrote in message <kr9cfi$lfu$1@newscl01ah.mathworks.com>...
> "Vithoban Vithoban" wrote in message <kr5o9d$jih$1@newscl01ah.mathworks.com>...
> > hi ! i am getting an infinite while loop.
> >
> > can anyone tell me why?
> >
> > while length(visited_list)~=total_num_nodes
> >
> > [new_node,new_route]=objective_func_nnb(wait_list,tabu_major,tabu_minor,prev_node,load);
> >
> > cap_para=capacity1(load,new_node);
> >
> > if cap_para==1
> >
> > [time_para,edprev]=time1(new_node,new_route,prev_node);
> >
> > else
> > time_para=0;
> > end
> >
> >
> > if time_para==1
> >
> > x=length(visited_list);
> > visited_list(x+1)=new_node;
> >
> > for c=1:length(wait_list)
> > if wait_list(c)==new_node
> > wait_list(c)=0;
> > end % end of if statement
> > end % end of for loop
> >
> > g=length(tour);
> > tour(g+1)=new_node;
> > route(g)=new_route;
> > load=load+demand(new_node);
> > tabu_minor=zeros(total_num_nodes,3);
> > tabu_major=0;
> >
> > prev_node=new_node; % assigns the newly accepted node as the prev_node ot be used in the next iteration
> >
> > disp('updation of node has been done')
> >
> >
> > % end of if
> >
> > % if either time/capacity is not satisfied
> > else
> >
> > tabu_minor(new_node+1,new_route)=1;
> >
> > p=0;
> >
> > for d=1:3
> > if tabu_minor(new_node+1,d)==1
> > p=p+1;
> > end % end of if statement
> >
> > end % end of for loop
> > if p==3 % if statement to assign next node as depot if condition satisfied
> > y=length(tabu_major);
> > tabu_major(y+1)=new_node;
> >
> > length_wait=0;
> > for c=1:length(wait_list)
> > if wait_list(c)~=0
> > length_wait=length_wait+1;
> > end
> > end
> >
> > if length(tabu_major)==length_wait % to check if the vehicle has to move to the depot next
> > %this check is performed only after the tabu_major has been updated. without updation,there is no need to check since more nodes will be available to be searched
> > x=length(tour);
> > tour(x+1)=0;
> >
> > min=10;% assigns value to min
> >
> > carried=capacity-load;
> > for e=1:3
> > if sum(b(prev_node+1,1,e),a(prev_node+1,1,e)*carried)<=min
> > min=b(prev_node+1,1,e)+a(prev_node+1,1,e)*carried;
> > fact=e;
> > end %end of if
> > end % end of for loop
> > route(x)=fact;
> > prev_node=0;
> > load=0;
> > tabu_major=0;
> > tabu_minor=zeros(total_num_nodes,3);
> >
> > end % end of if statement
> >
> > end % end of if statement
> >
> > end
> >
> > end % end of while
>
> It is very hard to trouble shoot this without some of the values for length and total_num_nodes.
>
> That said, I don't see where either of these two arrays(?) are being updated in this loop, so why would you expect the condition up front to change if your not changing those values?
>
> I may have missed something but if you can add some values or a snippet of the code before hand, so that we can run on our machines, maybe I can follow it a bit better.

I'm an idiot... I meant length(visited_list) but I see now where you are updating it. I think guru has it nailed down.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us