Constraining a value to a set of integers

100 views
Skip to first unread message

Scott Anderson

unread,
Mar 10, 2013, 12:56:25 PM3/10/13
to or-tools...@googlegroups.com
I'm using the Python bindings...

I have an IntVar:

object_vars.append(solver.IntVar(0, num_positions-1, 'object[%i]' % object["id"]))

The value for most objects can range from 0 to the maximum number of positions - 1. However, some objects can take on only a discrete set of values within that range.

For example, if the range is 0-99, some of the objects can take on any value from 0-99, but some can only take on the values (5, 15, 25, 35, 45, 55, 65, 75, 85, 95).

I haven't been able to figure out how to constrain to a set like this (although I just started using the library yesterday). My first approach will be to do something like this:

for i in (all the numbers from 0-99 that aren't in the set of required values):
    solver.Add(object_vars[idx] != i)

This seems less than optimal, however. What I was expecting was something like an InSetConstraint from python-constraint:

solver.Add(object_vars[idx] in (5, 15, 25, 35, 45, 55, 65, 75, 85, 95))

Any suggestions would be appreciated, or even just pointing out that one of the examples is doing this and I didn't recognize that fact.

A related question: is there a list of variable types and constraints with descriptions somewhere? I'm finding it rough going to figure out all the possible Things To Use, let along what the various Things are used for.

Regards,
-scott

Laurent Perron

unread,
Mar 10, 2013, 3:11:48 PM3/10/13
to or-tools-discuss
Hello, 

the API is 

solver.Add(object_vars[idx].Member([5, 15, ...]))

The in API is a good idea. I will have a look.

Thanks


--
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/groups/opt_out.
 
 



--
--Laurent

Graham Laight

unread,
Mar 11, 2013, 4:54:32 AM3/11/13
to or-tools...@googlegroups.com
One possible solution:

5 x1 +  15 x2 +  25 x3 +  35 x4  + 45 x5 + 55 x6 +  65 x7 + 75 x8 +  85 x9 +  95 x10

Laurent Perron

unread,
Mar 11, 2013, 6:32:41 AM3/11/13
to or-tools...@googlegroups.com
no, this would propagate much less than the Member version.
Better still, you can create the IntVar directly with the sparse domain.

solver.IntVar([1, 3, 5, ..], 'x')

Thanks

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



--

Filip Camara

unread,
Mar 11, 2013, 6:53:56 AM3/11/13
to or-tools...@googlegroups.com
Regarding this, please, can someone explain to me why the first value can't be part of a constraint (I have a working example of this behavior here : https://groups.google.com/forum/?fromgroups=#!topic/or-tools-discuss/ooIdIq76cNw)?


Scott Anderson

unread,
Mar 11, 2013, 11:42:48 AM3/11/13
to or-tools...@googlegroups.com
Thanks.

Interestingly, my original version seems to have much better performance characteristics. I randomize the inputs before running the constraints, and certain orderings will cause the sparse domain version to go off into the weeds and get cut off by the time constraint (1 second), whereas the solver.Add(object_vars[idx] != i) version always seems to return in less than 15ms no matter the input.

Regards,
-scott
Reply all
Reply to author
Forward
0 new messages