KeyError when adding a constraint in python-constraint
I am making a function that takes in a list of drivers and passengers with their locations, and returns a list of allocations of passengers to drivers that maximise the number of passengers assigned to a driver, subject to the following constraints:
A passenger can only be in one car
The number of passengers assigned to each car cannot exceed the specified number of seats in the car
The distance for the journey of each driver picking up all the passengers cannot exceed some arbitrary constant
The problem I am having is with adding the first constraint. I have been following a tutorial at http://uswaretech.com/blog/2009/03/constraint-programming-in-python/ and I have used a similar style to how they solve their magic square problem: the assignment of a passenger to a car is stored as a tuple (driver, passenger) and these tuples are stored in a list. Actual details of drivers and passengers are stored in classes containing their ID, latitude and longitude, as well as how many seats are in each driver's car. Here's the code I've used to build up the problem, having extracted the test data from a file:
self.problem = Problem()
drivers = range(len(self.drivers))
passengers = range(len(self.passengers))
p = [(driver, passenger) for driver in drivers for passenger in passengers]
driver_set = [zip([e1]*len(passengers), passengers) for e1 in drivers]
passenger_set = [zip([e1]*len(drivers), drivers) for e1 in passengers]
self.problem.addVariables(p, [0,1])
for passenger in passenger_set:
self.problem.addConstraint(MaxSumConstraint(1), passenger)
print self.problem.getSolutions()
Having run this interactively, I found I can run getSolutions() before adding the constraints, but then I get the following error when running the whole thing:
Traceback (most recent call last):
File "allocation.py", line 84, in <module>
obj1.buildProblem("testdata.txt")
File "allocation.py", line 81, in buildProblem
self.problem.getSolutions()
File "/Users/wadben/Documents/Dev/Python/sp-allocation/constraint.py", l开发者_如何学Cine 233, in getSolutions
domains, constraints, vconstraints = self._getArgs()
File "/Users/wadben/Documents/Dev/Python/sp-allocation/constraint.py", line 275, in _getArgs
vconstraints[variable].append((constraint, variables))
KeyError: (2, 0)
It appears that sometime during the getSolutions() method it tries to look up (2,0), even though the maximum value of the first tuple is 1 (there are only 2 drivers in my data set). I ran the code from the tutorial and it worked fine, I'm just not sure how my code differs enough to cause an error, apart from my use of MaxSumConstraint rather than ExactSumConstraint.
Your variables have the form (driver, passenger)
:
p = [(driver, passenger) for driver in drivers for passenger in passengers]
self.problem.addVariables(p, [0,1])
The variables you give to addConstraint()
, however, have the form (passenger, driver)
:
passenger_set = [zip([e1]*len(drivers), drivers) for e1 in passengers]
for passenger in passenger_set:
self.problem.addConstraint(MaxSumConstraint(1), passenger)
So when the solver tries to group the constraints by variable, and comes to the constraint on the variable (2, 0)
, it throws an error because it doesn't know this variable (I assume your example only has two drivers).
精彩评论