0

My question is very simple. Is it possible to delete a variable that I have already created?

Or my only hope is to not create the variable to begin with?

I guess if you can print a variable then using:

del prob.variable

would delete it. But I can't find it exactly, Python says that:

prob.variables

is not subscriptable.

drizo
  • 267
  • 6
  • 14
  • I didn't see anything in the docs, but i'm no expert. In what context do you need this feature? If there are no constraints using it; well it does not matter (just keep them). If there are it might be easier to remove the constraints. – sascha Aug 19 '16 at 17:36
  • Problem is that there is one constraint containing all those variables that are not really useful. it is a sum constraint. I was hoping after creating them to delete them right before this very important constraint. – drizo Aug 19 '16 at 17:45
  • So this constraint does not change the feasibility/objective? Just keep it in. The solver won't care that much. And why you can't deduce this *not helpful* before adding these vars or this constraint? – sascha Aug 19 '16 at 17:50
  • How did you create the variables? There are a few ways. – ayhan Aug 19 '16 at 18:03
  • After a short look into the sources of pulp: *it seems there is no support for deleting variables!*. So your possible approaches are mostly domain-dependent like the one i described above. Another approach: just set these variables to some constant value (set lower- and upper bound). Depending on your problem this could be neg-inf, 0, pos-inf or something else (in some complex models). This should help the solver (especially it's preprocessing) to remove any unneeded work! – sascha Aug 19 '16 at 18:25
  • Most likely I will have to come up with some conditions in order to not create those variables at all. – drizo Aug 20 '16 at 12:22

1 Answers1

1

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.

Pieter
  • 11
  • 3
  • The comparator `==` does not work because `LpVariable`'s `__eq__` method is overloaded. Instead of the usual boolean return value, it returns an object of type `LpConstraint`. Since most objects are truthy in python (unless their `__bool__` method returns `False`), including presumably any `LpConstraint`, `if var1 == var2: print('Equal')` will alway print. On the contrary, `is` checks whether two objects have the same memory address. This can't be the case if `var1` is not identical to `var2`. See https://stackoverflow.com/questions/3588776/how-is-eq-handled-in-python-and-in-what-order – AlexNe Nov 24 '22 at 09:34