Same code but different results-very strange?

11 visualizzazioni (ultimi 30 giorni)
Sadiq Akbar
Sadiq Akbar il 22 Dic 2022
Commentato: Sadiq Akbar il 24 Dic 2022
I asked a question on December 10, 2022 on this forum. The URL of the question is:
I named that code as "MathworkVersionVectorized.m". My own code name is "myversion.m". Now when I run both of them with ga, the results of my own code is much closer to my desired vector but the code that Mathworks site told me gives very wrong results though the fitness of both of them is same.I have attached both the codes along with main.m and TestingResults.m. Can any body guide me why it is so?
%u=[-35 35 50];% % 3-element desired vector
u=[-15 15 -25 25 -35 35 -45 45];% 8-element desired vector
dim=length(u);
lb=-90*ones(1,dim);
ub=90*ones(1,dim);
Noise=5;
PopulationSize_Data=100;
MaxGenerations_Data=500;
FunctionTolerance_Data=1e-6;
ConstraintTolerance_Data=1e-6;
opts = optimoptions('ga','PopulationSize', PopulationSize_Data);
opts = optimoptions('ga','MaxGenerations', MaxGenerations_Data);
opts = optimoptions('ga','FunctionTolerance', FunctionTolerance_Data);
opts = optimoptions('ga','ConstraintTolerance', ConstraintTolerance_Data);
opts = optimoptions('ga','CreationFcn', @gacreationuniform);
opts = optimoptions('ga','CrossoverFcn', { @crossoverheuristic [] });
opts = optimoptions('ga','MutationFcn', @mutationadaptfeasible);
opts = optimoptions('ga','Display', 'off');
Runs=5;
one=zeros(Runs,1);
time1=zeros(Runs,1);
two=zeros(Runs,dim);
temp=zeros(Runs,dim);
nn=0;
for n=1:Runs
nn=nn+1;
tic;
% [B,fval,exitflag,output] = ga(@(b)myfun(b,u,Noise), Parms,[],[],[],[],[],[],[],opts);
% [B,fval] = ga(@(b)myfun(b,u,Noise), dim,[],[],[],[],[],[],[],opts);
% [B,fval] = ga(@(b)myversion(b,u,Noise), dim,[],[],[],[],[],[],[],opts);
[B,fval] = ga(@(b)MathworkVersionVectorized(b,u,Noise), dim,[],[],[],[],[],[],[],opts);
B
fval
one(nn)=fval;
temp(nn,:)=B;
time(nn)=toc;
%%%%%%%%%%%%%%%%%%%
% Swapping
%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(temp(nn,:));
two(nn,:) = temp(nn,ix1);
end
function e=myversion(b,u,Noise)
M = 6;%Constant1
N = 6;%Constant2
d = 0.5; % %Constant3
%%%%%%%%%%%%%
% Note: Put K always as half of u-elements
%%%%%%%%%%%%%
K = length(u)/2; %Constant4
alpha=ones(1,K);%Constant5
a=zeros(M,K);% a matrix
f=zeros(N,K);% f matrix
c=zeros(M*N, length(u)-K);% c matrix
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Calculating a, f and c
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
a(h,i)=exp(j*2*pi*(h-1)*d*sind(u(i)));
end
for p=1:N
f(p,i)=exp(j*2*pi*(p-1)*d*sind(u(K+i)));
end
end
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));
end
yo=c*alpha'; % yo vector
yo=awgn(yo,Noise);% yo vector with Noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculating ae, fe and ce
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, length(u)-K);
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i)));
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i)));
end
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
ye=ce*alpha';% ye vector
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
end
function e=MathworkVersionVectorized(b,u,Noise)
M = 6;% Constant1
N = 6;% Constant2
d = 0.5;% Constant3
K = length(u)/2;% Constant3
alpha=ones(1,K);% Constant4
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Calculating ao, fo and co
%%%%%%%%%%%%%%%%%%%
ho=(1:M)';
po=(1:N)';
i=1:K;
bro = u(:).';
ao=exp(j*2*pi*(ho-1)*d.*sind(bro(i)));
fo=exp(j*2*pi*(po-1)*d.*sind(bro(K+i)));
co = reshape(reshape(ao,1,[],K).*reshape(fo,[],1,K),[],K);
yo=co*alpha';
yo=awgn(yo,Noise);
%%%%%%%%%%%%%%%%%%%%%%
% Calculating ae, fe and ce
%%%%%%%%%%%%%%%%%%%%%%
he=(1:M)';
pe=(1:N)';
i=1:K;
bre = b(:).';
ae=exp(j*2*pi*(he-1)*d.*sind(bre(i)));
fe=exp(j*2*pi*(pe-1)*d.*sind(bre(K+i)));
ce = reshape(reshape(ae,1,[],K).*reshape(fe,[],1,K),[],K);
ye=ce*alpha';
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e2 = sum( (yo - ye).^2 );
e=norm(e2);
end
  6 Commenti
Torsten
Torsten il 22 Dic 2022
Modificato: Torsten il 22 Dic 2022
As Steven23 noted (who seems to have deleted his comment), you use "awgn" in your code.
Since all deterministic optimizers in MATLAB cannot deal with stochastic output from the objective function, your problem is not suited to be solved with "ga". So you shouldn't be surprised to get answers you cannot interprete.
Sadiq Akbar
Sadiq Akbar il 24 Dic 2022
Thanks to both of you dear Jan and Torsten for your comments. Dear Torsten let me try to make you understand once again. In my above codes:
my desired vector is u.
I want to run ga and it estimates the best solution vector which is exactly equal or nearly equal to my desried vector u.
For that ga generates its random population in the form of vectors of the size of u. Then it sends them one by one to my function 'myversion.m' from where it gets the correspondinf fitness value. At the last ga returns me that best solution vecor along with that fitness value. The surprize is that when I run ga with 'myversion.m', it returns me 5 best solution vectors and 5 corresponding fitness values and those 5 best solution vectors are either exactly equal to u or are nearly equal to u and the corresponding fitness values is also very small which shows that ga has estimated correct answers. But when I run ga with the "MathworkVersionVectorized.m", ti returns 5 best solution vectors and 5 fitness values. Here the fitness values are exactly the same but the best solution vectors are wrong; very far from u.
How will you know: Just keep all of them in one folder and run 'main.m'. The data will be saved in the same folder automatically. Now comment the line inside main.m where it calls 'myversion.m' and uncomment the other i.e., the line where it calls "MathworkVersionVectorized.m" and run the main.m again. Its data will also be saved to the same folder but keep its name different so that to avoid overlapping. Now for checking the best soltion vector and fitness values, just run the "TestingResults.m'. You will see that the fitness values stored in variables 'one' are identical but the best solution vectors stored in variable two are quite different. Those estimated with 'myversion.m' are correct but those estimated with "MathworkVersionVectorized.m" are wrong. So its very strange that the fitness values are quite the same but the corresponding best vectors are quite different. So I ask all of you why it is so?
I hope now you understand the problem.
Regards,

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by