Clear Filters
Clear Filters

ga Taking too Much Time wtihout any Results

2 views (last 30 days)
I am currently working on a project. The main script is using other functions and data from an Excel file as given in the attached .zip file.
The main file is "optTCv2" and my goal is to conduct a ga opitimzation for 20 items. Although, when I run on a single input it gives results, when I use ga it doesn't bring any results or terminate.
Would you please help me identify the reason?
The files are attached.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Oct 2023
demand = table2array (mainData(9:62 , 2:21));
That is a 2D array.
expDemand = expValue(demand, meanDemandResize, stdDemandResize);
The array is passed as the first parameter to expValue()
function [expValue] = expValue(x , mu , sigma)
Where it is received under the name x
for i=1:x
where the 2D array is used as the upper bound for a for loop.
In MATLAB, when you use a non-scalar as a bound in a colon operation, MATLAB uses the first element of the array. The for loop is not iterating over the elements of x in any way: it is equivalent to
for i = 1 : x(1,1)
Then
y1 = cdf('Normal',x-1,mu,sigma); %% Cumulative Normal Dist. for Integer Value x-1
y2 = cdf('Normal',x,mu,sigma); %% Cumulative Normal Dist. for Integer Value x
y3 = y2- y1; %% Difference between Cum. N. Dist. (x- (x-1))
y4 = y3 .* x; %% Expected Value Difference Between * Value
Those steps involving processing all of x but are independent of the loop control variable, and those steps overwrite all of the variables involved completely. So each iteration of the for loop, those lines calculate exactly the same thing.
y5 = y5 + y4; %% Calculation of Expected Value
Except there. y5 was initialized to 0 before the for i loop. If you think about what is happening, with y4 being the same each iteration, the end result is going to be the same as if you had not done any for loop and had instead done
if x(1,1) >= 1
y1 = cdf('Normal',x-1,mu,sigma); %% Cumulative Normal Dist. for Integer Value x-1
y2 = cdf('Normal',x,mu,sigma); %% Cumulative Normal Dist. for Integer Value x
y3 = y2- y1; %% Difference between Cum. N. Dist. (x- (x-1))
y4 = y3 .* x; %% Expected Value Difference Between * Value
%repeated addition is the same as multiplication
y5 = y4 .* floor(x(1,1));
else
y5 = 0;
end
  4 Comments
Walter Roberson
Walter Roberson on 29 Oct 2023
Your expValue() calculations are taking 43475 times longer then they need to, and they are being done every iteration when they do not change and so should have been calculated only once before-hand. Big waste of time there.
Fatih
Fatih on 29 Oct 2023
Edited: Fatih on 29 Oct 2023
Great thanks. I removed the expVaşlue from the function and put it in the script. So that seems the main problem. I have at least results in acceptable time. Thanks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!