Bin-packing problem: struggling to add constraint such that all items packed in bin MUST have same delivery destination #3419
-
As the title suggests, I am using OR-Tools to tackle a bin-packing problem. I would like to require that all orders packed into a given truck have the same delivery destination. The following is my attempt at implementing this, which doesn't seem to be working: data['trucks'] = [0, 1, 2, 3, ...]
data['orders'] = [0, 1, 2, 3, ...]
data['delivery_county_id'] = [8, 8, 8, 1, 3, 2, ...]
from itertools import groupby
# Checks if all elements of list are equal
def all_equal(iterable):
g = groupby(iterable)
return next(g, True) and not next(g, False)
for j in data['trucks']:
solver.Add( all_equal ( [ x[(i, j)] * data['delivery_county_id'][i] for i in data['orders'] if x[(i, j)] == 1 ] ) == True ) Strangely, I am not getting any errors when I execute the code, but my constraint is not being obeyed. I'm not sure why that is. Any assistance or suggestions would be profoundly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
First, which solver. I assume CPSat. From a quick look, my guess is that you are not doing what you think you are doing, with the call to all_equal returning next() and next(). I have very bad luck using clever constructs and iterators and the like from python when creating constraints. It is much safer in my opinion to be explicit. Second, What is You can't really expect much meaningful help if you don't include a bit more from your program. But I'll take a guess. If those are just python variables (as I suspect from the phrase And if those are IntVars, then you can't really call "==" in python-land and expect a meaningful result. The I think you should look at channeling constraints in the help, so that you can set up boolean variables (NewBoolVar type things) that map your notion of "these are equal" (true) or these are not equal (false) into solver-land, and then you can set up constraints that use those BoolVars as conditions. But again I'm just guessing that this is your problem. Maybe I'm misreading. |
Beta Was this translation helpful? Give feedback.
-
I would recommend:
|
Beta Was this translation helpful? Give feedback.
I would recommend: