The variables associated with a PuLP LP problem prob can be found in prob._variables, which is a list, so you'd think you could delete a variable var using
prob._variables.remove(var)
However, this simply deletes the first variable in prob._variables, since for some reason, Python doesn't distinguish between the problem variables on the level of ==. By this I mean that if var1 and var2 are any two variables associated to prob,
if var1 == var2:
print('Equal')
always returns 'Equal'. (Don't try var1 == var2; this will be interpreted as an equation)
. This means that if you try find the index of var using
prob._variables.index(var)
the output will always be 0.
Someone who understands Python better than I do will have to explain what's happpening, but here's a work around:
for i, v in enumerate(prob._variables):
if v is var:
del prob._variables[i]
This won't delete the variable from any constraints involving it, and it seems like PuLP will solve the LP problem as if the variable were still there. So I think all this does is change the attribute prob._variables. I don't think this is helpful in your situation (which I don't really understand), but it's necessary if you want to delete a variable, and then introduce a new variable with the same name as the old one.