フィルターのクリア

Problem-based Optimization: Constraint sum(x==1) >=1 doesn't work.

8 ビュー (過去 30 日間)
b999
b999 2023 年 9 月 15 日
コメント済み: Matt J 2023 年 10 月 29 日
Hello all,
i have a few questions:
1) I would like to show for a problem-based optimization problem that my variable x, a matrix, must contain the integer 1, 2 and 3 at least once. I have tried it like this:
A = [1 2 3;
0 3 0];
[rowA, colA] = size(A);
prob = optimproblem("Description","Topologyoptimization");
x = optimvar("x",rowA,colA,"Type","integer","LowerBound",0,"UpperBound",3);
prob.Constraints.min1 = sum(x == 1) >= 1;
prob.Constraints.min2 = sum(x == 2) >= 1;
prob.Constraints.min3 = sum(x == 3) >= 1;
Unfortunately it does not work and this error message appears:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in test_problembased_topo (line 22)
prob.Constraints.mindF = sum(x == 1) >= 1;
What can I change to implement the constraints?
2) In addition, I want to create two boundary conditions which say that the integers 1 and 3 must always be located next to an integer 2 in the matrix (like it is in the matrix A). How can I implement this as a boundary condition?
  2 件のコメント
Matt J
Matt J 2023 年 9 月 15 日
2) In addition, I want to create two boundary conditions which say that the integers 1 and 3 must always be located next to an integer 2 in the matrix (like it is in the matrix A). How can I implement this as a boundary condition?
Does that mean they will be horizontal neighbors, or could they also be vertical/diagonal neighbors?
b999
b999 2023 年 9 月 15 日
The could be horizontal or vertical neighbours, but not diagonal.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2023 年 9 月 15 日
編集済み: Matt J 2023 年 9 月 15 日
I think it would be better to formulate it like this:
A = [1 2 3;
0 3 0];
[rowA, colA] = size(A);
k=zeros(3);
k([2 4 6 8])=1; %Neighbor-counting kernel
C=func2mat(@(z) conv2(z,k,'same'), zeros([rowA,colA]));%From the File Exchange:https://www.mathworks.com/matlabcentral/fileexchange/44669-convert-linear-functions-to-matrix-form
col=@(z) z(:);
x = optimvar("x",[rowA,colA,3],"Type","integer","LowerBound",0,"UpperBound",1);
prob = optimproblem("Description","Topologyoptimization");
prob.Constraints.noOverlap = sum(x,3) <= 1;
prob.Constraints.minContent = sum(sum(x,1),2)>=1;
prob.Constraints.Neighbors1 = col(x(:,:,1)) - C*col(x(:,:,2)) <=0;
prob.Constraints.Neighbors3 = col(x(:,:,3)) - C*col(x(:,:,2)) <=0;
...
xsol=solve(prob).x;
A=xsol(:,:,1) + 2*xsol(:,:,2) + 3*xsol(:,:,3);
  76 件のコメント
b999
b999 2023 年 10 月 29 日
I have a question regarding the function noBlocks. The constraint is:
con{i}= K*x(:,2)<=9*(1-x(:,2))+2*x(:,2);
The part
K*x(:,2)<= 2*x(:,2);
says, that every 2 should have maximum two 2s as neighbours around. (That's how I understand it)
But why do you need this part:
9*(1-x(:,2))
It defines the cells where is no 2. I don't understand why you implement this part. Thank you.
Matt J
Matt J 2023 年 10 月 29 日
So that no bound is placed on locations where there are no 2s.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber games についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by