Skip to content

Commit

Permalink
Revert change to persistent_base and instead declare xpres.var as not…
Browse files Browse the repository at this point in the history
… hashable
  • Loading branch information
jsiirola committed Oct 28, 2024
1 parent e66f571 commit 8a1f209
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
11 changes: 11 additions & 0 deletions pyomo/common/collections/component_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def _unhashable(val):
def _tuple(self, val):
return tuple(self[i.__class__](i) for i in val)

def hashable(self, obj, hashable=None):
if isinstance(obj, type):
cls = obj
else:
cls = type(cls)
if hashable is None:
return self.get(cls, None) is self._hashable
self[cls] = self._hashable if hashable else self._unhashable


_hasher = _Hasher()

Expand Down Expand Up @@ -78,6 +87,8 @@ class ComponentMap(AutoSlots.Mixin, collections.abc.MutableMapping):

__slots__ = ("_dict",)
__autoslot_mappers__ = {'_dict': _rehash_keys}
# A "public" interface to the global _hasher dict
hasher = _hasher

def __init__(self, *args, **kwds):
# maps id_hash(obj) -> (obj,val)
Expand Down
6 changes: 1 addition & 5 deletions pyomo/solvers/plugins/solvers/persistent_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,11 @@ def remove_var(self, var):
+ 'objective or one or more constraints'
)
solver_var = self._pyomo_var_to_solver_var_map[var]
self._remove_var(solver_var)
self._symbol_map.removeSymbol(var)
del self._referenced_variables[var]
del self._pyomo_var_to_solver_var_map[var]
del self._solver_var_to_pyomo_var_map[solver_var]
# Note: we want to remove the solver_var from all our internal
# mappings *before* removing it from the solver (the solver may
# invalidate the object, leading to exceptions when looking it
# up in ComponentMaps)
self._remove_var(solver_var)

""" This method should be implemented by subclasses."""

Expand Down
7 changes: 7 additions & 0 deletions pyomo/solvers/plugins/solvers/xpress_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ def _addConstraint(
XpressDirect._getDuals = lambda self, prob, con: prob.getDuals(con)
XpressDirect._getRedCosts = lambda self, prob, con: prob.getRedCosts(con)

# Note that as of 9.5, xpress.var raises an exception when
# compared using '==' after it has been removed from the model.
# This can fould up ComponentMaps in the persistent interface,
# so we will hard-code the `var` as not being hashable (so the
# ComponentMap will use the id() as the key)
ComponentMap.hasher.hashable(xpress.var, False)


class _xpress_importer_class(object):
# We want to be able to *update* the message that the deferred
Expand Down

0 comments on commit 8a1f209

Please sign in to comment.