필터 지우기
필터 지우기

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 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