How to restart a for loop when specific condition is met?
5 次查看(过去 30 天)
显示 更早的评论
Sir, i am trying to restart the route construction (vehicle_route: in my case) whenever the number of vehicles (NVU) exceeds 4. In some cases NVU =4, while in other cases it exceeds the NVU =4.
Below is the detailed code.
clear;clc;
% PROBLEM INSTANCE
nodes = csvread('E_C.csv')'; %Co-ordinates of Customers
number_of_nodes = size(nodes,2);%Number of Co-ordinates
demand = csvread('E_D.csv')';%Customers Demand
number_of_customers = 22; %Number of Customers
number_of_vehicles = 4;
vehicle_capacity = 6000;
depot = 1;
length_of_string = number_of_customers+number_of_vehicles-1; %Maximum Possible Stops
vehicle_route = []; %Empty Array for tracking Vehicle Route
unvisited = colon(1,number_of_nodes); %Initialization of Unvisited Customers or Co-ordinates
NVU=1; %Number of Vehicles Used Initially ONE
for n=1:length_of_string
if n==1
vehicle_route(n) = depot; %Start of the Route
unvisited(unvisited==vehicle_route(n))=[]; % Unvisited Co-ordinates
filled_capacity = demand(vehicle_route(n)); % Load filled by the particular customr
else
next_customer = unvisited(randperm(length(unvisited),1)); %Random picking of Customer
% to check feasilibility of route
if demand(next_customer)<=vehicle_capacity
vehicle_route(n)=next_customer; %Tracking Vehicle Route
unvisited(find(unvisited==vehicle_route(n)))=[];% Unvisited Co-ordinates
filled_capacity = demand(vehicle_route(n));% Load filled by the particular customer
vehicle_capacity=vehicle_capacity-filled_capacity;% Remaining Capacity of Vehicle
else
%This resets everything and New Vehicle Starts
vehicle_route(n) = depot;
vehicle_capacity = 6000;
filled_capacity = 0;
%i = i+1;
NVU=NVU+1;
end
end
if length(unvisited)==0 %In case demand is fulfilled before max nodes visited.
disp('Damand is fulfilled, Vehicle Route Followed is below')
vehicle_route
disp('Number of Vehicles used')
NVU
break;
end
end
0 个评论
采纳的回答
darova
2021-8-1
Place for loop inside while loop
k = 0;
while k < 5
for i = 1:10
if i > 5
k = k + 1;
break
end
end
end
3 个评论
Image Analyst
2021-8-8
编辑:darova
2021-8-8
The code will work. I voted for this answer because it's clear and simple
更多回答(1 个)
Image Analyst
2021-8-8
Try this:
k = 1;
maxWhileLoopIterations = 8;
maxForLoopIterations = 3;
while k <= maxWhileLoopIterations
repeatLoop = false;
for i = 1:maxForLoopIterations
NVU = randi(6, 1, 1); % Get random number for NVU.
fprintf('In the for loop with i = %d: NVU = %d. k = %d.\n', i, NVU, k);
if NVU >= 4 % Some condition to repeat the loop.
fprintf(' Going to repeat for loop because NVU = %d.\n', NVU);
repeatLoop = true;
break;
end
end
% See if for loop finished (i = 10) and so does not need repeating.
if i == maxForLoopIterations && ~repeatLoop
% We want to quit if the condition was never met and the for loop completely finished.
% We need to check repeatLoop because it's possible the condition could be met on the last iteration.
fprintf('1 : The for loop completely finished this last time with no need to repeat it.\n');
fprintf('1 : Exiting while loop after for loop after running the for loop %d times.\n', k);
break;
end
% Fail safe to prevent infinite loop.
k = k + 1;
% Tell user if we're breaking because of the failsafe.
if k == maxWhileLoopIterations
fprintf('2 : Exiting while loop because max iterations of %d have been done.\n', maxWhileLoopIterations);
break;
end
end
You'll see:
In the for loop with i = 1: NVU = 4. k = 1.
Going to repeat for loop because NVU = 4.
In the for loop with i = 1: NVU = 1. k = 2.
In the for loop with i = 2: NVU = 3. k = 2.
In the for loop with i = 3: NVU = 1. k = 2.
1 : The for loop completely finished this last time with no need to repeat it.
1 : Exiting while loop after for loop after running the for loop 2 times.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!