简体   繁体   English

制定线性整数规划约束

[英]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]. 因此,假设选择了产品1,2和5,该解决方案由[1,1,0,0,1]表示。 These products have another property (location) that has to be in order. 这些产品具有必须按顺序排列的另一个属性(位置)。 A Python function for checking would be: 用于检查的Python函数将是:

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. 最大位置是99,环绕:所以[98,99,0]也是一个有效的解决方案。

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). 现在我正在枚举所有可能的配置,导致100个约束(这使得事情变得缓慢)。

JR JR

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. 由于选择位置X的产品推断出其他允许的选择(即X + 1和X + 2),因此优化产品组而不是单个产品是有意义的。 I've built this and it works beautifully. 我已经建立了它,它的工作非常精彩。

Thanks for the responses! 谢谢你的回复!

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 它通常用于计算圆中2个给定点的角距,其中范围为2*pi ,距离为angle2-angle1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM