Removing an element of a Set and updating the model

726 views
Skip to first unread message

IA

unread,
Oct 19, 2016, 11:37:29 AM10/19/16
to Pyomo Forum
Hello,

I am writing an Abstract model where I have a Set that takes the values from 1 to 5. Many parameters and constraints are indexed with this set, and I was wondering if there is a way with which I can remove one element of this set, update the model and solve it again without the need for creating a new .dat file with the reduced parameter indices each time

Thank you in advance

Gabriel Hackebeil

unread,
Oct 19, 2016, 12:31:24 PM10/19/16
to pyomo...@googlegroups.com
You have a few options:

(1) You can deactivate indices of constraints without changing the index set, which is equivalent to removing the constraint from the model until you call activate() on it. E.g.,

instance.c[5].deactivate()

(2) You can delete elements from the set, but you also have to manually remove elements from the constraints and params that it indexes if you don’t want to run into problems later. E.g.,

instance.some_index_set.remove(5)
del instance.some_indexed_constraint[5]
del instance.some_indexed_param[5]

This issue becomes more complicated if you want to add elements. In this case it might be easier to use something like a ConstraintList where you don’t need to keep track of an external set and you can add or remove elements on the fly. However, the current implementation of ConstraintList makes it difficult to keep track which index new items are placed in, so beware of that:

# c is a ConstraintList (assume empty)
instance.c = ConstraintList()
instance.c.add(instance.x >= ...) # goes to index 1
instance.c.add(instance.x >= ...) # goes to index 2
del instance.c[2]
print(len(instance.c))                    # there is 1 element in c
instance.c.add(instance.x >= …) # goes to index 3
print(len(instance.c))                    # there are 2 elements in c

Finally, when starting from an AbstractModel be sure all of these operations are performed on an instance returned from model.create_instance(…) and not the model itself.

Gabe

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages