在 python-constraint 中添加约束时出现 KeyError

发布于 2024-10-17 08:12:27 字数 1839 浏览 1 评论 0原文

我正在制作一个函数,该函数接收司机和乘客及其位置的列表,并向司机返回乘客分配列表,以最大限度地增加分配给司机的乘客数量,但须遵守以下约束:

  1. 乘客只能乘坐一辆车

  2. 每节车厢分配的乘客数量不能超过该车厢规定的座位数

  3. 每个司机接载所有乘客的行程距离不能超过某个任意常数

我遇到的问题是添加第一个约束。我一直在关注 http://uswaretech.com/blog 上的教程/2009/03/constraint-programming-in-python/ 和我使用了类似的风格来解决他们的幻方问题:将乘客分配给汽车存储为元组(司机,乘客)并且这些元组存储在列表中。驾驶员和乘客的实际详细信息存储在包含他们的 ID、纬度和经度以及每个驾驶员的汽车中有多少个座位的类中。这是我用来构建问题的代码,从文件中提取了测试数据:

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()

以交互方式运行此代码后,我发现我可以在添加约束之前运行 getSolutions() ,但是在运行时出现以下错误整个事情:

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", line 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)

似乎在 getSolutions() 方法中的某个时候,它尝试查找 (2,0),即使第一个元组的最大值是 1(我的数据集中只有 2 个驱动程序)。我运行了教程中的代码,它工作得很好,除了我使用 MaxSumConstraint 而不是 ExactSumConstraint 之外,我只是不确定我的代码有何不同足以导致错误。

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:

  1. A passenger can only be in one car

  2. The number of passengers assigned to each car cannot exceed the specified number of seats in the car

  3. 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", line 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-10-24 08:12:27

您的变量的形式为(driver,passenger)

p = [(driver, passenger) for driver in drivers for passenger in passengers]
self.problem.addVariables(p, [0,1])

但是,您提供给addConstraint()的变量的形式为(passenger,driver) >:

passenger_set = [zip([e1]*len(drivers), drivers) for e1 in passengers]
for passenger in passenger_set:
    self.problem.addConstraint(MaxSumConstraint(1), passenger)

因此,当求解器尝试按变量对约束进行分组,并遇到变量 (2, 0) 的约束时,它会抛出错误,因为它不知道该变量(我假设您的示例只有两个驱动程序)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文