Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 8 (최근 30일)
yugen11
yugen11 2020년 6월 4일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Rik
Rik 2020년 6월 4일
You are overwriting x1 and x2 at the beginning of your loop. Your code is not commented, so I can't propose a solution.

답변 (1개)

dipak nigam
dipak nigam 2020년 6월 4일
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
yugen11
yugen11 2020년 6월 4일
Already corrected bro...what you said is true...thank you for the respond

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by