Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3967268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:41:59+00:00 2026-05-20T03:41:59+00:00

I am making a function that takes in a list of drivers and passengers

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:41:59+00:00Added an answer on May 20, 2026 at 3:41 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want a function that takes a number and returns it as a
I've been making a system that takes in data about drivers, potential passengers and
i am making insert function that takes $table argument and $cols(as array)argument. it inserts
I'm making this function that takes a string input, converts it to an int,
Hey so I'm making a serialization function that takes a base class pointer 'Joint'
I need to write a function that takes in two constraints and returns a
I am making a function that turns a list of words into an array
I'm making a mailing list script that takes advantage of ajax (async=false) to send
When making a function call in Linux (or OS X for that matter), can
Im making a program for class that manages a Hotel. I have a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.