Why does my code take so long to run?

21 views (last 30 days)
Abdullah
Abdullah on 27 Sep 2023
Answered: David Hill on 27 Sep 2023
So i have this optimization code here to figure out a multitude of scores and display them. I have been running the code for a little over 2 days now until I stopped it as I am unable to keep my laptop open for that amount of time. Also as a University student I need to utilize my computer for other things. I'll also mention I have no formal training in matlab(I'm a freshman). Basically I'm asking how to I optimize my code to make it run faster.
% Define the ranges and possible values for variables
a_values = 0:12;
b_values = [0, 10, 20];
c_values = 0:12;
d_values = [0, 10, 20];
e_values = [0, 1, 2];
f_values = 0:47;
h_values = 0:47;
i_values = 0:19;
j_values = [0, 1, 2];
l_values = [0, 1];
k_values = [0, 10, 20, 30];
% Initialize variables to store the highest and lowest Total Scores
highest_score = -inf;
lowest_score = inf;
% Initialize arrays to store Total Scores for all combinations
total_scores = [];
% Initialize arrays to store Autonomous, Teleop, and Endgame values
autonomous_values = [];
teleop_values = [];
endgame_values = [];
% Loop through all possible combinations of variables
for a = a_values
for b = b_values
for c = c_values
for d = d_values
for e = e_values
for f = f_values
for h = h_values
for i = i_values
for j = j_values
for l = l_values
for k = k_values
% Check the constraint
if a + c + f + h <= 47
% Calculate Autonomous, Teleop, Endgame
autonomous = (5*a + b) + 3*c + d + 5*e;
g = 0; % Initialize g to 0
% Determine g based on the condition
if f >= 5
g = 10;
end
if f >= 12
g = 20;
end
if f >= 29
g = 30;
end
teleop = (3*f + g) + h + 10*i;
endgame = 5*j + 20*l + k;
% Calculate Total Score
total_score = autonomous + teleop + endgame;
% Update the highest and lowest scores
if total_score > highest_score
highest_score = total_score;
end
if total_score < lowest_score
lowest_score = total_score;
end
% Store values for Autonomous, Teleop, Endgame
autonomous_values = [autonomous_values; autonomous];
teleop_values = [teleop_values; teleop];
endgame_values = [endgame_values; endgame];
% Store Total Score
total_scores = [total_scores; total_score];
end
end
end
end
end
end
end
end
end
end
end
end
% Display the highest and lowest scores
disp(['Highest Possible Score: ', num2str(highest_score)]);
disp(['Lowest Possible Score: ', num2str(lowest_score)]);
% Create a 4D plot
figure;
scatter3(autonomous_values, teleop_values, endgame_values, 10, total_scores, 'filled');
xlabel('Autonomous');
ylabel('Teleop');
zlabel('Endgame');
title('Total Score vs. Autonomous, Teleop, Endgame');
% Create tables for Autonomous, Teleop, and Endgame values and Total Scores
autonomous_table = table(autonomous_values, total_scores, 'VariableNames', {'Autonomous', 'TotalScore'});
teleop_table = table(teleop_values, total_scores, 'VariableNames', {'Teleop', 'TotalScore'});
endgame_table = table(endgame_values, total_scores, 'VariableNames', {'Endgame', 'TotalScore'});
% Display the tables
disp('Autonomous Values and Total Scores:');
disp(autonomous_table);
disp('Teleop Values and Total Scores:');
disp(teleop_table);
disp('Endgame Values and Total Scores:');
disp(endgame_table);
  4 Comments
Abdullah
Abdullah on 27 Sep 2023
@Stephen23 I wouldn't know, but I will look into that thank you
Torsten
Torsten on 27 Sep 2023
a_values = 0:12;
b_values = [0, 10, 20];
c_values = 0:12;
d_values = [0, 10, 20];
e_values = [0, 1, 2];
f_values = 0:47;
h_values = 0:47;
i_values = 0:19;
j_values = [0, 1, 2];
l_values = [0, 1];
k_values = [0, 10, 20, 30];
number_of_iterations = numel(a_values)*numel(b_values)*numel(c_values)*numel(d_values)*numel(e_values)*numel(f_values)*numel(h_values)*numel(i_values)*numel(j_values)*numel(k_values)*numel(l_values)
number_of_iterations = 5.0463e+09

Sign in to comment.

Answers (1)

David Hill
David Hill on 27 Sep 2023
You can use this code, but you will have to still split up f and h to have enough memory. Based on your ranges, the highest score is 495.
a_values = 0:12;
b_values = [0, 10, 20];
c_values = 0:12;
d_values = [0, 10, 20];
e_values = [0, 1, 2];
f_values = 31:39;%47
h_values = 0:20;%47
i_values = 0:19;
j_values = [0, 1, 2];
l_values = [0, 1];
k_values = [0, 10, 20, 30];
[a,c,f,h]=ndgrid(a_values,c_values,f_values,h_values);
T=a+c+f+h;
idx=T<=47;
a=a(idx);c=c(idx);f=f(idx);h=h(idx);
g=zeros(size(f));
g(f>=5)=10;
g(f>=12)=20;
g(f>=29)=30;
[b,d,e,i,j,l,k]=ndgrid(b_values,d_values,e_values,i_values,j_values,l_values,k_values);
b=b(:)';d=d(:)';e=e(:)';i=i(:)';j=j(:)';l=l(:)';k=k(:)';
autonomous = (5*a + b) + 3*c + d + 5*e;
teleop = (3*f + g) + h + 10*i;
endgame = 5*j + 20*l + k;
total_score = autonomous + teleop + endgame;
highest_score=max(total_score,[],'all')
highest_score = 495
lowest_score = min(total_score,[],'all');

Categories

Find more on Testing Frameworks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!