Matrix dimenssion must agree
1 次查看(过去 30 天)
显示 更早的评论
Hello,
this is my code to find out the best values but getting some error please any one help me out to solve this.
function KH
clc;
close all;
clear all
format long
% Human Inserted %
figure;
imshow('pc3.png');
hold on
%Randomly CS are Distributed%
Mmin = 65;
Mmax = 88;
M = Mmin+rand(1,4)*(Mmax-Mmin);
Nmin = 10;
Nmax= 50;
N = Nmin+rand(1,4)*(Nmax-Nmin);
plot(M, N, 'b*');
grid on
grid on
Xmin = 58;
Xmax = 105;
XX = Xmin+rand(1,30)*(Xmax-Xmin);
Ymin = 60;
Ymax= 150;
YY = Ymin+rand(1,30)*(Ymax-Ymin);
plot(XX, YY, 'b*');
hold on;
plot(49,70, 'b*');
plot(45,110, 'b*');
plot(30,139, 'b*');
plot(138,149, 'b*');
plot(128,129, 'b*');
plot(108,89, 'b*');
hold on
Emin=56;
Emax=65;
E = Emin+rand(1,5)*(Emax-Emin);
Fmin=155;
Fmax=280;
F = Fmin+rand(1,5)*(Fmax-Fmin);
plot(E,F, 'b*');
hold on
Gmin=100;
Gmax=108;
G = Gmin+rand(1,5)*(Gmax-Gmin);
Hmin=155;
Hmax=280;
H = Hmin+rand(1,5)*(Hmax-Hmin);
plot(G,H, 'b*');
hold on
A=100;
B=140;
plot(A,B, 'mo-', 'MarkerSize', 10,'LineWidth', 2);
hold on
% plot 13 Sensor nodes%
x1 = [105,107,58,58,75,146,118,18,48,55, 90,80,80];
y1 = [190,280,190,280,145,153,103,153,103,65,85,50,10];
plot(x1, y1, 'r.','MarkerSize', 20, 'LineWidth', 2);
for i = 1:4
x2(i) = M(i);
y2(i) = N(i);
end
for i = 1:30
x2(4+i) = XX(i);
y2(4+i) = YY(i);
end
for i = 1:5
x2(34+i) = E(i);
y2(34+i) = F(i);
end
for i = 1:5
x2(39+i) = G(i);
y2(39+i) = H(i);
end
x2(45)= 49;
y2(45)= 70;
x2(46)= 45;
y2(46)=110;
x2(47)= 30;
y2(47)=139;
x2(48)= 138;
y2(48)= 149;
x2(49)= 128;
y2(49)=129;
x2(50)= 108;
y2(50)=89;
% Numbering 13 Sensor%
for i = 1:13
O(i) = x1(i);
P(i) = y1(i);
text(O(i),P(i), num2str(i));
end
%Numbering 50 CS%
for i = 1:50
O(i) = x2(i);
P(i) = y2(i);
text(O(i),P(i),num2str(i));
end
% Plot set 1 in red
plot(x1, y1, 'r.', 'MarkerSize', 13);
% Plot set 2 in blue
hold on;
plot(x2, y2, 'b*', 'MarkerSize', 5);
grid on;
% calculate the distance from 13 sensor to Relay%
for i=1:13
for j=1:50
% Find distances between every point in set 1 to every point in set #2.
distances(i,j) = pdist2([x1(i),y1(i)], [x2(j), y2(j)], 'euclidean');
end
end
% find the minimum Distances%
[minDistance, indexOfClosest] = min(distances);
x1Closest = x2(indexOfClosest);
y1Closest = y2(indexOfClosest);
disp (distances);
% calculate the distance between Relay to Relay%
for i=1:50
for j=1:50
% Find distances between every point in set 1 to every point in set #2.
dis_rly(i,j) = pdist2([x2(i),y2(i)], [x2(j), y2(j)], 'euclidean');
end
end
disp (dis_rly);
% find out the distance between Relay to sink%%
dist1 = sqrt((x2-A).^2+(y2-B).^2)
%logicalindexes = dist1 < 30.00000;
%x2Nearby = x2(logicalindexes)
%y2Nearby = y2(logicalindexes)
%disp (dist1);
% Find out the distance between Sensor to Sink %%%
dist2 =sqrt((x1-A).^2+(y1-B).^2)
logicalindexes = dist2 < 30.00000;
x1Nearby = x1(logicalindexes);
y1Nearby = y1(logicalindexes);
disp (minDistance);
% opposition based alloation %
NR=10;
MI=200;
NP=10;
NK=5;
UB = 10*ones(1,10);
LB = -10*ones(1,10);
Dt = mean(abs(UB-LB))/2; % Scale Factor
F = zeros(NP,NK);D = zeros(1,NK);V = zeros(NP,NK); %R = zeros(NP,NK);
Vf = 0.02; Dmax = 0.005; Nmax = 0.01; Sr = 0;
C_flag = 1;
for nr = 1:NR
a=1:50;
for i = 1:5
out(i,:) = a(randperm(numel(a),10))
opo_out(i,:) = 51-out(i,:)
end
%for i = 1:NP
%for j = 1:NK
%out(i,j) = LB(j) + (UB(j) - LB(j)).*rand(0,1);
%opo_out(i,j) = LB(j) + (UB(j)-out(z1,z2));
%end
%end
N=10;
data_agg=1;
Er = 36.1;
s=250;
Et=16.7;
Eamp=1.97;
% Calculate the Energy of Relay nodes %
for j = 1:5
for i=1:10
k= 1;
if ((dist2(j))> 30.000000)
Etf(j,i,k)= N*data_agg*Et*(s*dist1(k,out(j,i)))+N*s*data_agg*Et;
Erf = (N-1)*Er*s;
OEtf(j,i,k)= N*data_agg*Et*(s*dist1(k,opo_out(j,i)))+N*s*data_agg*Et;
Ef_total(j,i,k) = Etf(j,i,k)+Erf ;
OEf_total(j,i,k) = OEtf(j,i,k)+Erf;
end
end
end
% Calculate The Energy Of Sensor nodes %%%
for j = 1 :5
for i = 1:10
for k = 1:13
Ets(j,i,k)=s*(Et+Eamp*(distances(k,out(j,i)).^2));
OEts(j,i,k)=s*(Et+Eamp*(distances(k,opo_out(j,i)).^2));
end
end
end
disp(Ets);
disp(OEts);
d0=30;
c=10;
tic
%Claculate the fitness %%%
for i = 1:5
fitness(i,:)= (c*out(i))+(c*((Ets(i)*1)+Ef_total(1)))+(c*sqrt((distances(k,out(i))-d0).^2))+(c*1);
Ofitness(i,:)= (c*opo_out(i))+(c*((OEts(i)*1)+OEf_total(1)))+(c*sqrt((distances(k,opo_out(i))-d0).^2))+(c*1);
end
wtime=toc
fprintf(1, 'fitness took %f seconds to Run.\n',wtime);
disp(fitness);
disp(Ofitness);
%K = min(Fit)
i = 1:5;
if (fitness(i) > Ofitness(i))
K(i) = Ofitness(i)
X = opo_out
else
K(i) = fitness(i)
X = out
end
%% Optimization & Simulation
Kib=K;
Xib=X;
[Kgb(1,nr), A] = min(K)
Xgb(:,1,nr) = X(:,A)
for j = 1:MI
% Virtual Food
for ll = 1:NP
Sf = (sum(X(ll))./K);
end
Xf(:,j) = Sf./(sum(1./K)) %Food Location
Kf = (c*Xf(j))+(c*((Ets(i)*1)+Ef_total(1)))+(c*sqrt((distances(k,int64(Xf(j)))-d0).^2))+(c*1);
if 2<=j
if Kf(j-1)<Kf(j)
Xf(:,j) = Xf(:,j-1);
Kf(j) = Kf(j-1);
end
end
Kw_Kgb = max(K)-Kgb(j,nr);
w = (0.1+0.8*(1-j/MI))
for i = 1:NK
% Calculation of distances
Rf = Xf(:,j)-X(:,i);
Rgb = Xgb(:,j,nr)-X(:,i);
for ii = 1:NK
RR(:,ii) = X(:,ii)-X(:,i);
end
R = sqrt(sum(RR.*RR))
% % % % % % % % % % % % % Movement Induced % % % % % % % % % %
% Calculation of BEST KRILL effect
if Kgb(j,nr) < K(i)
alpha_b = -2*(1+rand*(j/MI))*(Kgb(j,nr) - K(i)) /Kw_Kgb/ sqrt(sum(Rgb.*Rgb)) * Rgb
else
alpha_b=0;
end
% Calculation of NEIGHBORS KRILL effect
nn=0;
ds = mean(R)/5;
alpha_n = 0;
for n=1:NK
if and(R(i)<ds,n~=i)
nn=nn+1;
if and(nn<=4,K(i)~=K(n))
alpha_n = alpha_n-(K(n) - K(i)) /Kw_Kgb/ R(n) * RR(:,n)
end
end
end
disp(alpha_n);
% Movement Induced
V(:,i) = w*V(:,i)+Nmax*(alpha_b+alpha_n);
% % % % % % % % % % % % % Foraging Motion % % % % % % % % % %
% Calculation of FOOD attraction
if Kf(j) < K(i)
Beta_f=-2*(1-j/MI)*(Kf(j) - K(i)) /Kw_Kgb/ sqrt(sum(Rf.*Rf)) * Rf;
else
Beta_f=0;
end
% Calculation of BEST psition attraction
Rib = Xib(:,i)-X(:,i);
if Kib(i) < K(i)
Beta_b=-(Kib(i) - K(i)) /Kw_Kgb/ sqrt(sum(Rib.*Rib)) *Rib;
else
Beta_b=0;
end
% Foraging Motion
F(:,i) = w*F(:,i)+Vf*(Beta_b+Beta_f);
% % % % % % % % % % % % % Physical Diffusion % % % % % % % % %
D = Dmax*(1-j/MI)*floor(rand+(K(i)-Kgb(j,nr))/Kw_Kgb)*(2*rand(NP,1)-ones(NP,1));
% % % % % % % % % % % % % Motion Process % % % % % % % % % % %
DX = Dt*(V(:,i)+F(:,i));
% % % % % % % % % % % % % Crossover % % % % % % % % % % % % %
if C_flag ==1
C_rate = 0.8 + 0.2*(K(i)-Kgb(j,nr))/Kw_Kgb;
Cr = rand(NP,1) < C_rate ;
% Random selection of Krill No. for Crossover
NK4Cr = round(NK*rand+.5);
% Crossover scheme
X(:,i)=X(:,NK4Cr).*(1-Cr)+X(:,i).*Cr;
end
% Update the position
X(:,i)=X(:,i)+DX;
%X(:,i)=findlimits(X(:,i),LB,UB,Xgb(:,j,nr)); % Bounds Checking
K(i)=(c*X(i))+(c*((Ets(i)*1)+Ef_total(1)))+(c*sqrt((distances(k,int64(X(i)))-d0).^2))+(c*1);;
if K(i)<Kib(i)
Kib(i)=K(i);
Xib(:,i)=X(:,i);
end
end
% Update the current best
[Kgb(j+1,nr), A] = min(K);
if Kgb(j+1,nr)<Kgb(j,nr)
Xgb(:,j+1,nr) = X(:,A);
else
Kgb(j+1,nr) = Kgb(j,nr);
Xgb(:,j+1,nr) = Xgb(:,j,nr);
end
end
end
%% Post-Processing
[Best, Ron_No] = min(Kgb(end,:))
Xgb(:,end,Ron_No)
Mean = mean(Kgb(end,:))
Worst = max(Kgb(end,:))
Standard_Deviation = std(Kgb(end,:))
Thank you
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!