How to simplfy the code for time saving? (nested while loop)

1 visualización (últimos 30 días)
snr matlb
snr matlb el 23 de En. de 2021
Respondida: Anurag Ojha el 10 de Oct. de 2023
for p = 1:30 %particle loop
for h = 1:8 %hour window each number represent 1-hour time window.
conflictjobs=[];
conflictlist=[];
row=1;
list=[];
****************************************************************
for i = 1:100
if job(p,h,i).entrypoint == 5 || job(p,h,i).entrypoint == 8 && (job(p,h,i).hourzone==h)
job(p,h,i).m1time = job(p,h,i).entrytime + (table2array(firstconflictdist(job(p,h,i).entrypoint, job(p,h,i).m1point))/line_speed)*3600;
conflictlist(row,1)=i;
conflictjobs = unique(conflictlist);
row=row+1;
for i3 = 1:size(conflictjobs,1)
list(i3,1)=job(p,conflictjobs(i3)).m1time;
list(i3,2)=job(p,conflictjobs(i3)).id;
list(i3,3)=job(p,conflictjobs(i3)).sequence;
list(i3,4)=1;
list(i3,5)=0;
end
end
end
list=sortrows(list,1);
i4=1;
i5=i4+1;
while i4 <= size(list,1)-1
i5=i4+1;
while i5 <= size(list,1)
if abs(list(i5,1)-list(i4,1))>=sep
list(i4,4)=0000;
else
delay = abs(sep-(list(i5,1)-list(i4,1)));
list(i5,1) = list(i5,1) + delay;
list(i5,5) = list(i5,5) + delay;
job(p,list(i5,2)).delay1 = list(i5,5);
job(p,list(i5,2)).m1time = list(i5,1);
i4=0;
i5=2;
list=sortrows(list,1);
break
end
list=sortrows(list,1);
i5=i5+1;
end
i4=i4+1;
end
% for loop again
for i = 1:100
if job(p,h,i).entrypoint == 5 || job(p,h,i).entrypoint == 7 || job(p,h,i).entrypoint == 8 && (job(p,h,i).hourzone==h)
if job(p,h,i).entrypoint == 5 || job(p,h,i).entrypoint == 8
job(p,h,i).m2time = job(p,h,i).m1time + (table2array(secondconflictdist(job(p,h,i).entrypoint, job(p,h,i).m2point))/line_speed)*3600;
end
if flight(p,h,i).entrypoint == 7
flight(p,h,i).m2time = flight(p,h,i).entrytime + (table2array(secondconflictdist(flight(p,h,i).entrypoint, flight(p,h,i).m2point))/280)*3600;
end
conflictlist(row,1)=i;
conflictjobs = unique(conflictlist);
row=row+1;
for i3 = 1:size(conflictjobs,1)
list(i3,1)=job(p,conflictjobs(i3)).m1time;
list(i3,2)=job(p,conflictjobs(i3)).id;
list(i3,3)=job(p,conflictjobs(i3)).sequence;
list(i3,4)=1;
list(i3,5)=0;
end
end
end
list=sortrows(list,1);
i4=1;
i5=i4+1;
while i4 <= size(list,1)-1
i5=i4+1;
while i5 <= size(list,1)
if abs(list(i5,1)-list(i4,1))>=sep
list(i4,4)=0000;
else
delay = abs(sep-(list(i5,1)-list(i4,1)));
list(i5,1) = list(i5,1) + delay;
list(i5,5) = list(i5,5) + delay;
job(p,list(i5,2)).delay1 = list(i5,5);
job(p,list(i5,2)).m1time = list(i5,1);
i4=0;
i5=2;
list=sortrows(list,1);
break
end
list=sortrows(list,1);
i5=i5+1;
end
i4=i4+1;
end
... for loop again for filtering different jobs regarding different if conditions...
end
end
In the first for-loop is for particle in particle swarm optimization, second one is for looping at hour zone and third one is for getting each job.
After the third for-loop, the if condition filters the jobs which fits the condition, then I calculates their m1time. I store their row in a conflictlist, then I pass the jobs into the "list" by regarding conflictjobs list. This will enable the algorithm to check the conflict among the jobs that fits the condition in the if statement.
Then,, sorting the "list". After, I will use nested while loop to check time conflict of each job. So, i4 goes to size(list,1)-1. because the last job has already controlled after i4 finished.
I have 6 more if statement to select the jobs according to the conditions, so imagine that I use this structure (below ************* in the code section). This means that for each "if statement" I check 100 job over and over again, 6 times, and it means also in each "for-loop" I use nested while loop. I have to use for i=1:100 loop 6 more times because in each I calculate different times with different if conditions. You can see two of them into the code section.
I notice that using for i = 1:100 over and over again is a problem, I already try to gather some if conditions that shows similarities into the same for-loop, but I am not succeded so far, also, while loop is another problem to increasing the solution period, or I think like that.
Is there a way to reduce nested while loop and save solution period. But, maybe the problem is using "for loop" over and over again (6 more times), please let me know any idea.
  2 comentarios
Les Beckham
Les Beckham el 23 de En. de 2021
I think you will have better luck getting a satisfactory answer if you explain what you are really trying to do. Provide an example of the input data (list, I presume) and the desired transformed output data (job?). Writing code to execute an algorithm (and making it efficient) is much easier if you can explain what the algorithm is supposed to do.
snr matlb
snr matlb el 23 de En. de 2021
@Les Beckham Thanks for the comment, I just updated the code and explanation.

Iniciar sesión para comentar.

Respuestas (1)

Anurag Ojha
Anurag Ojha el 10 de Oct. de 2023
Hi,
As per my understanding, you are facing difficulty in reducing the number of nested while loop and for-loop.
One possible workaround to reduce number of nested loops is using “Vectorization. Instead of looping through job indices, you can often perform operations on entire arrays of job data. MATLAB is highly optimized for vector operations, so this can significantly improve performance.
For example, consider calculating the element-wise sum of two arrays:
Without Vectorization (Using a Loop):
% Initialize two arrays
array1 = rand(1000, 1000);
array2 = rand(1000, 1000);
result = zeros(1000, 1000);
% Loop to calculate the sum element-wise
for i = 1:1000
for j = 1:1000
result(i, j) = array1(i, j) + array2(i, j);
end
end
With Vectorization:
% Initialize two arrays
array1 = rand(1000, 1000);
array2 = rand(1000, 1000);
% Calculate the sum element-wise using vectorization
result = array1 + array2;
In the above example, the vectorized code is more concise and potentially faster because it leverages MATLAB's optimized vector operations.
You can apply similar principles of vectorization to your code by identifying areas where you can operate on entire arrays or matrices at once rather than using explicit loops.
Kindly, refer to following MATLAB documentation to explore more:
Hope it helps!

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by