Search code examples
pythonjupyter-notebookpulp

How can I fix a Traceback (most recent call last) error in a Pulp constraint


I am trying add constraints to this linear optimization problem using a for loop. The constraints are... x1A + x1B + x1C + x1D = 1, x2A + x2B + x2C + x2D = 1, etc...

The Traceback error occurs on the first run of the loop, and I am struggling to figure out why. Any help would be greatly appreciated!

problem2 = LpProblem("Problem_2",LpMinimize)
Letter=['A','B','C','D']
Number=['1','2','3','4']
NumArray = [185,225,193,207],[200,190,175,225],[330,320,315,300],[375,389,425,445]
NumArray=makeDict([Number,Letter], NumArray)

[problem2_vars = LpVariable.dicts("x",(Number, Letter),lowBound=0,cat='Binary')
problem2_vars\['1'\]\['A'\]

problem2 += lpSum(\[problem2_vars\[i\]\[j\]*NumArray\[i\]\[j\] for i in Number for j in Letter\])

for j in Number:
    problem2 += lpSum(\[problem2_vars\[j,'A'\]\]) == 1,"%s"%j][1]

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-31-04a89d6c02e1> in <module>
      2 
      3 for j in Number[0:]:
----> 4     problem2 += lpSum([problem2_vars[j,'A']]) == 1,"%s"%j

KeyError: ('1', 'A')

Solution

  • You have quite a few syntax problems packed into a small piece of code! :)

    A couple pointers:

    • Why are you "escaping" everything with backslash?
    • You don't need to make your variables inside of a list... it isn't clear what you are doing there with the list brackets, etc.
    • The main issue with your error is that you are not passing to the LpVariable.dicts a list of the indices you want to use. You are just passing in a tuple with two lists in it.

    In the future, try printing your model as I do in the example below to see what the heck is being constructed. And if you google pulp examples (or look at the many on this site that are tagged) I think you can avoid some of the syntax mess you have. Good luck!

    from pulp import *
    
    p2 = LpProblem("p2", LpMinimize)
    
    # data
    ltrs = ['A', 'B']
    nums = [1, 2]
    
    coefs =  {  ('A', 1): 22,
                ('A', 2): 42,
                ('B', 1): 7,
                ('B', 2): 79}
    
    x = LpVariable.dicts('x', indexs=coefs.keys(), cat='Binary')
    
    # objective
    p2 += lpSum(x[ltr, num] * coefs[ltr, num] for (ltr, num) in coefs.keys())
    
    # constraint
    for num in nums:
        p2 += lpSum(x[ltr, num] for ltr in ltrs) == 1
    
    sol = p2.solve()
    
    print(p2)
    
    print(sol)
    

    Yields:

    ...
    Result - Optimal solution found
    
    Objective value:                49.00000000
    Enumerated nodes:               0
    Total iterations:               0
    Time (CPU seconds):             0.00
    Time (Wallclock seconds):       0.00
    
    Option for printingOptions changed from normal to all
    Total time (CPU seconds):       0.00   (Wallclock seconds):       0.00
    
    p2:
    MINIMIZE
    22*x_('A',_1) + 42*x_('A',_2) + 7*x_('B',_1) + 79*x_('B',_2) + 0
    SUBJECT TO
    _C1: x_('A',_1) + x_('B',_1) = 1
    
    _C2: x_('A',_2) + x_('B',_2) = 1
    
    VARIABLES
    0 <= x_('A',_1) <= 1 Integer
    0 <= x_('A',_2) <= 1 Integer
    0 <= x_('B',_1) <= 1 Integer
    0 <= x_('B',_2) <= 1 Integer
    
    1
    [Finished in 242ms]