How do I express optimization variable as ones and zeros in constraints?
Show older comments
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
You said you want to have x1,...,x4 to be integers between 0 and 5.
Now you want to constrain them as
x1(1:24) + x2(1:24) + x3(1:24) + x4(1:24) <= ones(24,1).
So x1,...,x4 become integers between 0 and 1 instead of 0 and 5.
Is it this what you want ?
Johannes Hjalmarsson
on 3 Nov 2022
Edited: Johannes Hjalmarsson
on 3 Nov 2022
Using ones and zeros was just one of my suggestions
Yes, but it restricts the values in x1,...,x4 to 0 or 1 (which - as far as I understood now) is not what you want.
I think you will have to introduce the contraints
x1(i) + x2(i) + x3(i) + x4(i) = max(x1(i),x2(i),x3(i),x4(i))
which means that you will have to introduce new binary variables to express the maximum expression.
Johannes Hjalmarsson
on 3 Nov 2022
Torsten
on 3 Nov 2022
Won't work without introducing new (binary) variables.
Here is one way to handle the max(...) expression:
Accepted Answer
More Answers (0)
Categories
Find more on Write Constraints in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!