Filter löschen
Filter löschen

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

8 Ansichten (letzte 30 Tage)
b999
b999 am 15 Sep. 2023
Kommentiert: Matt J am 29 Okt. 2023
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 Kommentare
Matt J
Matt J am 15 Sep. 2023
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 am 15 Sep. 2023
The could be horizontal or vertical neighbours, but not diagonal.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 15 Sep. 2023
Bearbeitet: Matt J am 15 Sep. 2023
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 Kommentare
b999
b999 am 29 Okt. 2023
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 am 29 Okt. 2023
So that no bound is placed on locations where there are no 2s.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Number games finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by