I'm trying to formulate a constraint for an optimization problem that forces the solution (a combination of products, represented by binary numbers to signify if they got picked or not) to have a certain property in order.
So let's say products 1, 2 and 5 got selected, that solution is represented by [1, 1, 0, 0, 1]. These products have another property (location) that has to be in order. A Python function for checking would be:
def products_in_order(products_selected):
locations = [p.location for p in products_selected]
# locations = [80, 79, 81] (for instance)
return max(locations) - min(locations) <= 2
(This works because they're never in the same location)
However, it gets harder. The maximum location is 99, wrapping around: so [98, 99, 0] is a valid solution as well.
There are always exactly three products selected.
Thanks for any help you can give, I've been struggling with this for quite some time. Right now I'm enumerating all possible configurations, resulting in 100 constraints (which makes things sloooow).
JR