0

Is it possible to write these constraints in Lp solver?

I want to check whether two rectangles are overlapping or not? Let us assume, there is one rectangle whose left bottom corner is (xk, yk) and (wi,hi) be there width and height respectively. Similarly, there is another rectangle for this left bottom corner is (xl,yl) and (wj,hj) be the width and height.

I can write for pair of (i,j) rectangles that they will not overlap such that

There does not exist any J such that

  {xk < (xl+wj)^(xk+wi)>xl}^{yk<(yl+hj)^(yk+hi)>yl}

how can I write it in Lp Solver or can I use OR operator?

Thanks

user38375
  • 31
  • 6
  • This can't be formulated as LP. Only as (Mixed-)Integer-Program. In the latter case, you will need binary-variables and indicator-constraints. – sascha Jul 01 '17 at 12:23

1 Answers1

1

A standard way to enforce two rectangles i and j do not overlap is:

x(i)+w(i) <= x(j) or
x(j)+w(j) <= x(i) or
y(i)+h(i) <= y(j) or
y(j)+h(j) <= y(i)

This can be written as a set of linear equalities:

enter image description here

Note that when comparing all rectangles i and j you only need to do this for i < j. The big-M constants need to be chosen with care.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
  • Why is it important to choose big-M with care? Isn’t it sufficient that it puts the points out of the container bounds (e.g. M=W+H)? – Adi Shavit May 06 '20 at 16:27
  • 1
    @AdiShavit For numerical reasons, these big-M constants should be as tight as possible. This is very important in practice. – Erwin Kalvelagen May 06 '20 at 17:34
  • thanks! For large problems, how would you introduce symmetry breaking to this problem (e.g. when many of the rectangles are the same size)? – Adi Shavit May 06 '20 at 17:57
  • 1
    @AdiShavit Not sure if there is much symmetry to exploit, except for only doing the comparison for `i – Erwin Kalvelagen May 06 '20 at 19:00