# 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',
'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 healthy 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.

• Your problem is not with your model, rather with GLPK solver. Probably you need to install glpk again. I suggest you run your model with CBC solver and see for yourself that you get an optimal solution (pulp uses CBC as its default solver anyway).
– EhsanK
Aug 6, 2021 at 0:58

I believe the issue was with the following constraint name:

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


It seems that PuLP did not like the name starting with a number even though it was a string.

If I changed the constraint to this, the model ran.

# If Koramangala 5th Block, then one must be Italian
model += (y['koramangala_5th_block_bin'] <= x['italian_bin'], 'fifth_block_italian')