Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Unable to perform assignment because the left and right sides have a different number

1 visualización (últimos 30 días)
clc
clear
close all
% Problem Statement
Npar = 17;
VarLow=0.1;
VarHigh = 35;
%BBBC parameters
N=50; %number of candidates
MaxIter=100; %number of iterations
% initialize a random value as best value
XBest = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
FBest=fitnessFunc(XBest);
GB=FBest;
t = cputime;
%intialize solutions and memory
X = zeros(N, Npar);
F = zeros(N, 1);
for ii = 1:N
X(ii,:) = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
% calculate the fitness of solutions
F(ii) = fitnessFunc(X(ii,:));
end
%Main Loop
for it=1:MaxIter
%Find the centre of mass
%-----------------------
%numerator term
num=zeros(1,Npar);
for ii=1:N
for jj=1:Npar
num(jj)=num(jj)+(X(ii,jj)/F(ii));
end
end
%denominator term
den=sum(1./F);
%centre of mass
Xc=num/den;
%generate new solutions
%----------------------
for ii=1:N
%new solution from centre of mass
for jj=1:Npar
New=X(ii,:);
New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);
end
%boundary constraints
New=limiter(New,VarHigh,VarLow);
%new fitness
newFit=fitnessFunc(New);
%check whether the solution is better than previous solution
if newFit<F(ii)
X(ii,:)=New;
F(ii)=newFit;
if F(ii)<FBest
XBest=X(ii,:);
FBest=F(ii);
end
end
end
% store the best value in each iteration
GB=[GB FBest];
end
t1=cputime;
fprintf('The time taken is %3.2f seconds \n',t1-t);
fprintf('The best value is :');
XBest
FBest
% Convergence Plot
figure(1)
plot(0:MaxIter,GB, 'linewidth',1.2);
title('Convergence');
xlabel('Iterations');
ylabel('Objective Function (Cost)');
grid('on')
function newP=limiter(P,VarHigh,VarLow)
% LIMITER Function to perfrom range limit operation on the solution
% to be within the search space bounds
newP=P;
for i=1:length(P)
if newP(i)>VarHigh(i)
newP(i)=VarHigh(i);
elseif newP(i)<VarLow(i)
newP(i)=VarLow(i);
end
end
end
function fit=main1(X)
global X;
global fit;
for ii=1:17
w=0;
L=[100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100;100];
D=[0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268;0.268];
Li=L(ii);
Di=D(ii);
Xi=X(ii);
fit=w+Xi*Li*Di;
end
end
function penalty=main2(X)
global X;
global fit ;
global penalty;
E=[30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000;30000000];
XE=X*E
NodeCoordinate=[0 0;0 100;100 0;100 100;200 0;200 100;300 0;300 100;400 0];
elementNode=[2 4;2 3;1 3;3 4;4 6; 4 5; 3 5; 5 6; 6 8; 6 7; 5 7; 7 8; 8 9; 7 9;1 4;3 6; 5 8];
numberNode=size(NodeCoordinate,1)
numberelement=size(elementNode,1)
Dof=2*numberNode
Kg=zeros(Dof)
Fg=zeros(Dof,1)
Ug=zeros(Dof,1)
for e1=1:numberelement
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y2=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L
S=(y2-y1)/L
ke1=(X*E/L)*[C^2 C*S -C^2 -C*S ;
C*S S^2 -C*S -S^2 ;
-C^2 -C*S C^2 C*S ;
-C*S -S^2 C*S S^2 ]
k1=2*n1-1;k2=2*n1;
k3=2*n2-1;k4=2*n2;
Kg(k1:k2,k1:k2) =Kg(k1:k2,k1:k2)+ke1(1:2,1:2);
Kg(k1:k2,k3:k4) =Kg(k1:k2,k3:k4)+ke1(1:2,3:4);
Kg(k3:k4,k1:k2) =Kg(k3:k4,k1:k2)+ke1(3:4,1:2);
Kg(k3:k4,k3:k4) =Kg(k3:k4,k3:k4)+ke1(3:4,3:4);
end
Kg
k = [Kg(5:18,5:18)]
f=[0;0;0;0;0;0;0;0;0;0;0;0;0;-100000]
u=k\f
Ug=[0;0;0;0;u(1:14)]
Fg=Kg*Ug
u1=[Ug(3);Ug(4);Ug(7);Ug(8)]
u2=[Ug(3);Ug(4);Ug(5);Ug(6)]
u3=[Ug(1);Ug(2);Ug(5);Ug(6)]
u4=[Ug(5);Ug(6);Ug(7);Ug(8)]
u5=[Ug(7);Ug(8);Ug(11);Ug(12)]
u6=[Ug(7);Ug(8);Ug(9);Ug(10)]
u7=[Ug(5);Ug(6);Ug(9);Ug(10)]
u8=[Ug(9);Ug(10);Ug(11);Ug(12)]
u9=[Ug(11);Ug(12);Ug(15);Ug(16)]
u10=[Ug(11);Ug(12);Ug(13);Ug(14)]
u11=[Ug(9);Ug(10);Ug(13);Ug(14)]
u12=[Ug(13);Ug(14);Ug(15);Ug(16)]
u13=[Ug(15);Ug(16);Ug(17);Ug(18)]
u14=[Ug(13);Ug(14);Ug(17);Ug(18)]
u15=[Ug(1);Ug(2);Ug(7);Ug(8)]
u16=[Ug(5);Ug(6);Ug(11);Ug(12)]
u17=[Ug(9);Ug(10);Ug(15);Ug(16)]
%strease in each elemnt
for e1=1
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L1=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L1
S=(y2-y1)/L1
sigma1=E/L1*[-C -S C S]*u1
end
for el= 2
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L2=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L2
S=(y2-y1)/L2
sigma2=E/L2*[-C -S C S]*u2
end
for e1=3
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L3=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L3
S=(y2-y1)/L3
sigma3=E/L3*[-C -S C S]*u3
end
for e1=4
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L4=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L4
S=(y2-y1)/L4
sigma4=E/L4*[-C -S C S]*u4
end
for e1=5
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L5=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L5
S=(y2-y1)/L5
sigma5=E/L5*[-C -S C S]*u5
end
for e1=6
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L6=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L6
S=(y2-y1)/L6
sigma6=E/L6*[-C -S C S]*u6
end
for e1=7
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L7=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L7
S=(y2-y1)/L7
sigma7=E/L7*[-C -S C S]*u7
end
for e1=8
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L8=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L8
S=(y2-y1)/L8
sigma8=E/L8*[-C -S C S]*u8
end
for e1=9
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L9=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L9
S=(y2-y1)/L9
sigma9=E/L9*[-C -S C S]*u9
end
for e1=10
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L10=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L10
S=(y2-y1)/L10
sigma10=E/L*[-C -S C S]*u10
end
for e1=11
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L11=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L11
S=(y2-y1)/L11
sigma11=E/L11*[-C -S C S]*u11
end
for e1=12
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L12=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L12
S=(y2-y1)/L12
sigma12=E/L12*[-C -S C S]*u12
end
for e1=13
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L13=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L13
S=(y2-y1)/L13
sigma13=E/L13*[-C -S C S]*u13
end
for e1=14
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L14=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L14
S=(y2-y1)/L14
sigma14=E/L14*[-C -S C S]*u14
end
for e1=15
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L15=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L15
S=(y2-y1)/L15
sigma15=E/L15*[-C -S C S]*u15
end
for e1=16
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L16=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L16
S=(y2-y1)/L16
sigma16=E/L16*[-C -S C S]*u16
end
for e1=17
n1=elementNode(e1,1)
n2=elementNode(e1,2)
x1=NodeCoordinate(n1,1)
y1=NodeCoordinate(n1,2)
x2=NodeCoordinate(n2,1)
y=NodeCoordinate(n2,2)
theta=atan2(y2-y1,x2-x1)
L17=sqrt((x2-x1)^2+(y2-y1)^2)
C=(x2-x1)/L17
S=(y2-y1)/L17
sigma17=E/L17*[-C -S C S]*u17
end
sigma1;
sigma2;
sigma3;
sigma4;
sigma5;
sigma6;
sigma7;
sigma8;
sigma9;
sigma10;
sigma11;
sigma12;
sigma13;
sigma14;
sigma15;
sigma16;
sigma17;
for ii=1:17
N=0;
S=[sigma1;sigma2;sigma3;sigma4;sigma5;sigma6;sigma7;sigma8;sigma9;sigma10;sigma11;sigma12;sigma13;sigma14;sigma15;sigma16;sigma17];
TM=[50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000;50000];
Si=S(ii);
TMi=TM(ii);
if -(TMi)<= Si <= (TMi)
F= 0;
else
F= abs((Si)/(TMi))-1;
end
N=N+sum(F);
end
for ii=1:18
H=0;
U=Ug
DM=[2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2];
Ui=U(ii);
DMi=DM(ii);
if -(DMi) <= Ui <= (DMi)
A= 0;
else
A= abs((Ui)/(DMi))-1;
end
H=H+sum(A);
end
J=sum(N)+sum(H);
K=0.9
penalty=K*J*fit;
end
function fitness = fitnessFunc(X)
global fit;
global penalty;
fitness=penalty+fit;
end
Unable to perform assignment because the left and right sides have a different number .Can any one help me with this [ F(ii) = fitnessFunc(X(ii,:));]
  1 comentario
Sindar
Sindar el 19 de Feb. de 2020
Consider the size of fitnessFunc(X(ii,:)):
  • What do you think it should be?
  • What should it be to fit in F(ii)?
  • What is it actually? (add:
disp(size(fitnessFunc(X(ii,:))))
above
F(ii) = fitnessFunc(X(ii,:));

Respuestas (1)

Walter Roberson
Walter Roberson el 19 de Feb. de 2020
You do not assign to the global variables fit or penalty except inside the function main2(). Global variables that are not assigned to, default to being empty. Your fitness function calculation depends on those variables, so it always returns empty, but emptiness is too small to fit inside something that is explicitly indexed as a scalar.
You do not have any calls to main2() so the code there never runs.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by