Optimization: Unable to perform assignment because value of type 'optim.problemdef.OptimizationExpression' 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;
回答(2 个)
Walter Roberson
2023-1-8
编辑:Walter Roberson
2023-1-8
1 个投票
Do not preallocate YD_1 as zeros, use optimexpr()
8 个评论
Torsten
2023-1-8
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.
Walter Roberson
2023-1-8
What is the error message?
Walter Roberson
2023-1-8
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
Walter Roberson
2023-1-8
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(:)
A = [1;2;3]
A(:)
Walter Roberson
2023-1-8
The code I posted at https://www.mathworks.com/matlabcentral/answers/1890467-optimization-unable-to-perform-assignment-because-value-of-type-optim-problemdef-optimizationexpre#comment_2552437 already has the correction for both of those problems.
Laura Grönniger
2023-1-8
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.
Walter Roberson
2023-1-8
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);
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
类别
在 帮助中心 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!