Same code but different results-very strange?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Torsten
le 22 Déc 2022
Modifié(e) : Torsten
le 22 Déc 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.
Réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!