Johan Rensink
Johan Rensink

Reputation: 193

Formulating Linear Integer Programming Constraint

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

Upvotes: 3

Views: 202

Answers (2)

Johan Rensink
Johan Rensink

Reputation: 193

I ended up solving this problem with a meta solution, that a friend of mine came up with.

Since choosing the product with position X infers the other allowed choices (namely X+1 and X+2) it makes sense to optimize on groups of products, not on individual products. I've built this and it works beautifully.

Thanks for the responses!

Upvotes: 1

Ander Biguri
Ander Biguri

Reputation: 35525

The condition you are looking for is

return abs( mod( max(locations) - min(locations) +50,100)-50) <=2

or in a general form:

abs(mod( distance + range/2,range)-range/2)

This gives the minimum distance in a circular space. It is commonly used to compute the angular distance of 2 given points in a circle, where range is 2*pi and distance is angle2-angle1

Upvotes: 0

Related Questions