Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow calling del_component with ComponentData arguments #3440

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pyomo/contrib/fme/fourier_motzkin_elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,9 @@ def post_process_fme_constraints(
continue
# deactivate the constraint
projected_constraints[i].deactivate()
m.del_component(obj)
# make objective to maximize its infeasibility
obj = Objective(
expr=projected_constraints[i].body - projected_constraints[i].lower
)
m.add_component(obj_name, obj)
# Our constraint looks like: 0 <= a^Tx - b, so make objective to
# maximize its infeasibility
obj.expr = projected_constraints[i].body - projected_constraints[i].lower
results = solver_factory.solve(m)
if results.solver.termination_condition == TerminationCondition.unbounded:
obj_val = -float('inf')
Expand All @@ -753,13 +750,13 @@ def post_process_fme_constraints(
obj_val = value(obj)
# if we couldn't make it infeasible, it's useless
if obj_val >= tolerance:
m.del_component(projected_constraints[i])
del projected_constraints[i]
else:
projected_constraints[i].activate()

# clean up
m.del_component(obj)
del obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary (it implicitly happens when you reassign obj in the for loop in the next line)

for obj in active_objs:
obj.activate()
# undo relax integrality
Expand Down
9 changes: 9 additions & 0 deletions pyomo/core/base/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from pyomo.common.timing import ConstructionTimer
from pyomo.core.base.component import (
Component,
ComponentData,
ActiveComponentData,
ModelComponentFactory,
)
Expand Down Expand Up @@ -1139,6 +1140,14 @@ def del_component(self, name_or_object):
# return

name = obj.local_name
if isinstance(obj, ComponentData) and not isinstance(obj, Component):
raise ValueError(
"Argument '%s' to del_component is a ComponentData object. Please "
"use the Python 'del' function to delete members of indexed "
"Pyomo objects. The del_component function can only be used to "
"delete IndexedComponents and ScalarComponents." % name
)

Comment on lines +1143 to +1150
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do what you think it does: line 1133 (obj = self.component(name_or_object)) maps the incoming object into its component, so you shouldn't be able to trigger this exception. If we are going to change the behavior of del_component to only admit Components (either by name or reference), we should

  • change (possibly inline the logic from) line 1133
  • document this in the docstring
  • possibly add a deprecation path (because we are changing behavior that has been in Pyomo for over a decade)??

if name in self._Block_reserved_words:
raise ValueError(
"Attempting to delete a reserved block component:\n\t%s" % (obj.name,)
Expand Down
6 changes: 1 addition & 5 deletions pyomo/core/base/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,7 @@ class ComponentData(ComponentBase):
This is the base class for the component data used
in Pyomo modeling components. Subclasses of ComponentData are
used in indexed components, and this class assumes that indexed
components are subclasses of IndexedComponent. Note that
ComponentData instances do not store their index. This makes
some operations significantly more expensive, but these are (a)
associated with I/O generation and (b) this cost can be managed
with caches.
components are subclasses of IndexedComponent.

Constructor arguments:
owner The component that owns this data object
Expand Down
Loading