Optimization, optimconstr: Unable to perform assignment because the left and right sides have a different number of elements.

2 views (last 30 days)
This is a program about optimization. I use con=optimiconstr() and for loop to read the constraints. But Matlab shows something wrong with my code as follows. I don't know why it is wrong. Could you give me some tips?
clear;
data=[9.4888 8.7928 11.5960 11.5643 5.6756 9.8497 9.1756 13.1385 15.4663 15.5464;
5.6817 10.3868 3.9294 4.4325 9.9658 17.6632 6.1517 11.8569 8.8721 15.5858];
d=zeros(10);
for i=1:9
for j=2:10
if j>i
d(i,j)=norm(data(:,i)-data(:,j));
end
end
end
x=optimvar('x',10,1,LowerBound=0,UpperBound=1,Type='integer');
y=optimvar('y',10,10,LowerBound=0,UpperBound=1,Type='integer');
prob=optimproblem("ObjectiveSense","min");
prob.Objective=sum(x);
% Above are initiation, and constraints are as follows.
con=optimconstr(32,1);
con(32)=sum(y,1)>=1;% Matlab says here "Unable to perform assignment because
Unable to perform assignment because the left and right sides have a different number of elements.
% the left and right sides have a different number of elements."
con(31)=sum(y,2)<=5;% Matlab says here the same mistake too as above.
for i=1:10
for j=1:10
con(i)=d(i,j)*y(i,j)<=10*x(i);
con(i+10)=x(i)>=y(i,j);
end
con(i+20)=x(i)==y(i,i);
end
prob.Constraints=con;
[s,fval]=solve(prob);
show(prob);
s.x
s.y
fval
However, if I write the constraints in another way in which I don't use con=optimconstr(), then the program can run:
clear;
data=[9.4888 8.7928 11.5960 11.5643 5.6756 9.8497 9.1756 13.1385 15.4663 15.5464;
5.6817 10.3868 3.9294 4.4325 9.9658 17.6632 6.1517 11.8569 8.8721 15.5858];
d=zeros(10);
for i=1:9
for j=2:10
if j>i
d(i,j)=norm(data(:,i)-data(:,j));
end
end
end
x=optimvar('x',10,1,LowerBound=0,UpperBound=1,Type='integer');
y=optimvar('y',10,10,LowerBound=0,UpperBound=1,Type='integer');
prob=optimproblem("ObjectiveSense","min");
prob.Objective=sum(x);
% Above are initiation (same as the first program), and constraints are as follows.
con1=[1<=sum(y)';sum(y,2)<=5];% I don't use con=optimconstr(), I just create an ordinary vector.
con2=[];
for i=1:10
con2=[con2;x(i)==y(i,i)];
for j=1:10
con1=[con1;d(i,j)*y(i,j)<=10*x(i);y(i,j)<=x(i)];
end
end
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
[s,fval]=solve(prob);
s.x
s.y
fval

Accepted Answer

Aditya
Aditya on 24 Jan 2023
Hi,
I understand that you are facing error when performing assignment to an OptimizationConstraint array.
The array con has preallocated memory for 32 OptimizationConstraint elements. When you perform the assignment operation:
con(32)=sum(y,1)>=1;
It is expected that sum(y,1) is a single OptimizationConstraint.
However, y is a 10x10 matrix. sum(y,1) is the sum along the 1st dimension, which is sum of each column, resulting in a 1x10 vector. The inequality constraint: sum(y,1)>=1 results in the following 10 inequality constraints:
sum(y,1)>=1 is same as [
sum(y(:,1)) >= 1,
sum(y(:,2)) >= 1,
sum(y(:,3)) >= 1,
sum(y(:,4)) >= 1,
sum(y(:,5)) >= 1,
sum(y(:,6)) >= 1,
sum(y(:,7)) >= 1,
sum(y(:,8)) >= 1,
sum(y(:,9)) >= 1,
sum(y(:,10)) >= 1
]
In summary, left side was expecting 1 OptimizationConstraint and right side had 10 OptimizationInequality constraints. Hence, the error "Unable to perform assignment because the left and right sides have a different number of elements."
You get the same error due to the same reason in this assignment
con(31)=sum(y,2)<=5;

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!