ga Taking too Much Time wtihout any Results

조회 수: 1 (최근 30일)
Fatih
Fatih 2023년 10월 29일
편집: Fatih 2023년 10월 29일
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.

채택된 답변

Walter Roberson
Walter Roberson 2023년 10월 29일
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
Walter Roberson
Walter Roberson 2023년 10월 29일
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 2023년 10월 29일
편집: Fatih 2023년 10월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by