Info

This question is closed. Reopen it to edit or answer.

Values keep repeating in a loop...really need your help..there is no error in my code

1 view (last 30 days)
LLx1 = 0 ;%Lower limit
HLx1 = 1 ;%Upper limit
LLx2 =0;
HLx2 = 2;
x1 = LLx1 + (HLx1 - LLx1)*rand ;
x2 = LLx2 + (HLx2 - LLx2)*rand ;
f=(x1-1)^2+(x2-2)^2 ; %fitness function
I = 1 ;%start at 1
delta01=100;
delta02=100;
p1=rand;
p2=rand;
iteration = 1000000;
d=zeros(2,iteration);
while I<iteration+1
x1 = LLx1 + (HLx1 - LLx1)*rand;
x2 = LLx2 + (HLx2 - LLx2)*rand; %fitness function
d(1,I)=f; %1st row
if I==1
d(2,I)=d(1,I);
else
if d(1,I)<d(2,I-1)
d(2,I)=d(1,I);
else
d(2,I)=d(2,I-1);
end
end
delta1=exp(-0.1*I/iteration)*delta01;
delta2=exp(-0.1*I/iteration)*delta02;
x1predict=(d(2,I)-delta1)+((d(2,I)+delta1)-(d(2,I)-delta1))*rand;
x2predict=(d(2,I)-delta2)+((d(2,I)+delta2)-(d(2,I)-delta2))*rand;
p1=p1+rand;
p2=p2+rand;
z1=x1predict+sin(rand*2*pi)*abs(x1predict-d(2,I));
z2=x2predict+sin(rand*2*pi)*abs(x2predict-d(2,I));
k1=p1/(p1+rand);
k2=p2/(p2+rand);
x1=x1predict+k1*(z1-x1predict);
x2=x2predict+k2*(z2-x2predict);
if 0<=x1 && x1<=1
else
x1 = LLx1 + (HLx1 - LLx1)*rand ;
end
if 0<=x2 && x2<=2
else
x2 = LLx2 + (HLx2 - LLx2)*rand ;
end
I=I+1;
end

Answers (1)

dipak nigam
dipak nigam on 4 Jun 2020
You are not updating the value of the fitness function, 'f', inside the loop.
From my understanding, you want to fill 1000000 random values of 'f' in the first row of the 2*1000000 matrix. But, the value of 'f' gets calculated only once outside the loop at the beginning.
Try adding this line after overwriting x1 and x2 in the while loop -
f=(x1-1)^2+(x2-2)^2 ;

This question is closed.

Community Treasure Hunt

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

Start Hunting!