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

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

2 Comments

You are overwriting x1 and x2 at the beginning of your loop. Your code is not commented, so I can't propose a solution.

Answers (1)

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 ;

1 Comment

Already corrected bro...what you said is true...thank you for the respond

This question is closed.

Asked:

on 4 Jun 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!