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

8 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number games 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by