top of page

QA0006

In Mathworks Central question 187472139, following mismatch assignment error,
Ate Kasa asked how to get his piece of code back up and running.



LINK TO QUESTION https://www.mathworks.com/matlabcentral/profile/authors/18772139

CODE WITH ERROR

%% Description

% 1. This function returns the objective functions f1, and f2 in the vector 'fit' and
% constraints in the vector 'c' for the chromosome 'x'.
% 2. 'V' is the number of optimization variables.
% 3. All the constraints 'c' are converted to the form h(x)<=0.
% 4. Nine unconstrained test probems are used (p=1 to p=9)
% 5. Five constrained test problems are used (p=10 to p=14)
% 6. Refer above references for the details of each test problem: number of objectives, number of design variabes, their lower and upper limits,
% number of constraints, type of constraints etc,.

%% reference
% 1. BINH, Thanh. "A multiobjective evolutionary algorithm. The study cases".
% Technical report. Barleben, Germany. 1999.
% 2. DEB, Kalyanmoy. "Multi-Objective optimization using evolutionary
% algorithms". John Wiley & Sons, LTD. Kanpur, India. 2004.

function [fit, err]=test_case(x)
global p
%% Constrained Test functions (for p=10 to p=14)

if p==10 %WAND
x = [200 ; 6];
N=length (x);
C_p = 7600 ; % Piping cost for water reuse in $/ton
for i=1:size(x,1)
for j=1:size(x,2)

if(i~=j)
y_(i,j) = 1; % because water is exchanged b/n processses of i and j otherwise,0.
else
y_(i,j) = 0;
end
f1 = sum(x(1:N))*sum(x(1:N))*(C_p*y_(i,j));
%f1 = C_p*sum(y_(i,j),'all');
%f2 = 0.45.*sum(x(:,1))+ 0.06.*(sum(x(:,1)).*sum(x(1:N)).*x(i,j)) + 1.20*sum(x(:,2));
f2 = 0.45*sum(x(1),1:N)+ 0.06*(sum(x(1:N))*sum(x(1:N))*x(i,j)) + 1.20*sum(x(2),1:N);
%% constraints, since they are M and E balbce they are equality constraints most of the time.
% Overall mass Balance
c(1)= x(1) + sum(x(1:N).*x(i,j))- x(2) - sum(x(1:N).*x(i,j));

%% component mass Balance
C_i_in = [0 ; 0; 0] ; % in kg/h in, contaiminats are HCs,H2s and salts respectively
C_i_out = [15 ; 400; 35 ] ; % in k/h out, contaiminats are HCs,H2s and salts respectively
c(2)= (x(2) + sum(x(1:N).*x(i,j))).* C_i_out - (x(1) + sum(x(1:N).*x(i,j))).*C_i_in; % Cmax_i_k_in
c(3) = C_i_in.*(x(1) + sum(x(1:N).*x(i,j))) - sum(x(1:N).*x(i,j).*C_i_out);%Cmax_i_k_out

%% Process constraints, they are nonlinear inequalities(c)
Cmax_load = [675; 18000 ;1575 ]; % contaiminats are HCs,H2s and salts respectively
c(4) = C_i_in <= Cmax_load ;
c(5) = C_i_out <= Cmax_load ;
err=(c>0).*c;
end
end
end
if p==11 %SRN
f1=(x(1)-2)^2+(x(2)-1)^2+2;
f2=9*x(1)-(x(2)-1)^2;
c(1,1)=x(1)^2+x(2)^2-225;
c(1,2)=x(1)-(3*x(2))+10;
err=(c>0).*c;
end

result shown

Test problem index :10
Unable to perform assignment because the left and right sides have a different number of elements.

Error in test_case (line 45)
c(2)= (x(2) + sum(x(1:N).*x(i,j))).* C_i_out - (x(1) + sum(x(1:N).*x(i,j))).*C_i_in; % Cmax_i_k_in

Error in Main_NSGA2 (line 64)
[ff(i,:), err(i,:)] =feval(fname, x(i,:)); % Objective function evaulation

CODE ASSIGNING VALUES TO VECTOR c DIMENSIONED TO AVOID ABOVE ERROR

I got your code to run, but 

 

1. because there was a mismatch between

c dimensions and the amount of things (contaminants?) that you tried to store in c,

I assumed one position for each input. You have to set this to another value if you need it on different value.

2. also had to assume p to something (10?), again change accordingly.

3. the way I solved c(2) c(3) mismatched assignments may not be the way you need such values calculated. There are different ways to solve the mismatch assignment because the underlying formula you supplied had syntax errors, than when fixed, it can be done on different ways, without any further input from your side. Either concisely define the formula, and make sure the obtained values for c(2) c(3) are coherent, not far from expected measurements.

p=10
%% Constrained Test functions (for p=10 to p=14)
c=zeros(1,9);
if p==10 %WAND
x = [200 ; 6];
N=length (x);
C_p = 7600 ;
% Piping cost for water reuse in $/ton
for i=1:size(x,1)
    for j=1:size(x,2)

        if(i~=j)
            y_(i,j) = 1; 
            else
                y_(i,j) = 0;
        end
        
        f1 = sum(x(1:N))*sum(x(1:N))*(C_p*y_(i,j));

        

        f2 = 0.45*sum(x(1),1:N)+ 0.06*(sum(x(1:N))*sum(x(1:N))*x(i,j)) + 1.20*sum(x(2),1:N);
               

        %constraints, since they are M and E balbce they are equality constraints most of the time.
        % Overall mass Balance

        c(1)= x(1) + sum(x(1:N).*x(i,j)- x(2)) - sum(x(1:N).*x(i,j));

        % component mass Balance
        C_i_in = [0 ; 0; 0] ; % in kg/h in, contaminants are HCs,H2s and salts respectively
        C_i_out = [15 ; 400; 35 ] ; 
        c(2)= x(2) + sum(sum(x(1:N).*x(i,j)*C_i_out')) - x(1) + sum(sum(x(1:N).*x(i,j)*C_i_in'));
% Cmax_i_k_in
        c(3) =  sum(C_i_in.*(x(1) + sum(x(1:N).*x(i,j)) - sum(sum(x(1:N).*x(i,j)*C_i_out'))));%Cmax_i_k_out

        % Process constraints, they are nonlinear inequalities(c)
        Cmax_load = [675; 18000 ;1575 ]; 
        cL1 = C_i_in <= Cmax_load ;
        c([4:6])=cL1

        cL2 = C_i_out <= Cmax_load ;
        c([7:9])=cL2

        err=(c>0).*c;
end
end
end
if p==11 %SRN
f1=(x(1)-2)^2+(x(2)-1)^2+2;
f2=9*x(1)-(x(2)-1)^2;
c(1,1)=x(1)^2+x(2)^2-225;
c(1,2)=x(1)-(3*x(2))+10;
err=(c>0).*c;
end

%%

bottom of page