Search code examples
pythonlinear-programmingpulp

Conditional constraints in PuLP


I have the following optimization model using PuLP.

neighborhood_lst = [
    'btm_bin',
    'hsr_bin',
    'jayanagar_bin',
    'koramangala_5th_block_bin',
    'jp_nagar_bin',
    'marathahalli_bin',
    'whitefield_bin',
    'indiranagar_bin',
    'bannerghatta_road_bin',
    'bellandur_bin'
]

cuisines_lst = [
    'north_indian_bin',
    'chinese_bin',
    'south_indian_bin',
    'biryani_bin',
    'fast_food_bin',
    'street_food_bin',
    'seafood_bin',
    'continental_bin',
    'andhra_bin',
    'beverages_bin',
    'italian_bin',
    'other_bin'
]

# set up model
model = LpProblem(name='restaurant_cuisine_neigborhood', sense=LpMaximize)

# set variables
x = {i: LpVariable(name=f"x{i}", lowBound=0, cat='Binary') for i in cuisines_lst}
y = {i: LpVariable(name=f"y{i}", lowBound=0, cat='Binary') for i in neighborhood_lst}

# Set objective
model += 0.0267 * x['north_indian_bin'] + 0.0243 * x['chinese_bin'] + 0.0088 * x['south_indian_bin'] + \
         0.0108 * x['biryani_bin'] + 0.0084 * x['fast_food_bin'] + 0.0022 * x['street_food_bin'] + \
         0.0030 * x['seafood_bin'] + 0.0038 * x['continental_bin'] + 0.0036 * x['andhra_bin'] + \
         0.0030 * x['beverages_bin'] + 0.0176 * x['other_bin'] + \
         0.0011 * y['btm_bin'] + 0.0018 * y['hsr_bin'] + 0.0041 * y['jayanagar_bin'] + \
         0.0039 * y['koramangala_5th_block_bin'] + 0.0005 * y['jp_nagar_bin'] + 0.0094 * y['marathahalli_bin'] + \
         0.0012 * y['whitefield_bin'] + 0.0004 * y['indiranagar_bin'] + 0.0108 * y['bannerghatta_road_bin'] + \
         0.0085 * y['bellandur_bin']

I am having issues with my constraints below.

# Add constraints
model += (lpSum(y.values()) == 1, 'neighborhood_selection')
model += (lpSum(x.values()) <= 4, 'cuisine_selection')

# If Indiranagar then must NOT be andhra food
model += (y['indiranagar_bin'] + x['andhra_bin'] <= 1, 'andhra_not_in_indirangar')

# If Koramangala 5th Block, then one must be Italian
model += (y['koramangala_5th_block_bin'] <= x['italian_bin'], '5th_block_italian')
# If Whitefield, then one must be Other
model += (y['whitefield_bin'] <= x['other_bin'], 'whitefield_other')

If I use only the andhra_not_in_indirangar constraint the model will run and determine an optimal solution. When I try to add the 5th_block_italian and whitefield_other constraints, the model will not run.

status = model.solve()

This returns a PulpSolverError: PuLP: Error while executing glpsol.exe

My logic for the last two constraints is that the y variable will be less than or equal to the x variable. So if y is 1, then so is x, but x can be 1 without y having to be 1.

I have read about the Big M method but wasn't sure that it applied to my model.


Solution

  • It seems that the solver (or perhaps an interface to it from PuLP) does not like having a constraint name which begins with a number. Replacing your problematic constraint with:

    model += (y['koramangala_5th_block_bin'] <= x['italian_bin'], 'fifth_block_italian')

    Or any other variation which avoids the leading numeric digit should solve the problem.

    I don't know the actual source of this bug - but have seen similar errors arise in the past where duplicate variable names are used. I raised a GitHub issue about that problem and there now seems to be a commit to fix it so might be worth you submitting an issue on the github page about this constraint naming problem.