How can I reduce my code ? It suddenly become so so slow

1 次查看(过去 30 天)
I have a big code, and the tic-toc timing of this part is so big. few days ago my code was work fast. I tried a lot if solutions such as:
  • Pre-allocate variables.
  • Unistall and re-install Matlab.
But with any positive solutions, I can't use parallel because there are a relation between loops. I want my code that become fast as old-days.
The tic toc timing is between 6 seconds and 4 seconds. ( is not take a constant value).
function [Res]=...
ArAddSolutions(Ne, Ar,So,ArMaxSize, Tr,ObF,Constraints,MA,lb,ub)
global ObjOne;
global ObjTwo;
global ObjThree;
NewP=[];
if( size (So,1)>=1 )
% Remove Redudndance In soulution
i=1;
while( i<=size(So,1)-1)
j= i+1;
while( j<=size(So,1))
if(So(i,:)==So(j,:))
So(j,:)=[];
else
j= j+1;
end
end
i=i+1;
end
for i=1: size(So,1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Accept Conditions or nop
Flag4ub=So(i,:)>ub;
Flag4lb=So(i,:)<lb;
Respectb=true;
In =1;
while( ( In<= size(Flag4ub,2) )&& (Respectb) )
if( ( sum(Flag4ub)>0) || (sum (Flag4lb)>0) )
Respectb=false;
end
In =1+In;
end
if( Constraints(So(i,:),Tr) && (Respectb) )
%%%%%%%%%%%%%%%%%%%%%%%%% new soulution exist or In Ar nooooop ;)
ExistInAr=false;
ii=1;
while( ( ii<=size(Ar,1)) && (ExistInAr==false))
if(So(i,:)==Ar(ii,:))
ExistInAr=true;
else
ii= ii+1;
end
end
if(~ExistInAr)
% Non Dominated or nop
NonDominated=true;
DomineListe=[];
Obj1 = ObF(Ne,So(i,:),Tr,ObjOne,MA);
Obj2 = ObF(Ne,So(i,:),Tr,ObjTwo,MA);
Obj3 = ObF(Ne,So(i,:),Tr,ObjThree,MA);
j=1;
while ( (j <= size(Ar,1)) && (NonDominated) )
ArObj1 = ObF(Ne,Ar(j,:),Tr,ObjOne,MA);
ArObj2 = ObF(Ne,Ar(j,:),Tr,ObjTwo,MA);
ArObj3 = ObF(Ne,Ar(j,:),Tr,ObjThree,MA);
if ( (Obj1>ArObj1 ) && (Obj2>ArObj2) && (Obj3>ArObj3 ))
NonDominated=false;
%NewP=[NewP; ArObj1,ArObj2,ArObj3];
else
if ( (Obj1<ArObj1 ) && (Obj2<ArObj2) && (Obj3<ArObj3 ))
DomineListe=[DomineListe,j];
end
end
j=j+1;
end
% Add soulition in Ar
if(NonDominated)
% Remove from Ar If dominate
forj=1:size(DomineListe,1)
Ar( DomineListe(j) ,:)=[];
end
while (size(Ar,1)>=ArMaxSize )
[Ar,ArScore]=...
CrowdingDistanceAlgorithm(Ne, Ar,Tr,ObF,MA);
Ar(size(Ar,1),:)=[];
end
Ar=[Ar; So(i,:)];
end
end
end
end
end
Res=Ar;
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by