Optimization: Unable to perform assignment because value of type 'optim.pro​blemdef.Op​timization​Expression​' is not convertible to 'double'.

Hello,
I would like to perform a multiplication with a decision variable Yi. When doing so, I get the following error:
Unable to perform assignment because value of type 'optim.problemdef.OptimizationExpression' is not convertible to 'double'.
What's the Problem and how could I convert Yi to an double?
The code is:
Yi = optimvar('Yi', ns, 1,'type','integer','LowerBound',0,'UpperBound',1);
Dij = csvread("test_distanzmatrix_20.csv",1,1);
ns = size(Dij,1);
[...]
Yi_transponiert = transpose(Yi);
YD_1 = zeros(ns);
YD_2 = zeros(ns);
for i = 1:ns
YD_1(:,i) = Yi(:) .* Dij(:,i);
end
for j = 1:ns
YD_2(j,:) = Yi_transponiert(:) .* Dij(j,:);
end
LP_Distanz = YD_1 .* YD_2;
for i = 1:ns
LP_Distanz (i,i) = 0;
end
logistikpunktsuche.Constraints.cons9 = LP_Distanz >= D^2;

Answers (2)

Do not preallocate YD_1 as zeros, use optimexpr()

8 Comments

Yi_transponiert(:) is always a column vector, Dij(j,:) is a row vector.
The elementwise product of a column and a row vector gives a matrix by implicit expansion.
You can't assign a matrix to YD_2(j,:) which is a single row.
Your array sizes are confused. And your code only works if Dij is a square matrix (well, or if the number of columns exceeds the number of rows maybe it would not fail.)
I cannot tell what sizes you are intending... and I am left wondering whether perhaps you should just be calculating something like YD_1 = Yi .* Dij
%Dij = csvread("test_distanzmatrix_20.csv",1,1);
Dij = rand(5, 5);
ns = size(Dij,1);
Yi = optimvar('Yi', ns, 'type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
Yi_transponiert = transpose(Yi);
YD_1 = optimexpr([ns,1]);
YD_2 = optimexpr([1,ns]);
for i = 1:ns
YD_1(:,i) = Yi(:) .* Dij(:,i);
end
for j = 1:ns
YD_2(j,:) = Yi_transponiert .* Dij(j,:);
end
LP_Distanz = YD_1 .* YD_2;
for i = 1:ns
LP_Distanz (i,i) = 0;
end
Yi = optimvar('Yi', ns, 'type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
That generates a column vector
Yi_transponiert = transpose(Yi);
That transposes it to be a row vector.
YD_2(j,:) = Yi_transponiert(:) .* Dij(j,:);
That (:) reshapes the row vector to be a column vector. When you use (:) then no matter what size the input was, the output will be a column vector. Which is a problem if you are multiplying by a row vector.
A(:) will never be a row vector, independent of what A is.
A = [1 2 3]
A = 1×3
1 2 3
A(:)
ans = 3×1
1 2 3
A = [1;2;3]
A = 3×1
1 2 3
A(:)
ans = 3×1
1 2 3
I stand corrected... Any factor other than 0 should be greater than D. Can this be represented? So: If not 0, then greater than D.
I do not understand what you are requesting about facters other than 0 should be greater than D ??
Are you looking at
logistikpunktsuche.Constraints.cons9 = LP_Distanz >= D^2;
and saying that the real constraint is that the value is permitted to be 0 if some corresponding entry is 0, otherwise has to be >= D^2 ?
If so then I am not clear as to which value to refer to for the "factor" that is permitted to be 0 ?
If you had an array of factors the same size as LD_Distanz then
logistikpunktsuche.Constraints.cons9 = (FactorArray == 0) | (LP_Distanz >= D^2);

Sign in to comment.

Here's a way to express the constraints linearly:
Dij=rand(5); D=1; %hypothetical input data
ns = size(Dij,1);
Yi = optimvar('Yi', ns, 'type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
M = optimvar('M', [ns,ns], 'type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
YY=Yi*ones(1,ns);
logistikpunktsuche.Constraints.Mupper= M<=(YY+YY.')/2;%Mupper and Mlower together
logistikpunktsuche.Constraints.Mlower= M>=(YY+YY.')-1;%equivalent to M==Yi&Yi.'
logistikpunktsuche.Constraints.LP_Distanz = Dij.*(1-eye(ns)).*M >= D^2.*M
logistikpunktsuche = struct with fields:
Constraints: [1×1 struct]

Asked:

on 8 Jan 2023

Edited:

on 9 Jan 2023

Community Treasure Hunt

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

Start Hunting!