Precedence constraint in a binary matrix

130 views
Skip to first unread message

Ernesto

unread,
Jul 1, 2017, 6:21:18 AM7/1/17
to or-tools-discuss
Hi again!

I don't know how to write this constraints:

0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 1 1 1 1 0 0
1 1 1 1 0 0 1 0 1 1

There must be at least two 0 between the last 1 of row 0 and the first 1 of row 1. It only applies to this rows.

It can't be no 1 in row 1 when row 0 hasn't any 1 yet. Think of this as a precedence, you must have "completed" row 0 before starting with row 1.

0 0 1 1 1 1 0 0 0 0
1 1 0 0 0 0 0 0 0 0 (this matrix combination is not possible)
0 0 0 0 1 1 1 1 0 0
1 1 1 1 0 0 1 0 1 1  

For the first one I have thought the following:

for j in range(columns -2):
  solver.Add(variable[0, j] + variable[1, j+1] + variable[1, j+2] <= 1)

Do you thinks is factible this constraint? Could be any errors while looping?

But for the other one, I can't come up with an idea!

Could you give a hand with this please?

Thank you all!

Laurent Perron

unread,
Jul 1, 2017, 12:32:13 PM7/1/17
to or-tools-discuss
I would do:

pos1 = max (i * b0i)
pos2 = min(i * b1i)

pos2 >= pos + 2

--Laurent



--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ernesto

unread,
Jul 1, 2017, 1:18:00 PM7/1/17
to or-tools-discuss
Hi Laurent!

Thanks for your help. I've got your idea, that should do. However, I'm kind of stuck while trying to implement it on python as every time I run the code I get this:

File "assign.py", line 115, in main
    for j in range(num_shifts)]
TypeError: 'IntExpr' object is not iterable

And this is how I coded it:

  [solver.Add(pos1 = max (j * x[0][j]))
  for j in range(num_columns)]

  [solver.Add(pos2 = min (j * x[1][j]))
  for j in range(num_columns)]

  solver.Add(pos2 >= pos1 + 2)

#i: rows
#j: columns

What did I do wrong?

Laurent Perron

unread,
Jul 1, 2017, 1:40:46 PM7/1/17
to or-tools-discuss
Something like solver.Add(x == solver.Min([x[0][i] * i for i in range(n)]))

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discuss+unsubscribe@googlegroups.com.

Ernesto

unread,
Jul 1, 2017, 2:25:06 PM7/1/17
to or-tools-discuss
Hi Laurent,

I tried this that you wrote, but I don't know why, it isn't working out. The code has other constraints, but even though I deactivate them with # at the beginning of each constraint and leaving theses 3 constraints that I want to implement, the program failures to get a feasible solution.

 pos1 = solver.IntVar(1, columns, "pos1")
 pos2 = solver.IntVar(1, columns, "pos2")

  solver.Add(pos1 == solver.Max([x[2][i] * i for i in range(columns)]))
  solver.Add(pos2 == solver.Min([x[3][i] * i for i in range(columns)]))

  solver.Add(pos2 >= pos1 + 2)

Thank you for you help!

Ernesto

unread,
Jul 1, 2017, 2:30:48 PM7/1/17
to or-tools-discuss
Forget about the 2 and 3 that appears, is it well written in the program. Actually, those are the real rows in my program, but I just wanted to make as simply as possible my initial post.

x[2][i] is the same that x[0][j]
x[3][i] is the same that x[1][j]
Reply all
Reply to author
Forward
0 new messages