How do I express optimization variable as ones and zeros in constraints?
8 views (last 30 days)
Show older comments
Johannes Hjalmarsson
on 3 Nov 2022
Commented: Johannes Hjalmarsson
on 4 Nov 2022
Hi!
I struggle with an optimization problem with four optimization variables x1 to x4, all with size 24x1. I have formulated the problem as an integer linear program, i.e. all variables are positive integers only, between 0 and 5.
My problem is the following: I would like to create a constraint which results in that maximum one variable can be nonzero in every row. E.g, if x1(1,1) is non-zero, then x2(1,1), x3(1,1) and x4(1,1) should be zero.
I tried to solve this using Matlab functions "spones" in combination with "full", ending up with the following constraint:
full(spones(x1)) + full(spones(x2)) + full(spones(x3)) + full(spones(x4)) <= 1
, in hope that the solver then would consider ones and zeros only and limit the sum to one. Although, when doing, this I get an error from "spones" saying that the "find" function is used with wrong type or with incorrect number of inputs.
I could of course change optimization method, and implement new variables which are between 0 and 1. But I would like to investigate if there are any smart tricks to solve it without changing method.
Please let me know your thoughts on this!!
BR
Johannes
5 Comments
Torsten
on 3 Nov 2022
Won't work without introducing new (binary) variables.
Here is one way to handle the max(...) expression:
Accepted Answer
Matt J
on 3 Nov 2022
Edited: Matt J
on 3 Nov 2022
You would need to introduce additional binary variables Z:
X=optimvar('X',24,4,'Lower',0,'Upper',5,'type','integer'); %original variables
Z=optimvar('Z',24,4,'Lower',0,'Upper',1,'type','integer'); %additional variables
Con.Zlow=Z>=X/5; %X>0 implies Z=1
Con.Zhigh=Z<=X; %X=0 implies Z=0
Con.Zrows=sum(Z,2)<=1; %Only 1 element/row of Z (and therefore of X) can be non-zero
prob=optimproblem('Constraints',Con)
3 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!